@koine/api 2.0.0-beta.9 → 2.0.0-beta.91
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.cjs.js +27 -0
- package/createApi.cjs.mjs +2 -0
- package/createApi.d.ts +2 -7
- package/createApi.esm.js +22 -0
- package/createApiResultFail.cjs.d.ts +2 -0
- package/createApiResultFail.cjs.default.js +1 -0
- package/createApiResultFail.cjs.js +9 -0
- package/createApiResultFail.cjs.mjs +2 -0
- package/createApiResultFail.d.ts +4 -0
- package/createApiResultFail.esm.js +3 -0
- package/createApiResultOk.cjs.d.ts +2 -0
- package/createApiResultOk.cjs.default.js +1 -0
- package/createApiResultOk.cjs.js +8 -0
- package/createApiResultOk.cjs.mjs +2 -0
- package/createApiResultOk.d.ts +3 -0
- package/createApiResultOk.esm.js +3 -0
- package/createSwrApi.cjs.js +50 -0
- package/createSwrApi.esm.js +43 -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 +14 -0
- package/next.cjs.mjs +2 -0
- package/next.d.ts +2 -0
- package/next.esm.js +9 -0
- package/package.json +53 -12
- package/swr/createSwrApi.d.ts +10 -0
- package/swr-mutation/createSwrMutationApi.d.ts +15 -0
- package/swr-mutation.cjs.d.ts +1 -0
- package/swr-mutation.cjs.default.js +1 -0
- package/swr-mutation.cjs.js +26 -0
- package/swr-mutation.cjs.mjs +2 -0
- package/swr-mutation.d.ts +1 -0
- package/swr-mutation.esm.js +18 -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/createApi.js +0 -142
- package/createApi.mjs +0 -128
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./ApiError.cjs.js').default;
|
package/ApiError.cjs.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Custom `ApiError` class extending `Error` to throw in failed response.
|
|
7
|
+
*
|
|
8
|
+
* @see https://eslint.org/docs/rules/no-throw-literal
|
|
9
|
+
* @see https://github.com/sindresorhus/ky/blob/main/source/errors/HTTPError.ts
|
|
10
|
+
*
|
|
11
|
+
*/class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r);}}
|
|
12
|
+
|
|
13
|
+
exports.ApiError = ApiError;
|
|
14
|
+
exports["default"] = ApiError;
|
package/ApiError.cjs.mjs
ADDED
package/ApiError.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
*/
|
|
8
|
-
export declare class ApiError<TResponseFail extends Koine.Api.ResponseFail = unknown> extends Error {
|
|
9
|
-
constructor(result: Koine.Api.ResultFail<TResponseFail>);
|
|
1
|
+
import type { Api } from "./types";
|
|
2
|
+
export declare class ApiError<TResponseFail extends Api.ResponseFail = unknown> extends Error {
|
|
3
|
+
constructor(result: Api.ResultFail<TResponseFail>);
|
|
10
4
|
}
|
|
11
5
|
export default ApiError;
|
package/ApiError.esm.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
*/class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r);}}
|
|
8
|
+
|
|
9
|
+
export { ApiError, ApiError as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApi.cjs.js').default;
|
package/createApi.cjs.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var utils = require('@koine/utils');
|
|
6
|
+
|
|
7
|
+
let o=["get","post","put","patch","delete"];/**
|
|
8
|
+
* Create api client
|
|
9
|
+
*
|
|
10
|
+
* @param apiName Short name to use in debug logs
|
|
11
|
+
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
12
|
+
*/let createApi=(l,a,i)=>{let{headers:s={},request:n={},throwErr:p,timeout:c=1e4,processReq:u,processRes:f,processErr:d}=i||{};return o.reduce((o,i)=>(// @ts-expect-error FIXME: type
|
|
13
|
+
o[i]=async(o,h)=>{let $,m;let{request:w=n,headers:y=s,timeout:g=c,processReq:b,processRes:E=f,processErr:N=d,throwErr:k=p}=h||{},{params:A,json:C,query:j}=h||{},q=`${a}/${o+"".replace(/^\/*/,"")}`,v={method:i.toUpperCase(),...w,headers:{"content-type":"application/json",...y}};if(u){let e=u(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4];}if(b){let e=b(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4];}if(utils.isFullObject(A))for(let e in A)q=q.replace(`{${e}}`,A[e].toString());let x=Number(g);C&&(v.body=JSON.stringify(C)),x>0&&(// TODO: combine multiple abort signals
|
|
14
|
+
// @see https://dev.to/rashidshamloo/adding-timeout-and-multiple-abort-signals-to-fetch-typescriptreact-33bb
|
|
15
|
+
$=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
16
|
+
(q+=utils.buildUrlQueryString(j));let O=null,R=null,S="";try{O=await fetch(q,v);}catch(e){S=utils.errorToString(e);}if(m&&clearTimeout(m),O)try{R=E?await E(O,h||{}):await O.json();}catch(e){S=utils.errorToString(e);}if(null===R&&(R=N?await N(S,h||{}):// this error should only happen on network errors or wrong API urls
|
|
17
|
+
// there is no specific HTTP error for this, we can consider these
|
|
18
|
+
// two statuses though:
|
|
19
|
+
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
20
|
+
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
21
|
+
{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)// throw new ApiError<Failed>(result);
|
|
22
|
+
// I prefer to throw an object literal despite what eslint says
|
|
23
|
+
// eslint-disable-next-line no-throw-literal
|
|
24
|
+
throw R;if("development"===process.env.NODE_ENV){let e=`${R?.status}: api[${l}] ${i.toUpperCase()} ${q}`;R?.ok?console.info(`🟢 ${e}`):console.info(`🔴 ${e}`);}return R},o),{})};
|
|
25
|
+
|
|
26
|
+
exports.createApi = createApi;
|
|
27
|
+
exports["default"] = createApi;
|
package/createApi.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @param apiName Short name to use in debug logs
|
|
5
|
-
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
6
|
-
*/
|
|
7
|
-
export declare const createApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions) => Koine.Api.Client<TEndpoints>;
|
|
1
|
+
import type { Api } from "./types";
|
|
2
|
+
export declare let createApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions) => Api.Client<TEndpoints>;
|
|
8
3
|
export default createApi;
|
package/createApi.esm.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { isFullObject, buildUrlQueryString, errorToString } from '@koine/utils';
|
|
2
|
+
|
|
3
|
+
let o=["get","post","put","patch","delete"];/**
|
|
4
|
+
* Create api client
|
|
5
|
+
*
|
|
6
|
+
* @param apiName Short name to use in debug logs
|
|
7
|
+
* @param baseUrl Either relativ eor absolute, it must end without trailing slash
|
|
8
|
+
*/let createApi=(l,a,i)=>{let{headers:s={},request:n={},throwErr:p,timeout:c=1e4,processReq:u,processRes:f,processErr:d}=i||{};return o.reduce((o,i)=>(// @ts-expect-error FIXME: type
|
|
9
|
+
o[i]=async(o,h)=>{let $,m;let{request:w=n,headers:y=s,timeout:g=c,processReq:b,processRes:E=f,processErr:N=d,throwErr:k=p}=h||{},{params:A,json:C,query:j}=h||{},q=`${a}/${o+"".replace(/^\/*/,"")}`,v={method:i.toUpperCase(),...w,headers:{"content-type":"application/json",...y}};if(u){let e=u(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4];}if(b){let e=b(i,q,j,C,A,v);q=e[0],j=e[1],C=e[2],A=e[3],v=e[4];}if(isFullObject(A))for(let e in A)q=q.replace(`{${e}}`,A[e].toString());let x=Number(g);C&&(v.body=JSON.stringify(C)),x>0&&(// TODO: combine multiple abort signals
|
|
10
|
+
// @see https://dev.to/rashidshamloo/adding-timeout-and-multiple-abort-signals-to-fetch-typescriptreact-33bb
|
|
11
|
+
$=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&// FIXME: ts-expect-error this assertion is not the best, but nevermind for now
|
|
12
|
+
(q+=buildUrlQueryString(j));let O=null,R=null,S="";try{O=await fetch(q,v);}catch(e){S=errorToString(e);}if(m&&clearTimeout(m),O)try{R=E?await E(O,h||{}):await O.json();}catch(e){S=errorToString(e);}if(null===R&&(R=N?await N(S,h||{}):// this error should only happen on network errors or wrong API urls
|
|
13
|
+
// there is no specific HTTP error for this, we can consider these
|
|
14
|
+
// two statuses though:
|
|
15
|
+
// - [100 Continue](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/100)
|
|
16
|
+
// - [501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501)
|
|
17
|
+
{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)// throw new ApiError<Failed>(result);
|
|
18
|
+
// I prefer to throw an object literal despite what eslint says
|
|
19
|
+
// eslint-disable-next-line no-throw-literal
|
|
20
|
+
throw R;if("development"===process.env.NODE_ENV){let e=`${R?.status}: api[${l}] ${i.toUpperCase()} ${q}`;R?.ok?console.info(`🟢 ${e}`):console.info(`🔴 ${e}`);}return R},o),{})};
|
|
21
|
+
|
|
22
|
+
export { createApi, createApi as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApiResultFail.cjs.js').default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
let createApiResultOk=(t={},e)=>({ok:!0,fail:!1,data:t,msg:e||"",status:200});let createApiResultFail=(t={},e,a)=>({fail:!0,data:t,msg:e||"",status:a||404});
|
|
6
|
+
|
|
7
|
+
exports.createApiResultFail = createApiResultFail;
|
|
8
|
+
exports.createApiResultOk = createApiResultOk;
|
|
9
|
+
exports["default"] = createApiResultFail;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./createApiResultOk.cjs.js').default;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var e = require('swr');
|
|
4
|
+
var utils = require('@koine/utils');
|
|
5
|
+
var createApi = require('./createApi.cjs.js');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var e__default = /*#__PURE__*/_interopDefaultLegacy(e);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Conditional fetching as option
|
|
13
|
+
*
|
|
14
|
+
* Moving this to an option allows us to keep the endpoints typed dictionary,
|
|
15
|
+
* e.g. we can write:
|
|
16
|
+
*
|
|
17
|
+
* ```js
|
|
18
|
+
* const { data, mutate } = myApi.use("User/{id}",
|
|
19
|
+
* { params: { id: aVariableMaybeContainingAnId || "" }, },
|
|
20
|
+
* { when: !!aVariableMaybeContainingAnId }
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* // we still have typed `data`, `mutate`
|
|
24
|
+
* ```
|
|
25
|
+
* @see https://swr.vercel.app/docs/conditional-fetching
|
|
26
|
+
*//**
|
|
27
|
+
* @private
|
|
28
|
+
*/let createUseApi=t=>(i,o,p)=>{// const fetcher = async (_endpoint: TEndpoint) => {
|
|
29
|
+
// try {
|
|
30
|
+
// const { ok, data } = await api.get(_endpoint, {
|
|
31
|
+
// ...(options || {}),
|
|
32
|
+
// throwErr: true,
|
|
33
|
+
// });
|
|
34
|
+
// if (ok) {
|
|
35
|
+
// return data;
|
|
36
|
+
// }
|
|
37
|
+
// throw new Error() as unknown as Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
38
|
+
// } catch(e) {
|
|
39
|
+
// throw new Error() as unknown as Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
40
|
+
// }
|
|
41
|
+
// };
|
|
42
|
+
// }
|
|
43
|
+
let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
44
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
45
|
+
return e__default["default"](p?.when===!1||utils.isFunction(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};/**
|
|
46
|
+
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
47
|
+
*/let createSwrApi=(...e)=>{let r=createApi.createApi(...e);return r.use=createUseApi(r),r};
|
|
48
|
+
|
|
49
|
+
exports.createSwrApi = createSwrApi;
|
|
50
|
+
exports.createUseApi = createUseApi;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import e from 'swr';
|
|
2
|
+
import { isFunction } from '@koine/utils';
|
|
3
|
+
import { createApi } from './createApi.esm.js';
|
|
4
|
+
|
|
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.use("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
|
+
* @private
|
|
22
|
+
*/let createUseApi=t=>(i,o,p)=>{// const fetcher = async (_endpoint: TEndpoint) => {
|
|
23
|
+
// try {
|
|
24
|
+
// const { ok, data } = await api.get(_endpoint, {
|
|
25
|
+
// ...(options || {}),
|
|
26
|
+
// throwErr: true,
|
|
27
|
+
// });
|
|
28
|
+
// if (ok) {
|
|
29
|
+
// return data;
|
|
30
|
+
// }
|
|
31
|
+
// throw new Error() as unknown as Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;
|
|
32
|
+
// } catch(e) {
|
|
33
|
+
// throw new Error() as unknown as Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>;;
|
|
34
|
+
// }
|
|
35
|
+
// };
|
|
36
|
+
// }
|
|
37
|
+
let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
38
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
39
|
+
return e(p?.when===!1||isFunction(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};/**
|
|
40
|
+
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
41
|
+
*/let createSwrApi=(...e)=>{let r=createApi(...e);return r.use=createUseApi(r),r};
|
|
42
|
+
|
|
43
|
+
export { createUseApi as a, createSwrApi as c };
|
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var ApiError = require('./ApiError.cjs.js');
|
|
6
|
+
var createApi = require('./createApi.cjs.js');
|
|
7
|
+
var createApiResultFail = require('./createApiResultFail.cjs.js');
|
|
8
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
9
|
+
require('@koine/utils');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.ApiError = ApiError.ApiError;
|
|
14
|
+
exports.createApi = createApi.createApi;
|
|
15
|
+
exports.createApiResultFail = createApiResultFail.createApiResultFail;
|
|
16
|
+
exports.createApiResultOk = createApiResultOk.createApiResultOk;
|
package/index.cjs.mjs
ADDED
package/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { ApiError } from "./ApiError";
|
|
2
2
|
export { createApi } from "./createApi";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
+
export { createApiResultOk } from "./createApiResultOk";
|
|
5
|
+
export type { Api } from "./types";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
export declare let nextApiResponse: {
|
|
3
|
+
ok<T>(data: T, msg?: string): NextResponse<import("..").Api.ResultOk<T>>;
|
|
4
|
+
fail<T_1>(data: T_1, msg?: string, status?: number): NextResponse<import("..").Api.ResultFail<T_1>>;
|
|
5
|
+
};
|
|
6
|
+
export default nextApiResponse;
|
package/next.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./next";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./next.cjs.js').default;
|
package/next.cjs.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var server = require('next/server');
|
|
6
|
+
var createApiResultFail = require('./createApiResultFail.cjs.js');
|
|
7
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
8
|
+
|
|
9
|
+
let nextApiResponse={ok:(t,r)=>server.NextResponse.json(createApiResultOk.createApiResultOk(t,r)),fail:(o,r,p)=>server.NextResponse.json(createApiResultFail.createApiResultFail(o,r,p))};
|
|
10
|
+
|
|
11
|
+
let nextApiResponse12=s=>({ok(e,o){s.status(200).json(createApiResultOk.createApiResultOk(e,o));},fail(t,o,p){s.status(p||404).json(createApiResultFail.createApiResultFail(t,o,p));}});
|
|
12
|
+
|
|
13
|
+
exports.nextApiResponse = nextApiResponse;
|
|
14
|
+
exports.nextApiResponse12 = nextApiResponse12;
|
package/next.cjs.mjs
ADDED
package/next.d.ts
ADDED
package/next.esm.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { createApiResultFail } from './createApiResultFail.esm.js';
|
|
3
|
+
import { createApiResultOk } from './createApiResultOk.esm.js';
|
|
4
|
+
|
|
5
|
+
let nextApiResponse={ok:(t,r)=>NextResponse.json(createApiResultOk(t,r)),fail:(o,r,p)=>NextResponse.json(createApiResultFail(o,r,p))};
|
|
6
|
+
|
|
7
|
+
let nextApiResponse12=s=>({ok(e,o){s.status(200).json(createApiResultOk(e,o));},fail(t,o,p){s.status(p||404).json(createApiResultFail(t,o,p));}});
|
|
8
|
+
|
|
9
|
+
export { nextApiResponse, nextApiResponse12 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koine/api",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@koine/utils": "2.0.0-beta.91"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"next": "^14.0.4",
|
|
9
|
+
"swr": "^2.2.4"
|
|
10
|
+
},
|
|
4
11
|
"peerDependenciesMeta": {
|
|
5
12
|
"next": {
|
|
6
13
|
"optional": true
|
|
@@ -9,16 +16,50 @@
|
|
|
9
16
|
"optional": true
|
|
10
17
|
}
|
|
11
18
|
},
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
"exports": {
|
|
20
|
+
"./package.json": "./package.json",
|
|
21
|
+
".": {
|
|
22
|
+
"module": "./index.esm.js",
|
|
23
|
+
"import": "./index.cjs.mjs",
|
|
24
|
+
"default": "./index.cjs.js"
|
|
25
|
+
},
|
|
26
|
+
"./ApiError": {
|
|
27
|
+
"module": "./ApiError.esm.js",
|
|
28
|
+
"import": "./ApiError.cjs.mjs",
|
|
29
|
+
"default": "./ApiError.cjs.js"
|
|
30
|
+
},
|
|
31
|
+
"./createApi": {
|
|
32
|
+
"module": "./createApi.esm.js",
|
|
33
|
+
"import": "./createApi.cjs.mjs",
|
|
34
|
+
"default": "./createApi.cjs.js"
|
|
35
|
+
},
|
|
36
|
+
"./createApiResultFail": {
|
|
37
|
+
"module": "./createApiResultFail.esm.js",
|
|
38
|
+
"import": "./createApiResultFail.cjs.mjs",
|
|
39
|
+
"default": "./createApiResultFail.cjs.js"
|
|
40
|
+
},
|
|
41
|
+
"./createApiResultOk": {
|
|
42
|
+
"module": "./createApiResultOk.esm.js",
|
|
43
|
+
"import": "./createApiResultOk.cjs.mjs",
|
|
44
|
+
"default": "./createApiResultOk.cjs.js"
|
|
45
|
+
},
|
|
46
|
+
"./next": {
|
|
47
|
+
"module": "./next.esm.js",
|
|
48
|
+
"import": "./next.cjs.mjs",
|
|
49
|
+
"default": "./next.cjs.js"
|
|
50
|
+
},
|
|
51
|
+
"./swr": {
|
|
52
|
+
"module": "./swr.esm.js",
|
|
53
|
+
"import": "./swr.cjs.mjs",
|
|
54
|
+
"default": "./swr.cjs.js"
|
|
55
|
+
},
|
|
56
|
+
"./swr-mutation": {
|
|
57
|
+
"module": "./swr-mutation.esm.js",
|
|
58
|
+
"import": "./swr-mutation.cjs.mjs",
|
|
59
|
+
"default": "./swr-mutation.cjs.js"
|
|
60
|
+
}
|
|
22
61
|
},
|
|
23
|
-
"
|
|
24
|
-
|
|
62
|
+
"module": "./index.esm.js",
|
|
63
|
+
"main": "./index.cjs.js",
|
|
64
|
+
"version": "2.0.0-beta.91"
|
|
65
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type BareFetcher, type SWRConfiguration, type SWRResponse } from "swr";
|
|
2
|
+
import type { Api } from "../types";
|
|
3
|
+
type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
4
|
+
when?: boolean | (() => boolean);
|
|
5
|
+
};
|
|
6
|
+
export declare let createUseApi: <TEndpoints extends Api.Endpoints>(api: Api.Client<TEndpoints>) => <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get">, config?: SWRConfigurationExtended<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">>) => SWRResponse<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, any>;
|
|
7
|
+
export declare let createSwrApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
8
|
+
use: ReturnType<typeof createUseApi<TEndpoints>>;
|
|
9
|
+
};
|
|
10
|
+
export default createSwrApi;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
2
|
+
import { createUseApi } from "../swr/createSwrApi";
|
|
3
|
+
import type { Api } from "../types";
|
|
4
|
+
type MutationRequestMethod = Exclude<Api.RequestMethod, "get">;
|
|
5
|
+
type MutationHookName = Exclude<keyof Api.HooksMapsByName, "use">;
|
|
6
|
+
type KoineApiMethodHookSWR<THookName extends MutationHookName, TEndpoints extends Api.Endpoints> = <TEndpoint extends Api.EndpointUrl<TEndpoints>, TMethod extends MutationRequestMethod = Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, config?: SWRMutationConfiguration<Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, TEndpoint, Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>>) => SWRMutationResponse<Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, TEndpoint, Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>>;
|
|
7
|
+
export declare let createSwrMutationApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
8
|
+
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
9
|
+
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
10
|
+
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
11
|
+
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
12
|
+
} & {
|
|
13
|
+
use: ReturnType<typeof createUseApi<TEndpoints>>;
|
|
14
|
+
};
|
|
15
|
+
export default createSwrMutationApi;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./swr-mutation";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./swr-mutation.cjs.js').default;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var r = require('swr/mutation');
|
|
6
|
+
var createApi = require('./createApi.cjs.js');
|
|
7
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
8
|
+
require('@koine/utils');
|
|
9
|
+
require('swr');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var r__default = /*#__PURE__*/_interopDefaultLegacy(r);
|
|
14
|
+
|
|
15
|
+
let a=(t,e)=>(a,o,i)=>// config.fetcher = sender;
|
|
16
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
17
|
+
r__default["default"](// @ts-expect-error FIXME: I can't get it...
|
|
18
|
+
o?[a,o]:a,async(// if the first argument is an array the second one are the base options
|
|
19
|
+
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
20
|
+
// by the _options just here below
|
|
21
|
+
r,// these are the options arriving when calling `trigger({ json, query, etc... })
|
|
22
|
+
a)=>{let o=Array.isArray(r)?r[0]:r,i=Array.isArray(r)?r[1]:{},{ok:p,data:s}=await t[e](o,{...i,...a.arg||{},throwErr:!0});return s},i);/**
|
|
23
|
+
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
24
|
+
*/let createSwrMutationApi=(...r)=>{let o=createApi.createApi(...r);return o.use=createSwrApi.createUseApi(o),["post","put","patch","delete"].forEach(r=>{o[`use${r.charAt(0).toUpperCase()+r.slice(1)}`]=a(o,r);}),o};
|
|
25
|
+
|
|
26
|
+
exports.createSwrMutationApi = createSwrMutationApi;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSwrMutationApi } from "./swr-mutation/createSwrMutationApi";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import r from 'swr/mutation';
|
|
2
|
+
import { createApi } from './createApi.esm.js';
|
|
3
|
+
import { a as createUseApi } from './createSwrApi.esm.js';
|
|
4
|
+
import '@koine/utils';
|
|
5
|
+
import 'swr';
|
|
6
|
+
|
|
7
|
+
let a=(t,e)=>(a,o,i)=>// config.fetcher = sender;
|
|
8
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
9
|
+
r(// @ts-expect-error FIXME: I can't get it...
|
|
10
|
+
o?[a,o]:a,async(// if the first argument is an array the second one are the base options
|
|
11
|
+
// defined when calling the usePost/Put/etc. hook, these will be overriden
|
|
12
|
+
// by the _options just here below
|
|
13
|
+
r,// these are the options arriving when calling `trigger({ json, query, etc... })
|
|
14
|
+
a)=>{let o=Array.isArray(r)?r[0]:r,i=Array.isArray(r)?r[1]:{},{ok:p,data:s}=await t[e](o,{...i,...a.arg||{},throwErr:!0});return s},i);/**
|
|
15
|
+
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
16
|
+
*/let createSwrMutationApi=(...r)=>{let o=createApi(...r);return o.use=createUseApi(o),["post","put","patch","delete"].forEach(r=>{o[`use${r.charAt(0).toUpperCase()+r.slice(1)}`]=a(o,r);}),o};
|
|
17
|
+
|
|
18
|
+
export { createSwrMutationApi };
|
package/swr.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./swr";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./swr.cjs.js').default;
|
package/swr.cjs.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
6
|
+
require('swr');
|
|
7
|
+
require('@koine/utils');
|
|
8
|
+
require('./createApi.cjs.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
exports.createSwrApi = createSwrApi.createSwrApi;
|
package/swr.cjs.mjs
ADDED
package/swr.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSwrApi } from "./swr/createSwrApi";
|
package/swr.esm.js
ADDED