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
Changes from 1 commit
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
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ By default, `RESTDatasource` uses the full request URL as the cache key. Overrid
For example, you could use this to use header fields as part of the cache key. Even though we do validate header fields and don't serve responses from cache when they don't match, new responses overwrite old ones with different header fields.

##### `willSendRequest`
This method is invoked just before the fetch call is made. If a `Promise` is returned from this method it will wait until the promise is completed to continue executing the request.
This method is invoked immediately after `fetch` is called (or any of the
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
convenience functions which call it like `.get()`). It's called with the `path`
and `request` provided to `fetch`, with a guaranteed non-empty `headers` and
`params` objects. If a `Promise` is returned from this method it will wait until
the promise is completed to continue executing the request.

##### `cacheOptionsFor`
Allows setting the `CacheOptions` to be used for each request/response in the HTTPCache. This is separate from the request-only cache. You can use this to set the TTL.
Expand Down Expand Up @@ -180,7 +184,7 @@ You can easily set a header on every request:

```javascript
class PersonalizationAPI extends RESTDataSource {
willSendRequest(request) {
willSendRequest(path, request) {
request.headers['authorization'] = this.context.token;
}
}
Expand All @@ -190,13 +194,13 @@ Or add a query parameter:

```javascript
class PersonalizationAPI extends RESTDataSource {
willSendRequest(request) {
willSendRequest(path, request) {
request.params.set('api_key', this.context.token);
}
}
```

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

Expand All @@ -207,7 +211,7 @@ class PersonalizationAPI extends RESTDataSource {
super();
}

override willSendRequest(request: AugmentedRequest) {
override willSendRequest(_path: string, request: AugmentedRequest) {
request.headers['authorization'] = this.token;
}
}
Expand All @@ -223,7 +227,7 @@ class PersonalizationAPI extends RESTDataSource {
super();
}

override async resolveURL(path: string) {
override async resolveURL(path: string, _request: AugmentedRequest) {
if (!this.baseURL) {
const addresses = await resolveSrv(path.split("/")[1] + ".service.consul");
this.baseURL = addresses[0];
Expand Down