@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.
Files changed (67) hide show
  1. package/ApiError.cjs.d.ts +2 -0
  2. package/ApiError.cjs.default.js +1 -0
  3. package/ApiError.cjs.js +14 -0
  4. package/ApiError.cjs.mjs +2 -0
  5. package/ApiError.d.ts +3 -9
  6. package/ApiError.esm.js +9 -0
  7. package/createApi.cjs.d.ts +2 -0
  8. package/createApi.cjs.default.js +1 -0
  9. package/{createApi.js → createApi.cjs.js} +41 -61
  10. package/createApi.cjs.mjs +2 -0
  11. package/createApi.d.ts +2 -7
  12. package/{createApi.mjs → createApi.esm.js} +32 -43
  13. package/createApiResultFail.cjs.d.ts +2 -0
  14. package/createApiResultFail.cjs.default.js +1 -0
  15. package/createApiResultFail.cjs.js +21 -0
  16. package/createApiResultFail.cjs.mjs +2 -0
  17. package/createApiResultFail.d.ts +4 -0
  18. package/createApiResultFail.esm.js +15 -0
  19. package/createApiResultOk.cjs.d.ts +2 -0
  20. package/createApiResultOk.cjs.default.js +1 -0
  21. package/createApiResultOk.cjs.js +14 -0
  22. package/createApiResultOk.cjs.mjs +2 -0
  23. package/createApiResultOk.d.ts +3 -0
  24. package/createApiResultOk.esm.js +9 -0
  25. package/createSwrApi.cjs.js +30 -0
  26. package/createSwrApi.esm.js +23 -0
  27. package/index.cjs.d.ts +1 -0
  28. package/index.cjs.default.js +1 -0
  29. package/index.cjs.js +16 -0
  30. package/index.cjs.mjs +2 -0
  31. package/index.d.ts +3 -2
  32. package/index.esm.js +5 -0
  33. package/next/nextApiResponse.d.ts +6 -0
  34. package/next/nextApiResponse12.d.ts +6 -0
  35. package/next.cjs.d.ts +1 -0
  36. package/next.cjs.default.js +1 -0
  37. package/next.cjs.js +28 -0
  38. package/next.cjs.mjs +2 -0
  39. package/next.d.ts +2 -0
  40. package/next.esm.js +23 -0
  41. package/package.json +53 -12
  42. package/swr/createSwrApi.d.ts +9 -0
  43. package/swr-mutation/createSwrMutationApi.d.ts +19 -0
  44. package/swr-mutation.cjs.d.ts +1 -0
  45. package/swr-mutation.cjs.default.js +1 -0
  46. package/swr-mutation.cjs.js +38 -0
  47. package/swr-mutation.cjs.mjs +2 -0
  48. package/swr-mutation.d.ts +1 -0
  49. package/swr-mutation.esm.js +30 -0
  50. package/swr.cjs.d.ts +1 -0
  51. package/swr.cjs.default.js +1 -0
  52. package/swr.cjs.js +12 -0
  53. package/swr.cjs.mjs +2 -0
  54. package/swr.d.ts +1 -0
  55. package/swr.esm.js +4 -0
  56. package/types.d.ts +126 -0
  57. package/typings.d.ts +11 -508
  58. package/ApiError.js +0 -32
  59. package/ApiError.mjs +0 -14
  60. package/createSwrApi.d.ts +0 -34
  61. package/createSwrApi.js +0 -99
  62. package/createSwrApi.mjs +0 -82
  63. package/index.js +0 -28
  64. package/index.mjs +0 -4
  65. package/nextApiResponse.d.ts +0 -3
  66. package/nextApiResponse.js +0 -23
  67. package/nextApiResponse.mjs +0 -5
