Skip to content

Commit

Permalink
core: Warn when using data (#1421)
Browse files Browse the repository at this point in the history
* Add a warning when using .data in RequestInit

* Add a warning when using .data in Response

* Switch custom solution for utils.deprecate

* Remove unused line in request tests

* moved error handler into the body class

* lint fix

Co-authored-by: Lubomir <lubomir.yudenko@gmail.com>
  • Loading branch information
jimmywarting and TheGLander committed Jan 8, 2022
1 parent 41f53b9 commit 4ae3538
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/body.js
Expand Up @@ -178,7 +178,10 @@ Object.defineProperties(Body.prototype, {
arrayBuffer: {enumerable: true},
blob: {enumerable: true},
json: {enumerable: true},
text: {enumerable: true}
text: {enumerable: true},
data: {get: deprecate(() => {},
'data doesn\'t exist, use json(), text(), arrayBuffer(), or body instead',
'https://github.com/node-fetch/node-fetch/issues/1000 (response)')}
});

/**
Expand Down
9 changes: 9 additions & 0 deletions src/request.js
Expand Up @@ -7,6 +7,7 @@
*/

import {format as formatUrl} from 'node:url';
import {deprecate} from 'node:util';
import Headers from './headers.js';
import Body, {clone, extractContentType, getTotalBytes} from './body.js';
import {isAbortSignal} from './utils/is.js';
Expand All @@ -30,6 +31,10 @@ const isRequest = object => {
);
};

const doBadDataWarn = deprecate(() => {},
'.data is not a valid RequestInit property, use .body instead',
'https://github.com/node-fetch/node-fetch/issues/1000 (request)');

/**
* Request class
*
Expand Down Expand Up @@ -58,6 +63,10 @@ export default class Request extends Body {
let method = init.method || input.method || 'GET';
method = method.toUpperCase();

if ('data' in init) {
doBadDataWarn();
}

// eslint-disable-next-line no-eq-null, eqeqeq
if ((init.body != null || (isRequest(input) && input.body !== null)) &&
(method === 'GET' || method === 'HEAD')) {
Expand Down
12 changes: 12 additions & 0 deletions test/request.js
Expand Up @@ -282,4 +282,16 @@ describe('Request', () => {
expect(result).to.equal('a=1');
});
});

it('should warn once when using .data (request)', () => new Promise(resolve => {
process.once('warning', evt => {
expect(evt.message).to.equal('.data is not a valid RequestInit property, use .body instead');
resolve();
});

// eslint-disable-next-line no-new
new Request(base, {
data: ''
});
}));
});
9 changes: 9 additions & 0 deletions test/response.js
Expand Up @@ -241,4 +241,13 @@ describe('Response', () => {
expect(res.status).to.equal(0);
expect(res.statusText).to.equal('');
});

it('should warn once when using .data (response)', () => new Promise(resolve => {
process.once('warning', evt => {
expect(evt.message).to.equal('data doesn\'t exist, use json(), text(), arrayBuffer(), or body instead');
resolve();
});

new Response('a').data;
}));
});

0 comments on commit 4ae3538

Please sign in to comment.