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 context option #777

Merged
merged 12 commits into from Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
38 changes: 38 additions & 0 deletions readme.md
Expand Up @@ -174,6 +174,44 @@ Type: `object | Array | number | string | boolean | null` *(JSON-serializable va

JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.

###### context

Type: `unknown`
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

User data. In contrast to other options, `context` is not enumerable.
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

Example:

```js
const got = require('got');

const instance = got.extend({
hooks: {
afterResponse: [
response => {
const {context} = response.request.options;
context.latestFetchedSite = response.url;
}
]
}
});

(async () => {
const context = {
latestFetchedSite: ''
};

const response = await instance('https://example.com', {context});

// Let's see our context data
console.log(context.latestFetchedSite); //=> 'https://example.com/'

// The `.context` won't be displayed here because it's not enumerable.
// You can, however, access it directly: `response.request.options.context`
console.log(response.request.options);
})();
```

###### responseType

Type: `string`<br>
Expand Down
9 changes: 9 additions & 0 deletions source/merge.ts
Expand Up @@ -37,6 +37,15 @@ export default function merge<Target extends Record<string, unknown>, Source ext
target[key] = sourceValue;
}
}

if (Reflect.has(source, 'context')) {
Object.defineProperty(target, 'context', {
writable: true,
configurable: true,
enumerable: false,
value: source.context
});
}
}

return target as Target & Source;
Expand Down
6 changes: 2 additions & 4 deletions source/utils/types.ts
Expand Up @@ -138,13 +138,11 @@ export interface Options extends Omit<https.RequestOptions, 'agent' | 'timeout'
dnsCache?: Map<string, string> | Keyv | false;
url?: URL | string;
searchParams?: Record<string, string | number | boolean | null> | URLSearchParams | string;
/*
Deprecated
*/
query?: Options['searchParams'];
query?: Options['searchParams']; // Deprecated
useElectronNet?: boolean;
form?: Record<string, any>;
json?: Record<string, any>;
context?: object;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How do I make this generic?

szmarczak marked this conversation as resolved.
Show resolved Hide resolved
}

export interface NormalizedOptions extends Omit<Required<Options>, 'timeout' | 'dnsCache' | 'retry'> {
Expand Down
51 changes: 51 additions & 0 deletions test/arguments.ts
Expand Up @@ -262,3 +262,54 @@ test('throws if the `searchParams` value is invalid', async t => {
message: 'The `searchParams` value \'\' must be a string, number, boolean or null'
});
});

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

const context = {
foo: 'bar'
};

await got({
context,
hooks: {
beforeRequest: [
options => {
t.is(options.context, context);
t.false({}.propertyIsEnumerable.call(options, 'context'));
}
]
}
});
});
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

test('`context` option is accessible when using hooks', withServer, async (t, server, got) => {
server.get('/', echoUrl);

const context = {
foo: 'bar'
};

await got({
context,
hooks: {
init: [
options => {
t.is(options.context, context);
t.false({}.propertyIsEnumerable.call(options, 'context'));
}
]
}
});
});

test('`context` option is accessible when extending instances', t => {
const context = {
foo: 'bar'
};

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

t.is(instance.defaults.options.context, context);
t.false({}.propertyIsEnumerable.call(instance.defaults.options, 'context'));
});