Skip to content

Commit d7feeee

Browse files
authored
Merge pull request #139 from jcdcdev/dev/v16
16.0.1
2 parents ad29ed1 + a1e2e58 commit d7feeee

File tree

14 files changed

+2446
-818
lines changed

14 files changed

+2446
-818
lines changed

src/Umbraco.Community.SimpleDashboards.Client/package-lock.json

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

src/Umbraco.Community.SimpleDashboards.Client/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
"generate": "openapi-ts"
1111
},
1212
"devDependencies": {
13-
"lit": "^3.3.0",
14-
"@hey-api/client-fetch": "^0.10.0",
15-
"@hey-api/openapi-ts": "^0.67.3",
16-
"@umbraco-cms/backoffice": "^16.0.0-rc5",
13+
"lit": "^3.3.1",
14+
"@hey-api/openapi-ts": "^0.80.1",
15+
"@umbraco-cms/backoffice": "^16.1.0",
1716
"typescript": "^5.8.3",
18-
"vite": "^6.3.5"
17+
"vite": "^7.0.6"
1918
},
2019
"volta": {
2120
"node": "22.14.0"

src/Umbraco.Community.SimpleDashboards.Client/src/api/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: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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.requestValidator) {
50+
await opts.requestValidator(opts);
51+
}
52+
53+
if (opts.body && opts.bodySerializer) {
54+
opts.body = opts.bodySerializer(opts.body);
55+
}
56+
57+
// remove Content-Type header if body is empty to avoid sending invalid requests
58+
if (opts.body === undefined || opts.body === '') {
59+
opts.headers.delete('Content-Type');
60+
}
61+
62+
const url = buildUrl(opts);
63+
const requestInit: ReqInit = {
64+
redirect: 'follow',
65+
...opts,
66+
};
67+
68+
let request = new Request(url, requestInit);
69+
70+
for (const fn of interceptors.request._fns) {
71+
if (fn) {
72+
request = await fn(request, opts);
73+
}
74+
}
75+
76+
// fetch must be assigned here, otherwise it would throw the error:
77+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
78+
const _fetch = opts.fetch!;
79+
let response = await _fetch(request);
80+
81+
for (const fn of interceptors.response._fns) {
82+
if (fn) {
83+
response = await fn(response, request, opts);
84+
}
85+
}
86+
87+
const result = {
88+
request,
89+
response,
90+
};
91+
92+
if (response.ok) {
93+
if (
94+
response.status === 204 ||
95+
response.headers.get('Content-Length') === '0'
96+
) {
97+
return opts.responseStyle === 'data'
98+
? {}
99+
: {
100+
data: {},
101+
...result,
102+
};
103+
}
104+
105+
const parseAs =
106+
(opts.parseAs === 'auto'
107+
? getParseAs(response.headers.get('Content-Type'))
108+
: opts.parseAs) ?? 'json';
109+
110+
let data: any;
111+
switch (parseAs) {
112+
case 'arrayBuffer':
113+
case 'blob':
114+
case 'formData':
115+
case 'json':
116+
case 'text':
117+
data = await response[parseAs]();
118+
break;
119+
case 'stream':
120+
return opts.responseStyle === 'data'
121+
? response.body
122+
: {
123+
data: response.body,
124+
...result,
125+
};
126+
}
127+
128+
if (parseAs === 'json') {
129+
if (opts.responseValidator) {
130+
await opts.responseValidator(data);
131+
}
132+
133+
if (opts.responseTransformer) {
134+
data = await opts.responseTransformer(data);
135+
}
136+
}
137+
138+
return opts.responseStyle === 'data'
139+
? data
140+
: {
141+
data,
142+
...result,
143+
};
144+
}
145+
146+
const textError = await response.text();
147+
let jsonError: unknown;
148+
149+
try {
150+
jsonError = JSON.parse(textError);
151+
} catch {
152+
// noop
153+
}
154+
155+
const error = jsonError ?? textError;
156+
let finalError = error;
157+
158+
for (const fn of interceptors.error._fns) {
159+
if (fn) {
160+
finalError = (await fn(error, response, request, opts)) as string;
161+
}
162+
}
163+
164+
finalError = finalError || ({} as string);
165+
166+
if (opts.throwOnError) {
167+
throw finalError;
168+
}
169+
170+
// TODO: we probably want to return error and improve types
171+
return opts.responseStyle === 'data'
172+
? undefined
173+
: {
174+
error: finalError,
175+
...result,
176+
};
177+
};
178+
179+
return {
180+
buildUrl,
181+
connect: (options) => request({ ...options, method: 'CONNECT' }),
182+
delete: (options) => request({ ...options, method: 'DELETE' }),
183+
get: (options) => request({ ...options, method: 'GET' }),
184+
getConfig,
185+
head: (options) => request({ ...options, method: 'HEAD' }),
186+
interceptors,
187+
options: (options) => request({ ...options, method: 'OPTIONS' }),
188+
patch: (options) => request({ ...options, method: 'PATCH' }),
189+
post: (options) => request({ ...options, method: 'POST' }),
190+
put: (options) => request({ ...options, method: 'PUT' }),
191+
request,
192+
setConfig,
193+
trace: (options) => request({ ...options, method: 'TRACE' }),
194+
};
195+
};
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)