Skip to content

Commit 83c10e2

Browse files
committed
hey-api update
1 parent ac12fd7 commit 83c10e2

20 files changed

+4539
-995
lines changed

frontend/package-lock.json

Lines changed: 298 additions & 291 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
"@tailwindcss/postcss": "^4.1.13",
5151
"express": "^5.2.1",
5252
"http-proxy": "^1.18.1",
53-
"@hey-api/client-fetch": "^0.8.4",
54-
"@hey-api/openapi-ts": "^0.66.1",
53+
"@hey-api/openapi-ts": "0.89.1",
5554
"rollup-plugin-serve": "^1.1.1",
5655
"tailwindcss": "^4.1.13",
5756
"tslib": "^2.8.1",

frontend/src/lib/api/client.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { ClientOptions } from './types.gen';
4-
import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from '@hey-api/client-fetch';
3+
import { type ClientOptions, type Config, createClient, createConfig } from './client';
4+
import type { ClientOptions as ClientOptions2 } from './types.gen';
55

66
/**
77
* The `createClientConfig()` function will be called on client initialization
@@ -11,6 +11,6 @@ import { type Config, type ClientOptions as DefaultClientOptions, createClient,
1111
* `setConfig()`. This is useful for example if you're using Next.js
1212
* to ensure your client always has the correct values.
1313
*/
14-
export type CreateClientConfig<T extends DefaultClientOptions = ClientOptions> = (override?: Config<DefaultClientOptions & T>) => Config<Required<DefaultClientOptions> & T>;
14+
export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
1515

