Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#179 into [email protected] 👹 add new response interceptor params: 'getRequestHeader' and 'getRequestHeaders' #183

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,51 @@ describe('callResponseInterceptors: order of calls', () => {
});

describe('callResponseInterceptors: params functions', () => {
test('Should correctly call response getHeader method when use getHeader param', async () => {
test('Should correctly get header from request.headers when use getRequestHeader param', async () => {
const data = null;
const request = { headers: { name: 'value' } };
const response = {};

const getCookieRouteInterceptor: ResponseInterceptor = (data, { getRequestHeader }) => {
expect(getRequestHeader('name')).toBe('value');
return data;
};
await callResponseInterceptors({
data,
request: request as unknown as Request,
response: response as Response,
interceptors: {
routeInterceptor: getCookieRouteInterceptor
}
});
});

test('Should correctly get headers property from request when use getRequestHeaders param', async () => {
const data = null;
const request = { headers: { name: 'value' } };
const response = {};

const getCookieRouteInterceptor: ResponseInterceptor = (data, { getRequestHeaders }) => {
expect(getRequestHeaders()).toBe(request.headers);
return data;
};
await callResponseInterceptors({
data,
request: request as unknown as Request,
response: response as Response,
interceptors: {
routeInterceptor: getCookieRouteInterceptor
}
});
});

test('Should correctly call response getHeader method when use getResponseHeader param', async () => {
const data = null;
const request = {};
const response = { getHeader: vi.fn() };

const getHeaderRouteInterceptor: ResponseInterceptor = (data, { getHeader }) => {
getHeader('header');
const getHeaderRouteInterceptor: ResponseInterceptor = (data, { getResponseHeader }) => {
getResponseHeader('header');
return data;
};
await callResponseInterceptors({
Expand All @@ -78,13 +116,13 @@ describe('callResponseInterceptors: params functions', () => {
expect(response.getHeader).toHaveBeenCalledTimes(1);
});

test('Should correctly call response getHeaders method when use getHeaders param', async () => {
test('Should correctly call response getHeaders method when use getResponseHeaders param', async () => {
const data = null;
const request = {};
const response = { getHeaders: vi.fn() };

const getHeadersRouteInterceptor: ResponseInterceptor = (data, { getHeaders }) => {
getHeaders();
const getHeadersRouteInterceptor: ResponseInterceptor = (data, { getResponseHeaders }) => {
getResponseHeaders();
return data;
};
await callResponseInterceptors({
Expand Down
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 getHeader = (field: string) => response.getHeader(field);
const getHeaders = () => response.getHeaders();
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,8 +59,10 @@ export const callResponseInterceptors = async (params: CallResponseInterceptorsP
setStatusCode,
setHeader,
appendHeader,
getHeader,
getHeaders,
getRequestHeader,
getRequestHeaders,
getResponseHeader,
getResponseHeaders,
setCookie,
getCookie,
clearCookie,
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;
getHeader: (field: string) => RequestInterceptorHeaderValue;
getHeaders: () => 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