Skip to content

Commit 29a6629

Browse files
committed
fix(client-next): build URL after request interceptors
1 parent 982e431 commit 29a6629

15 files changed

Lines changed: 233 additions & 52 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
Fix `@hey-api/client-next` request interceptors so URL mutations are reflected in the final request URL.
6+
7+
Previously, the client built the URL before request interceptors ran, so interceptor changes to `baseUrl`, `url`, `path`, or `query` were ignored by the fetch call.

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/base-url-false/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/base-url-number/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/base-url-strict/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/base-url-string/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/clean-false/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/default/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/import-file-extension-ts/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-next/sdk-client-optional/client/client.gen.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ export const createClient = (config: Config = {}): Client => {
6767
}
6868

6969
const resolvedOpts = opts as typeof opts & ResolvedRequestOptions<ThrowOnError, Url>;
70-
const url = buildUrl(resolvedOpts);
7170

72-
return { opts: resolvedOpts, url };
71+
return { opts: resolvedOpts };
7372
};
7473

7574
// @ts-expect-error
@@ -79,14 +78,18 @@ export const createClient = (config: Config = {}): Client => {
7978
let response: Response | undefined;
8079

8180
try {
82-
const { opts, url } = await beforeRequest(options);
81+
const { opts } = await beforeRequest(options);
8382

8483
for (const fn of interceptors.request.fns) {
8584
if (fn) {
8685
await fn(opts);
8786
}
8887
}
8988

89+
// Build the URL after request interceptors have run so any mutations to
90+
// `opts.baseUrl`, `opts.url`, `opts.path`, or `opts.query` are honored.
91+
const url = buildUrl(opts);
92+
9093
// fetch must be assigned here, otherwise it would throw the error:
9194
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
9295
const _fetch = opts.fetch!;
@@ -212,7 +215,11 @@ export const createClient = (config: Config = {}): Client => {
212215
request({ ...options, method });
213216

214217
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
215-
const { opts, url } = await beforeRequest(options);
218+
const { opts } = await beforeRequest(options);
219+
// The SSE path applies request interceptors again inside `onRequest`
220+
// (see below), so the URL we seed `createSseClient` with is only the
221+
// initial value; any per-request URL mutation happens there.
222+
const url = buildUrl(opts);
216223
return createSseClient({
217224
...opts,
218225
body: opts.body as BodyInit | null | undefined,

0 commit comments

Comments
 (0)