@koine/api 1.0.15 → 1.0.18

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/README.md CHANGED
@@ -1,5 +1 @@
1
- # @koine/utils
2
-
3
- ## Code
4
-
5
- - Transformative functions like `truncate` or `titleCase` should allow for nullable inputs as much as possible, acceptin both `undefined` and `null`.
1
+ # @koine/api
package/core/index.js CHANGED
@@ -83,7 +83,7 @@ export var createApi = function (apiName, baseUrl, options) {
83
83
  case 8:
84
84
  _f.trys.push([8, 13, , 14]);
85
85
  if (!adapter) return [3 /*break*/, 10];
86
- return [4 /*yield*/, adapter(response, options)];
86
+ return [4 /*yield*/, adapter(response, options || {})];
87
87
  case 9:
88
88
  result = _f.sent();
89
89
  return [3 /*break*/, 12];
@@ -99,7 +99,7 @@ export var createApi = function (apiName, baseUrl, options) {
99
99
  case 14: return [3 /*break*/, 19];
100
100
  case 15:
101
101
  if (!adapter) return [3 /*break*/, 17];
102
- return [4 /*yield*/, adapter(response, options)];
102
+ return [4 /*yield*/, adapter(response, options || {})];
103
103
  case 16:
104
104
  result = _f.sent();
105
105
  return [3 /*break*/, 19];
@@ -86,7 +86,7 @@ var createApi = function (apiName, baseUrl, options) {
86
86
  case 8:
87
87
  _f.trys.push([8, 13, , 14]);
88
88
  if (!adapter) return [3 /*break*/, 10];
89
- return [4 /*yield*/, adapter(response, options)];
89
+ return [4 /*yield*/, adapter(response, options || {})];
90
90
  case 9:
91
91
  result = _f.sent();
92
92
  return [3 /*break*/, 12];
@@ -102,7 +102,7 @@ var createApi = function (apiName, baseUrl, options) {
102
102
  case 14: return [3 /*break*/, 19];
103
103
  case 15:
104
104
  if (!adapter) return [3 /*break*/, 17];
105
- return [4 /*yield*/, adapter(response, options)];
105
+ return [4 /*yield*/, adapter(response, options || {})];
106
106
  case 16:
107
107
  result = _f.sent();
108
108
  return [3 /*break*/, 19];
package/package.json CHANGED
@@ -4,14 +4,13 @@
4
4
  "main": "./node/index.js",
5
5
  "typings": "./index.d.ts",
6
6
  "dependencies": {
7
- "@koine/utils": "1.0.15",
7
+ "@koine/utils": "1.0.18",
8
8
  "next": "^12.1.6",
9
9
  "swr": "^2.0.0-beta.3",
10
- "type-fest": "^2.13.0",
11
10
  "tslib": "^2.4.0"
12
11
  },
13
12
  "peerDependencies": {},
14
- "version": "1.0.15",
13
+ "version": "1.0.18",
15
14
  "module": "./index.js",
16
15
  "types": "./index.d.ts"
17
16
  }
package/typings.d.ts CHANGED
@@ -4,7 +4,7 @@ type _Response = Response;
4
4
 
5
5
  declare namespace Koine.Api {
6
6
  // @see https://stackoverflow.com/a/60702896/1938970
7
- import { Exact } from "type-fest";
7
+ // import { Exact } from "type-fest";
8
8
 
9
9
  //////////////////////////////////////////////////////////////////////////////
10
10
  //
@@ -48,7 +48,7 @@ declare namespace Koine.Api {
48
48
  /**
49
49
  * The `api` interface generated by `createApi`
50
50
  */
51
- type Client<TEndpoints> = {
51
+ type Client<TEndpoints extends Endpoints> = {
52
52
  [TMethod in RequestMethod]: ClientMethod<TMethod, TEndpoints>;
53
53
  };
54
54
 
@@ -112,9 +112,11 @@ declare namespace Koine.Api {
112
112
  // T : "Endpoint must follow `Koine.Api.Endpoint` shape" : never;
113
113
  type DefineEndpoint<T extends EndpointShape> = T;
114
114
  type DefineEndpoints<T extends Endpoints> = {};
115
- type EndpointShape = { [TMethod in Uppercase<RequestMethod>]?: DataTypes };
115
+ type EndpointShape = {
116
+ [TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
117
+ };
116
118
 
117
- type Endpoints = readonly Record<string, Endpoint>;
119
+ type Endpoints = Record<string, Endpoint>;
118
120
  type Endpoint = {
119
121
  [TMethod in Uppercase<RequestMethod>]?: DataTypes<TMethod>;
120
122
  };
@@ -124,42 +126,25 @@ declare namespace Koine.Api {
124
126
  string
125
127
  >;
126
128
 
127
- type DataTypes<TMethod extends Uppercase<RequestMethod>> =
128
- TMethod extends "GET"
129
- ? {
130
- /**
131
- * The JSON response data returned by the request in case of success
132
- */
133
- response?: null | unknown;
134
- /**
135
- * The parameters to encode in the URL of the request
136
- */
137
- params?: RequestParams;
138
- /**
139
- * The shape of the error data returned by the request in case of
140
- * failure
141
- */
142
- error?: null | unknown;
143
- }
144
- : {
145
- /**
146
- * The request body of a non-GET request
147
- */
148
- request?: null | unknown;
149
- /**
150
- * The JSON response data returned by the request in case of success
151
- */
152
- response?: null | unknown;
153
- /**
154
- * The parameters to encode in the URL of the request
155
- */
156
- params?: RequestParams;
157
- /**
158
- * The shape of the error data returned by the request in case of
159
- * failure
160
- */
161
- error?: null | unknown;
162
- };
129
+ type DataTypes<TMethod extends Uppercase<RequestMethod>> = {
130
+ /**
131
+ * The request body of a non-GET request
132
+ */
133
+ request?: RequestJson;
134
+ /**
135
+ * The JSON response data returned by the request in case of success
136
+ */
137
+ response?: null | unknown;
138
+ /**
139
+ * The parameters to encode in the URL of the request
140
+ */
141
+ params?: RequestParams;
142
+ /**
143
+ * The shape of the error data returned by the request in case of
144
+ * failure
145
+ */
146
+ error?: null | unknown;
147
+ };
163
148
 
164
149
  //////////////////////////////////////////////////////////////////////////////
165
150
  //
@@ -167,6 +152,8 @@ declare namespace Koine.Api {
167
152
  //
168
153
  //////////////////////////////////////////////////////////////////////////////
169
154
 
155
+ type RequestJson = undefined | null | Record<string | number, unknown>;
156
+
170
157
  type RequestParams = undefined | null | Record<string | number, unknown>;
171
158
 
172
159
  /**
@@ -285,7 +272,7 @@ declare namespace Koine.Api {
285
272
  Failed extends DataFailed = DataFailed
286
273
  >(
287
274
  response: _Response,
288
- options: RequestOptions
275
+ options: TOptions
289
276
  ) => Promise<Koine.Api.Response<Succesfull, Failed>>;
290
277
 
291
278
  //////////////////////////////////////////////////////////////////////////////
@@ -301,9 +288,101 @@ declare namespace Koine.Api {
301
288
  * These hooks are implemented with different libraries or, in the future as
302
289
  * standalone hooks, see SWR ones to start with.
303
290
  */
304
- type HooksMaps = readonly {
291
+ type HooksMaps = {
305
292
  [TMethod in RequestMethod]: `use${Capitalize<TMethod>}`;
306
293
  };
307
294
 
308
- type HooksMapsByName = readonly { [K in keyof HooksMaps as HooksMaps[K]]: K };
295
+ type HooksMapsByName = { [K in keyof HooksMaps as HooksMaps[K]]: K };
296
+
297
+ //////////////////////////////////////////////////////////////////////////////
298
+ //
299
+ // Generate shortcuts
300
+ //
301
+ //////////////////////////////////////////////////////////////////////////////
302
+
303
+ /**
304
+ * To generate all available helpers use in your `API` types:
305
+ *
306
+ * ```ts
307
+ * type Response = Koine.Api.GenerateResponseHelpers<Endpoints>;
308
+ * type Request = Koine.Api.GenerateRequestHelpers<Endpoints>;
309
+ * type Get = Koine.Api.GenerateGetHelpers<Endpoints>;
310
+ * type Post = Koine.Api.GeneratePostHelpers<Endpoints>;
311
+ * ```
312
+ */
313
+ type Generate = "here just to read the example usage";
314
+
315
+ type _ShortcutsMaps = {
316
+ [TMethod in RequestMethod]: Capitalize<TMethod>;
317
+ };
318
+
319
+ type _ShortcutsMapsByMethod = {
320
+ [K in keyof _ShortcutsMaps as _ShortcutsMaps[K]]: K;
321
+ };
322
+
323
+ /**
324
+ * @example
325
+ * ```ts
326
+ * // define the type on your `API` types:
327
+ * type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
328
+ *
329
+ * // consume the type wherever in your app:
330
+ * type MyData = API.Response["get"]["my/endpoint"];
331
+ * ```
332
+ */
333
+ type GenerateResponseShortcuts<TEndpoints extends Endpoints> = {
334
+ [TMethod in RequestMethod]: {
335
+ [TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl][Uppercase<TMethod>]["response"];
336
+ };
337
+ };
338
+
339
+ /**
340
+ * @example
341
+ * ```ts
342
+ * // define the type on your `API` types:
343
+ * type Get = Koine.Api.GenerateResponseShortcuts<Endpoints>;
344
+ *
345
+ * // consume the type wherever in your app:
346
+ * type MyData = API.Get["my/endpoint"];
347
+ * ```
348
+ */
349
+ type GenerateGetShortcuts<TEndpoints extends Endpoints> = {
350
+ [TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["GET"]["response"];
351
+ };
352
+
353
+ /**
354
+ * @example
355
+ * ```ts
356
+ * // define the type on your `API` types:
357
+ * type Post = Koine.Api.GenerateResponseShortcuts<Endpoints>;
358
+ *
359
+ * // consume the type wherever in your app:
360
+ * type MyData = API.Post["my/endpoint"];
361
+ * ```
362
+ */
363
+ type GeneratePostShortcuts<TEndpoints extends Endpoints> = {
364
+ [TEndpointUrl in keyof TEndpoints]: TEndpoints[TEndpointUrl]["POST"]["response"];
365
+ };
366
+
367
+ /**
368
+ * This is not useful as it is the same as doing
369
+ * `API.Endpoints["my/endpoint"]["GET"]["response"];`
370
+ *
371
+ * @example
372
+ * ```ts
373
+ * // define the type on your `API` types:
374
+ * type Response = Koine.Api.GenerateResponseShortcuts<Endpoints>;
375
+ *
376
+ * // consume the type wherever in your app:
377
+ * type MyData = API.$["my/endpoint"]["get"]["response"];
378
+ * ```
379
+ * @deprecated
380
+ */
381
+ // type GenerateAllShortcuts<TEndpoints extends Endpoints> = {
382
+ // [TEndpointUrl in keyof TEndpoints]: {
383
+ // [TMethod in RequestMethod]: {
384
+ // [DataType in EndpointDataType]: TEndpoints[TEndpointUrl][Uppercase<TMethod>][DataType];
385
+ // }
386
+ // }
387
+ // }
309
388
  }