package/createSwrApi.js DELETED
@@ -1,99 +0,0 @@
1
- "use client";
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- function _export(target, all) {
7
- for(var name in all)Object.defineProperty(target, name, {
8
- enumerable: true,
9
- get: all[name]
10
- });
11
- }
12
- _export(exports, {
13
- createSwrApi: function() {
14
- return createSwrApi;
15
- },
16
- default: function() {
17
- return _default;
18
- }
19
- });
20
- const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
21
- const _swr = /*#__PURE__*/ _interop_require_default._(require("swr"));
22
- const _mutation = /*#__PURE__*/ _interop_require_default._(require("swr/mutation"));
23
- const _isFunction = /*#__PURE__*/ _interop_require_default._(require("@koine/utils/isFunction"));
24
- const _createApi = /*#__PURE__*/ _interop_require_default._(require("./createApi"));
25
- function createUseApi(api, method) {
26
- return function useApi(endpoint, options, _config) {
27
- if (method === "get") {
28
- // const fetcher = async (_endpoint: TEndpoint) => {
29
- // try {
30
- // const { ok, data } = await api[method](_endpoint, {
31
- // ...(options || {}),
32
- // throwErr: true,
33
- // });
34
- // if (ok) {
35
- // return data;
36
- // }
37
- // throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
38
- // } catch(e) {
39
- // throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
40
- // }
41
- // };
42
- // }
43
- const fetcher = async ()=>{
44
- const { data } = await api[method](endpoint, {
45
- ...options || {},
46
- throwErr: true
47
- });
48
- return data;
49
- };
50
- const config = _config;
51
- const shouldNotFetch = config?.when === false || (0, _isFunction.default)(config?.when) && config?.when() === false;
52
- // <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
53
- // eslint-disable-next-line react-hooks/rules-of-hooks
54
- return (0, _swr.default)(shouldNotFetch ? null : options ? [
55
- endpoint,
56
- options
57
- ] : [
58
- endpoint
59
- ], fetcher, config);
60
- }
61
- const config = _config;
62
- const sender = async (// if the first argument is an array the second tem are the base options
63
- // defined when calling the usePost/Put/etc. hook, these will be overriden
64
- // by the _options just here below
65
- _endpoint, // these are the options arriving when calling `trigger({ json, query, etc... })
66
- _options)=>{
67
- const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
68
- const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
69
- const { ok, data } = await api[method](endpoint, {
70
- ...options,
71
- ..._options.arg || {},
72
- throwErr: true
73
- });
74
- return ok ? data : data;
75
- };
76
- // config.fetcher = sender;
77
- // eslint-disable-next-line react-hooks/rules-of-hooks
78
- return (0, _mutation.default)(// @ts-expect-error FIXME: I can't get it...
79
- options ? [
80
- endpoint,
81
- options
82
- ] : endpoint, sender, config);
83
- };
84
- }
85
- const createSwrApi = (...args)=>{
86
- const api = (0, _createApi.default)(...args);
87
- [
88
- "get",
89
- "post",
90
- "put",
91
- "patch",
92
- "delete"
93
- ].forEach((method)=>{
94
- const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
95
- api[hookName] = createUseApi(api, method);
96
- });
97
- return api;
98
- };
99
- const _default = createSwrApi;
package/createSwrApi.mjs DELETED
@@ -1,82 +0,0 @@
1
- "use client";
2
- import useSWR from "swr";
3
- import useSWRMutation from "swr/mutation";
4
- import isFunction from "@koine/utils/isFunction";
5
- import createApi from "./createApi";
6
- function createUseApi(api, method) {
7
- return function useApi(endpoint, options, _config) {
8
- if (method === "get") {
9
- // const fetcher = async (_endpoint: TEndpoint) => {
10
- // try {
11
- // const { ok, data } = await api[method](_endpoint, {
12
- // ...(options || {}),
13
- // throwErr: true,
14
- // });
15
- // if (ok) {
16
- // return data;
17
- // }
18
- // throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
19
- // } catch(e) {
20
- // throw new Error() as unknown as Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
21
- // }
22
- // };
23
- // }
24
- const fetcher = async ()=>{
25
- const { data } = await api[method](endpoint, {
26
- ...options || {},
27
- throwErr: true
28
- });
29
- return data;
30
- };
31
- const config = _config;
32
- const shouldNotFetch = config?.when === false || isFunction(config?.when) && config?.when() === false;
33
- // <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
34
- // eslint-disable-next-line react-hooks/rules-of-hooks
35
- return useSWR(shouldNotFetch ? null : options ? [
36
- endpoint,
37
- options
38
- ] : [
39
- endpoint
40
- ], fetcher, config);
41
- }
42
- const config = _config;
43
- const sender = async (// if the first argument is an array the second tem are the base options
44
- // defined when calling the usePost/Put/etc. hook, these will be overriden
45
- // by the _options just here below
46
- _endpoint, // these are the options arriving when calling `trigger({ json, query, etc... })
47
- _options)=>{
48
- const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
49
- const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
50
- const { ok, data } = await api[method](endpoint, {
51
- ...options,
52
- ..._options.arg || {},
53
- throwErr: true
54
- });
55
- return ok ? data : data;
56
- };
57
- // config.fetcher = sender;
58
- // eslint-disable-next-line react-hooks/rules-of-hooks
59
- return useSWRMutation(// @ts-expect-error FIXME: I can't get it...
60
- options ? [
61
- endpoint,
62
- options
63
- ] : endpoint, sender, config);
64
- };
65
- }
66
- /**
67
- * It creates an api client extended with auto-generated SWR wrapper hooks
68
- */ export const createSwrApi = (...args)=>{
69
- const api = createApi(...args);
70
- [
71
- "get",
72
- "post",
73
- "put",
74
- "patch",
75
- "delete"
76
- ].forEach((method)=>{
77
- const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
78
- api[hookName] = createUseApi(api, method);
79
- });
80
- return api;
81
- };
82
- export default createSwrApi;
package/index.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- ApiError: function() {
13
- return _ApiError.ApiError;
14
- },
15
- createApi: function() {
16
- return _createApi.createApi;
17
- },
18
- createSwrApi: function() {
19
- return _createSwrApi.createSwrApi;
20
- },
21
- nextApiResponse: function() {
22
- return _nextApiResponse.nextApiResponse;
23
- }
24
- });
25
- const _ApiError = require("./ApiError");
26
- const _createApi = require("./createApi");
27
- const _createSwrApi = require("./createSwrApi");
28
- const _nextApiResponse = require("./nextApiResponse");
package/index.mjs DELETED
@@ -1,4 +0,0 @@
1
- export { ApiError } from "./ApiError";
2
- export { createApi } from "./createApi";
3
- export { createSwrApi } from "./createSwrApi";
4
- export { nextApiResponse } from "./nextApiResponse";
@@ -1,3 +0,0 @@
1
- import type { NextApiResponse } from "next";
2
- export declare const nextApiResponse: (nextRes: NextApiResponse, result: Koine.Api.ResultOk | Koine.Api.ResultFail) => void;
3
- export default nextApiResponse;
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- nextApiResponse: function() {
13
- return nextApiResponse;
14
- },
15
- default: function() {
16
- return _default;
17
- }
18
- });
19
- const nextApiResponse = (nextRes, result)=>{
20
- // nextRes.status(result.status).json(result.data || result.msg);
21
- nextRes.status(result.status).json(result);
22
- };
23
- const _default = nextApiResponse;
@@ -1,5 +0,0 @@
1
- export const nextApiResponse = (nextRes, result)=>{
2
- // nextRes.status(result.status).json(result.data || result.msg);
3
- nextRes.status(result.status).json(result);
4
- };
5
- export default nextApiResponse;