-
Notifications
You must be signed in to change notification settings - Fork 0
/
nswag-typed.ts
179 lines (135 loc) · 4.65 KB
/
nswag-typed.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/** Constants */
const jsonRequestInfo : RequestInit = {
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}
const defaultRequestInfo : RequestInit = {
headers: {
"Accept": "application/json"
}
}
/** Generated Add Post description goes here */
async function apiPost(info: "/api/posts", params: null, init: TypedRequestInit<AddPostModel>): Promise<AddPostReturnModel>;
/** Generated Add User description goes here */
async function apiPost(info: "/api/users", params: null, init: TypedRequestInit<AddUserModel>): Promise<AddUserReturnModel>;
/** IMPL of POST */
function apiPost(info: string, params?: object, init?: TypedRequestInit) {
return apiJson("POST", info, init, params, getModel(init));
}
/** Delete a user */
async function apiDelete(info: "/api/users", init: TypedRequestInit<DeleteUserModel>): Promise<Response>;
/** IMPL of DELETE */
function apiDelete(info: string, init?: TypedRequestInit) {
return apiBase("DELETE", info, init, getModel(init));
}
/** Get all users (matching filter) */
async function apiGet(info: "/api/users", init?: TypedRequestInit<{ filter: string }>): Promise<GetUserModel[]>;
/** IMPL of GET */
function apiGet(info: string, init?: TypedRequestInit) {
return apiBase("GET", info, init, getModel(init));
}
/** IMPL of PUT */
function apiPut(info: string, params?: object, init?: TypedRequestInit) {
return apiJson("PUT", info, init, params, getModel(init));
}
/** Helpers */
async function apiJson(method: HttpMethodsWithBody, info: string, init: RequestInit, params?: object, body?: object) {
return apiBase(method, info, init, params, !!body ? JSON.stringify(body) : undefined);
}
async function apiForm(method: HttpMethodsWithBody, info: string, init: RequestInit, params?: object, body?: object) {
return apiBase(method, info, init, params, !!body ? getFormData(body) : undefined);
}
async function apiBase(method: HttpMethods, info: string, init: RequestInit, params?: object, body?: string | FormData) {
info = !!params ? formatParams(info, params) : info;
const qs = !!params ? formatQueryString(params) : "";
const response = await fetch(
info + qs,
{
method,
...body instanceof FormData ? defaultRequestInfo : jsonRequestInfo,
...init,
body
});
return await response.json();
}
/** Return 'model' or 'init' object, but if 'model' is inside 'init', removes 'model' property */
function getModel(init: { model?: any }) {
if ("model" in init) {
const model = init.model;
delete init.model;
return model;
}
return init;
}
const pattern = /\/\{\w+\}/g;
/** Format params string, removing {template} values and any matches from the params object */
function formatParams<T extends object>(info: string, model: T) {
for (const m of info.match(pattern)) {
const key = m.slice(2, -1);
const value = model[key];
if (value !== null && value !== undefined) {
info = info.replace(m, "/" + encodeURIComponent("" + value));
delete model[key];
}
else
info = info.replace(m, "");
}
return info;
}
/** Return formdata */
function getFormData(model: object) {
const data = new FormData();
for (const key of Object.keys(model)) {
const value = model[key];
if (value !== null && value !== undefined)
data.append(key, value.toString());
}
return data;
}
/** Format a querystring from an object */
function formatQueryString<T extends object>(model: T) {
const keys = Object.keys(model);
let qs = "";
for (let i = 0; i < keys.length; ++i) {
const key = keys[i],
value = model[key];
qs += (i === 0 ? "?" : "&") +
encodeURIComponent(key) + "=" + encodeURIComponent("" + value);
}
return qs;
}
/** Example Usage */
apiPost("/api/users", null, { name: "Ted", age: 25 });
apiPost("/api/posts", null, { body: "This is a test" });
apiDelete("/api/users", { id: 5 });
apiGet("/api/users");
/** Types */
type HttpMethodsWithBody = "PUT" | "POST" | "PATCH";
type HttpMethods = "HEAD" | "GET" | "DELETE" | HttpMethodsWithBody;
type AddPostModel = {
subject?: string
body: string
}
type AddPostReturnModel = {
id: number
}
type AddUserModel = {
name: string
age: number
}
type AddUserReturnModel = {
id: number
}
type DeleteUserModel = {
id: number
};
type GetUserModel = {
id: number
name: string
age: string
}
type TypedRequestInit<T = any> = T | ({ model: any } & RequestInit);
type TypedResponse<T = any> = T;
export {}