@apollo/datasource-rest
Advanced tools
Comparing version 6.1.1 to 6.2.0
@@ -10,3 +10,3 @@ /// <reference types="node" /> | ||
} | ||
export declare class HTTPCache { | ||
export declare class HTTPCache<CO extends CacheOptions = CacheOptions> { | ||
private keyValueCache; | ||
@@ -17,3 +17,3 @@ private httpFetch; | ||
cacheKey?: string; | ||
cacheOptions?: CacheOptions | ((url: string, response: FetcherResponse, request: RequestOptions) => ValueOrPromise<CacheOptions | undefined>); | ||
cacheOptions?: CO | ((url: string, response: FetcherResponse, request: RequestOptions) => ValueOrPromise<CO | undefined>); | ||
httpCacheSemanticsCachePolicyOptions?: HttpCacheSemanticsOptions; | ||
@@ -20,0 +20,0 @@ }): Promise<ResponseWithCacheWritePromise>; |
@@ -106,2 +106,3 @@ "use strict"; | ||
policy, | ||
cacheOptions, | ||
ttl, | ||
@@ -113,3 +114,3 @@ ttlOverride, | ||
} | ||
async readResponseAndWriteToCache({ response, policy, ttl, ttlOverride, cacheKey, }) { | ||
async readResponseAndWriteToCache({ response, policy, cacheOptions, ttl, ttlOverride, cacheKey, }) { | ||
const body = await response.text(); | ||
@@ -122,2 +123,3 @@ const entry = JSON.stringify({ | ||
await this.keyValueCache.set(cacheKey, entry, { | ||
...cacheOptions, | ||
ttl, | ||
@@ -124,0 +126,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
export { RESTDataSource, RequestOptions, AugmentedRequest, DataSourceFetchResult, DataSourceConfig, GetRequest, PatchRequest, PostRequest, PutRequest, DeleteRequest, HeadRequest, RequestWithoutBody, RequestWithBody, DataSourceRequest, } from './RESTDataSource'; | ||
export { RESTDataSource, RequestOptions, AugmentedRequest, DataSourceFetchResult, DataSourceConfig, GetRequest, PatchRequest, PostRequest, PutRequest, DeleteRequest, HeadRequest, RequestWithoutBody, RequestWithBody, DataSourceRequest, CacheOptions, } from './RESTDataSource'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,35 +10,35 @@ /// <reference types="node" /> | ||
export type ValueOrPromise<T> = T | Promise<T>; | ||
export type RequestOptions = FetcherRequestInit & { | ||
export type RequestOptions<CO extends CacheOptions = CacheOptions> = FetcherRequestInit & { | ||
params?: Record<string, string | undefined> | URLSearchParams; | ||
cacheKey?: string; | ||
cacheOptions?: CacheOptions | ((url: string, response: FetcherResponse, request: RequestOptions) => Promise<CacheOptions | undefined>); | ||
cacheOptions?: CO | ((url: string, response: FetcherResponse, request: RequestOptions) => Promise<CO | undefined>); | ||
httpCacheSemanticsCachePolicyOptions?: HttpCacheSemanticsOptions; | ||
}; | ||
export interface HeadRequest extends RequestOptions { | ||
export interface HeadRequest<CO extends CacheOptions = CacheOptions> extends RequestOptions<CO> { | ||
method?: 'HEAD'; | ||
body?: never; | ||
} | ||
export interface GetRequest extends RequestOptions { | ||
export interface GetRequest<CO extends CacheOptions = CacheOptions> extends RequestOptions<CO> { | ||
method?: 'GET'; | ||
body?: never; | ||
} | ||
interface WithBody extends Omit<RequestOptions, 'body'> { | ||
interface WithBody<CO extends CacheOptions = CacheOptions> extends Omit<RequestOptions<CO>, 'body'> { | ||
body?: FetcherRequestInit['body'] | object; | ||
} | ||
export interface PostRequest extends WithBody { | ||
export interface PostRequest<CO extends CacheOptions = CacheOptions> extends WithBody<CO> { | ||
method?: 'POST'; | ||
} | ||
export interface PutRequest extends WithBody { | ||
export interface PutRequest<CO extends CacheOptions = CacheOptions> extends WithBody<CO> { | ||
method?: 'PUT'; | ||
} | ||
export interface PatchRequest extends WithBody { | ||
export interface PatchRequest<CO extends CacheOptions = CacheOptions> extends WithBody<CO> { | ||
method?: 'PATCH'; | ||
} | ||
export interface DeleteRequest extends WithBody { | ||
export interface DeleteRequest<CO extends CacheOptions = CacheOptions> extends WithBody<CO> { | ||
method?: 'DELETE'; | ||
} | ||
export type RequestWithoutBody = HeadRequest | GetRequest; | ||
export type RequestWithBody = PostRequest | PutRequest | PatchRequest | DeleteRequest; | ||
export type DataSourceRequest = RequestWithoutBody | RequestWithBody; | ||
export type AugmentedRequest = (Omit<WithRequired<RequestWithoutBody, 'headers'>, 'params'> | Omit<WithRequired<RequestWithBody, 'headers'>, 'params'>) & { | ||
export type RequestWithoutBody<CO extends CacheOptions = CacheOptions> = HeadRequest<CO> | GetRequest<CO>; | ||
export type RequestWithBody<CO extends CacheOptions = CacheOptions> = PostRequest<CO> | PutRequest<CO> | PatchRequest<CO> | DeleteRequest<CO>; | ||
export type DataSourceRequest<CO extends CacheOptions = CacheOptions> = RequestWithoutBody<CO> | RequestWithBody<CO>; | ||
export type AugmentedRequest<CO extends CacheOptions = CacheOptions> = (Omit<WithRequired<RequestWithoutBody<CO>, 'headers'>, 'params'> | Omit<WithRequired<RequestWithBody<CO>, 'headers'>, 'params'>) & { | ||
params: URLSearchParams; | ||
@@ -77,4 +77,4 @@ }; | ||
}; | ||
export declare abstract class RESTDataSource { | ||
protected httpCache: HTTPCache; | ||
export declare abstract class RESTDataSource<CO extends CacheOptions = CacheOptions> { | ||
protected httpCache: HTTPCache<CO>; | ||
protected deduplicationPromises: Map<string, Promise<any>>; | ||
@@ -88,3 +88,3 @@ baseURL?: string; | ||
protected resolveURL(path: string, _request: AugmentedRequest): ValueOrPromise<URL>; | ||
protected cacheOptionsFor?(url: string, response: FetcherResponse, request: FetcherRequestInit): ValueOrPromise<CacheOptions | undefined>; | ||
protected cacheOptionsFor?(url: string, response: FetcherResponse, request: FetcherRequestInit): ValueOrPromise<CO | undefined>; | ||
protected didEncounterError(_error: Error, _request: RequestOptions, _url?: URL): void; | ||
@@ -107,10 +107,10 @@ protected parseBody(response: FetcherResponse): Promise<object | string>; | ||
}): Promise<GraphQLError>; | ||
protected head(path: string, request?: HeadRequest): Promise<FetcherResponse>; | ||
protected get<TResult = any>(path: string, request?: GetRequest): Promise<TResult>; | ||
protected post<TResult = any>(path: string, request?: PostRequest): Promise<TResult>; | ||
protected patch<TResult = any>(path: string, request?: PatchRequest): Promise<TResult>; | ||
protected put<TResult = any>(path: string, request?: PutRequest): Promise<TResult>; | ||
protected delete<TResult = any>(path: string, request?: DeleteRequest): Promise<TResult>; | ||
protected head(path: string, request?: HeadRequest<CO>): Promise<FetcherResponse>; | ||
protected get<TResult = any>(path: string, request?: GetRequest<CO>): Promise<TResult>; | ||
protected post<TResult = any>(path: string, request?: PostRequest<CO>): Promise<TResult>; | ||
protected patch<TResult = any>(path: string, request?: PatchRequest<CO>): Promise<TResult>; | ||
protected put<TResult = any>(path: string, request?: PutRequest<CO>): Promise<TResult>; | ||
protected delete<TResult = any>(path: string, request?: DeleteRequest<CO>): Promise<TResult>; | ||
private urlSearchParamsFromRecord; | ||
fetch<TResult>(path: string, incomingRequest?: DataSourceRequest): Promise<DataSourceFetchResult<TResult>>; | ||
fetch<TResult>(path: string, incomingRequest?: DataSourceRequest<CO>): Promise<DataSourceFetchResult<TResult>>; | ||
protected catchCacheWritePromiseErrors(cacheWritePromise: Promise<void>): void; | ||
@@ -117,0 +117,0 @@ protected trace<TResult>(url: URL, request: RequestOptions, fn: () => Promise<TResult>): Promise<TResult>; |
{ | ||
"name": "@apollo/datasource-rest", | ||
"description": "REST DataSource for Apollo Server v4", | ||
"version": "6.1.1", | ||
"version": "6.2.0", | ||
"author": "Apollo <packages@apollographql.com>", | ||
@@ -38,12 +38,12 @@ "license": "MIT", | ||
"@changesets/cli": "2.26.2", | ||
"@types/jest": "29.5.4", | ||
"@types/jest": "29.5.5", | ||
"@types/lodash.isplainobject": "4.0.7", | ||
"@types/node": "16.18.50", | ||
"@typescript-eslint/eslint-plugin": "6.6.0", | ||
"@typescript-eslint/parser": "6.6.0", | ||
"cspell": "7.3.5", | ||
"@types/node": "16.18.52", | ||
"@typescript-eslint/eslint-plugin": "6.7.0", | ||
"@typescript-eslint/parser": "6.7.0", | ||
"cspell": "7.3.6", | ||
"eslint": "8.49.0", | ||
"form-data": "4.0.0", | ||
"graphql": "16.8.0", | ||
"jest": "29.6.4", | ||
"jest": "29.7.0", | ||
"jest-junit": "16.0.0", | ||
@@ -58,3 +58,3 @@ "nock": "13.3.3", | ||
"@apollo/utils.fetcher": "^3.0.0", | ||
"@apollo/utils.keyvaluecache": "^3.0.0", | ||
"@apollo/utils.keyvaluecache": "^3.1.0", | ||
"@apollo/utils.logger": "^3.0.0", | ||
@@ -61,0 +61,0 @@ "@apollo/utils.withrequired": "^3.0.0", |
@@ -39,8 +39,8 @@ import nodeFetch, { | ||
export class HTTPCache { | ||
private keyValueCache: KeyValueCache; | ||
export class HTTPCache<CO extends CacheOptions = CacheOptions> { | ||
private keyValueCache: KeyValueCache<string, CO>; | ||
private httpFetch: Fetcher; | ||
constructor( | ||
keyValueCache: KeyValueCache = new InMemoryLRUCache(), | ||
keyValueCache: KeyValueCache = new InMemoryLRUCache<string, CO>(), | ||
httpFetch: Fetcher = nodeFetch, | ||
@@ -61,3 +61,3 @@ ) { | ||
cacheOptions?: | ||
| CacheOptions | ||
| CO | ||
| (( | ||
@@ -67,3 +67,3 @@ url: string, | ||
request: RequestOptions, | ||
) => ValueOrPromise<CacheOptions | undefined>); | ||
) => ValueOrPromise<CO | undefined>); | ||
httpCacheSemanticsCachePolicyOptions?: HttpCacheSemanticsOptions; | ||
@@ -195,3 +195,3 @@ }, | ||
cacheOptions?: | ||
| CacheOptions | ||
| CO | ||
| (( | ||
@@ -201,3 +201,3 @@ url: string, | ||
request: RequestOptions, | ||
) => ValueOrPromise<CacheOptions | undefined>), | ||
) => ValueOrPromise<CO | undefined>), | ||
): Promise<ResponseWithCacheWritePromise> { | ||
@@ -254,2 +254,3 @@ if (typeof cacheOptions === 'function') { | ||
policy, | ||
cacheOptions, | ||
ttl, | ||
@@ -265,2 +266,3 @@ ttlOverride, | ||
policy, | ||
cacheOptions, | ||
ttl, | ||
@@ -272,2 +274,3 @@ ttlOverride, | ||
policy: CachePolicy; | ||
cacheOptions?: CO; | ||
ttl: number | null | undefined; | ||
@@ -284,5 +287,7 @@ ttlOverride: number | undefined; | ||
// Set the value into the cache, and forward all the set cache option into the setter function | ||
await this.keyValueCache.set(cacheKey, entry, { | ||
...cacheOptions, | ||
ttl, | ||
}); | ||
} as CO); | ||
} | ||
@@ -289,0 +294,0 @@ } |
@@ -16,2 +16,3 @@ export { | ||
DataSourceRequest, | ||
CacheOptions, | ||
} from './RESTDataSource'; |
@@ -16,41 +16,43 @@ import type { | ||
export type RequestOptions = FetcherRequestInit & { | ||
/** | ||
* URL search parameters can be provided either as a record object (in which | ||
* case keys with `undefined` values are ignored) or as an URLSearchParams | ||
* object. If you want to specify a parameter multiple times, use | ||
* URLSearchParams with its "array of two-element arrays" constructor form. | ||
* (The URLSearchParams object is globally available in Node, and provided to | ||
* TypeScript by @types/node.) | ||
*/ | ||
params?: Record<string, string | undefined> | URLSearchParams; | ||
/** | ||
* The default implementation of `cacheKeyFor` returns this value if it is | ||
* provided. This is used both as part of the request deduplication key and as | ||
* the key in the shared HTTP-header-sensitive cache. | ||
*/ | ||
cacheKey?: string; | ||
/** | ||
* This can be a `CacheOptions` object or a (possibly async) function | ||
* returning such an object. The details of what its fields mean are | ||
* documented under `CacheOptions`. The function is called after a real HTTP | ||
* request is made (and is not called if a response from the cache can be | ||
* returned). If this is provided, the `cacheOptionsFor` hook is not called. | ||
*/ | ||
cacheOptions?: | ||
| CacheOptions | ||
| (( | ||
url: string, | ||
response: FetcherResponse, | ||
request: RequestOptions, | ||
) => Promise<CacheOptions | undefined>); | ||
/** | ||
* If provided, this is passed through as the third argument to `new | ||
* CachePolicy()` from the `http-cache-semantics` npm package as part of the | ||
* HTTP-header-sensitive cache. | ||
*/ | ||
httpCacheSemanticsCachePolicyOptions?: HttpCacheSemanticsOptions; | ||
}; | ||
export type RequestOptions<CO extends CacheOptions = CacheOptions> = | ||
FetcherRequestInit & { | ||
/** | ||
* URL search parameters can be provided either as a record object (in which | ||
* case keys with `undefined` values are ignored) or as an URLSearchParams | ||
* object. If you want to specify a parameter multiple times, use | ||
* URLSearchParams with its "array of two-element arrays" constructor form. | ||
* (The URLSearchParams object is globally available in Node, and provided to | ||
* TypeScript by @types/node.) | ||
*/ | ||
params?: Record<string, string | undefined> | URLSearchParams; | ||
/** | ||
* The default implementation of `cacheKeyFor` returns this value if it is | ||
* provided. This is used both as part of the request deduplication key and as | ||
* the key in the shared HTTP-header-sensitive cache. | ||
*/ | ||
cacheKey?: string; | ||
/** | ||
* This can be a `CacheOptions` object or a (possibly async) function | ||
* returning such an object. The details of what its fields mean are | ||
* documented under `CacheOptions`. The function is called after a real HTTP | ||
* request is made (and is not called if a response from the cache can be | ||
* returned). If this is provided, the `cacheOptionsFor` hook is not called. | ||
*/ | ||
cacheOptions?: | ||
| CO | ||
| (( | ||
url: string, | ||
response: FetcherResponse, | ||
request: RequestOptions, | ||
) => Promise<CO | undefined>); | ||
/** | ||
* If provided, this is passed through as the third argument to `new | ||
* CachePolicy()` from the `http-cache-semantics` npm package as part of the | ||
* HTTP-header-sensitive cache. | ||
*/ | ||
httpCacheSemanticsCachePolicyOptions?: HttpCacheSemanticsOptions; | ||
}; | ||
export interface HeadRequest extends RequestOptions { | ||
export interface HeadRequest<CO extends CacheOptions = CacheOptions> | ||
extends RequestOptions<CO> { | ||
method?: 'HEAD'; | ||
@@ -60,3 +62,4 @@ body?: never; | ||
export interface GetRequest extends RequestOptions { | ||
export interface GetRequest<CO extends CacheOptions = CacheOptions> | ||
extends RequestOptions<CO> { | ||
method?: 'GET'; | ||
@@ -66,31 +69,40 @@ body?: never; | ||
interface WithBody extends Omit<RequestOptions, 'body'> { | ||
interface WithBody<CO extends CacheOptions = CacheOptions> | ||
extends Omit<RequestOptions<CO>, 'body'> { | ||
body?: FetcherRequestInit['body'] | object; | ||
} | ||
export interface PostRequest extends WithBody { | ||
export interface PostRequest<CO extends CacheOptions = CacheOptions> | ||
extends WithBody<CO> { | ||
method?: 'POST'; | ||
} | ||
export interface PutRequest extends WithBody { | ||
export interface PutRequest<CO extends CacheOptions = CacheOptions> | ||
extends WithBody<CO> { | ||
method?: 'PUT'; | ||
} | ||
export interface PatchRequest extends WithBody { | ||
export interface PatchRequest<CO extends CacheOptions = CacheOptions> | ||
extends WithBody<CO> { | ||
method?: 'PATCH'; | ||
} | ||
export interface DeleteRequest extends WithBody { | ||
export interface DeleteRequest<CO extends CacheOptions = CacheOptions> | ||
extends WithBody<CO> { | ||
method?: 'DELETE'; | ||
} | ||
export type RequestWithoutBody = HeadRequest | GetRequest; | ||
export type RequestWithoutBody<CO extends CacheOptions = CacheOptions> = | ||
| HeadRequest<CO> | ||
| GetRequest<CO>; | ||
export type RequestWithBody = | ||
| PostRequest | ||
| PutRequest | ||
| PatchRequest | ||
| DeleteRequest; | ||
export type RequestWithBody<CO extends CacheOptions = CacheOptions> = | ||
| PostRequest<CO> | ||
| PutRequest<CO> | ||
| PatchRequest<CO> | ||
| DeleteRequest<CO>; | ||
export type DataSourceRequest = RequestWithoutBody | RequestWithBody; | ||
export type DataSourceRequest<CO extends CacheOptions = CacheOptions> = | ||
| RequestWithoutBody<CO> | ||
| RequestWithBody<CO>; | ||
@@ -105,5 +117,5 @@ // While tempting, this union can't be reduced / factored out to just | ||
*/ | ||
export type AugmentedRequest = ( | ||
| Omit<WithRequired<RequestWithoutBody, 'headers'>, 'params'> | ||
| Omit<WithRequired<RequestWithBody, 'headers'>, 'params'> | ||
export type AugmentedRequest<CO extends CacheOptions = CacheOptions> = ( | ||
| Omit<WithRequired<RequestWithoutBody<CO>, 'headers'>, 'params'> | ||
| Omit<WithRequired<RequestWithBody<CO>, 'headers'>, 'params'> | ||
) & { | ||
@@ -180,4 +192,4 @@ params: URLSearchParams; | ||
export abstract class RESTDataSource { | ||
protected httpCache: HTTPCache; | ||
export abstract class RESTDataSource<CO extends CacheOptions = CacheOptions> { | ||
protected httpCache: HTTPCache<CO>; | ||
protected deduplicationPromises = new Map<string, Promise<any>>(); | ||
@@ -188,3 +200,3 @@ baseURL?: string; | ||
constructor(config?: DataSourceConfig) { | ||
this.httpCache = new HTTPCache(config?.cache, config?.fetch); | ||
this.httpCache = new HTTPCache<CO>(config?.cache, config?.fetch); | ||
this.logger = config?.logger ?? console; | ||
@@ -268,3 +280,3 @@ } | ||
request: FetcherRequestInit, | ||
): ValueOrPromise<CacheOptions | undefined>; | ||
): ValueOrPromise<CO | undefined>; | ||
@@ -392,3 +404,3 @@ protected didEncounterError( | ||
path: string, | ||
request?: HeadRequest, | ||
request?: HeadRequest<CO>, | ||
): Promise<FetcherResponse> { | ||
@@ -400,3 +412,3 @@ return (await this.fetch(path, { method: 'HEAD', ...request })).response; | ||
path: string, | ||
request?: GetRequest, | ||
request?: GetRequest<CO>, | ||
): Promise<TResult> { | ||
@@ -413,3 +425,3 @@ return ( | ||
path: string, | ||
request?: PostRequest, | ||
request?: PostRequest<CO>, | ||
): Promise<TResult> { | ||
@@ -426,3 +438,3 @@ return ( | ||
path: string, | ||
request?: PatchRequest, | ||
request?: PatchRequest<CO>, | ||
): Promise<TResult> { | ||
@@ -439,3 +451,3 @@ return ( | ||
path: string, | ||
request?: PutRequest, | ||
request?: PutRequest<CO>, | ||
): Promise<TResult> { | ||
@@ -452,3 +464,3 @@ return ( | ||
path: string, | ||
request?: DeleteRequest, | ||
request?: DeleteRequest<CO>, | ||
): Promise<TResult> { | ||
@@ -479,3 +491,3 @@ return ( | ||
path: string, | ||
incomingRequest: DataSourceRequest = {}, | ||
incomingRequest: DataSourceRequest<CO> = {}, | ||
): Promise<DataSourceFetchResult<TResult>> { | ||
@@ -525,3 +537,3 @@ const downcasedHeaders: Record<string, string> = {}; | ||
const outgoingRequest = augmentedRequest as WithRequired< | ||
RequestOptions, | ||
RequestOptions<CO>, | ||
'method' | ||
@@ -528,0 +540,0 @@ >; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110034
1605