/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/openapi-codegen/src/client/generated/sdk.gen.ts
85 строк
3 KB
Nick Lucas
feat: Support OpenAPI json generation for any tRPC appRouter (#7231)
15 мар 2026, 02:34
Не верифицирован
15 мар 2026, 02:34
4b955d2
Код
Авторство
О чём код?
// This file is auto-generated by @hey-api/openapi-ts import type { Client, Options as Options2, TDataShape } from './client'; import { client } from './client.gen'; import type { UserByIdData, UserByIdErrors, UserByIdResponses, UserCreateData, UserCreateErrors, UserCreateResponses, UserListData, UserListErrors, UserListResponses } from './types.gen'; export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = Options2<TData, ThrowOnError> & { /** * You can provide a client instance returned by `createClient()` instead of * individual options. This might be also useful if you want to implement a * custom client. */ client?: Client; /** * You can pass arbitrary values through the `meta` object. This can be * used to access values that aren't defined as part of the SDK function. */ meta?: Record<string, unknown>; }; class HeyApiClient { protected client: Client; constructor(args?: { client?: Client; }) { this.client = args?.client ?? client; } } class HeyApiRegistry<T> { private readonly defaultKey = 'default'; private readonly instances: Map<string, T> = new Map(); get(key?: string): T { const instance = this.instances.get(key ?? this.defaultKey); if (!instance) { throw new Error(`No SDK client found. Create one with "new Sdk()" to fix this error.`); } return instance; } set(value: T, key?: string): void { this.instances.set(key ?? this.defaultKey, value); } } export class User extends HeyApiClient { public list<ThrowOnError extends boolean = false>(options?: Options<UserListData, ThrowOnError>) { return (options?.client ?? this.client).get<UserListResponses, UserListErrors, ThrowOnError>({ url: '/user.list', ...options }); } public byId<ThrowOnError extends boolean = false>(options: Options<UserByIdData, ThrowOnError>) { return (options.client ?? this.client).get<UserByIdResponses, UserByIdErrors, ThrowOnError>({ url: '/user.byId', ...options }); } public create<ThrowOnError extends boolean = false>(options: Options<UserCreateData, ThrowOnError>) { return (options.client ?? this.client).post<UserCreateResponses, UserCreateErrors, ThrowOnError>({ url: '/user.create', ...options, headers: { 'Content-Type': 'application/json', ...options.headers } }); } } export class Sdk extends HeyApiClient { public static readonly __registry = new HeyApiRegistry<Sdk>(); constructor(args?: { client?: Client; key?: string; }) { super(args); Sdk.__registry.set(this, args?.key); } private _user?: User; get user(): User { return this._user ??= new User({ client: this.client }); } }