@koine/api 2.0.0-beta.8 → 2.0.0-beta.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ApiError.cjs.d.ts +2 -0
- package/ApiError.cjs.default.js +1 -0
- package/ApiError.cjs.js +14 -0
- package/ApiError.cjs.mjs +2 -0
- package/ApiError.d.ts +3 -9
- package/ApiError.esm.js +9 -0
- package/createApi.cjs.d.ts +2 -0
- package/createApi.cjs.default.js +1 -0
- package/{createApi.js → createApi.cjs.js} +41 -61
- package/createApi.cjs.mjs +2 -0
- package/createApi.d.ts +2 -7
- package/{createApi.mjs → createApi.esm.js} +32 -43
- package/createApiResultFail.cjs.d.ts +2 -0
- package/createApiResultFail.cjs.default.js +1 -0
- package/createApiResultFail.cjs.js +21 -0
- package/createApiResultFail.cjs.mjs +2 -0
- package/createApiResultFail.d.ts +4 -0
- package/createApiResultFail.esm.js +15 -0
- package/createApiResultOk.cjs.d.ts +2 -0
- package/createApiResultOk.cjs.default.js +1 -0
- package/createApiResultOk.cjs.js +14 -0
- package/createApiResultOk.cjs.mjs +2 -0
- package/createApiResultOk.d.ts +3 -0
- package/createApiResultOk.esm.js +9 -0
- package/createSwrApi.cjs.js +30 -0
- package/createSwrApi.esm.js +23 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +16 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +3 -2
- package/index.esm.js +5 -0
- package/next/nextApiResponse.d.ts +6 -0
- package/next/nextApiResponse12.d.ts +6 -0
- package/next.cjs.d.ts +1 -0
- package/next.cjs.default.js +1 -0
- package/next.cjs.js +28 -0
- package/next.cjs.mjs +2 -0
- package/next.d.ts +2 -0
- package/next.esm.js +23 -0
- package/package.json +53 -12
- package/swr/createSwrApi.d.ts +9 -0
- package/swr-mutation/createSwrMutationApi.d.ts +19 -0
- package/swr-mutation.cjs.d.ts +1 -0
- package/swr-mutation.cjs.default.js +1 -0
- package/swr-mutation.cjs.js +38 -0
- package/swr-mutation.cjs.mjs +2 -0
- package/swr-mutation.d.ts +1 -0
- package/swr-mutation.esm.js +30 -0
- package/swr.cjs.d.ts +1 -0
- package/swr.cjs.default.js +1 -0
- package/swr.cjs.js +12 -0
- package/swr.cjs.mjs +2 -0
- package/swr.d.ts +1 -0
- package/swr.esm.js +4 -0
- package/types.d.ts +126 -0
- package/typings.d.ts +11 -508
- package/ApiError.js +0 -32
- package/ApiError.mjs +0 -14
- package/createSwrApi.d.ts +0 -34
- package/createSwrApi.js +0 -99
- package/createSwrApi.mjs +0 -82
- package/index.js +0 -28
- package/index.mjs +0 -4
- package/nextApiResponse.d.ts +0 -3
- package/nextApiResponse.js +0 -23
- package/nextApiResponse.mjs +0 -5
package/typings.d.ts
CHANGED
|
@@ -1,510 +1,13 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
|
|
3
|
-
type _Response = Response;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @borrows [awesome-template-literal-types](https://github.com/ghoullier/awesome-template-literal-types#router-params-parsing)
|
|
7
|
-
*/
|
|
8
|
-
type ExtractEndpointParams<T extends string> = string extends T
|
|
9
|
-
? Record<string, string>
|
|
10
|
-
: // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
-
T extends `${infer _Start}{${infer Param}}${infer Rest}`
|
|
12
|
-
? { [k in Param | keyof ExtractEndpointParams<Rest>]: string | number }
|
|
13
|
-
: // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
14
|
-
T extends `${infer _Start}{${infer Param}}`
|
|
15
|
-
? { [k in Param]: string | number }
|
|
16
|
-
: never;
|
|
17
|
-
|
|
18
1
|
declare namespace Koine.Api {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
type
|
|
29
|
-
|
|
30
|
-
baseUrl: string,
|
|
31
|
-
options?: ClientOptions
|
|
32
|
-
) => Client<TEndpoints>;
|
|
33
|
-
|
|
34
|
-
type ClientOptions = {
|
|
35
|
-
/**
|
|
36
|
-
* Headers will be merged with
|
|
37
|
-
* ```
|
|
38
|
-
* { "content-type": "application/json" }
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @default {}
|
|
42
|
-
*/
|
|
43
|
-
headers?: RequestInit["headers"];
|
|
44
|
-
/**
|
|
45
|
-
* Basic request options to supply to `fetch`
|
|
46
|
-
*
|
|
47
|
-
* @see RequestInit
|
|
48
|
-
*
|
|
49
|
-
* @default { credentials: "include", referrerPolicy: "no-referrer" }
|
|
50
|
-
*/
|
|
51
|
-
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
52
|
-
/**
|
|
53
|
-
* Flag to throw error within the catch block, by default we return a
|
|
54
|
-
* normalised error result {@link ResultFail}
|
|
55
|
-
*
|
|
56
|
-
* @default false
|
|
57
|
-
*/
|
|
58
|
-
throwErr?: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Timeout in `ms`, if `falsy` there is no timeout
|
|
61
|
-
*
|
|
62
|
-
* @default 10000
|
|
63
|
-
*/
|
|
64
|
-
timeout?: number | false | null;
|
|
65
|
-
/**
|
|
66
|
-
* Process request before actual http call
|
|
67
|
-
*
|
|
68
|
-
* @default undefined
|
|
69
|
-
*/
|
|
70
|
-
processReq?: RequestProcessor;
|
|
71
|
-
/**
|
|
72
|
-
* Process ok/failed response just after http response
|
|
73
|
-
*
|
|
74
|
-
* @default undefined
|
|
75
|
-
*/
|
|
76
|
-
processRes?: ResponseProcessorRes;
|
|
77
|
-
/**
|
|
78
|
-
* Process maybe-thrown error originated either from `fetch` function
|
|
79
|
-
* invokation or from its `response.json()` parsing
|
|
80
|
-
*
|
|
81
|
-
* @default undefined
|
|
82
|
-
*/
|
|
83
|
-
processErr?: ResponseProcessorErr;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
type ClientMethod<
|
|
87
|
-
TMethod extends RequestMethod,
|
|
88
|
-
TEndpoints extends Endpoints
|
|
89
|
-
> = <
|
|
90
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
91
|
-
TOptions extends EndpointOptions<TEndpoints, TEndpoint, TMethod>,
|
|
92
|
-
TOk extends ResponseOk = EndpointResponseOk<TEndpoints, TEndpoint, TMethod>,
|
|
93
|
-
TFail extends ResponseFail = EndpointResponseFail<
|
|
94
|
-
TEndpoints,
|
|
95
|
-
TEndpoint,
|
|
96
|
-
TMethod
|
|
97
|
-
>
|
|
98
|
-
>(
|
|
99
|
-
endpoint: TEndpoint,
|
|
100
|
-
options?: TOptions
|
|
101
|
-
) => Promise<EndpointResult<TEndpoints, TEndpoint, TMethod>>;
|
|
102
|
-
// ) => Promise<Result<TOk, TFail>>;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* The `api` interface generated by `createApi`
|
|
106
|
-
*/
|
|
107
|
-
type Client<TEndpoints extends Endpoints> = {
|
|
108
|
-
[TMethod in RequestMethod]: ClientMethod<TMethod, TEndpoints>;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
112
|
-
//
|
|
113
|
-
// Endpoints
|
|
114
|
-
//
|
|
115
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
116
|
-
|
|
117
|
-
type EndpointOptions<
|
|
118
|
-
TEndpoints extends Endpoints,
|
|
119
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
120
|
-
TMethod extends RequestMethod
|
|
121
|
-
> = RequestOptions<
|
|
122
|
-
TEndpoints,
|
|
123
|
-
TEndpoint,
|
|
124
|
-
TMethod,
|
|
125
|
-
TEndpoints[TEndpoint][Uppercase<TMethod>]["json"],
|
|
126
|
-
TEndpoints[TEndpoint][Uppercase<TMethod>]["query"]
|
|
127
|
-
>;
|
|
128
|
-
|
|
129
|
-
type EndpointResultOk<
|
|
130
|
-
TEndpoints extends Endpoints,
|
|
131
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
132
|
-
TMethod extends RequestMethod
|
|
133
|
-
> = ResultOk<TEndpoints[TEndpoint][Uppercase<TMethod>]["ok"]>;
|
|
134
|
-
|
|
135
|
-
type EndpointResultFail<
|
|
136
|
-
TEndpoints extends Endpoints,
|
|
137
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
138
|
-
TMethod extends RequestMethod
|
|
139
|
-
> = ResultFail<TEndpoints[TEndpoint][Uppercase<TMethod>]["fail"]>;
|
|
140
|
-
|
|
141
|
-
type EndpointResponseOk<
|
|
142
|
-
TEndpoints extends Endpoints,
|
|
143
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
144
|
-
TMethod extends RequestMethod
|
|
145
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["ok"];
|
|
146
|
-
|
|
147
|
-
type EndpointResponseFail<
|
|
148
|
-
TEndpoints extends Endpoints,
|
|
149
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
150
|
-
TMethod extends RequestMethod
|
|
151
|
-
> = TEndpoints[TEndpoint][Uppercase<TMethod>]["fail"];
|
|
152
|
-
|
|
153
|
-
type EndpointResult<
|
|
154
|
-
TEndpoints extends Endpoints,
|
|
155
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
156
|
-
TMethod extends RequestMethod
|
|
157
|
-
> = Result<
|
|
158
|
-
EndpointResponseOk<TEndpoints, TEndpoint, TMethod>,
|
|
159
|
-
EndpointResponseFail<TEndpoints, TEndpoint, TMethod>
|
|
160
|
-
>;
|
|
161
|
-
|
|
162
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
163
|
-
//
|
|
164
|
-
// Definitions
|
|
165
|
-
//
|
|
166
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Validate the `Endpoints` definition against the `Endpoint` shape defined
|
|
170
|
-
* here.
|
|
171
|
-
*
|
|
172
|
-
* FIXME: this does not work yet...constraining the API endpoints definitions
|
|
173
|
-
*
|
|
174
|
-
* @see https://github.com/sindresorhus/type-fest/blob/main/source/exact.d.ts
|
|
175
|
-
* @see https://fettblog.eu/typescript-match-the-exact-object-shape/
|
|
176
|
-
* @see https://stackoverflow.com/a/51496652/1938970
|
|
177
|
-
* @see https://github.com/Microsoft/TypeScript/issues/12936
|
|
178
|
-
*/
|
|
179
|
-
// type DefineEndpoint<T> =
|
|
180
|
-
// T extends Endpoint ?
|
|
181
|
-
// Exclude<keyof T, keyof Endpoint> extends never ?
|
|
182
|
-
// T : "Endpoint must follow `Koine.Api.Endpoint` shape" : never;
|
|
183
|
-
type DefineEndpoint<T extends EndpointShape> = T;
|
|
184
|
-
type DefineEndpoints<T extends Endpoints> = {};
|
|
185
|
-
type EndpointShape = {
|
|
186
|
-
[TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
type Endpoints = Record<string, Endpoint>;
|
|
190
|
-
type Endpoint = {
|
|
191
|
-
[TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
type EndpointUrl<TEndpoints extends Endpoints> = Extract<
|
|
195
|
-
keyof TEndpoints,
|
|
196
|
-
string
|
|
197
|
-
>;
|
|
198
|
-
|
|
199
|
-
type DataTypes<TMethod extends Uppercase<RequestMethod>> = {
|
|
200
|
-
/**
|
|
201
|
-
* The request body of a non-GET request
|
|
202
|
-
*/
|
|
203
|
-
json?: RequestJson;
|
|
204
|
-
/**
|
|
205
|
-
* The parameters to encode in the URL of the request
|
|
206
|
-
*/
|
|
207
|
-
query?: RequestQuery;
|
|
208
|
-
/**
|
|
209
|
-
* The JSON response data returned by the request in case of success
|
|
210
|
-
*/
|
|
211
|
-
ok?: null | unknown;
|
|
212
|
-
/**
|
|
213
|
-
* The shape of the error data returned by the request in case of
|
|
214
|
-
* failure
|
|
215
|
-
*/
|
|
216
|
-
fail?: null | unknown;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
220
|
-
//
|
|
221
|
-
// Request
|
|
222
|
-
//
|
|
223
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
224
|
-
|
|
225
|
-
type RequestMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
226
|
-
|
|
227
|
-
type RequestJson = unknown;
|
|
228
|
-
|
|
229
|
-
type RequestQuery = unknown;
|
|
230
|
-
|
|
231
|
-
type RequestParams = unknown;
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Request options
|
|
235
|
-
*
|
|
236
|
-
* `ClientOptions` can be overriden here at the single request level.
|
|
237
|
-
*/
|
|
238
|
-
type RequestOptions<
|
|
239
|
-
TEndpoints extends Endpoints,
|
|
240
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
241
|
-
TMethod extends RequestMethod,
|
|
242
|
-
TJson extends RequestJson,
|
|
243
|
-
TQuery extends RequestQuery
|
|
244
|
-
> = Omit<ClientOptions, "processReq"> & {
|
|
245
|
-
processReq?: EndpointRequestProcessor<TEndpoints, TEndpoint, TMethod>;
|
|
246
|
-
/**
|
|
247
|
-
* A dictionary to dynamically interpolate endpoint url params, e.g.:
|
|
248
|
-
*
|
|
249
|
-
* ```js
|
|
250
|
-
* myapi.get("user/{id}", { path: { id: "12" }})
|
|
251
|
-
* ```
|
|
252
|
-
* results in a call to the endpoint `"user/12"`
|
|
253
|
-
*/
|
|
254
|
-
params?: ExtractEndpointParams<TEndpoint>;
|
|
255
|
-
/**
|
|
256
|
-
* Query parameters will be serialized into a string and appended to the URL
|
|
257
|
-
*/
|
|
258
|
-
query?: TQuery;
|
|
259
|
-
/**
|
|
260
|
-
* JSON request body
|
|
261
|
-
*/
|
|
262
|
-
json?: TJson;
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
266
|
-
//
|
|
267
|
-
// Response/Result
|
|
268
|
-
//
|
|
269
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
270
|
-
|
|
271
|
-
type ResponseOk = unknown;
|
|
272
|
-
|
|
273
|
-
type ResponseFail = unknown;
|
|
274
|
-
|
|
275
|
-
type ResultShared<
|
|
276
|
-
T extends Record<string, unknown> = Record<string, unknown>
|
|
277
|
-
> = T & {
|
|
278
|
-
status: _Response["status"];
|
|
279
|
-
msg: _Response["statusText"];
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
type ResultOk<TResponse extends ResponseOk = ResponseOk> = {
|
|
283
|
-
status: _Response["status"];
|
|
284
|
-
msg: _Response["statusText"];
|
|
285
|
-
ok: true;
|
|
286
|
-
fail?: false;
|
|
287
|
-
data: TResponse;
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
type ResultFail<TResponse extends ResponseFail = ResponseFail> = {
|
|
291
|
-
status: _Response["status"];
|
|
292
|
-
msg: Error["message"] | _Response["statusText"];
|
|
293
|
-
ok?: false;
|
|
294
|
-
fail: true;
|
|
295
|
-
data: TResponse;
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
type Result<
|
|
299
|
-
TResponseOk extends ResponseOk,
|
|
300
|
-
TResponseFail extends ResponseFail
|
|
301
|
-
> =
|
|
302
|
-
| {
|
|
303
|
-
status: _Response["status"];
|
|
304
|
-
msg: _Response["statusText"];
|
|
305
|
-
ok: true;
|
|
306
|
-
fail?: false;
|
|
307
|
-
data: TResponseOk;
|
|
308
|
-
}
|
|
309
|
-
| {
|
|
310
|
-
status: _Response["status"];
|
|
311
|
-
msg: Error["message"] | _Response["statusText"];
|
|
312
|
-
ok?: false;
|
|
313
|
-
fail: true;
|
|
314
|
-
data: TResponseFail;
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* The request processor at the client level, this is meant to apply global
|
|
319
|
-
* transformations to all endpoints requests
|
|
320
|
-
*/
|
|
321
|
-
type RequestProcessor = (
|
|
322
|
-
method: RequestMethod,
|
|
323
|
-
url: string,
|
|
324
|
-
query: any,
|
|
325
|
-
json: any,
|
|
326
|
-
params: any,
|
|
327
|
-
requestInit: RequestInit
|
|
328
|
-
) => [
|
|
329
|
-
string, // url
|
|
330
|
-
RequestQuery, // query
|
|
331
|
-
RequestJson, // json
|
|
332
|
-
RequestParams, // params
|
|
333
|
-
RequestInit // requestInit
|
|
334
|
-
];
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* The request processor at the request level, this is meant to apply
|
|
338
|
-
* transformations to a single endpoint request. Request processor applied at
|
|
339
|
-
* the whole client level is still applied just before this one, hence one
|
|
340
|
-
* might set some global processing and override it or undo it at the single
|
|
341
|
-
* request level.
|
|
342
|
-
*/
|
|
343
|
-
type EndpointRequestProcessor<
|
|
344
|
-
TEndpoints extends Endpoints,
|
|
345
|
-
TEndpoint extends EndpointUrl<TEndpoints>,
|
|
346
|
-
TMethod extends RequestMethod
|
|
347
|
-
> = (
|
|
348
|
-
method: TMethod,
|
|
349
|
-
url: string,
|
|
350
|
-
query: EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"],
|
|
351
|
-
json: EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"],
|
|
352
|
-
params: EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"],
|
|
353
|
-
requestInit: RequestInit
|
|
354
|
-
) => [
|
|
355
|
-
string, // url
|
|
356
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"], // query
|
|
357
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"], // json
|
|
358
|
-
EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"], // params
|
|
359
|
-
RequestInit // requestInit
|
|
360
|
-
];
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* The ok/fail response processor at the request level, this is meant to apply
|
|
364
|
-
* transformations to a single or all endpoint responses
|
|
365
|
-
*/
|
|
366
|
-
type ResponseProcessorRes = <TResponseOk extends ResponseOk = ResponseOk>(
|
|
367
|
-
response: _Response,
|
|
368
|
-
options: TOptions
|
|
369
|
-
) => Promise<Koine.Api.Result<TResponseOk>>;
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* The error response processor at the request level, this is meant to apply
|
|
373
|
-
* transformations to a single or all endpoint responses
|
|
374
|
-
*/
|
|
375
|
-
type ResponseProcessorErr = <
|
|
376
|
-
TResponseFail extends ResponseFailed = ResponseFailed
|
|
377
|
-
>(
|
|
378
|
-
msg: string,
|
|
379
|
-
options: TOptions
|
|
380
|
-
) => Promise<Koine.Api.Result<TResponseFail>>;
|
|
381
|
-
|
|
382
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
383
|
-
//
|
|
384
|
-
// Hooks
|
|
385
|
-
//
|
|
386
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Api hooks map for `react`, each request method has its own `use{Method}`
|
|
390
|
-
* hook.
|
|
391
|
-
*
|
|
392
|
-
* These hooks are implemented with different libraries or, in the future as
|
|
393
|
-
* standalone hooks, see SWR ones to start with.
|
|
394
|
-
*/
|
|
395
|
-
type HooksMaps = {
|
|
396
|
-
[TMethod in RequestMethod]: `use${Capitalize<TMethod>}`;
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
type HooksMapsByName = { [K in keyof HooksMaps as HooksMaps[K]]: K };
|
|
400
|
-
|
|
401
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
402
|
-
//
|
|
403
|
-
// Generate shortcuts
|
|
404
|
-
//
|
|
405
|
-
//////////////////////////////////////////////////////////////////////////////
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* To generate all available helpers use in your `API` types:
|
|
409
|
-
*
|
|
410
|
-
* ```ts
|
|
411
|
-
* type Response = Koine.Api.GenerateResponseHelpers<Endpoints>;
|
|
412
|
-
* type Request = Koine.Api.GenerateRequestHelpers<Endpoints>;
|
|
413
|
-
* type Get = Koine.Api.GenerateGetHelpers<Endpoints>;
|
|
414
|
-
* type Post = Koine.Api.GeneratePostHelpers<Endpoints>;
|
|
415
|
-
* ```
|
|
416
|
-
*/
|
|
417
|
-
type Generate = "here just to read the example usage";
|
|
418
|
-
|
|
419
|
-
type _ShortcutsMaps = {
|
|
420
|
-
[TMethod in RequestMethod]: Capitalize<TMethod>;
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
type _ShortcutsMapsByMethod = {
|
|
424
|
-
[K in keyof _ShortcutsMaps as _ShortcutsMaps[K]]: K;
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* @example
|
|
429
|
-
* ```ts
|
|
430
|
-
* // define the type on your `API` types:
|
|
431
|
-
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
432
|
-
*
|
|
433
|
-
* // consume the type wherever in your app:
|
|
434
|
-
* type MyData = API.Response["get"]["my/endpoint"];
|
|
435
|
-
* ```
|
|
436
|
-
*/
|
|
437
|
-
type GenerateResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
438
|
-
[TMethod in RequestMethod]: {
|
|
439
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["ok"];
|
|
440
|
-
};
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* @example
|
|
445
|
-
* ```ts
|
|
446
|
-
* // define the type on your `API` types:
|
|
447
|
-
* type Request = Koine.Api.GenerateRequestShortcuts<Endpoints>;
|
|
448
|
-
*
|
|
449
|
-
* // consume the type wherever in your app:
|
|
450
|
-
* type MyData = API.Request["get"]["my/endpoint"];
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
type GenerateRequestShortcuts<TEndpoints extends Endpoints> = {
|
|
454
|
-
[TMethod in RequestMethod]: {
|
|
455
|
-
[TEndpointUrl in keyof TEndpoints]: TMethod extends "get"
|
|
456
|
-
? TEndpoints[TEndpointUrl][Uppercase<TMethod>]["query"]
|
|
457
|
-
: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["json"];
|
|
458
|
-
};
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* @example
|
|
463
|
-
* ```ts
|
|
464
|
-
* // define the type on your `API` types:
|
|
465
|
-
* type Get = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
466
|
-
*
|
|
467
|
-
* // consume the type wherever in your app:
|
|
468
|
-
* type MyData = API.Get["my/endpoint"];
|
|
469
|
-
* ```
|
|
470
|
-
*/
|
|
471
|
-
type GenerateGetShortcuts<TEndpoints extends Endpoints> = {
|
|
472
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["ok"];
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* @example
|
|
477
|
-
* ```ts
|
|
478
|
-
* // define the type on your `API` types:
|
|
479
|
-
* type Post = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
480
|
-
*
|
|
481
|
-
* // consume the type wherever in your app:
|
|
482
|
-
* type MyData = API.Post["my/endpoint"];
|
|
483
|
-
* ```
|
|
484
|
-
*/
|
|
485
|
-
type GeneratePostShortcuts<TEndpoints extends Endpoints> = {
|
|
486
|
-
[TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["ok"];
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* This is not useful as it is the same as doing
|
|
491
|
-
* `API.Endpoints["my/endpoint"]["GET"]["ok"];`
|
|
492
|
-
*
|
|
493
|
-
* @example
|
|
494
|
-
* ```ts
|
|
495
|
-
* // define the type on your `API` types:
|
|
496
|
-
* type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
|
|
497
|
-
*
|
|
498
|
-
* // consume the type wherever in your app:
|
|
499
|
-
* type MyData = API.$["my/endpoint"]["get"]["ok"];
|
|
500
|
-
* ```
|
|
501
|
-
* @deprecated
|
|
502
|
-
*/
|
|
503
|
-
// type GenerateAllShortcuts<TEndpoints extends Endpoints> = {
|
|
504
|
-
// [TEndpointUrl in keyof TEndpoints]: {
|
|
505
|
-
// [TMethod in RequestMethod]: {
|
|
506
|
-
// [DataType in EndpointDataType]: TEndpoints[TEndpointUrl][Uppercase<TMethod>][DataType];
|
|
507
|
-
// }
|
|
508
|
-
// }
|
|
509
|
-
// }
|
|
2
|
+
type Endpoints = import("./types").Api.Endpoints;
|
|
3
|
+
type GenerateGetShortcuts<E extends Endpoints> =
|
|
4
|
+
import("./types").Api.Generate.GetShortcuts<E>;
|
|
5
|
+
type GeneratePostShortcuts<E extends Endpoints> =
|
|
6
|
+
import("./types").Api.Generate.PostShortcuts<E>;
|
|
7
|
+
type GenerateRequestShortcuts<E extends Endpoints> =
|
|
8
|
+
import("./types").Api.Generate.RequestShortcuts<E>;
|
|
9
|
+
type GenerateResponseShortcuts<E extends Endpoints> =
|
|
10
|
+
import("./types").Api.Generate.ResponseShortcuts<E>;
|
|
11
|
+
type GenerateResultShortcuts<E extends Endpoints> =
|
|
12
|
+
import("./types").Api.Generate.ResultShortcuts<E>;
|
|
510
13
|
}
|
package/ApiError.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom `ApiError` class extending `Error` to throw in failed response.
|
|
3
|
-
*
|
|
4
|
-
* @see https://eslint.org/docs/rules/no-throw-literal
|
|
5
|
-
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
6
|
-
*
|
|
7
|
-
*/ "use strict";
|
|
8
|
-
Object.defineProperty(exports, "__esModule", {
|
|
9
|
-
value: true
|
|
10
|
-
});
|
|
11
|
-
function _export(target, all) {
|
|
12
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: all[name]
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
_export(exports, {
|
|
18
|
-
ApiError: function() {
|
|
19
|
-
return ApiError;
|
|
20
|
-
},
|
|
21
|
-
default: function() {
|
|
22
|
-
return _default;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
let ApiError = class ApiError extends Error {
|
|
26
|
-
constructor(result){
|
|
27
|
-
super(`Request failed with ${result.status} ${result.msg}`);
|
|
28
|
-
this.name = "ApiError";
|
|
29
|
-
Object.assign(this, result);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const _default = ApiError;
|
package/ApiError.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Custom `ApiError` class extending `Error` to throw in failed response.
|
|
3
|
-
*
|
|
4
|
-
* @see https://eslint.org/docs/rules/no-throw-literal
|
|
5
|
-
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
6
|
-
*
|
|
7
|
-
*/ export class ApiError extends Error {
|
|
8
|
-
constructor(result){
|
|
9
|
-
super(`Request failed with ${result.status} ${result.msg}`);
|
|
10
|
-
this.name = "ApiError";
|
|
11
|
-
Object.assign(this, result);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default ApiError;
|
package/createSwrApi.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type BareFetcher, // type Fetcher,
|
|
2
|
-
type SWRConfiguration, type SWRResponse } from "swr";
|
|
3
|
-
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
4
|
-
type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
5
|
-
/**
|
|
6
|
-
* Conditional fetching as option
|
|
7
|
-
*
|
|
8
|
-
* Moving this to an option allows us to keep the endpoints typed dictionary,
|
|
9
|
-
* e.g. we can write:
|
|
10
|
-
*
|
|
11
|
-
* ```js
|
|
12
|
-
* const { data, mutate } = myApi.useGet("User/{id}",
|
|
13
|
-
* { params: { id: aVariableMaybeContainingAnId || "" }, },
|
|
14
|
-
* { when: !!aVariableMaybeContainingAnId }
|
|
15
|
-
* );
|
|
16
|
-
*
|
|
17
|
-
* // we still have typed `data`, `mutate`
|
|
18
|
-
* ```
|
|
19
|
-
* @see https://swr.vercel.app/docs/conditional-fetching
|
|
20
|
-
*/
|
|
21
|
-
when?: boolean | (() => boolean);
|
|
22
|
-
};
|
|
23
|
-
type KoineApiMethodHookSWR<THookName extends keyof Koine.Api.HooksMapsByName, TEndpoints extends Koine.Api.Endpoints> = <TEndpoint extends Koine.Api.EndpointUrl<TEndpoints>, TMethod extends Koine.Api.RequestMethod = Koine.Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfigurationExtended<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationConfiguration<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>) => THookName extends "useGet" ? SWRResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>;
|
|
24
|
-
/**
|
|
25
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
26
|
-
*/
|
|
27
|
-
export declare const createSwrApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions | undefined) => Koine.Api.Client<TEndpoints> & {
|
|
28
|
-
useGet: KoineApiMethodHookSWR<"useGet", TEndpoints>;
|
|
29
|
-
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
30
|
-
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
31
|
-
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
32
|
-
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
33
|
-
};
|
|
34
|
-
export default createSwrApi;
|