Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Warn when using data #1421

Merged
merged 7 commits into from Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/body.js
Expand Up @@ -177,7 +177,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
2 changes: 1 addition & 1 deletion test/referrer.js
Expand Up @@ -127,7 +127,7 @@ describe('Request constructor', () => {
expect(() => {
const req = new Request('http://example.com', {referrer: 'foobar'});
expect.fail(req);
}).to.throw(TypeError, 'Invalid URL: foobar');
}).to.throw(TypeError, /Invalid URL/);
});
});

Expand Down
12 changes: 12 additions & 0 deletions test/request.js
Expand Up @@ -283,4 +283,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 @@ -248,4 +248,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;
}));
});