-
Notifications
You must be signed in to change notification settings - Fork 308
/
models.ts
155 lines (133 loc) · 3.77 KB
/
models.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
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
import { RetryConfiguration } from './retry.js';
import { Dispatcher } from 'undici-types';
/**
* @private
*/
export type FetchAPI = (url: URL | RequestInfo, init?: RequestInit) => Promise<Response>;
export interface ClientOptions
extends Omit<Configuration, 'baseUrl' | 'parseError' | 'middleware'> {
telemetry?: boolean;
clientInfo?: { name: string; [key: string]: unknown };
}
export interface Configuration {
baseUrl: string; // override base path
parseError: (response: Response) => Promise<Error>; // Custom error parser
/**
* Provide your own fetch implementation.
*/
fetch?: FetchAPI;
/**
* Provide a middleware that will run either before the request, after the request or when the request fails.
*/
middleware?: Middleware[];
/**
* Pass your own http agent to support proxies.
*/
agent?: Dispatcher;
/**
* Custom headers that will be added to every request.
*/
headers?: HTTPHeaders;
/**
* Timeout in ms before aborting the request (default 10,000)
*/
timeoutDuration?: number;
/**
* Retry configuration.
*/
retry?: RetryConfiguration;
}
export interface RequestOpts {
path: string;
method: HTTPMethod;
headers?: HTTPHeaders;
query?: HTTPQuery;
body?: HTTPBody;
}
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = {
[key: string]:
| string
| number
| null
| boolean
| Array<string | number | null | boolean>
| HTTPQuery;
};
export type HTTPBody = any | FormData | URLSearchParams;
export type HTTPRequestInit = {
headers?: HTTPHeaders;
method: HTTPMethod;
credentials?: RequestCredentials;
body?: HTTPBody;
};
export type InitOverrideFunction = (requestContext: {
init: HTTPRequestInit;
context: RequestOpts;
}) => Promise<RequestInit>;
export type InitOverride = RequestInit | InitOverrideFunction;
export interface ApiResponse<T> {
data: T;
headers: Headers;
status: number;
statusText: string;
}
export class JSONApiResponse<T> implements ApiResponse<T> {
constructor(
public data: T,
public headers: Headers,
readonly status: number,
readonly statusText: string
) {}
static async fromResponse<T = unknown>(raw: Response) {
const value = (await raw.json()) as T;
return new JSONApiResponse<T>(value, raw.headers, raw.status, raw.statusText);
}
}
export class VoidApiResponse implements ApiResponse<undefined> {
public data: undefined;
constructor(public headers: Headers, readonly status: number, readonly statusText: string) {}
static async fromResponse(raw: Response) {
return new VoidApiResponse(raw.headers, raw.status, raw.statusText);
}
}
export class TextApiResponse implements ApiResponse<string> {
constructor(
public data: string,
public headers: Headers,
readonly status: number,
readonly statusText: string
) {}
static async fromResponse(raw: Response) {
const value = await raw.text();
return new TextApiResponse(value, raw.headers, raw.status, raw.statusText);
}
}
export interface FetchParams {
url: URL | RequestInfo;
init: RequestInit;
}
export interface RequestContext {
fetch: FetchAPI;
url: URL | RequestInfo;
init: RequestInit;
}
export interface ResponseContext {
fetch: FetchAPI;
url: URL | RequestInfo;
init: RequestInit;
response: Response;
}
export interface ErrorContext {
fetch: FetchAPI;
url: URL | RequestInfo;
init: RequestInit;
error: unknown;
response?: Response;
}
export interface Middleware {
pre?(context: RequestContext): Promise<FetchParams | void> | FetchParams | void;
post?(context: ResponseContext): Promise<Response | void> | Response | void;
onError?(context: ErrorContext): Promise<Response | void> | Response | void;
}