apollo-datasource-http
Advanced tools
Comparing version 0.3.0 to 0.4.0
import { DataSource, DataSourceConfig } from 'apollo-datasource'; | ||
import { NormalizedOptions, OptionsOfJSONResponseBody, Response } from 'got'; | ||
export declare type Request = OptionsOfJSONResponseBody | NormalizedOptions; | ||
import { NormalizedOptions, OptionsOfJSONResponseBody, Response, GotReturn as Request } from 'got'; | ||
export declare type RequestOptions = OptionsOfJSONResponseBody | NormalizedOptions; | ||
export interface LRUOptions { | ||
@@ -9,3 +9,3 @@ readonly maxAge?: number; | ||
export interface HTTPDataSourceOptions { | ||
request?: OptionsOfJSONResponseBody; | ||
requestOptions?: RequestOptions; | ||
lru?: LRUOptions; | ||
@@ -24,10 +24,10 @@ } | ||
abort(): void; | ||
protected didReceiveResponse<TResult = unknown>(response: Response<TResult>, _request: Request): Promise<Response<TResult>>; | ||
protected cacheKey(request: Request): string; | ||
protected willSendRequest?(request?: Request): Promise<void>; | ||
protected didEncounterError(_error: Error): void; | ||
protected get<TResult = unknown>(url: string, request?: Request): Promise<Response<TResult>>; | ||
protected post<TResult = unknown>(url: string, request?: Request): Promise<Response<TResult>>; | ||
protected delete<TResult = unknown>(url: string, request?: Request): Promise<Response<TResult>>; | ||
protected put<TResult = unknown>(url: string, request?: Request): Promise<Response<TResult>>; | ||
protected onResponse<TResult = unknown>(response: Response<TResult>, _request: Request): Promise<Response<TResult>>; | ||
protected onCacheKeyCalculation(requestOptions: RequestOptions): string; | ||
protected beforeRequest?(requestOptions?: RequestOptions): Promise<void>; | ||
protected onRequestError?(_error: Error, _request?: Request): Promise<void>; | ||
protected get<TResult = unknown>(url: string, requestOptions?: RequestOptions): Promise<Response<TResult>>; | ||
protected post<TResult = unknown>(url: string, requestOptions?: RequestOptions): Promise<Response<TResult>>; | ||
protected delete<TResult = unknown>(url: string, requestOptions?: RequestOptions): Promise<Response<TResult>>; | ||
protected put<TResult = unknown>(url: string, requestOptions?: RequestOptions): Promise<Response<TResult>>; | ||
private performRequest; | ||
@@ -34,0 +34,0 @@ private request; |
@@ -74,38 +74,37 @@ "use strict"; | ||
} | ||
async didReceiveResponse(response, _request) { | ||
async onResponse(response, _request) { | ||
return response; | ||
} | ||
cacheKey(request) { | ||
if (request.url) | ||
return request.url.toString(); | ||
onCacheKeyCalculation(requestOptions) { | ||
if (requestOptions.url) | ||
return requestOptions.url.toString(); | ||
throw new Error('No Cache key provided'); | ||
} | ||
didEncounterError(_error) { } | ||
async get(url, request) { | ||
async get(url, requestOptions) { | ||
return await this.request(url, { | ||
method: 'GET', | ||
...request, | ||
...requestOptions, | ||
}); | ||
} | ||
async post(url, request) { | ||
async post(url, requestOptions) { | ||
return await this.request(url, { | ||
method: 'POST', | ||
...request, | ||
...requestOptions, | ||
}); | ||
} | ||
async delete(url, request) { | ||
async delete(url, requestOptions) { | ||
return await this.request(url, { | ||
method: 'DELETE', | ||
...request, | ||
...requestOptions, | ||
}); | ||
} | ||
async put(url, request) { | ||
async put(url, requestOptions) { | ||
return await this.request(url, { | ||
method: 'PUT', | ||
...request, | ||
...requestOptions, | ||
}); | ||
} | ||
async performRequest(options) { | ||
if (this.willSendRequest != null) { | ||
await this.willSendRequest(options); | ||
if (this.beforeRequest != null) { | ||
await this.beforeRequest(options); | ||
} | ||
@@ -119,18 +118,32 @@ const cancelableRequest = got_1.default(options); | ||
const response = await cancelableRequest; | ||
return this.didReceiveResponse(response, options); | ||
return this.onResponse(response, cancelableRequest); | ||
} | ||
catch (error) { | ||
let error_ = error; | ||
this.didEncounterError(error); | ||
if (error instanceof got_1.HTTPError) { | ||
if (error.response.statusCode === 401) { | ||
error_ = new apollo_server_errors_1.AuthenticationError(error.message); | ||
const err = new apollo_server_errors_1.AuthenticationError(error.message); | ||
err.originalError = error; | ||
error_ = err; | ||
} | ||
else if (error.response.statusCode === 403) { | ||
error_ = new apollo_server_errors_1.ForbiddenError(error.message); | ||
const err = new apollo_server_errors_1.ForbiddenError(error.message); | ||
err.originalError = error; | ||
error_ = err; | ||
} | ||
else { | ||
error_ = new apollo_server_errors_1.ApolloError(error.message); | ||
const err = new apollo_server_errors_1.ApolloError(error.message); | ||
err.originalError = error; | ||
error_ = err; | ||
} | ||
} | ||
if (this.onRequestError && | ||
(error instanceof got_1.RequestError || | ||
error instanceof got_1.HTTPError || | ||
error instanceof got_1.CacheError || | ||
error instanceof got_1.UploadError || | ||
error instanceof got_1.ReadError || | ||
error instanceof got_1.MaxRedirectsError)) { | ||
await this.onRequestError(error, error.request); | ||
} | ||
throw error_; | ||
@@ -142,3 +155,3 @@ } | ||
} | ||
async request(path, request) { | ||
async request(path, requestOptions) { | ||
const options = got_1.default.mergeOptions({ | ||
@@ -152,5 +165,5 @@ cache: this.storageAdapter, | ||
}, { | ||
...this.options?.request, | ||
}, request); | ||
const cacheKey = this.cacheKey(options); | ||
...this.options?.requestOptions, | ||
}, requestOptions); | ||
const cacheKey = this.onCacheKeyCalculation(options); | ||
if (options.method === 'GET') { | ||
@@ -157,0 +170,0 @@ const cachedResponse = this.memoizedResults.get(cacheKey); |
@@ -1,3 +0,3 @@ | ||
export { HTTPDataSource, Request } from './http-data-source'; | ||
export { Response, CacheError, CancelError, MaxRedirectsError, ReadError, ParseError, UploadError, TimeoutError, RequestError, UnsupportedProtocolError, } from 'got'; | ||
export { HTTPDataSource, RequestOptions } from './http-data-source'; | ||
export { Response, CacheError, CancelError, MaxRedirectsError, ReadError, ParseError, UploadError, TimeoutError, RequestError, UnsupportedProtocolError, GotReturn as Request } from 'got'; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "apollo-datasource-http", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"author": "Dustin Deus <deusdustin@gmail.com>", | ||
@@ -19,3 +19,2 @@ "license": "MIT", | ||
"test": "ava", | ||
"bench": "npm run build && node benchmarks", | ||
"prepare": "npm run build", | ||
@@ -65,8 +64,10 @@ "release": "release-it --github.release" | ||
"devDependencies": { | ||
"@types/node": "^14.17.3", | ||
"apollo-datasource-rest": "^0.14.0", | ||
"ava": "^3.15.0", | ||
"h2url": "^0.2.0", | ||
"nock": "^13.1.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.3.1", | ||
"release-it": "^14.8.0", | ||
"release-it": "^14.9.0", | ||
"ts-node": "^10.0.0", | ||
@@ -73,0 +74,0 @@ "typescript": "^4.3.2", |
@@ -15,3 +15,3 @@ # Apollo HTTP Data Source | ||
- RFC 7234 compliant HTTP caching | ||
- LRU Cache with ttl to memoize GET requests within the same graphql request | ||
- Request Deduplication and a Resource Cache | ||
- Support [AbortController ](https://github.com/mysticatea/abort-controller) to manually cancel all running requests | ||
@@ -55,3 +55,3 @@ - Support for [Apollo Cache Storage backend](https://www.apollographql.com/docs/apollo-server/data/data-sources/#using-memcachedredis-as-a-cache-storage-backend) | ||
super({ | ||
request: { | ||
requestOptions: { | ||
timeout: 2000, | ||
@@ -67,8 +67,8 @@ http2: true, | ||
cacheKey() {} | ||
onCacheKeyCalculation() {} | ||
// lifecycle hooks for logging, tracing and request, response manipulation | ||
didEncounterError() {} | ||
async willSendRequest() {} | ||
async didReceiveResponse() {} | ||
async onRequestError() {} | ||
async beforeRequest() {} | ||
async onResponse() {} | ||
@@ -75,0 +75,0 @@ async getMovie(id) { |
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
34534
444
11