-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.ts
43 lines (39 loc) · 1.04 KB
/
api.ts
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
import * as z from "./deps";
import { HttpSchema } from "./model";
import { PickUnion } from "./utils";
export type ApiNames<T extends HttpSchema> = z.output<T> extends {
name: string;
}
? z.output<T>["name"]
: never;
export type ApiRequestAttributes =
| "method"
| "path"
| "query"
| "headers"
| "body";
export type ApiResponseAttributes = "status" | "headers" | "body";
export type ApiRequest<T extends HttpSchema, Key> = Extract<
z.output<T>,
{ name: Key }
>;
export type ApiResponse<T extends HttpSchema, Key> = ApiRequest<
T,
Key
>["responses"];
export type ApiFunction<T extends HttpSchema, Key> = (
request: Readonly<PickUnion<ApiRequest<T, Key>, ApiRequestAttributes>>
) => Readonly<
Promise<
ApiResponseAttributes extends keyof ApiResponse<T, Key>
? PickUnion<ApiResponse<T, Key>, ApiResponseAttributes>
: undefined
>
>;
export type Api<T extends HttpSchema> = {
[Key in ApiNames<T>]: ApiFunction<T, Key>;
};
export type ApiFragment<T extends HttpSchema, Key extends ApiNames<T>> = Pick<
Api<T>,
Key
>;