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

Preserve body on request object passed to willSendRequest; fix string and Buffer body handling #89

Merged
merged 19 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class PersonalizationAPI extends RESTDataSource {

If you're using TypeScript, you can use the `RequestOptions` type to define the `willSendRequest` signature:
```ts
import { RESTDataSource, WillSendRequestOptions } from '@apollo/datasource-rest';
import { RESTDataSource, AugmentedRequest } from '@apollo/datasource-rest';

class PersonalizationAPI extends RESTDataSource {
override baseURL = 'https://personalization-api.example.com/';
Expand All @@ -207,7 +207,7 @@ class PersonalizationAPI extends RESTDataSource {
super();
}

override willSendRequest(request: WillSendRequestOptions) {
override willSendRequest(request: AugmentedRequest) {
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
request.headers['authorization'] = this.token;
}
}
Expand Down
85 changes: 44 additions & 41 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ export type RequestOptions = FetcherRequestInit & {
) => CacheOptions | undefined);
};

export type WillSendRequestOptions = Omit<
WithRequired<RequestOptions, 'headers'>,
'params'
> & {
params: URLSearchParams;
};

export interface GetRequest extends RequestOptions {
method?: 'GET';
body?: never;
Expand All @@ -48,6 +41,13 @@ export interface RequestWithBody extends Omit<RequestOptions, 'body'> {

type DataSourceRequest = GetRequest | RequestWithBody;

export type AugmentedRequest = (
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth a comment that the point here is basically, this is like DataSourceRequest but with headers and params normalized for convenience in your callbacks.

BTW unrelated but it seems like method shouldn't be optional in GetRequest/RequestWithBody?

| Omit<WithRequired<GetRequest, 'headers'>, 'params'>
| Omit<WithRequired<RequestWithBody, 'headers'>, 'params'>
) & {
params: URLSearchParams;
};

export interface CacheOptions {
ttl?: number;
}
Expand Down Expand Up @@ -79,12 +79,12 @@ export abstract class RESTDataSource {
}

protected willSendRequest?(
requestOpts: WillSendRequestOptions,
requestOpts: AugmentedRequest,
): ValueOrPromise<void>;

protected resolveURL(
path: string,
_request: RequestOptions,
_request: AugmentedRequest,
): ValueOrPromise<URL> {
return new URL(path, this.baseURL);
}
Expand Down Expand Up @@ -205,71 +205,74 @@ export abstract class RESTDataSource {

private async fetch<TResult>(
path: string,
request: DataSourceRequest,
incomingRequest: DataSourceRequest,
): Promise<TResult> {
const modifiedRequest: WillSendRequestOptions = {
...request,
const augmentedRequest: AugmentedRequest = {
...incomingRequest,
// guarantee params and headers objects before calling `willSendRequest` for convenience
params:
request.params instanceof URLSearchParams
? request.params
: this.urlSearchParamsFromRecord(request.params),
headers: request.headers ?? Object.create(null),
body: undefined,
incomingRequest.params instanceof URLSearchParams
? incomingRequest.params
: this.urlSearchParamsFromRecord(incomingRequest.params),
headers: incomingRequest.headers ?? Object.create(null),
};

if (this.willSendRequest) {
await this.willSendRequest(modifiedRequest);
await this.willSendRequest(augmentedRequest);
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
}

const url = await this.resolveURL(path, modifiedRequest);
const url = await this.resolveURL(path, augmentedRequest);

// Append params from the request to any existing params in the path
for (const [name, value] of modifiedRequest.params as URLSearchParams) {
// Append params to existing params in the path
for (const [name, value] of augmentedRequest.params as URLSearchParams) {
url.searchParams.append(name, value);
}

// We accept arbitrary objects and arrays as body and serialize them as JSON
// We accept arbitrary objects and arrays as body and serialize them as JSON.
// `string`, `Buffer`, and `undefined` are passed through up above as-is.
if (
request.body !== undefined &&
request.body !== null &&
(request.body.constructor === Object ||
Array.isArray(request.body) ||
((request.body as any).toJSON &&
typeof (request.body as any).toJSON === 'function'))
augmentedRequest.body != null &&
(augmentedRequest.body.constructor === Object ||
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
Array.isArray(augmentedRequest.body) ||
((augmentedRequest.body as any).toJSON &&
typeof (augmentedRequest.body as any).toJSON === 'function'))
) {
modifiedRequest.body = JSON.stringify(request.body);
augmentedRequest.body = JSON.stringify(augmentedRequest.body);
// If Content-Type header has not been previously set, set to application/json
if (!modifiedRequest.headers) {
modifiedRequest.headers = { 'content-type': 'application/json' };
} else if (!modifiedRequest.headers['content-type']) {
modifiedRequest.headers['content-type'] = 'application/json';
if (!augmentedRequest.headers) {
augmentedRequest.headers = { 'content-type': 'application/json' };
} else if (!augmentedRequest.headers['content-type']) {
augmentedRequest.headers['content-type'] = 'application/json';
}
}

const cacheKey = this.cacheKeyFor(url, modifiedRequest);
// At this point we know the `body` is a `string`, `Buffer`, or `undefined`
// (not possibly an `object`).
const outgoingRequest = <RequestOptions>augmentedRequest;
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved

const cacheKey = this.cacheKeyFor(url, outgoingRequest);

const performRequest = async () => {
return this.trace(url, modifiedRequest, async () => {
const cacheOptions = modifiedRequest.cacheOptions
? modifiedRequest.cacheOptions
return this.trace(url, outgoingRequest, async () => {
const cacheOptions = outgoingRequest.cacheOptions
? outgoingRequest.cacheOptions
: this.cacheOptionsFor?.bind(this);
try {
const response = await this.httpCache.fetch(url, modifiedRequest, {
const response = await this.httpCache.fetch(url, outgoingRequest, {
cacheKey,
cacheOptions,
});
return await this.didReceiveResponse(response, modifiedRequest);
return await this.didReceiveResponse(response, outgoingRequest);
} catch (error) {
this.didEncounterError(error as Error, modifiedRequest);
this.didEncounterError(error as Error, outgoingRequest);
}
});
};

// Cache GET requests based on the calculated cache key
// Disabling the request cache does not disable the response cache
if (this.memoizeGetRequests) {
if (request.method === 'GET') {
if (outgoingRequest.method === 'GET') {
let promise = this.memoizedResults.get(cacheKey);
if (promise) return promise;

Expand Down
129 changes: 101 additions & 28 deletions src/__tests__/RESTDataSource.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
// import fetch, { Request } from 'node-fetch';
import {
AugmentedRequest,
AuthenticationError,
CacheOptions,
DataSourceConfig,
ForbiddenError,
RequestOptions,
RESTDataSource,
WillSendRequestOptions,
// RequestOptions
} from '../RESTDataSource';

// import { HTTPCache } from '../HTTPCache';
// import { MapKeyValueCache } from './MapKeyValueCache';
import { nockAfterEach, nockBeforeEach } from './nockAssertions';
import nock from 'nock';
import { GraphQLError } from 'graphql';

const apiUrl = 'https://api.example.com';

describe('RESTDataSource', () => {
// let httpCache: HTTPCache;

beforeEach(() => {
nockBeforeEach();
// httpCache = new HTTPCache(new MapKeyValueCache<string>());
});

beforeEach(nockBeforeEach);
afterEach(nockAfterEach);

describe('constructing requests', () => {
Expand Down Expand Up @@ -123,7 +113,7 @@ describe('RESTDataSource', () => {

it('allows resolving a base URL asynchronously', async () => {
const dataSource = new (class extends RESTDataSource {
override async resolveURL(path: string, request: RequestOptions) {
override async resolveURL(path: string, request: AugmentedRequest) {
if (!this.baseURL) {
this.baseURL = 'https://api.example.com';
}
Expand Down Expand Up @@ -205,7 +195,7 @@ describe('RESTDataSource', () => {
super(config);
}

override willSendRequest(request: WillSendRequestOptions) {
override willSendRequest(request: AugmentedRequest) {
request.params.set('apiKey', this.token);
}

Expand All @@ -225,7 +215,7 @@ describe('RESTDataSource', () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';

override willSendRequest(request: WillSendRequestOptions) {
override willSendRequest(request: AugmentedRequest) {
request.headers = { ...request.headers, credentials: 'include' };
}

Expand All @@ -247,7 +237,7 @@ describe('RESTDataSource', () => {
super(config);
}

override willSendRequest(request: WillSendRequestOptions) {
override willSendRequest(request: AugmentedRequest) {
request.headers = { ...request.headers, authorization: this.token };
}

Expand All @@ -267,18 +257,6 @@ describe('RESTDataSource', () => {
await dataSource.getFoo('1');
});

// function expectJSONFetch(url: string, bodyJSON: unknown) {
// expect(fetch).toBeCalledTimes(1);
// const request = fetch.mock.calls[0][0] as Request;
// expect(request.url).toEqual(url);
// // request.body is a node-fetch extension which we aren't properly
// // capturing in our TS types.
// expect((request as any).body.toString()).toEqual(
// JSON.stringify(bodyJSON),
// );
// expect(request.headers.get('Content-Type')).toEqual('application/json');
// }

it('serializes a request body that is an object as JSON', async () => {
const expectedFoo = { foo: 'bar' };
const dataSource = new (class extends RESTDataSource {
Expand Down Expand Up @@ -360,6 +338,48 @@ describe('RESTDataSource', () => {
await dataSource.postFoo(form);
});

it('does not serialize (but does include) string request bodies', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';

updateFoo(id: number, urlEncodedFoo: string) {
return this.post(`foo/${id}`, {
headers: { 'content-type': 'application/x-www-urlencoded' },
body: urlEncodedFoo,
});
}
})();

nock(apiUrl)
.post('/foo/1', (body) => {
return body === 'id=1&name=bar';
})
.reply(200, 'ok', { 'content-type': 'text/plain' });

await dataSource.updateFoo(1, 'id=1&name=bar');
});

it('does not serialize (but does include) `Buffer` request bodies', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';

updateFoo(id: number, fooBuf: Buffer) {
return this.post(`foo/${id}`, {
headers: { 'content-type': 'application/octet-stream' },
body: fooBuf,
});
}
})();

nock(apiUrl)
.post('/foo/1', (body) => {
return Buffer.from(body.data).toString() === 'id=1&name=bar';
})
.reply(200, 'ok', { 'content-type': 'text/plain' });

await dataSource.updateFoo(1, Buffer.from('id=1&name=bar'));
});

describe('all methods', () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';
Expand Down Expand Up @@ -868,5 +888,58 @@ describe('RESTDataSource', () => {
jest.useRealTimers();
});
});

describe('user hooks', () => {
describe('willSendRequest', () => {
it('sees the same request body as provided by the caller', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override async willSendRequest(requestOpts: AugmentedRequest) {
expect(requestOpts.body).toMatchInlineSnapshot(`
{
"name": "blah",
}
`);
}
})();

nock(apiUrl)
.post('/foo/1', JSON.stringify({ name: 'blah' }))
.reply(200);
await dataSource.updateFoo(1, { name: 'blah' });
});
});

describe('resolveURL', () => {
it('sees the same request body as provided by the caller', async () => {
const dataSource = new (class extends RESTDataSource {
override baseURL = apiUrl;

updateFoo(id: number, foo: { name: string }) {
return this.post(`foo/${id}`, { body: foo });
}

override resolveURL(path: string, requestOpts: AugmentedRequest) {
expect(requestOpts.body).toMatchInlineSnapshot(`
{
"name": "blah",
}
`);
return super.resolveURL(path, requestOpts);
}
})();

nock(apiUrl)
.post('/foo/1', JSON.stringify({ name: 'blah' }))
.reply(200);
await dataSource.updateFoo(1, { name: 'blah' });
});
});
});
});
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
RESTDataSource,
RequestOptions,
WillSendRequestOptions,
AugmentedRequest,
Copy link
Member Author

Choose a reason for hiding this comment

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

The name change here alone makes this a v5 change. Could continue exporting this as an alias, but maybe the naming is still up in the air anyway.

} from './RESTDataSource';