Skip to content

Commit

Permalink
feat(): allow http response interceptor to access request config
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Aug 25, 2023
1 parent ab2c712 commit ca53f6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 8 additions & 8 deletions packages/http/src/InterceptorManager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { HttpError } from "./http.js";

export interface InterceptorHandlers<T> {
fulfilled?: (config: T) => void;
rejected?: (error: HttpError) => void;
export interface InterceptorHandlers<T, C = never> {
fulfilled?: (value: T, config: C) => T | Promise<T>;
rejected?: (error: HttpError, config: C) => HttpError | Promise<HttpError>;
}

export default class InterceptorManager<T> {
handlers: (InterceptorHandlers<T> | null)[] = [];
export default class InterceptorManager<T, C = never> {
handlers: (InterceptorHandlers<T, C> | null)[] = [];

use(
onFulfilled?: (value: T) => T | Promise<T>,
onRejected?: (error: HttpError) => HttpError | Promise<HttpError>
onFulfilled?: (value: T, config: C) => T | Promise<T>,
onRejected?: (error: HttpError, config: C) => HttpError | Promise<HttpError>
): number {
this.handlers.push({
fulfilled: onFulfilled,
Expand All @@ -27,7 +27,7 @@ export default class InterceptorManager<T> {
}
}

forEach(fn: (h: InterceptorHandlers<T>) => void): void {
forEach(fn: (h: InterceptorHandlers<T, C>) => void): void {
this.handlers.forEach((handler) => {
// istanbul ignore else
if (handler !== null) {
Expand Down
17 changes: 11 additions & 6 deletions packages/http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface HttpResponse<T = unknown> {
status: number;
statusText: string;
headers: Headers;
config: HttpRequestConfig;
}

export interface HttpError {
Expand Down Expand Up @@ -137,7 +136,6 @@ const request = async <T>(
}

const res: HttpResponse<T> = {
config,
status: response.status,
statusText: response.statusText,
headers: response.headers,
Expand Down Expand Up @@ -214,11 +212,13 @@ const simpleRequest = <T = unknown>(
): Promise<HttpResponse<T>> => {
const {
params,
/* eslint-disable @typescript-eslint/no-unused-vars */
responseType,
interceptorParams,
observe,
noAbortOnRouteChange,
useCache,
/* eslint-enable @typescript-eslint/no-unused-vars */
...requestInit
} = config.options || {};
return request<T>(
Expand All @@ -239,12 +239,14 @@ const requestWithBody = <T = unknown>(
): Promise<HttpResponse<T>> => {
const {
params,
headers,
/* eslint-disable @typescript-eslint/no-unused-vars */
responseType,
interceptorParams,
observe,
noAbortOnRouteChange,
useCache,
headers,
/* eslint-enable @typescript-eslint/no-unused-vars */
...requestInit
} = config.options || {};
return request<T>(
Expand Down Expand Up @@ -273,7 +275,7 @@ const defaultAdapter: HttpAdapter = <T>(config: HttpRequestConfig) => {
class Http {
public readonly interceptors: {
request: InterceptorManager<HttpRequestConfig>;
response: InterceptorManager<HttpResponse>;
response: InterceptorManager<HttpResponse, HttpRequestConfig>;
};

#adapter: HttpAdapter = defaultAdapter;
Expand Down Expand Up @@ -331,12 +333,15 @@ class Http {
chain.push((config: HttpRequestConfig) => this.#adapter(config), undefined);

this.interceptors.response.forEach((interceptor) => {
chain.push(interceptor.fulfilled, interceptor.rejected);
chain.push(
(res: HttpResponse) => interceptor.fulfilled?.(res, config),
(error: HttpError) => interceptor.rejected?.(error, config)
);
});

chain.push(
(response: HttpResponse) => {
return response.config.options?.observe === "response"
return config.options?.observe === "response"
? response
: response.data;
},
Expand Down

0 comments on commit ca53f6d

Please sign in to comment.