Skip to content

Commit 7449cec

Browse files
committed
Add ability to read original response
Fixes #176
1 parent 07fe4c8 commit 7449cec

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/helpers/response.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ export type ApiResponse<T> =
55
| {
66
type: 'success';
77
response: T;
8+
originalResponse: Response;
89
errors?: never;
910
status: number;
1011
}
1112
| {
1213
type: 'error';
1314
source: ErrorSource;
1415
response?: never;
16+
originalResponse: Response;
1517
errors: Errors;
1618
status: number;
1719
};
@@ -27,13 +29,15 @@ export const handleFetchResponse = <ResponseType>(handleResponse: HandleResponse
2729
type: 'success',
2830
status: response.status,
2931
response: handledResponse,
32+
originalResponse: response,
3033
}),
3134
)
3235
: getJsonResponse(response).then(
3336
(jsonResponse): ApiResponse<ResponseType> => ({
3437
type: 'error',
3538
status: response.status,
3639
...getErrorForBadStatusCode(jsonResponse),
40+
originalResponse: response,
3741
}),
3842
)
3943
).catch(error => {
@@ -51,6 +55,7 @@ export const handleFetchResponse = <ResponseType>(handleResponse: HandleResponse
5155
type: 'error',
5256
source: 'decoding',
5357
status: response.status,
58+
originalResponse: response,
5459
errors: [error.message],
5560
};
5661
} else {

tests/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,10 @@ describe('requestParams', () => {
193193
});
194194
});
195195
});
196+
197+
describe('ApiResponse', () => {
198+
const api = createApi({ accessKey: 'MY_ACCESS_KEY' });
199+
api.topics.get({ topicIdOrSlug: 'foo' }).then(apiResponse => {
200+
apiResponse.originalResponse.headers.get('content-type');
201+
});
202+
});

0 commit comments

Comments
 (0)