Skip to content

Commit a95ec86

Browse files
author
Lubos ​
committed
test: update snapshots
1 parent 55f144f commit a95ec86

File tree

1,782 files changed

+214389
-9357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,782 files changed

+214389
-9357
lines changed

packages/custom-client/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @hey-api/client-custom
1+
# @hey-api/custom-client
22

33
## 0.1.1
44

packages/openapi-ts-tests/test/__snapshots__/2.0.x/body-response-text-plain/client.gen.ts

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

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

66
/**
77
* The `createClientConfig()` function will be called on client initialization
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import type { Client, Config, RequestOptions } from './types';
2+
import {
3+
buildUrl,
4+
createConfig,
5+
createInterceptors,
6+
getParseAs,
7+
mergeConfigs,
8+
mergeHeaders,
9+
setAuthParams,
10+
} from './utils';
11+
12+
type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
13+
body?: any;
14+
headers: ReturnType<typeof mergeHeaders>;
15+
};
16+
17+
export const createClient = (config: Config = {}): Client => {
18+
let _config = mergeConfigs(createConfig(), config);
19+
20+
const getConfig = (): Config => ({ ..._config });
21+
22+
const setConfig = (config: Config): Config => {
23+
_config = mergeConfigs(_config, config);
24+
return getConfig();
25+
};
26+
27+
const interceptors = createInterceptors<
28+
Request,
29+
Response,
30+
unknown,
31+
RequestOptions
32+
>();
33+
34+
const request: Client['request'] = async (options) => {
35+
const opts = {
36+
..._config,
37+
...options,
38+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
39+
headers: mergeHeaders(_config.headers, options.headers),
40+
};
41+
42+
if (opts.security) {
43+
await setAuthParams({
44+
...opts,
45+
security: opts.security,
46+
});
47+
}
48+
49+
if (opts.body && opts.bodySerializer) {
50+
opts.body = opts.bodySerializer(opts.body);
51+
}
52+
53+
// remove Content-Type header if body is empty to avoid sending invalid requests
54+
if (opts.body === undefined || opts.body === '') {
55+
opts.headers.delete('Content-Type');
56+
}
57+
58+
const url = buildUrl(opts);
59+
const requestInit: ReqInit = {
60+
redirect: 'follow',
61+
...opts,
62+
};
63+
64+
let request = new Request(url, requestInit);
65+
66+
for (const fn of interceptors.request._fns) {
67+
if (fn) {
68+
request = await fn(request, opts);
69+
}
70+
}
71+
72+
// fetch must be assigned here, otherwise it would throw the error:
73+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
74+
const _fetch = opts.fetch!;
75+
let response = await _fetch(request);
76+
77+
for (const fn of interceptors.response._fns) {
78+
if (fn) {
79+
response = await fn(response, request, opts);
80+
}
81+
}
82+
83+
const result = {
84+
request,
85+
response,
86+
};
87+
88+
if (response.ok) {
89+
if (
90+
response.status === 204 ||
91+
response.headers.get('Content-Length') === '0'
92+
) {
93+
return opts.responseStyle === 'data'
94+
? {}
95+
: {
96+
data: {},
97+
...result,
98+
};
99+
}
100+
101+
const parseAs =
102+
(opts.parseAs === 'auto'
103+
? getParseAs(response.headers.get('Content-Type'))
104+
: opts.parseAs) ?? 'json';
105+
106+
if (parseAs === 'stream') {
107+
return opts.responseStyle === 'data'
108+
? response.body
109+
: {
110+
data: response.body,
111+
...result,
112+
};
113+
}
114+
115+
let data = await response[parseAs]();
116+
if (parseAs === 'json') {
117+
if (opts.responseValidator) {
118+
await opts.responseValidator(data);
119+
}
120+
121+
if (opts.responseTransformer) {
122+
data = await opts.responseTransformer(data);
123+
}
124+
}
125+
126+
return opts.responseStyle === 'data'
127+
? data
128+
: {
129+
data,
130+
...result,
131+
};
132+
}
133+
134+
let error = await response.text();
135+
136+
try {
137+
error = JSON.parse(error);
138+
} catch {
139+
// noop
140+
}
141+
142+
let finalError = error;
143+
144+
for (const fn of interceptors.error._fns) {
145+
if (fn) {
146+
finalError = (await fn(error, response, request, opts)) as string;
147+
}
148+
}
149+
150+
finalError = finalError || ({} as string);
151+
152+
if (opts.throwOnError) {
153+
throw finalError;
154+
}
155+
156+
// TODO: we probably want to return error and improve types
157+
return opts.responseStyle === 'data'
158+
? undefined
159+
: {
160+
error: finalError,
161+
...result,
162+
};
163+
};
164+
165+
return {
166+
buildUrl,
167+
connect: (options) => request({ ...options, method: 'CONNECT' }),
168+
delete: (options) => request({ ...options, method: 'DELETE' }),
169+
get: (options) => request({ ...options, method: 'GET' }),
170+
getConfig,
171+
head: (options) => request({ ...options, method: 'HEAD' }),
172+
interceptors,
173+
options: (options) => request({ ...options, method: 'OPTIONS' }),
174+
patch: (options) => request({ ...options, method: 'PATCH' }),
175+
post: (options) => request({ ...options, method: 'POST' }),
176+
put: (options) => request({ ...options, method: 'PUT' }),
177+
request,
178+
setConfig,
179+
trace: (options) => request({ ...options, method: 'TRACE' }),
180+
};
181+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export type { Auth } from '../core/auth';
2+
export type { QuerySerializerOptions } from '../core/bodySerializer';
3+
export {
4+
formDataBodySerializer,
5+
jsonBodySerializer,
6+
urlSearchParamsBodySerializer,
7+
} from '../core/bodySerializer';
8+
export { buildClientParams } from '../core/params';
9+
export { createClient } from './client';
10+
export type {
11+
Client,
12+
ClientOptions,
13+
Config,
14+
CreateClientConfig,
15+
Options,
16+
OptionsLegacyParser,
17+
RequestOptions,
18+
RequestResult,
19+
ResponseStyle,
20+
TDataShape,
21+
} from './types';
22+
export { createConfig, mergeHeaders } from './utils';

0 commit comments

Comments
 (0)