@koine/api 2.0.0-beta.21 → 2.0.0-beta.210
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.default.js +1 -0
- package/ApiError.cjs.js +8 -0
- package/ApiError.cjs.mjs +2 -0
- package/ApiError.d.ts +10 -3
- package/ApiError.esm.js +3 -0
- package/README.md +1 -0
- package/createApi.cjs.default.js +1 -0
- package/createApi.cjs.js +10 -0
- package/createApi.cjs.mjs +2 -0
- package/createApi.d.ts +8 -1
- package/createApi.esm.js +5 -0
- package/createApiResultFail.cjs.default.js +1 -0
- package/createApiResultFail.cjs.js +8 -0
- package/createApiResultFail.cjs.mjs +2 -0
- package/createApiResultFail.d.ts +2 -1
- package/createApiResultFail.esm.js +3 -0
- package/createApiResultOk.cjs.default.js +1 -0
- package/createApiResultOk.cjs.js +8 -0
- package/createApiResultOk.cjs.mjs +2 -0
- package/createApiResultOk.d.ts +2 -1
- package/createApiResultOk.esm.js +3 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +13 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +5 -6
- package/index.esm.js +4 -0
- package/next/nextApiResponse.cjs.js +12 -0
- package/next/nextApiResponse.d.ts +6 -0
- package/next/nextApiResponse.esm.js +7 -0
- package/next/nextApiResponse12.cjs.js +11 -0
- package/next/nextApiResponse12.d.ts +6 -0
- package/next/nextApiResponse12.esm.js +6 -0
- package/next.cjs.default.js +1 -0
- package/next.cjs.js +9 -0
- package/next.cjs.mjs +2 -0
- package/next.d.ts +2 -0
- package/next.esm.js +2 -0
- package/package.json +76 -36
- package/swr/createSwrApi.cjs.js +13 -0
- package/swr/createSwrApi.d.ts +41 -0
- package/swr/createSwrApi.esm.js +7 -0
- package/swr-mutation/createSwrMutationApi.cjs.js +12 -0
- package/swr-mutation/createSwrMutationApi.d.ts +19 -0
- package/swr-mutation/createSwrMutationApi.esm.js +7 -0
- package/swr-mutation.cjs.default.js +1 -0
- package/swr-mutation.cjs.js +7 -0
- package/swr-mutation.cjs.mjs +2 -0
- package/swr-mutation.d.ts +1 -0
- package/swr-mutation.esm.js +1 -0
- package/swr.cjs.default.js +1 -0
- package/swr.cjs.js +7 -0
- package/swr.cjs.mjs +2 -0
- package/swr.d.ts +1 -0
- package/swr.esm.js +1 -0
- package/types.cjs.default.js +1 -0
- package/types.cjs.js +2 -0
- package/types.cjs.mjs +2 -0
- package/types.d.ts +313 -0
- package/types.esm.js +1 -0
- package/typings.d.ts +12 -533
- package/vite.config.d.ts +2 -0
- package/ApiError.js +0 -13
- package/cjs/ApiError.d.ts +0 -5
- package/cjs/ApiError.js +0 -16
- package/cjs/createApi.d.ts +0 -2
- package/cjs/createApi.js +0 -124
- package/cjs/createApiResultFail.d.ts +0 -2
- package/cjs/createApiResultFail.js +0 -14
- package/cjs/createApiResultOk.d.ts +0 -2
- package/cjs/createApiResultOk.js +0 -15
- package/cjs/createSwrApi.d.ts +0 -15
- package/cjs/createSwrApi.js +0 -61
- package/cjs/index.d.ts +0 -6
- package/cjs/index.js +0 -15
- package/cjs/nextApiResponse.d.ts +0 -6
- package/cjs/nextApiResponse.js +0 -15
- package/cjs/nextApiResponse12.d.ts +0 -6
- package/cjs/nextApiResponse12.js +0 -19
- package/cjs/package.json +0 -17
- package/createApi.js +0 -120
- package/createApiResultFail.js +0 -10
- package/createApiResultOk.js +0 -11
- package/createSwrApi.d.ts +0 -15
- package/createSwrApi.js +0 -57
- package/index.js +0 -6
- package/nextApiResponse.d.ts +0 -6
- package/nextApiResponse.js +0 -12
- package/nextApiResponse12.d.ts +0 -6
- package/nextApiResponse12.js +0 -15
package/types.d.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
type _Response = Response;
|
|
2
|
+
/**
|
|
3
|
+
* @borrows [awesome-template-literal-types](https://github.com/ghoullier/awesome-template-literal-types#router-params-parsing)
|
|
4
|
+
*/
|
|
5
|
+
type ExtractEndpointParams<T extends string> = string | number extends T ? Record<string, string> : T extends `${infer _Start}{${infer Param}}${infer Rest}` ? {
|
|
6
|
+
[k in Param | keyof ExtractEndpointParams<Rest>]: string | number;
|
|
7
|
+
} : T extends `${infer _Start}{${infer Param}}` ? {
|
|
8
|
+
[k in Param]: string | number;
|
|
9
|
+
} : {};
|
|
10
|
+
export declare namespace Api {
|
|
11
|
+
export type ClientOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Optionally override the `fetch` function
|
|
14
|
+
*
|
|
15
|
+
* @default fetch - The standard {@link fetch}
|
|
16
|
+
*/
|
|
17
|
+
fetchFn?: typeof fetch;
|
|
18
|
+
/**
|
|
19
|
+
* Headers will be merged with
|
|
20
|
+
* ```
|
|
21
|
+
* { "content-type": "application/json" }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @default {}
|
|
25
|
+
*/
|
|
26
|
+
headers?: RequestInit["headers"];
|
|
27
|
+
/**
|
|
28
|
+
* Basic request options to supply to `fetch`
|
|
29
|
+
*
|
|
30
|
+
* @see RequestInit
|
|
31
|
+
*
|
|
32
|
+
* @default {}
|
|
33
|
+
*/
|
|
34
|
+
request?: Omit<RequestInit, "body" | "headers" | "method">;
|
|
35
|
+
/**
|
|
36
|
+
* Flag to throw error within the catch block, by default we return a
|
|
37
|
+
* normalised error result {@link ResultFail}
|
|
38
|
+
*
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
throwErr?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Timeout in `ms`, if `falsy` there is no timeout
|
|
44
|
+
*
|
|
45
|
+
* @default 10000
|
|
46
|
+
*/
|
|
47
|
+
timeout?: number | false | null;
|
|
48
|
+
/**
|
|
49
|
+
* Process request before actual http call
|
|
50
|
+
*
|
|
51
|
+
* @default undefined
|
|
52
|
+
*/
|
|
53
|
+
processReq?: RequestProcessor;
|
|
54
|
+
/**
|
|
55
|
+
* Process ok/failed response just after http response
|
|
56
|
+
*
|
|
57
|
+
* @default undefined
|
|
58
|
+
*/
|
|
59
|
+
processRes?: ResponseProcessorRes;
|
|
60
|
+
/**
|
|
61
|
+
* Process maybe-thrown error originated either from `fetch` function
|
|
62
|
+
* invokation or from its `response.json()` parsing
|
|
63
|
+
*
|
|
64
|
+
* @default undefined
|
|
65
|
+
*/
|
|
66
|
+
processErr?: ResponseProcessorErr;
|
|
67
|
+
/**
|
|
68
|
+
* Log level:
|
|
69
|
+
*
|
|
70
|
+
* - `0`: silent
|
|
71
|
+
* - `1`: ok/fail logging
|
|
72
|
+
*
|
|
73
|
+
* @default 0
|
|
74
|
+
*/
|
|
75
|
+
log?: 0 | 1;
|
|
76
|
+
};
|
|
77
|
+
type ClientMethod<TMethod extends RequestMethod, TEndpoints extends Endpoints> = <TEndpoint extends EndpointUrl<TEndpoints>, TOptions extends EndpointOptions<TEndpoints, TEndpoint, TMethod>, TOk extends ResponseOk = EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, TFail extends ResponseFail = EndpointResponseFail<TEndpoints, TEndpoint, TMethod>>(endpoint: TEndpoint, options?: TOptions) => Promise<EndpointResult<TEndpoints, TEndpoint, TMethod>>;
|
|
78
|
+
/**
|
|
79
|
+
* The `api` interface generated by `createApi`
|
|
80
|
+
*/
|
|
81
|
+
export type Client<TEndpoints extends Endpoints> = {
|
|
82
|
+
[TMethod in RequestMethod]: ClientMethod<TMethod, TEndpoints>;
|
|
83
|
+
};
|
|
84
|
+
export type GetDataType<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod, TDataType extends keyof DataTypes<Uppercase<TMethod>>> = TEndpoints[TEndpoint][Uppercase<TMethod>] extends DataTypes<Uppercase<TMethod>> ? TEndpoints[TEndpoint][Uppercase<TMethod>][TDataType] : never;
|
|
85
|
+
export type EndpointOptions<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = RequestOptions<TEndpoints, TEndpoint, TMethod, GetDataType<TEndpoints, TEndpoint, TMethod, "json">, GetDataType<TEndpoints, TEndpoint, TMethod, "query">>;
|
|
86
|
+
export type EndpointResponseOk<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
87
|
+
export type EndpointResponseFail<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = GetDataType<TEndpoints, TEndpoint, TMethod, "fail">;
|
|
88
|
+
type EndpointResult<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = Result<EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, EndpointResponseFail<TEndpoints, TEndpoint, TMethod>>;
|
|
89
|
+
export type Endpoints = Record<string, Endpoint>;
|
|
90
|
+
type Endpoint = {
|
|
91
|
+
[TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
|
|
92
|
+
};
|
|
93
|
+
export type EndpointUrl<TEndpoints extends Endpoints> = Extract<keyof TEndpoints, string>;
|
|
94
|
+
type DataTypes<TMethod extends Uppercase<RequestMethod>> = {
|
|
95
|
+
/**
|
|
96
|
+
* The request body of a non-GET request
|
|
97
|
+
*/
|
|
98
|
+
json?: RequestJson;
|
|
99
|
+
/**
|
|
100
|
+
* The parameters to encode in the URL of the request
|
|
101
|
+
*/
|
|
102
|
+
query?: RequestQuery;
|
|
103
|
+
/**
|
|
104
|
+
* The JSON response data returned by the request in case of success
|
|
105
|
+
*/
|
|
106
|
+
ok?: null | unknown;
|
|
107
|
+
/**
|
|
108
|
+
* The shape of the error data returned by the request in case of
|
|
109
|
+
* failure
|
|
110
|
+
*/
|
|
111
|
+
fail?: null | unknown;
|
|
112
|
+
};
|
|
113
|
+
export type RequestMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
114
|
+
type RequestJson = unknown;
|
|
115
|
+
type RequestQuery = unknown;
|
|
116
|
+
type RequestParams = unknown;
|
|
117
|
+
/**
|
|
118
|
+
* Request options
|
|
119
|
+
*
|
|
120
|
+
* `ClientOptions` can be overriden here at the single request level.
|
|
121
|
+
*/
|
|
122
|
+
type RequestOptions<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod, TJson extends RequestJson, TQuery extends RequestQuery> = Omit<ClientOptions, "processReq"> & {
|
|
123
|
+
processReq?: EndpointRequestProcessor<TEndpoints, TEndpoint, TMethod>;
|
|
124
|
+
/**
|
|
125
|
+
* A dictionary to dynamically interpolate endpoint url params, e.g.:
|
|
126
|
+
*
|
|
127
|
+
* ```js
|
|
128
|
+
* myapi.get("user/{id}", { params: { id: "12" }})
|
|
129
|
+
* ```
|
|
130
|
+
* results in a call to the endpoint `"user/12"`
|
|
131
|
+
*/
|
|
132
|
+
params?: ExtractEndpointParams<TEndpoint>;
|
|
133
|
+
/**
|
|
134
|
+
* Query parameters will be serialized into a string and appended to the URL
|
|
135
|
+
*/
|
|
136
|
+
query?: TQuery;
|
|
137
|
+
/**
|
|
138
|
+
* JSON request body
|
|
139
|
+
*/
|
|
140
|
+
json?: TJson;
|
|
141
|
+
};
|
|
142
|
+
export type ResponseOk = unknown;
|
|
143
|
+
export type ResponseFail = unknown;
|
|
144
|
+
export type ResultOk<TResponse extends ResponseOk = ResponseOk> = {
|
|
145
|
+
status: _Response["status"];
|
|
146
|
+
msg: _Response["statusText"];
|
|
147
|
+
ok: true;
|
|
148
|
+
fail?: false;
|
|
149
|
+
data: TResponse;
|
|
150
|
+
};
|
|
151
|
+
export type ResultFail<TResponse extends ResponseFail = ResponseFail> = {
|
|
152
|
+
status: _Response["status"];
|
|
153
|
+
msg: Error["message"] | _Response["statusText"];
|
|
154
|
+
ok?: false;
|
|
155
|
+
fail: true;
|
|
156
|
+
data: TResponse;
|
|
157
|
+
};
|
|
158
|
+
export type Result<TResponseOk extends ResponseOk, TResponseFail extends ResponseFail> = {
|
|
159
|
+
status: _Response["status"];
|
|
160
|
+
msg: _Response["statusText"];
|
|
161
|
+
ok: true;
|
|
162
|
+
fail?: false;
|
|
163
|
+
data: TResponseOk;
|
|
164
|
+
} | {
|
|
165
|
+
status: _Response["status"];
|
|
166
|
+
msg: Error["message"] | _Response["statusText"];
|
|
167
|
+
ok?: false;
|
|
168
|
+
fail: true;
|
|
169
|
+
data: TResponseFail;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* The request processor at the client level, this is meant to apply global
|
|
173
|
+
* transformations to all endpoints requests
|
|
174
|
+
*/
|
|
175
|
+
export type RequestProcessor = (method: RequestMethod, url: string, query: any, json: any, params: any, requestInit: RequestInit) => [
|
|
176
|
+
string,
|
|
177
|
+
RequestQuery,
|
|
178
|
+
RequestJson,
|
|
179
|
+
RequestParams,
|
|
180
|
+
RequestInit
|
|
181
|
+
];
|
|
182
|
+
/**
|
|
183
|
+
* The request processor at the request level, this is meant to apply
|
|
184
|
+
* transformations to a single endpoint request. Request processor applied at
|
|
185
|
+
* the whole client level is still applied just before this one, hence one
|
|
186
|
+
* might set some global processing and override it or undo it at the single
|
|
187
|
+
* request level.
|
|
188
|
+
*/
|
|
189
|
+
export type EndpointRequestProcessor<TEndpoints extends Endpoints, TEndpoint extends EndpointUrl<TEndpoints>, TMethod extends RequestMethod> = (method: TMethod, url: string, query: EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"], json: EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"], params: EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"], requestInit: RequestInit) => [
|
|
190
|
+
string,
|
|
191
|
+
EndpointOptions<TEndpoints, TEndpoint, TMethod>["query"],
|
|
192
|
+
EndpointOptions<TEndpoints, TEndpoint, TMethod>["json"],
|
|
193
|
+
EndpointOptions<TEndpoints, TEndpoint, TMethod>["params"],
|
|
194
|
+
RequestInit
|
|
195
|
+
];
|
|
196
|
+
/**
|
|
197
|
+
* The ok/fail response processor at the request level, this is meant to apply
|
|
198
|
+
* transformations to a single or all endpoint responses
|
|
199
|
+
*/
|
|
200
|
+
type ResponseProcessorRes = <TResponseOk extends ResponseOk = ResponseOk, TResponseFail extends ResponseFail = ResponseFail>(response: _Response, options: any) => Promise<Result<TResponseOk, TResponseFail>>;
|
|
201
|
+
/**
|
|
202
|
+
* The error response processor at the request level, this is meant to apply
|
|
203
|
+
* transformations to a single or all endpoint responses
|
|
204
|
+
*/
|
|
205
|
+
type ResponseProcessorErr = <TResponseOk extends ResponseOk = ResponseOk, TResponseFail extends ResponseFail = ResponseFail>(msg: string, options: any) => Promise<Result<TResponseOk, TResponseFail>>;
|
|
206
|
+
/**
|
|
207
|
+
* Api hooks map for `react`, each request method has its own `use{Method}`
|
|
208
|
+
* hook.
|
|
209
|
+
*
|
|
210
|
+
* These hooks are implemented with different libraries or, in the future as
|
|
211
|
+
* standalone hooks, see SWR ones to start with.
|
|
212
|
+
*/
|
|
213
|
+
type HooksMaps = {
|
|
214
|
+
[TMethod in RequestMethod]: `use${TMethod extends "get" ? "" : Capitalize<TMethod>}`;
|
|
215
|
+
};
|
|
216
|
+
export type HooksMapsByName = {
|
|
217
|
+
[K in keyof HooksMaps as HooksMaps[K]]: K;
|
|
218
|
+
};
|
|
219
|
+
export {};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* To generate all available helpers use in your `API` types:
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* type Response = Api.Generate.ResponseHelpers<Endpoints>;
|
|
227
|
+
* type Request = Api.Generate.RequestHelpers<Endpoints>;
|
|
228
|
+
* type Get = Api.Generate.GetHelpers<Endpoints>;
|
|
229
|
+
* type Post = Api.Generate.PostHelpers<Endpoints>;
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export declare namespace Api.Generate {
|
|
233
|
+
/**
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* // define the type on your `API` types:
|
|
237
|
+
* type Result = Api.Generate.ResultShortcuts<Endpoints>;
|
|
238
|
+
*
|
|
239
|
+
* // consume the type wherever in your app:
|
|
240
|
+
* type MyResult = API.Result["get"]["my/endpoint"];
|
|
241
|
+
*
|
|
242
|
+
* MyResult["ok"];
|
|
243
|
+
* ^
|
|
244
|
+
* MyResult["fail"];
|
|
245
|
+
* ^
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
type ResultShortcuts<TEndpoints extends Endpoints> = {
|
|
249
|
+
[TMethod in RequestMethod]: {
|
|
250
|
+
[TEndpoint in Extract<keyof TEndpoints, string>]: {
|
|
251
|
+
ok: GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
252
|
+
fail: GetDataType<TEndpoints, TEndpoint, TMethod, "fail">;
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* // define the type on your `API` types:
|
|
260
|
+
* type Response = Api.Generate.ResponseShortcuts<Endpoints>;
|
|
261
|
+
*
|
|
262
|
+
* // consume the type wherever in your app:
|
|
263
|
+
* type MyData = API.Response["get"]["my/endpoint"];
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
type ResponseShortcuts<TEndpoints extends Endpoints> = {
|
|
267
|
+
[TMethod in RequestMethod]: {
|
|
268
|
+
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, TMethod, "ok">;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* // define the type on your `API` types:
|
|
275
|
+
* type Request = Api.Generate.RequestShortcuts<Endpoints>;
|
|
276
|
+
*
|
|
277
|
+
* // consume the type wherever in your app:
|
|
278
|
+
* type MyData = API.Request["get"]["my/endpoint"];
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
type RequestShortcuts<TEndpoints extends Endpoints> = {
|
|
282
|
+
[TMethod in RequestMethod]: {
|
|
283
|
+
[TEndpoint in Extract<keyof TEndpoints, string>]: TMethod extends "get" ? GetDataType<TEndpoints, TEndpoint, TMethod, "query"> : GetDataType<TEndpoints, TEndpoint, TMethod, "json">;
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* // define the type on your `API` types:
|
|
290
|
+
* type Get = Api.Generate.ResponseShortcuts<Endpoints>;
|
|
291
|
+
*
|
|
292
|
+
* // consume the type wherever in your app:
|
|
293
|
+
* type MyData = API.Get["my/endpoint"];
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
type GetShortcuts<TEndpoints extends Endpoints> = {
|
|
297
|
+
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, "get", "ok">;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* // define the type on your `API` types:
|
|
303
|
+
* type Post = Api.Generate.ResponseShortcuts<Endpoints>;
|
|
304
|
+
*
|
|
305
|
+
* // consume the type wherever in your app:
|
|
306
|
+
* type MyData = API.Post["my/endpoint"];
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
type PostShortcuts<TEndpoints extends Endpoints> = {
|
|
310
|
+
[TEndpoint in Extract<keyof TEndpoints, string>]: GetDataType<TEndpoints, TEndpoint, "post", "ok">;
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
export {};
|
package/types.esm.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|