core: Warn when using data (#1421)
* 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>
This commit is contained in:
parent
41f53b9065
commit
4ae35388b0
|
@ -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)')}
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
|
@ -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';
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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')) {
|
||||
|
|
|
@ -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: ''
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
}));
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue