Skip to content

Commit 4ae3538

Browse files
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 <[email protected]>
1 parent 41f53b9 commit 4ae3538

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/body.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ Object.defineProperties(Body.prototype, {
178178
arrayBuffer: {enumerable: true},
179179
blob: {enumerable: true},
180180
json: {enumerable: true},
181-
text: {enumerable: true}
181+
text: {enumerable: true},
182+
data: {get: deprecate(() => {},
183+
'data doesn\'t exist, use json(), text(), arrayBuffer(), or body instead',
184+
'https://github.com/node-fetch/node-fetch/issues/1000 (response)')}
182185
});
183186

184187
/**

src/request.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

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

34+
const doBadDataWarn = deprecate(() => {},
35+
'.data is not a valid RequestInit property, use .body instead',
36+
'https://github.com/node-fetch/node-fetch/issues/1000 (request)');
37+
3338
/**
3439
* Request class
3540
*
@@ -58,6 +63,10 @@ export default class Request extends Body {
5863
let method = init.method || input.method || 'GET';
5964
method = method.toUpperCase();
6065

66+
if ('data' in init) {
67+
doBadDataWarn();
68+
}
69+
6170
// eslint-disable-next-line no-eq-null, eqeqeq
6271
if ((init.body != null || (isRequest(input) && input.body !== null)) &&
6372
(method === 'GET' || method === 'HEAD')) {

test/request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,16 @@ describe('Request', () => {
282282
expect(result).to.equal('a=1');
283283
});
284284
});
285+
286+
it('should warn once when using .data (request)', () => new Promise(resolve => {
287+
process.once('warning', evt => {
288+
expect(evt.message).to.equal('.data is not a valid RequestInit property, use .body instead');
289+
resolve();
290+
});
291+
292+
// eslint-disable-next-line no-new
293+
new Request(base, {
294+
data: ''
295+
});
296+
}));
285297
});

test/response.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,13 @@ describe('Response', () => {
241241
expect(res.status).to.equal(0);
242242
expect(res.statusText).to.equal('');
243243
});
244+
245+
it('should warn once when using .data (response)', () => new Promise(resolve => {
246+
process.once('warning', evt => {
247+
expect(evt.message).to.equal('data doesn\'t exist, use json(), text(), arrayBuffer(), or body instead');
248+
resolve();
249+
});
250+
251+
new Response('a').data;
252+
}));
244253
});

0 commit comments

Comments
 (0)