/** * Generated by @skyleague/therefore * Do not manually touch this */ // biome-ignore-all lint: this file is generated /* eslint-disable */ import type { KyInstance, Options, ResponsePromise } from 'ky' import ky from 'ky' import type { ZodError, ZodSafeParseResult } from 'zod/v4' import { CreateTodoRequest, CreateTodoResponse, ListTodosResponse } from './02-openapi-client.zod.js' export type Status = Major extends string ? Major extends `1${number}` ? 'informational' : Major extends `2${number}` ? 'success' : Major extends `3${number}` ? 'redirection' : Major extends `4${number}` ? 'client-error' : 'server-error' : undefined export interface SuccessResponse { success: true statusCode: StatusCode status: Status headers: Headers right: T } export interface FailureResponse { success: false statusCode: StatusCode status: Status headers: HeaderResponse error: ZodError | undefined left: T where: Where } export type StatusCode = `${Major}${number}` /** * Todo API */ export class TodoClient { public client: KyInstance public constructor({ prefixUrl, options, client = ky, }: { prefixUrl: string options?: Options client?: KyInstance }) { this.client = client.extend({ prefixUrl, throwHttpErrors: false, ...options }) } /** * POST /todos */ public createTodo({ body, }: { body: CreateTodoRequest }): Promise< | SuccessResponse<'201', CreateTodoResponse> | FailureResponse | FailureResponse, string, 'response:body', Headers> | FailureResponse, string, 'response:statuscode', Headers> > { const _body = this.validateRequestBody(CreateTodoRequest, body) if ('left' in _body) { return Promise.resolve(_body) } return this.awaitResponse( this.client.post('todos', { json: _body.right, }), { 201: CreateTodoResponse, }, 'json', ) as ReturnType } /** * GET /todos */ public listTodos(): Promise< | SuccessResponse<'200', ListTodosResponse> | FailureResponse, string, 'response:body', Headers> | FailureResponse, string, 'response:statuscode', Headers> > { return this.awaitResponse( this.client.get('todos', {}), { 200: ListTodosResponse, }, 'json', ) as ReturnType } public validateRequestBody( parser: { safeParse: (o: unknown) => ZodSafeParseResult }, body: unknown, ): { right: Body } | FailureResponse { const _body = parser.safeParse(body) if (!_body.success) { return { success: false as const, statusCode: undefined, status: undefined, headers: undefined, left: body, error: _body.error, where: 'request:body', } satisfies FailureResponse } return { right: _body.data } } public async awaitResponse ZodSafeParseResult }>>( response: ResponsePromise, schemas: S, responseType?: 'json' | 'text', ) { const _body = (await (responseType !== undefined ? response[responseType]() : response.text())) as I const result = await response const status = result.status < 200 ? 'informational' : result.status < 300 ? 'success' : result.status < 400 ? 'redirection' : result.status < 500 ? 'client-error' : 'server-error' const validator = schemas[result.status] ?? schemas.default const body = validator?.safeParse?.(_body) if (result.status < 200 || result.status >= 300) { return { success: false as const, statusCode: result.status.toString(), status, headers: result.headers, left: body?.success ? body.data : _body, error: body !== undefined && !body.success ? body.error : undefined, where: 'response:statuscode', } } if (body === undefined || !body.success) { return { success: body === undefined, statusCode: result.status.toString(), status, headers: result.headers, left: _body, error: body?.error, where: 'response:body', } } return { success: true as const, statusCode: result.status.toString(), status, headers: result.headers, right: body.data } } }