Skip to content

Commit

Permalink
#179 👹 add new response interceptor params: 'getRequestHeader' and 'g…
Browse files Browse the repository at this point in the history
…etRequestHeaders'
  • Loading branch information
MiaInturi committed Jul 1, 2024
1 parent 257b9b2 commit cbd573a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ interface CallResponseInterceptorsParams {
export const callResponseInterceptors = async (params: CallResponseInterceptorsParams) => {
const { data, request, response, interceptors } = params;

const getRequestHeader = (field: string) => request.headers[field];
const getRequestHeaders = () => request.headers;

const getResponseHeader = (field: string) => response.getHeader(field);
const getResponseHeaders = () => response.getHeaders();

const setHeader = (field: string, value?: string | string[]) => {
response.set(field, value);
};
Expand Down Expand Up @@ -55,6 +59,8 @@ export const callResponseInterceptors = async (params: CallResponseInterceptorsP
setStatusCode,
setHeader,
appendHeader,
getRequestHeader,
getRequestHeaders,
getResponseHeader,
getResponseHeaders,
setCookie,
Expand Down
18 changes: 10 additions & 8 deletions src/utils/types/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { CookieOptions, Request, Response } from 'express';

type RequestInterceptorCookieValue = string | undefined;
type RequestInterceptorHeaderValue = string | number | string[] | undefined;
type InterceptorCookieValue = string | undefined;
type InterceptorHeaderValue = string | number | string[] | undefined;
export interface RequestInterceptorParams {
request: Request;
setDelay: (delay: number) => Promise<void>;
getCookie: (name: string) => RequestInterceptorCookieValue;
getHeader: (field: string) => RequestInterceptorHeaderValue;
getHeaders: () => Record<string, RequestInterceptorHeaderValue>;
getCookie: (name: string) => InterceptorCookieValue;
getHeader: (field: string) => InterceptorHeaderValue;
getHeaders: () => Record<string, InterceptorHeaderValue>;
}

export type RequestInterceptor = (params: RequestInterceptorParams) => void | Promise<void>;
Expand All @@ -19,10 +19,12 @@ export interface ResponseInterceptorParams {
setStatusCode: (statusCode: number) => void;
setHeader: (field: string, value?: string | string[]) => void;
appendHeader: (field: string, value?: string[] | string) => void;
getResponseHeader: (field: string) => RequestInterceptorHeaderValue;
getResponseHeaders: () => Record<string, RequestInterceptorHeaderValue>;
getRequestHeader: (field: string) => InterceptorHeaderValue;
getRequestHeaders: () => Record<string, InterceptorHeaderValue>;
getResponseHeader: (field: string) => InterceptorHeaderValue;
getResponseHeaders: () => Record<string, InterceptorHeaderValue>;
setCookie: (name: string, value: string, options?: CookieOptions) => void;
getCookie: (name: string) => RequestInterceptorCookieValue;
getCookie: (name: string) => InterceptorCookieValue;
clearCookie: (name: string, options?: CookieOptions) => void;
attachment: (filename: string) => void;
}
Expand Down

0 comments on commit cbd573a

Please sign in to comment.