Skip to content

Commit 7d28594

Browse files
committed
Make property enumerable
1 parent 0f932ba commit 7d28594

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

readme.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ JSON body. If the `Content-Type` header is not set, it will be set to `applicati
251251

252252
Type: `object`
253253

254-
User data. In contrast to other options, `context` is not enumerable.
255-
256-
**Note:** The object is never merged, it's just passed through. Got will not modify the object in any way.
254+
User data. `context` is shallow merged.
257255

258256
It's very useful for storing auth tokens:
259257

source/core/index.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,7 @@ interface PlainOptions extends URLOptions {
515515
dnsCache?: CacheableLookup | boolean;
516516

517517
/**
518-
User data. In contrast to other options, `context` is not enumerable.
519-
520-
__Note__: The object is never merged, it's just passed through.
521-
Got will not modify the object in any way.
518+
User data. `context` is shallow merged.
522519
523520
@example
524521
```
@@ -1138,9 +1135,8 @@ const waitForOpenFile = async (file: ReadStream): Promise<void> => new Promise((
11381135

11391136
const redirectCodes: ReadonlySet<number> = new Set([300, 301, 302, 303, 304, 307, 308]);
11401137

1141-
type NonEnumerableProperty = 'context' | 'body' | 'json' | 'form';
1138+
type NonEnumerableProperty = 'body' | 'json' | 'form';
11421139
const nonEnumerableProperties: NonEnumerableProperty[] = [
1143-
'context',
11441140
'body',
11451141
'json',
11461142
'form'
@@ -1764,9 +1760,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
17641760
}
17651761

17661762
// `options.context`
1767-
if (!options.context) {
1768-
options.context = {};
1769-
}
1763+
options.context = {...defaults?.context, ...options.context};
17701764

17711765
// `options.hooks`
17721766
const areHooksDefault = options.hooks === defaults?.hooks;

source/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export interface Got extends Record<HTTPAlias, GotRequestFunction>, GotRequestFu
371371
- If the parent property is a plain `object` too, both values are merged recursively into a new `object`.
372372
- Otherwise, only the new value is deeply cloned.
373373
- If the new property is an `Array`, it overwrites the old one with a deep clone of the new property.
374-
- Properties that are not enumerable, such as `context`, `body`, `json`, and `form`, will not be merged.
374+
- Properties that are not enumerable, such as `body`, `json`, and `form`, will not be merged.
375375
- Otherwise, the new value is assigned to the key.
376376
377377
**Note:** Only Got options are merged! Custom user options should be defined via [`options.context`](#context).

test/arguments.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ test('throws if the `searchParams` value is invalid', async t => {
328328
});
329329
});
330330

331-
test('`context` option is not enumerable', withServer, async (t, server, got) => {
331+
test('`context` option is enumerable', withServer, async (t, server, got) => {
332332
server.get('/', echoUrl);
333333

334334
const context = {
@@ -340,8 +340,8 @@ test('`context` option is not enumerable', withServer, async (t, server, got) =>
340340
hooks: {
341341
beforeRequest: [
342342
options => {
343-
t.is(options.context, context);
344-
t.false({}.propertyIsEnumerable.call(options, 'context'));
343+
t.deepEqual(options.context, context);
344+
t.true({}.propertyIsEnumerable.call(options, 'context'));
345345
}
346346
]
347347
}
@@ -360,8 +360,8 @@ test('`context` option is accessible when using hooks', withServer, async (t, se
360360
hooks: {
361361
beforeRequest: [
362362
options => {
363-
t.is(options.context, context);
364-
t.false({}.propertyIsEnumerable.call(options, 'context'));
363+
t.deepEqual(options.context, context);
364+
t.true({}.propertyIsEnumerable.call(options, 'context'));
365365
}
366366
]
367367
}
@@ -375,8 +375,23 @@ test('`context` option is accessible when extending instances', t => {
375375

376376
const instance = got.extend({context});
377377

378-
t.is(instance.defaults.options.context, context);
379-
t.false({}.propertyIsEnumerable.call(instance.defaults.options, 'context'));
378+
t.deepEqual(instance.defaults.options.context, context);
379+
t.true({}.propertyIsEnumerable.call(instance.defaults.options, 'context'));
380+
});
381+
382+
test('`context` option is shallow merged', t => {
383+
const context = {
384+
foo: 'bar'
385+
};
386+
387+
const instance1 = got.extend({context});
388+
389+
t.deepEqual(instance1.defaults.options.context, context);
390+
t.true({}.propertyIsEnumerable.call(instance1.defaults.options, 'context'));
391+
392+
const instance2 = instance1.extend({context: {}});
393+
394+
t.deepEqual(instance2.defaults.options.context, context);
380395
});
381396

382397
test('throws if `options.encoding` is `null`', async t => {

test/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ test('beforeRetry is called with options', withServer, async (t, server, got) =>
421421
beforeRetry: [
422422
(options, error, retryCount) => {
423423
t.is(options.url.hostname, 'localhost');
424-
t.is(options.context, context);
424+
t.deepEqual(options.context, context);
425425
t.truthy(error);
426426
t.true(retryCount! >= 1);
427427
}

0 commit comments

Comments
 (0)