Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 76df9e6

Browse files
committed
sdk: update request path fix to use new hook type
The previous workaround here didn't work with using the SDK in Browsers: Firefox would silently drop the `body` from the request; Chrome would throw an exception about a missing `duplex` setting. Signed-off-by: Stephan Renatus <stephan@styra.com>
1 parent 5d9e0d3 commit 76df9e6

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/hooks/registration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export function initHooks(hooks: Hooks) {
55
// Add hooks by calling hooks.register{ClientInit/BeforeRequest/AfterRequest/AfterError}Hook
66
// with an instance of a hook that implements that specific Hook interface
77
// Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance
8-
hooks.registerBeforeRequestHook(new RewriteRequestPathHook());
8+
hooks.registerBeforeCreateRequestHook(new RewriteRequestPathHook());
99
}

src/hooks/request-path-hook.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { BeforeRequestContext, BeforeRequestHook } from "./types";
1+
import { BeforeCreateRequestContext, BeforeCreateRequestHook } from "./types";
2+
import { RequestInput } from "../lib/http";
23

3-
export class RewriteRequestPathHook implements BeforeRequestHook {
4-
beforeRequest(_hookCtx: BeforeRequestContext, request: Request): Request {
5-
const url = new URL(request.url);
4+
export class RewriteRequestPathHook implements BeforeCreateRequestHook {
5+
beforeCreateRequest(
6+
_hookCtx: BeforeCreateRequestContext,
7+
input: RequestInput,
8+
): RequestInput {
9+
const url = new URL(input.url);
610
if (url.pathname.startsWith("/v1/data")) {
7-
return new Request(decodeURIComponent(request.url), request);
11+
url.pathname = decodeURIComponent(url.pathname);
12+
return { ...input, url };
813
}
9-
return request;
14+
return input;
1015
}
1116
}

0 commit comments

Comments
 (0)