mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
RESTDataSource: add didReceiveResponse method (#1325)
This commit is contained in:
parent
890e10b799
commit
ecc56690df
1 changed files with 12 additions and 7 deletions
|
@ -48,6 +48,17 @@ export abstract class RESTDataSource<TContext = any> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async didReceiveResponse<TResult = any>(
|
||||||
|
response: Response,
|
||||||
|
): Promise<TResult> {
|
||||||
|
const contentType = response.headers.get('Content-Type');
|
||||||
|
if (contentType && contentType.startsWith('application/json')) {
|
||||||
|
return response.json();
|
||||||
|
} else {
|
||||||
|
return response.text() as Promise<any>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected async didReceiveErrorResponse<TResult = any>(
|
protected async didReceiveErrorResponse<TResult = any>(
|
||||||
response: Response,
|
response: Response,
|
||||||
): Promise<TResult> {
|
): Promise<TResult> {
|
||||||
|
@ -156,13 +167,7 @@ export abstract class RESTDataSource<TContext = any> {
|
||||||
return this.trace(`${options.method || 'GET'} ${url}`, async () => {
|
return this.trace(`${options.method || 'GET'} ${url}`, async () => {
|
||||||
const response = await this.httpCache.fetch(request);
|
const response = await this.httpCache.fetch(request);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const contentType = response.headers.get('Content-Type');
|
return this.didReceiveResponse(response);
|
||||||
|
|
||||||
if (contentType && contentType.startsWith('application/json')) {
|
|
||||||
return response.json();
|
|
||||||
} else {
|
|
||||||
return response.text();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return this.didReceiveErrorResponse(response);
|
return this.didReceiveErrorResponse(response);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue