Skip to content
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
6 changes: 5 additions & 1 deletion internal/tsemit/templates/api.ts.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type FetchInterceptorChain = {

export type ClientOptions = {
baseURL?: string;
fetch?: typeof globalThis.fetch;
interceptors?: FetchInterceptor[];
responseTimeoutMs?: number;
sseIdleTimeoutMs?: number;
Expand Down Expand Up @@ -489,6 +490,7 @@ async function* parseEventStream<T>(
async function runInterceptors(
request: Request,
interceptors: FetchInterceptor[],
fetch: typeof globalThis.fetch,
): Promise<Response> {
async function dispatch(index: number, nextRequest: Request): Promise<Response> {
const interceptor = interceptors[index];
Expand All @@ -511,6 +513,7 @@ async function runInterceptors(

{{end}}export class DefaultApi {
private baseURL: string;
private fetch: typeof globalThis.fetch;
private interceptors: FetchInterceptor[];
private responseTimeoutMs: number | undefined;
{{if .HasSSE}}
Expand All @@ -522,6 +525,7 @@ async function runInterceptors(

constructor(options: ClientOptions = {}) {
this.baseURL = options.baseURL ?? '';
this.fetch = options.fetch ?? globalThis.fetch;
this.interceptors = options.interceptors ?? [];
this.responseTimeoutMs = configuredTimeout(
options,
Expand Down Expand Up @@ -559,7 +563,7 @@ async function runInterceptors(
};
try {
return {
response: await runInterceptors(finalRequest, this.interceptors),
response: await runInterceptors(finalRequest, this.interceptors, this.fetch),
timeout: timedRequest,
request: finalRequest,
};
Expand Down
57 changes: 57 additions & 0 deletions oasmith_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,63 @@ import assert from "node:assert/strict"
import { DefaultApi, type FetchInterceptor } from "./api.ts"

void describe("api interceptors", () => {
void test("passes the final intercepted request to a custom fetch", async () => {
let fetchedRequest: Request | undefined
const api = new DefaultApi({
baseURL: "https://example.test",
fetch: async request => {
assert.ok(request instanceof Request)
fetchedRequest = request
return new Response("[]")
},
interceptors: [
async chain => {
const headers = new Headers(chain.request.headers)
headers.set("traceparent", "custom-trace")
return await chain.proceed(new Request(chain.request, { headers }))
},
],
})

await api.listTestEmails()

assert.ok(fetchedRequest instanceof Request)
assert.equal(fetchedRequest.headers.get("traceparent"), "custom-trace")
})

void test("uses custom fetch for SSE reconnects", async () => {
let fetchCalls = 0
const encoder = new TextEncoder()
const api = new DefaultApi({
baseURL: "https://example.test",
fetch: async () => {
fetchCalls += 1
return new Response(new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode("data: {}\n\n"))
controller.close()
},
}), { headers: { "content-type": "text/event-stream" } })
},
responseTimeoutMs: 0,
sseMaxRetries: 1,
sseReconnectBaseDelayMs: 0,
})

const response = await api.episodeProcessingEventsResult({
episodeId: "episode_1",
showId: "show_1",
teamId: "team_1",
})
assert.equal(response.status, 200)
if (response.status !== 200) throw new Error("unexpected status")
const iterator = response.body[Symbol.asyncIterator]()
assert.equal((await iterator.next()).done, false)
assert.equal((await iterator.next()).done, false)
assert.equal(fetchCalls, 2)
await iterator.return?.()
})

void test("run in order and allow repeated proceed", async () => {
const events: Array<string> = []
const interceptors: Array<FetchInterceptor> = [
Expand Down
10 changes: 9 additions & 1 deletion testdata/golden/config-typescript/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type FetchInterceptorChain = {

export type ClientOptions = {
baseURL?: string
fetch?: typeof globalThis.fetch
interceptors?: FetchInterceptor[]
responseTimeoutMs?: number
sseIdleTimeoutMs?: number
Expand Down Expand Up @@ -99,6 +100,7 @@ function responseTimeout(
async function runInterceptors(
request: Request,
interceptors: FetchInterceptor[],
fetch: typeof globalThis.fetch,
): Promise<Response> {
async function dispatch(
index: number,
Expand All @@ -120,11 +122,13 @@ async function runInterceptors(

export class DefaultApi {
private baseURL: string
private fetch: typeof globalThis.fetch
private interceptors: FetchInterceptor[]
private responseTimeoutMs: number | undefined

constructor(options: ClientOptions = {}) {
this.baseURL = options.baseURL ?? ""
this.fetch = options.fetch ?? globalThis.fetch
this.interceptors = options.interceptors ?? []
this.responseTimeoutMs = configuredTimeout(
options,
Expand Down Expand Up @@ -157,7 +161,11 @@ export class DefaultApi {
}
try {
return {
response: await runInterceptors(finalRequest, this.interceptors),
response: await runInterceptors(
finalRequest,
this.interceptors,
this.fetch,
),
timeout: timedRequest,
request: finalRequest,
}
Expand Down
10 changes: 9 additions & 1 deletion testdata/golden/private-typescript/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type FetchInterceptorChain = {

export type ClientOptions = {
baseURL?: string
fetch?: typeof globalThis.fetch
interceptors?: FetchInterceptor[]
responseTimeoutMs?: number
sseIdleTimeoutMs?: number
Expand Down Expand Up @@ -555,6 +556,7 @@ async function* parseEventStream<T>(
async function runInterceptors(
request: Request,
interceptors: FetchInterceptor[],
fetch: typeof globalThis.fetch,
): Promise<Response> {
async function dispatch(
index: number,
Expand Down Expand Up @@ -686,6 +688,7 @@ export interface RenderShowRSSFeedRequest {

export class DefaultApi {
private baseURL: string
private fetch: typeof globalThis.fetch
private interceptors: FetchInterceptor[]
private responseTimeoutMs: number | undefined

Expand All @@ -696,6 +699,7 @@ export class DefaultApi {

constructor(options: ClientOptions = {}) {
this.baseURL = options.baseURL ?? ""
this.fetch = options.fetch ?? globalThis.fetch
this.interceptors = options.interceptors ?? []
this.responseTimeoutMs = configuredTimeout(
options,
Expand Down Expand Up @@ -736,7 +740,11 @@ export class DefaultApi {
}
try {
return {
response: await runInterceptors(finalRequest, this.interceptors),
response: await runInterceptors(
finalRequest,
this.interceptors,
this.fetch,
),
timeout: timedRequest,
request: finalRequest,
}
Expand Down
Loading