Skip to content

Commit a76f7ae

Browse files
committed
fix: keep browser routing helpers out of generated code
Restore the generated internal types file so browser routing changes stay in custom code only. Move joinURL into lib and keep browser.fetch limited to the SDK's existing HTTPMethod union. Made-with: Cursor
1 parent 2d0056e commit a76f7ae

5 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/internal/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
export type PromiseOrValue<T> = T | Promise<T>;
4-
export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' | 'options';
4+
export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
55

66
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
77

@@ -64,18 +64,14 @@ type OverloadedParameters<T> =
6464
* [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
6565
*/
6666
/** @ts-ignore For users with \@types/node */
67-
// prettier-ignore
6867
type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit>;
6968
/** @ts-ignore For users with undici */
70-
// prettier-ignore
7169
type UndiciRequestInit = NotAny<import('../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici/index.d.ts').RequestInit>;
7270
/** @ts-ignore For users with \@types/bun */
7371
type BunRequestInit = globalThis.FetchRequestInit;
7472
/** @ts-ignore For users with node-fetch@2 */
75-
// prettier-ignore
7673
type NodeFetch2RequestInit = NotAny<import('../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit>;
7774
/** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */
78-
// prettier-ignore
7975
type NodeFetch3RequestInit = NotAny<import('../node_modules/node-fetch').RequestInit> | NotAny<import('../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
8076
/** @ts-ignore For users who use Deno */
8177
type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;

src/lib/browser-fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { KernelError } from '../core/error';
33
import { buildHeaders } from '../internal/headers';
44
import type { FinalRequestOptions, RequestOptions } from '../internal/request-options';
55
import type { HTTPMethod } from '../internal/types';
6-
import { joinURL } from '../internal/utils/url';
6+
import { joinURL } from './join-url';
77
import type { Kernel } from '../client';
88

99
export interface BrowserFetchInit extends RequestInit {
@@ -54,7 +54,7 @@ export async function browserFetch(
5454

5555
function normalizeMethod(method: string): HTTPMethod {
5656
const methodLower = method.toLowerCase();
57-
const allowed = new Set(['get', 'post', 'put', 'patch', 'delete', 'head', 'options']);
57+
const allowed = new Set(['get', 'post', 'put', 'patch', 'delete']);
5858
if (!allowed.has(methodLower)) {
5959
throw new KernelError(`browser.fetch unsupported HTTP method: ${method}`);
6060
}

src/lib/browser-routing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Fetch, RequestInfo, RequestInit } from '../internal/builtin-types';
2-
import { joinURL } from '../internal/utils/url';
2+
import { joinURL } from './join-url';
33

44
export type BrowserRoute = {
55
sessionId: string;

tests/lib/browser-routing.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,11 @@ describe('browser routing', () => {
279279
});
280280
});
281281

282-
test('browser.fetch supports HEAD requests', async () => {
283-
const calls: Array<{ url: string; init: RequestInit | undefined }> = [];
282+
test('browser.fetch rejects methods outside the SDK HTTPMethod union', async () => {
284283
const kernel = new Kernel({
285284
apiKey: 'k',
286285
baseURL: 'https://api.example/',
287-
fetch: async (input, init?: RequestInit) => {
288-
const url = normalizeURL(input);
289-
calls.push({ url, init });
290-
return new Response(null, { status: 204 });
291-
},
286+
fetch: async () => new Response(null, { status: 204 }),
292287
});
293288

294289
kernel.browserRouteCache.set({
@@ -297,11 +292,12 @@ describe('browser routing', () => {
297292
jwt: 'token-abc',
298293
});
299294

300-
const response = await kernel.browsers.fetch('sess-1', 'https://example.com/hello', { method: 'HEAD' });
301-
302-
expect(response.status).toBe(204);
303-
expect(calls[0]?.url).toContain('http://browser-session.test/browser/kernel/curl/raw?');
304-
expect(calls[0]?.init?.method).toBe('HEAD');
295+
await expect(
296+
kernel.browsers.fetch('sess-1', 'https://example.com/hello', { method: 'HEAD' }),
297+
).rejects.toThrow(/unsupported HTTP method/i);
298+
await expect(
299+
kernel.browsers.fetch('sess-1', 'https://example.com/hello', { method: 'OPTIONS' }),
300+
).rejects.toThrow(/unsupported HTTP method/i);
305301
});
306302

307303
test('defaults browser routing subresources to curl when env is unset', async () => {

0 commit comments

Comments
 (0)