16-
export const client = createClient(createConfig<ClientOptions>());
16+
export const client = createClient(createConfig<ClientOptions2>());
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { createSseClient } from '../core/serverSentEvents.gen';
4+
import type { HttpMethod } from '../core/types.gen';
5+
import { getValidRequestBody } from '../core/utils.gen';
6+
import type {
7+
Client,
8+
Config,
9+
RequestOptions,
10+
ResolvedRequestOptions,
11+
} from './types.gen';
12+
import {
13+
buildUrl,
14+
createConfig,
15+
createInterceptors,
16+
getParseAs,
17+
mergeConfigs,
18+
mergeHeaders,
19+
setAuthParams,
20+
} from './utils.gen';
21+
22+
type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
23+
body?: any;
24+
headers: ReturnType<typeof mergeHeaders>;
25+
};
26+
27+
export const createClient = (config: Config = {}): Client => {
28+
let _config = mergeConfigs(createConfig(), config);
29+
30+
const getConfig = (): Config => ({ ..._config });
31+
32+
const setConfig = (config: Config): Config => {
33+
_config = mergeConfigs(_config, config);
34+
return getConfig();
35+
};
36+
37+
const interceptors = createInterceptors<
38+
Request,
39+
Response,
40+
unknown,
41+
ResolvedRequestOptions
42+
>();
43+
44+
const beforeRequest = async (options: RequestOptions) => {
45+
const opts = {
46+
..._config,
47+
...options,
48+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
49+
headers: mergeHeaders(_config.headers, options.headers),
50+
serializedBody: undefined,
51+
};
52+
53+
if (opts.security) {
54+
await setAuthParams({
55+
...opts,
56+
security: opts.security,
57+
});
58+
}
59+
60+
if (opts.requestValidator) {
61+
await opts.requestValidator(opts);
62+
}
63+
64+
if (opts.body !== undefined && opts.bodySerializer) {
65+
opts.serializedBody = opts.bodySerializer(opts.body);
66+
}
67+
68+
// remove Content-Type header if body is empty to avoid sending invalid requests
69+
if (opts.body === undefined || opts.serializedBody === '') {
70+
opts.headers.delete('Content-Type');
71+
}
72+
73+
const url = buildUrl(opts);
74+
75+
return { opts, url };
76+
};
77+
78+
const request: Client['request'] = async (options) => {
79+
// @ts-expect-error
80+
const { opts, url } = await beforeRequest(options);
81+
const requestInit: ReqInit = {
82+
redirect: 'follow',
83+
...opts,
84+
body: getValidRequestBody(opts),
85+
};
86+
87+
let request = new Request(url, requestInit);
88+
89+
for (const fn of interceptors.request.fns) {
90+
if (fn) {
91+
request = await fn(request, opts);
92+
}
93+
}
94+
95+
// fetch must be assigned here, otherwise it would throw the error:
96+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
97+
const _fetch = opts.fetch!;
98+
let response: Response;
99+
100+
try {
101+
response = await _fetch(request);
102+
} catch (error) {
103+
// Handle fetch exceptions (AbortError, network errors, etc.)
104+
let finalError = error;
105+
106+
for (const fn of interceptors.error.fns) {
107+
if (fn) {
108+
finalError = (await fn(
109+
error,
110+
undefined as any,
111+
request,
112+
opts,
113+
)) as unknown;
114+
}
115+
}
116+
117+
finalError = finalError || ({} as unknown);
118+
119+
if (opts.throwOnError) {
120+
throw finalError;
121+
}
122+
123+
// Return error response
124+
return opts.responseStyle === 'data'
125+
? undefined
126+
: {
127+
error: finalError,
128+
request,
129+
response: undefined as any,
130+
};
131+
}
132+
133+
for (const fn of interceptors.response.fns) {
134+
if (fn) {
135+
response = await fn(response, request, opts);
136+
}
137+
}
138+
139+
const result = {
140+
request,
141+
response,
142+
};
143+
144+
if (response.ok) {
145+
const parseAs =
146+
(opts.parseAs === 'auto'
147+
? getParseAs(response.headers.get('Content-Type'))
148+
: opts.parseAs) ?? 'json';
149+
150+
if (
151+
response.status === 204 ||
152+
response.headers.get('Content-Length') === '0'
153+
) {
154+
let emptyData: any;
155+
switch (parseAs) {
156+
case 'arrayBuffer':
157+
case 'blob':
158+
case 'text':
159+
emptyData = await response[parseAs]();
160+
break;
161+
case 'formData':
162+
emptyData = new FormData();
163+
break;
164+
case 'stream':
165+
emptyData = response.body;
166+
break;
167+
case 'json':
168+
default:
169+
emptyData = {};
170+
break;
171+
}
172+
return opts.responseStyle === 'data'
173+
? emptyData
174+
: {
175+
data: emptyData,
176+
...result,
177+
};
178+
}
179+
180+
let data: any;
181+
switch (parseAs) {
182+
case 'arrayBuffer':
183+
case 'blob':
184+
case 'formData':
185+
case 'json':
186+
case 'text':
187+
data = await response[parseAs]();
188+
break;
189+
case 'stream':
190+
return opts.responseStyle === 'data'
191+
? response.body
192+
: {
193+
data: response.body,
194+
...result,
195+
};
196+
}
197+
198+
if (parseAs === 'json') {
199+
if (opts.responseValidator) {
200+
await opts.responseValidator(data);
201+
}
202+
203+
if (opts.responseTransformer) {
204+
data = await opts.responseTransformer(data);
205+
}
206+
}
207+
208+
return opts.responseStyle === 'data'
209+
? data
210+
: {
211+
data,
212+
...result,
213+
};
214+
}
215+
216+
const textError = await response.text();
217+
let jsonError: unknown;
218+
219+
try {
220+
jsonError = JSON.parse(textError);
221+
} catch {
222+
// noop
223+
}
224+
225+
const error = jsonError ?? textError;
226+
let finalError = error;
227+
228+
for (const fn of interceptors.error.fns) {
229+
if (fn) {
230+
finalError = (await fn(error, response, request, opts)) as string;
231+
}
232+
}
233+
234+
finalError = finalError || ({} as string);
235+
236+
if (opts.throwOnError) {
237+
throw finalError;
238+
}
239+
240+
// TODO: we probably want to return error and improve types
241+
return opts.responseStyle === 'data'
242+
? undefined
243+
: {
244+
error: finalError,
245+
...result,
246+
};
247+
};
248+
249+
const makeMethodFn =
250+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
251+
request({ ...options, method });
252+
253+
const makeSseFn =
254+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
255+
const { opts, url } = await beforeRequest(options);
256+
return createSseClient({
257+
...opts,
258+
body: opts.body as BodyInit | null | undefined,
259+
headers: opts.headers as unknown as Record<string, string>,
260+
method,
261+
onRequest: async (url, init) => {
262+
let request = new Request(url, init);
263+
for (const fn of interceptors.request.fns) {
264+
if (fn) {
265+
request = await fn(request, opts);
266+
}
267+
}
268+
return request;
269+
},
270+
url,
271+
});
272+
};
273+
274+
return {
275+
buildUrl,
276+
connect: makeMethodFn('CONNECT'),
277+
delete: makeMethodFn('DELETE'),
278+
get: makeMethodFn('GET'),
279+
getConfig,
280+
head: makeMethodFn('HEAD'),
281+
interceptors,
282+
options: makeMethodFn('OPTIONS'),
283+
patch: makeMethodFn('PATCH'),
284+
post: makeMethodFn('POST'),
285+
put: makeMethodFn('PUT'),
286+
request,
287+
setConfig,
288+
sse: {
289+
connect: makeSseFn('CONNECT'),
290+
delete: makeSseFn('DELETE'),
291+
get: makeSseFn('GET'),
292+
head: makeSseFn('HEAD'),
293+
options: makeSseFn('OPTIONS'),
294+
patch: makeSseFn('PATCH'),
295+
post: makeSseFn('POST'),
296+
put: makeSseFn('PUT'),
297+
trace: makeSseFn('TRACE'),
298+
},
299+
trace: makeMethodFn('TRACE'),
300+
} as Client;
301+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type { Auth } from '../core/auth.gen';
4+
export type { QuerySerializerOptions } from '../core/bodySerializer.gen';
5+
export {
6+
formDataBodySerializer,
7+
jsonBodySerializer,
8+
urlSearchParamsBodySerializer,
9+
} from '../core/bodySerializer.gen';
10+
export { buildClientParams } from '../core/params.gen';
11+
export { serializeQueryKeyValue } from '../core/queryKeySerializer.gen';
12+
export { createClient } from './client.gen';
13+
export type {
14+
Client,
15+
ClientOptions,
16+
Config,
17+
CreateClientConfig,
18+
Options,
19+
RequestOptions,
20+
RequestResult,
21+
ResolvedRequestOptions,
22+
ResponseStyle,
23+
TDataShape,
24+
} from './types.gen';
25+
export { createConfig, mergeHeaders } from './utils.gen';

0 commit comments

Comments
 (0)