diff --git a/.changeset/brave-cows-attend.md b/.changeset/brave-cows-attend.md new file mode 100644 index 0000000..54b1b31 --- /dev/null +++ b/.changeset/brave-cows-attend.md @@ -0,0 +1,7 @@ +--- +'@apollo/datasource-rest': minor +--- + +Rename `requestCacheEnabled` to `memoizeGetRequests`. Acknowledging this is +actually a breaking change, but this package has been live for a weekend with +nothing recommending its usage yet. diff --git a/README.md b/README.md index 1a2030c..43f8c2c 100644 --- a/README.md +++ b/README.md @@ -65,19 +65,20 @@ class MoviesAPI extends RESTDataSource { } ``` -##### `requestCacheEnabled` +##### `memoizeGetRequests` By default, `RESTDataSource` caches all outgoing GET **requests** in a separate memoized cache from the regular response cache. It makes the assumption that all responses from HTTP GET calls are cacheable by their URL. If a request is made with the same cache key (URL by default) but with an HTTP method other than GET, the cached request is then cleared. -If you would like to disable the GET request cache, set the `requestCacheEnabled` property to `false`. You might want to do this if your API is not actually cacheable or your data changes over time. +If you would like to disable the GET request cache, set the `memoizeGetRequests` property to `false`. You might want to do this if your API is not actually cacheable or your data changes over time. -```js title="requestCacheEnabled.js" +```js title="memoizeGetRequests.js" class MoviesAPI extends RESTDataSource { constructor() { super(); // Defaults to true - this.requestCacheEnabled = false; + this.memoizeGetRequests = false; } + // Outgoing requests are never cached, however the response cache is still enabled async getMovie(id) { return this.get( @@ -102,9 +103,9 @@ Allows setting the `CacheOptions` to be used for each request/response in the HT ```javascript override cacheOptionsFor() { - return { - ttl: 1 - } + return { + ttl: 1 + } } ``` diff --git a/src/RESTDataSource.ts b/src/RESTDataSource.ts index afdd2a6..23cdd4f 100644 --- a/src/RESTDataSource.ts +++ b/src/RESTDataSource.ts @@ -58,7 +58,7 @@ export abstract class RESTDataSource { httpCache: HTTPCache; memoizedResults = new Map>(); baseURL?: string; - requestCacheEnabled: boolean = true; + memoizeGetRequests: boolean = true; constructor(config?: DataSourceConfig) { this.httpCache = new HTTPCache(config?.cache, config?.fetch); @@ -260,7 +260,7 @@ export abstract class RESTDataSource { // Cache GET requests based on the calculated cache key // Disabling the request cache does not disable the response cache - if (this.requestCacheEnabled) { + if (this.memoizeGetRequests) { if (request.method === 'GET') { let promise = this.memoizedResults.get(cacheKey); if (promise) return promise; diff --git a/src/__tests__/RESTDataSource.test.ts b/src/__tests__/RESTDataSource.test.ts index 3a04107..3aef2da 100644 --- a/src/__tests__/RESTDataSource.test.ts +++ b/src/__tests__/RESTDataSource.test.ts @@ -590,7 +590,7 @@ describe('RESTDataSource', () => { it('allows disabling the GET cache', async () => { const dataSource = new (class extends RESTDataSource { override baseURL = 'https://api.example.com'; - override requestCacheEnabled = false; + override memoizeGetRequests = false; getFoo(id: number) { return this.get(`foo/${id}`); @@ -745,7 +745,7 @@ describe('RESTDataSource', () => { it('allows setting cache options for each request', async () => { const dataSource = new (class extends RESTDataSource { override baseURL = 'https://api.example.com'; - override requestCacheEnabled = false; + override memoizeGetRequests = false; getFoo(id: number) { return this.get(`foo/${id}`); @@ -772,7 +772,7 @@ describe('RESTDataSource', () => { const dataSource = new (class extends RESTDataSource { override baseURL = 'https://api.example.com'; - override requestCacheEnabled = false; + override memoizeGetRequests = false; getFoo(id: number) { return this.get(`foo/${id}`);