-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02-openapi-client.client.ts
More file actions
168 lines (159 loc) · 5.39 KB
/
02-openapi-client.client.ts
File metadata and controls
168 lines (159 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* 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> = 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<StatusCode extends string, T> {
success: true
statusCode: StatusCode
status: Status<StatusCode>
headers: Headers
right: T
}
export interface FailureResponse<StatusCode = string, T = unknown, Where = never, HeaderResponse = Headers> {
success: false
statusCode: StatusCode
status: Status<StatusCode>
headers: HeaderResponse
error: ZodError<T> | undefined
left: T
where: Where
}
export type StatusCode<Major extends number = 1 | 2 | 3 | 4 | 5> = `${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<undefined, unknown, 'request:body', undefined>
| FailureResponse<StatusCode<2>, string, 'response:body', Headers>
| FailureResponse<StatusCode<1 | 3 | 4 | 5>, 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<this['createTodo']>
}
/**
* GET /todos
*/
public listTodos(): Promise<
| SuccessResponse<'200', ListTodosResponse>
| FailureResponse<StatusCode<2>, string, 'response:body', Headers>
| FailureResponse<StatusCode<1 | 3 | 4 | 5>, string, 'response:statuscode', Headers>
> {
return this.awaitResponse(
this.client.get('todos', {}),
{
200: ListTodosResponse,
},
'json',
) as ReturnType<this['listTodos']>
}
public validateRequestBody<Body>(
parser: { safeParse: (o: unknown) => ZodSafeParseResult<Body> },
body: unknown,
): { right: Body } | FailureResponse<undefined, unknown, 'request:body', undefined> {
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<undefined, unknown, 'request:body', undefined>
}
return { right: _body.data }
}
public async awaitResponse<I, S extends Record<PropertyKey, { safeParse: (o: unknown) => ZodSafeParseResult<I> }>>(
response: ResponsePromise<I>,
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 }
}
}