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

feat: add ability to customize debugHeaders via option proxyDebugHeaders #52

Merged
merged 3 commits into from
Apr 17, 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
11 changes: 11 additions & 0 deletions lib/components/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,21 @@ export default function createGrpcAction<Context extends GatewayContext>(
'x-request-id': requestId,
'x-gateway-version': VERSION,
};

if ('protoPath' in config) {
debugHeaders['x-api-request-protopath'] = config.protoPath;
}

if (typeof options.proxyDebugHeaders === 'function') {
Object.assign(debugHeaders, options.proxyDebugHeaders({...headers}, 'grpc'));
} else if (Array.isArray(options.proxyDebugHeaders)) {
for (const headerName of options.proxyDebugHeaders) {
if (headers[headerName] !== undefined) {
debugHeaders[`x-gateway-${headerName}`] = headers[headerName];
}
}
}

ctx.log('Initiating request', {debugHeaders: sanitizeDebugHeaders(debugHeaders)});

const sendStats = (status: number, data: any) => {
Expand Down
10 changes: 10 additions & 0 deletions lib/components/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ export default function createRestAction<Context extends GatewayContext>(
debugHeaders['x-api-content-type'] = headers['content-type'];
}

if (typeof options.proxyDebugHeaders === 'function') {
Object.assign(debugHeaders, options.proxyDebugHeaders({...requestHeaders}, 'rest'));
} else if (Array.isArray(options.proxyDebugHeaders)) {
for (const headerName of options.proxyDebugHeaders) {
if (headers[headerName] !== undefined) {
debugHeaders[`x-gateway-${headerName}`] = headers[headerName];
}
}
}

const startRequestTime = Date.now();

let axiosClient = defaultAxiosClient;
Expand Down
2 changes: 2 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function createApiAction<
timeout: config.timeout,
sendStats: config.sendStats,
proxyHeaders: config.proxyHeaders,
proxyDebugHeaders: config.proxyDebugHeaders,
axiosConfig: config.axiosConfig,
validationSchema: config.validationSchema,
encodePathArgs: config.encodePathArgs,
Expand All @@ -126,6 +127,7 @@ function createApiAction<
timeout: config.timeout,
sendStats: config.sendStats,
proxyHeaders: config.proxyHeaders,
proxyDebugHeaders: config.proxyDebugHeaders,
grpcOptions: config.grpcOptions,
grpcRecreateService,
getAuthHeaders: config.getAuthHeaders,
Expand Down
6 changes: 4 additions & 2 deletions lib/models/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type ProxyResponseHeadersFunction = (headers: Headers, type: ControllerTy
export type ProxyResponseHeaders = string[] | ProxyResponseHeadersFunction;

export type GetAuthHeadersParams<AuthArgs = Record<string, unknown>> = {
actionType: 'rest' | 'grpc';
actionType: ControllerType;
serviceName: string;
requestHeaders: Headers;
authArgs: AuthArgs | undefined;
Expand All @@ -110,6 +110,7 @@ export interface GatewayApiOptions<Context extends GatewayContext> {
grpcRecreateService?: boolean;
axiosConfig?: AxiosRequestConfig;
proxyHeaders?: ProxyHeaders;
proxyDebugHeaders?: ProxyHeaders;
validationSchema?: object;
encodePathArgs?: boolean;
expectedResponseContentType?: ResponseContentType | ResponseContentType[];
Expand Down Expand Up @@ -440,7 +441,8 @@ export interface GatewayConfig<
includeProtoRoots?: string[];
caCertificatePath: string | null;
proxyHeaders: ProxyHeaders;
withDebugHeaders: boolean | ((req: Req, res: Res) => boolean);
proxyDebugHeaders?: ProxyHeaders;
withDebugHeaders?: boolean | ((req: Req, res: Res) => boolean);
validationSchema?: object;
encodePathArgs?: boolean;
getAuthArgs: (req: Req, res: Res) => Record<string, unknown> | undefined;
Expand Down
Loading