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
11 changes: 5 additions & 6 deletions templates/UmbracoExtension/Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
"generate-client": "node scripts/generate-openapi.js https://localhost:44339/umbraco/swagger/umbracoextension/swagger.json"
},
"devDependencies": {
"@hey-api/client-fetch": "^0.10.0",
"@hey-api/openapi-ts": "^0.66.7",
"@hey-api/openapi-ts": "^0.80.14",
"@umbraco-cms/backoffice": "^UMBRACO_VERSION_FROM_TEMPLATE",
"chalk": "^5.4.1",
"cross-env": "^7.0.3",
"chalk": "^5.6.0",
"cross-env": "^10.0.0",
"node-fetch": "^3.3.2",
"typescript": "^5.8.3",
"vite": "^6.3.4"
"typescript": "^5.9.2",
"vite": "^7.1.3"
}
}
9 changes: 3 additions & 6 deletions templates/UmbracoExtension/Client/scripts/generate-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ fetch(swaggerUrl).then(async (response) => {
console.log(`Calling ${chalk.yellow('hey-api')} to generate TypeScript client`);

await createClient({
client: '@hey-api/client-fetch',
input: swaggerUrl,
output: 'src/api',
plugins: [
...defaultPlugins,
'@hey-api/client-fetch',
{
name: '@hey-api/typescript',
enums: 'typescript'
},
{
name: '@hey-api/sdk',
asClass: true
asClass: true,
classNameBuilder: '{{name}}Service',
}
],
});
Expand Down
2 changes: 1 addition & 1 deletion templates/UmbracoExtension/Client/src/api/client.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

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

/**
* The `createClientConfig()` function will be called on client initialization
Expand Down
199 changes: 199 additions & 0 deletions templates/UmbracoExtension/Client/src/api/client/client.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// This file is auto-generated by @hey-api/openapi-ts

import type { Client, Config, ResolvedRequestOptions } from './types.gen';
import {
buildUrl,
createConfig,
createInterceptors,
getParseAs,
mergeConfigs,
mergeHeaders,
setAuthParams,
} from './utils.gen';

type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
body?: any;
headers: ReturnType<typeof mergeHeaders>;
};

export const createClient = (config: Config = {}): Client => {
let _config = mergeConfigs(createConfig(), config);

const getConfig = (): Config => ({ ..._config });

const setConfig = (config: Config): Config => {
_config = mergeConfigs(_config, config);
return getConfig();
};

const interceptors = createInterceptors<
Request,
Response,
unknown,
ResolvedRequestOptions
>();

const request: Client['request'] = async (options) => {
const opts = {
..._config,
...options,
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
headers: mergeHeaders(_config.headers, options.headers),
serializedBody: undefined,
};

if (opts.security) {
await setAuthParams({
...opts,
security: opts.security,
});
}

if (opts.requestValidator) {
await opts.requestValidator(opts);
}

if (opts.body && opts.bodySerializer) {
opts.serializedBody = opts.bodySerializer(opts.body);
}

// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.serializedBody === undefined || opts.serializedBody === '') {
opts.headers.delete('Content-Type');
}

const url = buildUrl(opts);
const requestInit: ReqInit = {
redirect: 'follow',
...opts,
body: opts.serializedBody,
};

let request = new Request(url, requestInit);

for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
}
}

// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = opts.fetch!;
let response = await _fetch(request);

for (const fn of interceptors.response._fns) {
if (fn) {
response = await fn(response, request, opts);
}
}

const result = {
request,
response,
};

if (response.ok) {
if (
response.status === 204 ||
response.headers.get('Content-Length') === '0'
) {
return opts.responseStyle === 'data'
? {}
: {
data: {},
...result,
};
}

const parseAs =
(opts.parseAs === 'auto'
? getParseAs(response.headers.get('Content-Type'))
: opts.parseAs) ?? 'json';

let data: any;
switch (parseAs) {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'stream':
return opts.responseStyle === 'data'
? response.body
: {
data: response.body,
...result,
};
}

if (parseAs === 'json') {
if (opts.responseValidator) {
await opts.responseValidator(data);
}

if (opts.responseTransformer) {
data = await opts.responseTransformer(data);
}
}

return opts.responseStyle === 'data'
? data
: {
data,
...result,
};
}

const textError = await response.text();
let jsonError: unknown;

try {
jsonError = JSON.parse(textError);
} catch {
// noop
}

const error = jsonError ?? textError;
let finalError = error;

for (const fn of interceptors.error._fns) {
if (fn) {
finalError = (await fn(error, response, request, opts)) as string;
}
}

finalError = finalError || ({} as string);

if (opts.throwOnError) {
throw finalError;
}

// TODO: we probably want to return error and improve types
return opts.responseStyle === 'data'
? undefined
: {
error: finalError,
...result,
};
};

return {
buildUrl,
connect: (options) => request({ ...options, method: 'CONNECT' }),
delete: (options) => request({ ...options, method: 'DELETE' }),
get: (options) => request({ ...options, method: 'GET' }),
getConfig,
head: (options) => request({ ...options, method: 'HEAD' }),
interceptors,
options: (options) => request({ ...options, method: 'OPTIONS' }),
patch: (options) => request({ ...options, method: 'PATCH' }),
post: (options) => request({ ...options, method: 'POST' }),
put: (options) => request({ ...options, method: 'PUT' }),
request,
setConfig,
trace: (options) => request({ ...options, method: 'TRACE' }),
};
};
25 changes: 25 additions & 0 deletions templates/UmbracoExtension/Client/src/api/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This file is auto-generated by @hey-api/openapi-ts

export type { Auth } from '../core/auth.gen';
export type { QuerySerializerOptions } from '../core/bodySerializer.gen';
export {
formDataBodySerializer,
jsonBodySerializer,
urlSearchParamsBodySerializer,
} from '../core/bodySerializer.gen';
export { buildClientParams } from '../core/params.gen';
export { createClient } from './client.gen';
export type {
Client,
ClientOptions,
Config,
CreateClientConfig,
Options,
OptionsLegacyParser,
RequestOptions,
RequestResult,
ResolvedRequestOptions,
ResponseStyle,
TDataShape,
} from './types.gen';
export { createConfig, mergeHeaders } from './utils.gen';
Loading