Skip to content

Commit 9bcfff5

Browse files
committed
Implement req in beforeResponse #80
1 parent c9c4438 commit 9bcfff5

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/rules/requests/request-handler-definitions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ export interface PassThroughHandlerOptions extends PassThroughHandlerConnectionO
560560
*
561561
* See {@link CallbackResponseMessageResult} for the possible fields that can be set.
562562
*/
563-
beforeResponse?: (res: PassThroughResponse) => MaybePromise<CallbackResponseResult | void> | void;
563+
beforeResponse?: (res: PassThroughResponse, req: CompletedRequest) => MaybePromise<CallbackResponseResult | void> | void;
564564
}
565565

566566
export interface RequestTransform {
@@ -754,7 +754,7 @@ export interface BeforePassthroughRequestRequest {
754754
* @internal
755755
*/
756756
export interface BeforePassthroughResponseRequest {
757-
args: [Replace<PassThroughResponse, { body: string }>];
757+
args: [Replace<PassThroughResponse, { body: string }>, Replace<CompletedRequest, { body: string }>];
758758
}
759759

760760
/**
@@ -780,7 +780,7 @@ export class PassThroughHandlerDefinition extends Serializable implements Reques
780780

781781
public readonly beforeRequest?: (req: CompletedRequest) =>
782782
MaybePromise<CallbackRequestResult | void> | void;
783-
public readonly beforeResponse?: (res: PassThroughResponse) =>
783+
public readonly beforeResponse?: (res: PassThroughResponse, req: CompletedRequest) =>
784784
MaybePromise<CallbackResponseResult | void> | void;
785785

786786
public readonly proxyConfig?: ProxyConfig;
@@ -925,7 +925,8 @@ export class PassThroughHandlerDefinition extends Serializable implements Reques
925925
CallbackResponseResult | undefined
926926
>('beforeResponse', async (req) => {
927927
const callbackResult = await this.beforeResponse!(
928-
withDeserializedBodyReader(req.args[0])
928+
withDeserializedBodyReader(req.args[0]),
929+
withDeserializedBodyReader(req.args[1]),
929930
);
930931

931932
if (typeof callbackResult === 'string') {

src/rules/requests/request-handlers.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,13 +924,27 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
924924
originalBody = await streamToBuffer(serverRes);
925925
let serverHeaders = rawHeadersToObject(serverRawHeaders);
926926

927+
let reqHeader = rawHeadersToObjectPreservingCase(rawHeaders);
927928
modifiedRes = await this.beforeResponse({
928929
id: clientReq.id,
929930
statusCode: serverStatusCode,
930931
statusMessage: serverRes.statusMessage,
931932
headers: serverHeaders,
932933
rawHeaders: _.cloneDeep(serverRawHeaders),
933934
body: buildBodyReader(originalBody, serverHeaders)
935+
}, {
936+
id: clientReq.id,
937+
protocol: protocol?.replace(':', '') ?? '',
938+
method: method,
939+
url: reqUrl,
940+
path: path ?? '',
941+
headers: reqHeader,
942+
rawHeaders: rawHeaders,
943+
timingEvents: clientReq.timingEvents,
944+
tags: clientReq.tags,
945+
body: buildBodyReader(reqBodyOverride ? Buffer.from(reqBodyOverride.buffer) : await clientReq.body.asDecodedBuffer(), reqHeader),
946+
rawTrailers: clientReq.rawTrailers ?? [],
947+
trailers: rawHeadersToObject(clientReq.rawTrailers ?? []),
934948
});
935949

936950
if (modifiedRes === 'close') {
@@ -1214,17 +1228,17 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
12141228
};
12151229
}
12161230

1217-
let beforeResponse: ((res: PassThroughResponse) => MaybePromise<CallbackResponseResult | void>) | undefined;
1231+
let beforeResponse: ((res: PassThroughResponse, req: CompletedRequest) => MaybePromise<CallbackResponseResult | void>) | undefined;
12181232
if (data.hasBeforeResponseCallback) {
1219-
beforeResponse = async (res: PassThroughResponse) => {
1233+
beforeResponse = async (res: PassThroughResponse, req: CompletedRequest) => {
12201234
const callbackResult = await channel.request<
12211235
BeforePassthroughResponseRequest,
12221236
| WithSerializedCallbackBuffers<CallbackResponseMessageResult>
12231237
| 'close'
12241238
| 'reset'
12251239
| undefined
12261240
>('beforeResponse', {
1227-
args: [withSerializedBodyReader(res)]
1241+
args: [withSerializedBodyReader(res), withSerializedBodyReader(req)]
12281242
})
12291243

12301244
if (callbackResult && typeof callbackResult !== 'string') {

0 commit comments

Comments
 (0)