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

Add req res data warn #1333

Closed
wants to merge 5 commits into from
Closed
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
10 changes: 9 additions & 1 deletion src/request.js
Expand Up @@ -6,7 +6,7 @@
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
*/

import {format as formatUrl} from 'node:url';
import {deprecate, format as formatUrl} from 'node:url';
import Headers from './headers.js';
import Body, {clone, extractContentType, getTotalBytes} from './body.js';
import {isAbortSignal} from './utils/is.js';
Expand All @@ -30,6 +30,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 +62,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
6 changes: 5 additions & 1 deletion src/response.js
Expand Up @@ -7,6 +7,7 @@
import Headers from './headers.js';
import Body, {clone, extractContentType} from './body.js';
import {isRedirect} from './utils/is-redirect.js';
import {deprecate} from 'util';

const INTERNALS = Symbol('Response internals');

Expand Down Expand Up @@ -137,5 +138,8 @@ Object.defineProperties(Response.prototype, {
redirected: {enumerable: true},
statusText: {enumerable: true},
headers: {enumerable: true},
clone: {enumerable: true}
clone: {enumerable: true},
data: {get: deprecate(() => {},
'.data is not a valid Response property, use .json(), .text(), .arrayBuffer(), or .body instead',
'https://github.com/node-fetch/node-fetch/issues/1000 (response)')}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall i like this PR but wonder if not this getter fn should not be in the body mixin instead.

It's possible to use the Request too new Request(url, {body}).arrayBuffer()
all doe i don't thing anyone will make that misstake... seems almost unnecessary, nobody would do: new Request().data or assigning a value onto it...

});
15 changes: 15 additions & 0 deletions test/request.js
Expand Up @@ -283,4 +283,19 @@ describe('Request', () => {
expect(result).to.equal('a=1');
});
});

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

process.on('warning', listenerFunc);

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

it('should warn once when using .data (response)', () => new Promise(resolve => {
const listenerFunc = ev => {
process.off('warning', listenerFunc);
expect(ev.message).to.equal('.data is not a valid Response property, use .json(), .text(), .arrayBuffer(), or .body instead');
resolve();
};

process.on('warning', listenerFunc);

const response = new Response('a');
response.data;
}));
});