-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathinterfaces.ts
37 lines (30 loc) · 1.7 KB
/
interfaces.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
import {HttpStatusCode, HttpSuccessCodes} from "./utils/client/httpStatusCode.js";
import {Resolves} from "./utils/types.js";
/* eslint-disable @typescript-eslint/no-explicit-any */
export type APIClientHandler = (...args: any) => PromiseLike<ApiClientResponse>;
export type APIServerHandler = (...args: any) => PromiseLike<unknown>;
export type ApiClientSuccessResponse<S extends keyof any, T> = {ok: true; status: S; response: T; error?: never};
export type ApiClientSErrorResponse<S extends Exclude<HttpStatusCode, HttpSuccessCodes>> = {
ok: false;
status: S;
response?: never;
error: {code: S; operationId: string; message?: string};
};
export type ApiClientResponse<
S extends Partial<{[K in HttpSuccessCodes]: unknown}> = {[K in HttpSuccessCodes]: unknown},
E extends Exclude<HttpStatusCode, HttpSuccessCodes> = Exclude<HttpStatusCode, HttpSuccessCodes>,
> =
| {[K in keyof S]: ApiClientSuccessResponse<K, S[K]>}[keyof S]
| {[K in E]: ApiClientSErrorResponse<K>}[E]
| ApiClientSErrorResponse<HttpStatusCode.INTERNAL_SERVER_ERROR>;
export type ApiClientResponseData<T extends ApiClientResponse> = T extends {ok: true; response: infer R} ? R : never;
export type GenericRequestObject = Record<string, unknown>;
export type GenericResponseObject = {code: (code: number) => void};
export type ServerApi<T extends Record<string, APIClientHandler>> = {
[K in keyof T]: (
...args: [...args: Parameters<T[K]>, req?: GenericRequestObject, res?: GenericResponseObject]
) => Promise<ApiClientResponseData<Resolves<T[K]>>>;
};
export type ClientApi<T extends Record<string, APIServerHandler>> = {
[K in keyof T]: (...args: Parameters<T[K]>) => Promise<ApiClientResponse<{[HttpStatusCode.OK]: Resolves<T[K]>}>>;
};