@koine/api 2.0.0-beta.72 → 2.0.0-beta.74
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.d.ts +5 -0
- package/ApiError.js +8 -0
- package/createApi.cjs.js +7 -0
- package/createApi.d.ts +3 -0
- package/createApi.esm.js +5 -0
- package/createApi.js +115 -0
- package/createApiResultFail.d.ts +4 -0
- package/createApiResultFail.js +14 -0
- package/createApiResultOk.cjs.js +8 -0
- package/createApiResultOk.d.ts +3 -0
- package/createApiResultOk.esm.js +5 -0
- package/createApiResultOk.js +8 -0
- package/createSwrApi.cjs.js +14 -0
- package/createSwrApi.esm.js +7 -0
- package/index.cjs.js +7 -5
- package/index.d.ts +4 -7
- package/index.esm.js +5 -3
- package/index.js +4 -0
- package/next/nextApiResponse.d.ts +6 -0
- package/next/nextApiResponse.js +12 -0
- package/next/nextApiResponse12.d.ts +6 -0
- package/next/nextApiResponse12.js +11 -0
- package/next.cjs.js +4 -3
- package/next.d.ts +2 -10
- package/next.esm.js +4 -3
- package/next.js +2 -0
- package/package.json +7 -7
- package/swr/createSwrApi.d.ts +9 -0
- package/swr/createSwrApi.js +22 -0
- package/swr-mutation/createSwrMutationApi.d.ts +19 -0
- package/swr-mutation/createSwrMutationApi.js +27 -0
- package/swr-mutation.cjs.js +3 -3
- package/swr-mutation.d.ts +1 -17
- package/swr-mutation.esm.js +2 -2
- package/swr-mutation.js +1 -0
- package/swr.cjs.js +5 -9
- package/swr.d.ts +1 -10
- package/swr.esm.js +4 -7
- package/swr.js +1 -0
- package/types.js +1 -0
package/ApiError.d.ts
ADDED
package/ApiError.js
ADDED
package/createApi.cjs.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('@koine/utils');
|
|
4
|
+
|
|
5
|
+
let o=["get","post","put","patch","delete"];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)=>(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&&($=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&(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||{}):{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)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),{})};
|
|
6
|
+
|
|
7
|
+
exports.createApi = createApi;
|
package/createApi.d.ts
ADDED
package/createApi.esm.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { isFullObject, buildUrlQueryString, errorToString } from '@koine/utils';
|
|
2
|
+
|
|
3
|
+
let o=["get","post","put","patch","delete"];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)=>(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&&($=new AbortController,m=setTimeout(()=>$.abort(),x),v.signal=$.signal),j&&(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||{}):{data:null,msg:S,status:100,fail:!0,ok:!1}),k&&R?.fail)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),{})};
|
|
4
|
+
|
|
5
|
+
export { createApi as c };
|
package/createApi.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { buildUrlQueryString, errorToString, isFullObject, } from "@koine/utils";
|
|
2
|
+
let apiMethods = [
|
|
3
|
+
"get",
|
|
4
|
+
"post",
|
|
5
|
+
"put",
|
|
6
|
+
"patch",
|
|
7
|
+
"delete",
|
|
8
|
+
];
|
|
9
|
+
export let createApi = (apiName, baseUrl, options) => {
|
|
10
|
+
const { headers: headersBase = {}, request: requestBase = {}, throwErr: throwErrBase, timeout: timeoutBase = 10000, processReq: processReqBase, processRes: processResBase, processErr: processErrBase, } = options || {};
|
|
11
|
+
return apiMethods.reduce((api, method) => {
|
|
12
|
+
api[method] = async (endpoint, options) => {
|
|
13
|
+
const { request = requestBase, headers = headersBase, timeout = timeoutBase, processReq, processRes = processResBase, processErr = processErrBase, throwErr = throwErrBase, } = options || {};
|
|
14
|
+
let { params, json, query } = options || {};
|
|
15
|
+
let url = `${baseUrl}/${endpoint + "".replace(/^\/*/, "")}`;
|
|
16
|
+
let requestInit = {
|
|
17
|
+
method: method.toUpperCase(),
|
|
18
|
+
...request,
|
|
19
|
+
headers: {
|
|
20
|
+
"content-type": "application/json",
|
|
21
|
+
...headers,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
if (processReqBase) {
|
|
25
|
+
const transformed = processReqBase(method, url, query, json, params, requestInit);
|
|
26
|
+
url = transformed[0];
|
|
27
|
+
query = transformed[1];
|
|
28
|
+
json = transformed[2];
|
|
29
|
+
params = transformed[3];
|
|
30
|
+
requestInit = transformed[4];
|
|
31
|
+
}
|
|
32
|
+
if (processReq) {
|
|
33
|
+
const transformed = processReq(method, url, query, json, params, requestInit);
|
|
34
|
+
url = transformed[0];
|
|
35
|
+
query = transformed[1];
|
|
36
|
+
json = transformed[2];
|
|
37
|
+
params = transformed[3];
|
|
38
|
+
requestInit = transformed[4];
|
|
39
|
+
}
|
|
40
|
+
if (isFullObject(params)) {
|
|
41
|
+
for (const key in params) {
|
|
42
|
+
url = url.replace(`{${key}}`, params[key].toString());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const timeoutNumber = Number(timeout);
|
|
46
|
+
let controller;
|
|
47
|
+
let timeoutId;
|
|
48
|
+
if (json) {
|
|
49
|
+
requestInit.body = JSON.stringify(json);
|
|
50
|
+
}
|
|
51
|
+
if (timeoutNumber > 0) {
|
|
52
|
+
controller = new AbortController();
|
|
53
|
+
timeoutId = setTimeout(() => controller.abort(), timeoutNumber);
|
|
54
|
+
requestInit.signal = controller.signal;
|
|
55
|
+
}
|
|
56
|
+
if (query) {
|
|
57
|
+
url += buildUrlQueryString(query);
|
|
58
|
+
}
|
|
59
|
+
let response = null;
|
|
60
|
+
let result = null;
|
|
61
|
+
let msg = "";
|
|
62
|
+
try {
|
|
63
|
+
response = await fetch(url, requestInit);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
msg = errorToString(e);
|
|
67
|
+
}
|
|
68
|
+
if (timeoutId) {
|
|
69
|
+
clearTimeout(timeoutId);
|
|
70
|
+
}
|
|
71
|
+
if (response) {
|
|
72
|
+
try {
|
|
73
|
+
if (processRes) {
|
|
74
|
+
result = await processRes(response, options || {});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
result = await response.json();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
msg = errorToString(e);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (result === null) {
|
|
85
|
+
if (processErr) {
|
|
86
|
+
result = await processErr(msg, options || {});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
result = {
|
|
90
|
+
data: null,
|
|
91
|
+
msg,
|
|
92
|
+
status: 100,
|
|
93
|
+
fail: true,
|
|
94
|
+
ok: false,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (throwErr && result?.fail) {
|
|
99
|
+
throw result;
|
|
100
|
+
}
|
|
101
|
+
if (process.env["NODE_ENV"] === "development") {
|
|
102
|
+
const logMsg = `${result?.status}: api[${apiName}] ${method.toUpperCase()} ${url}`;
|
|
103
|
+
if (result?.ok) {
|
|
104
|
+
console.info(`🟢 ${logMsg}`);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
console.info(`🔴 ${logMsg}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
return api;
|
|
113
|
+
}, {});
|
|
114
|
+
};
|
|
115
|
+
export default createApi;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export let createApiResultOk = (data = {}, msg) => ({
|
|
2
|
+
ok: true,
|
|
3
|
+
fail: false,
|
|
4
|
+
data,
|
|
5
|
+
msg: msg || "",
|
|
6
|
+
status: 200,
|
|
7
|
+
});
|
|
8
|
+
export let createApiResultFail = (data = {}, msg, status) => ({
|
|
9
|
+
fail: true,
|
|
10
|
+
data,
|
|
11
|
+
msg: msg || "",
|
|
12
|
+
status: status || 404,
|
|
13
|
+
});
|
|
14
|
+
export default createApiResultFail;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let createApiResultFail=(t={},e,a)=>({fail:!0,data:t,msg:e||"",status:a||404});
|
|
4
|
+
|
|
5
|
+
let createApiResultOk=(e={},t)=>({ok:!0,fail:!1,data:e,msg:t||"",status:200});
|
|
6
|
+
|
|
7
|
+
exports.createApiResultFail = createApiResultFail;
|
|
8
|
+
exports.createApiResultOk = createApiResultOk;
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
let createUseApi=t=>(i,o,p)=>{let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};return e__default["default"](p?.when===!1||utils.isFunction(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};let createSwrApi=(...e)=>{let r=createApi.createApi(...e);return r.use=createUseApi(r),r};
|
|
12
|
+
|
|
13
|
+
exports.createSwrApi = createSwrApi;
|
|
14
|
+
exports.createUseApi = createUseApi;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import e from 'swr';
|
|
2
|
+
import { isFunction } from '@koine/utils';
|
|
3
|
+
import { c as createApi } from './createApi.esm.js';
|
|
4
|
+
|
|
5
|
+
let createUseApi=t=>(i,o,p)=>{let a=async()=>{let{data:e}=await t.get(i,{...o||{},throwErr:!0});return e};return e(p?.when===!1||isFunction(p?.when)&&p?.when()===!1?null:o?[i,o]:[i],a,p)};let createSwrApi=(...e)=>{let r=createApi(...e);return r.use=createUseApi(r),r};
|
|
6
|
+
|
|
7
|
+
export { createUseApi as a, createSwrApi as c };
|
package/index.cjs.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var createApi = require('./createApi.cjs.js');
|
|
6
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
7
|
+
require('@koine/utils');
|
|
6
8
|
|
|
7
|
-
class ApiError extends Error{constructor(
|
|
9
|
+
class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r);}}
|
|
8
10
|
|
|
11
|
+
exports.createApi = createApi.createApi;
|
|
12
|
+
exports.createApiResultFail = createApiResultOk.createApiResultFail;
|
|
13
|
+
exports.createApiResultOk = createApiResultOk.createApiResultOk;
|
|
9
14
|
exports.ApiError = ApiError;
|
|
10
|
-
exports.createApi = createApi;
|
|
11
|
-
exports.createApiResultFail = createApiResultFail;
|
|
12
|
-
exports.createApiResultOk = createApiResultOk;
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export declare let createApiResultOk: <T>(data?: T, msg?: string) => Api.ResultOk<T>;
|
|
6
|
-
export declare let createApiResultFail: <T>(data?: T, msg?: string, status?: number) => Api.ResultFail<T>;
|
|
7
|
-
export declare let createApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions) => Api.Client<TEndpoints>;
|
|
1
|
+
export { ApiError } from "./ApiError";
|
|
2
|
+
export { createApi } from "./createApi";
|
|
3
|
+
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
+
export { createApiResultOk } from "./createApiResultOk";
|
|
8
5
|
export type { Api } from "./types";
|
package/index.esm.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
export { c as createApi } from './createApi.esm.js';
|
|
2
|
+
export { c as createApiResultFail, a as createApiResultOk } from './createApiResultOk.esm.js';
|
|
3
|
+
import '@koine/utils';
|
|
2
4
|
|
|
3
|
-
class ApiError extends Error{constructor(
|
|
5
|
+
class ApiError extends Error{constructor(r){super(`Request failed with ${r.status} ${r.msg}`),this.name="ApiError",Object.assign(this,r);}}
|
|
4
6
|
|
|
5
|
-
export { ApiError
|
|
7
|
+
export { ApiError };
|
package/index.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;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { createApiResultFail } from "../createApiResultFail";
|
|
3
|
+
import { createApiResultOk } from "../createApiResultOk";
|
|
4
|
+
export let nextApiResponse = {
|
|
5
|
+
ok(data, msg) {
|
|
6
|
+
return NextResponse.json(createApiResultOk(data, msg));
|
|
7
|
+
},
|
|
8
|
+
fail(data, msg, status) {
|
|
9
|
+
return NextResponse.json(createApiResultFail(data, msg, status));
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
export default nextApiResponse;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createApiResultFail } from "../createApiResultFail";
|
|
2
|
+
import { createApiResultOk } from "../createApiResultOk";
|
|
3
|
+
export let nextApiResponse12 = (nextRes) => ({
|
|
4
|
+
ok(data, msg) {
|
|
5
|
+
nextRes.status(200).json(createApiResultOk(data, msg));
|
|
6
|
+
},
|
|
7
|
+
fail(data, msg, status) {
|
|
8
|
+
nextRes.status(status || 404).json(createApiResultFail(data, msg, status));
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
export default nextApiResponse12;
|
package/next.cjs.js
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var server = require('next/server');
|
|
6
|
-
var
|
|
7
|
-
require('@koine/utils');
|
|
6
|
+
var createApiResultOk = require('./createApiResultOk.cjs.js');
|
|
8
7
|
|
|
9
|
-
let nextApiResponse={ok:(
|
|
8
|
+
let nextApiResponse={ok:(t,r)=>server.NextResponse.json(createApiResultOk.createApiResultOk(t,r)),fail:(o,r,p)=>server.NextResponse.json(createApiResultOk.createApiResultFail(o,r,p))};
|
|
9
|
+
|
|
10
|
+
let nextApiResponse12=s=>({ok(e,o){s.status(200).json(createApiResultOk.createApiResultOk(e,o));},fail(t,o,p){s.status(p||404).json(createApiResultOk.createApiResultFail(t,o,p));}});
|
|
10
11
|
|
|
11
12
|
exports.nextApiResponse = nextApiResponse;
|
|
12
13
|
exports.nextApiResponse12 = nextApiResponse12;
|
package/next.d.ts
CHANGED
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export declare let nextApiResponse: {
|
|
4
|
-
ok<T>(data: T, msg?: string): NextResponse<import("./types").Api.ResultOk<T>>;
|
|
5
|
-
fail<T_1>(data: T_1, msg?: string, status?: number): NextResponse<import("./types").Api.ResultFail<T_1>>;
|
|
6
|
-
};
|
|
7
|
-
export declare let nextApiResponse12: (nextRes: NextApiResponse) => {
|
|
8
|
-
ok<T>(data: T, msg?: string): void;
|
|
9
|
-
fail<T_1>(data: T_1, msg?: string, status?: number): void;
|
|
10
|
-
};
|
|
1
|
+
export { nextApiResponse } from "./next/nextApiResponse";
|
|
2
|
+
export { nextApiResponse12 } from "./next/nextApiResponse12";
|
package/next.esm.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
|
-
import { createApiResultOk, createApiResultFail } from './
|
|
3
|
-
import '@koine/utils';
|
|
2
|
+
import { a as createApiResultOk, c as createApiResultFail } from './createApiResultOk.esm.js';
|
|
4
3
|
|
|
5
|
-
let nextApiResponse={ok:(
|
|
4
|
+
let nextApiResponse={ok:(t,r)=>NextResponse.json(createApiResultOk(t,r)),fail:(o,r,p)=>NextResponse.json(createApiResultFail(o,r,p))};
|
|
5
|
+
|
|
6
|
+
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));}});
|
|
6
7
|
|
|
7
8
|
export { nextApiResponse, nextApiResponse12 };
|
package/next.js
ADDED
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@koine/api",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@koine/utils": "2.0.0-beta.
|
|
5
|
+
"@koine/utils": "2.0.0-beta.74"
|
|
6
6
|
},
|
|
7
7
|
"peerDependencies": {
|
|
8
8
|
"next": "^14.0.4",
|
|
@@ -28,18 +28,18 @@
|
|
|
28
28
|
"import": "./next.cjs.mjs",
|
|
29
29
|
"default": "./next.cjs.js"
|
|
30
30
|
},
|
|
31
|
-
"./swr-mutation": {
|
|
32
|
-
"module": "./swr-mutation.esm.js",
|
|
33
|
-
"import": "./swr-mutation.cjs.mjs",
|
|
34
|
-
"default": "./swr-mutation.cjs.js"
|
|
35
|
-
},
|
|
36
31
|
"./swr": {
|
|
37
32
|
"module": "./swr.esm.js",
|
|
38
33
|
"import": "./swr.cjs.mjs",
|
|
39
34
|
"default": "./swr.cjs.js"
|
|
35
|
+
},
|
|
36
|
+
"./swr-mutation": {
|
|
37
|
+
"module": "./swr-mutation.esm.js",
|
|
38
|
+
"import": "./swr-mutation.cjs.mjs",
|
|
39
|
+
"default": "./swr-mutation.cjs.js"
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
"module": "./index.esm.js",
|
|
43
43
|
"main": "./index.cjs.js",
|
|
44
|
-
"version": "2.0.0-beta.
|
|
44
|
+
"version": "2.0.0-beta.74"
|
|
45
45
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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 createSwrApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
7
|
+
use: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: SWRConfigurationExtended<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, BareFetcher<any>> | undefined) => SWRResponse<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, any>;
|
|
8
|
+
};
|
|
9
|
+
export default createSwrApi;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import useSWR, {} from "swr";
|
|
3
|
+
import { isFunction } from "@koine/utils";
|
|
4
|
+
import { createApi } from "../createApi";
|
|
5
|
+
export let createUseApi = (api) => (endpoint, options, config) => {
|
|
6
|
+
const fetcher = async () => {
|
|
7
|
+
const { data } = await api.get(endpoint, {
|
|
8
|
+
...(options || {}),
|
|
9
|
+
throwErr: true,
|
|
10
|
+
});
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
const shouldNotFetch = config?.when === false ||
|
|
14
|
+
(isFunction(config?.when) && config?.when() === false);
|
|
15
|
+
return useSWR(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config);
|
|
16
|
+
};
|
|
17
|
+
export let createSwrApi = (...args) => {
|
|
18
|
+
const api = createApi(...args);
|
|
19
|
+
api.use = createUseApi(api);
|
|
20
|
+
return api;
|
|
21
|
+
};
|
|
22
|
+
export default createSwrApi;
|
|
@@ -0,0 +1,19 @@
|
|
|
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: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: (Partial<import("swr/dist/_internal").PublicConfiguration<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, import("swr/dist/_internal").BareFetcher<any>>> & Partial<import("swr/dist/_internal").ProviderConfiguration> & {
|
|
14
|
+
provider?: ((cache: Readonly<import("swr/dist/_internal").Cache<any>>) => import("swr/dist/_internal").Cache<any>) | undefined;
|
|
15
|
+
} & {
|
|
16
|
+
when?: boolean | (() => boolean) | undefined;
|
|
17
|
+
}) | undefined) => import("swr/dist/_internal").SWRResponse<Api.GetDataType<TEndpoints, TEndpoint, "get", "ok">, Api.GetDataType<TEndpoints, TEndpoint, "get", "fail">, any>;
|
|
18
|
+
};
|
|
19
|
+
export default createSwrMutationApi;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import useSWRMutation, {} from "swr/mutation";
|
|
3
|
+
import { createApi } from "../createApi";
|
|
4
|
+
import { createUseApi } from "../swr/createSwrApi";
|
|
5
|
+
let createUseMutationApi = (api, method) => (endpoint, options, config) => {
|
|
6
|
+
const sender = async (_endpoint, _options) => {
|
|
7
|
+
const endpoint = Array.isArray(_endpoint) ? _endpoint[0] : _endpoint;
|
|
8
|
+
const options = Array.isArray(_endpoint) ? _endpoint[1] : {};
|
|
9
|
+
const { ok, data } = await api[method](endpoint, {
|
|
10
|
+
...options,
|
|
11
|
+
...(_options.arg || {}),
|
|
12
|
+
throwErr: true,
|
|
13
|
+
});
|
|
14
|
+
return ok ? data : data;
|
|
15
|
+
};
|
|
16
|
+
return useSWRMutation(options ? [endpoint, options] : endpoint, sender, config);
|
|
17
|
+
};
|
|
18
|
+
export let createSwrMutationApi = (...args) => {
|
|
19
|
+
const api = createApi(...args);
|
|
20
|
+
api.use = createUseApi(api);
|
|
21
|
+
["post", "put", "patch", "delete"].forEach((method) => {
|
|
22
|
+
const hookName = `use${method.charAt(0).toUpperCase() + method.slice(1)}`;
|
|
23
|
+
api[hookName] = createUseMutationApi(api, method);
|
|
24
|
+
});
|
|
25
|
+
return api;
|
|
26
|
+
};
|
|
27
|
+
export default createSwrMutationApi;
|
package/swr-mutation.cjs.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var r = require('swr/mutation');
|
|
6
|
-
var
|
|
7
|
-
var
|
|
6
|
+
var createApi = require('./createApi.cjs.js');
|
|
7
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
8
8
|
require('@koine/utils');
|
|
9
9
|
require('swr');
|
|
10
10
|
|
|
@@ -12,6 +12,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
12
12
|
|
|
13
13
|
var r__default = /*#__PURE__*/_interopDefaultLegacy(r);
|
|
14
14
|
|
|
15
|
-
let a=(t,e)=>(a,o,i)=>r__default["default"](o?[a,o]:a,async(r,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);let createSwrMutationApi=(...r)=>{let o=
|
|
15
|
+
let a=(t,e)=>(a,o,i)=>r__default["default"](o?[a,o]:a,async(r,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);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};
|
|
16
16
|
|
|
17
17
|
exports.createSwrMutationApi = createSwrMutationApi;
|
package/swr-mutation.d.ts
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { createUseApi } from "./swr";
|
|
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: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: (Partial<import("swr/_internal").PublicConfiguration<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, import("swr/_internal").BareFetcher<any>>> & {
|
|
14
|
-
when?: boolean | (() => boolean) | undefined;
|
|
15
|
-
}) | undefined) => import("swr/_internal").SWRResponse<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, any>;
|
|
16
|
-
};
|
|
17
|
-
export {};
|
|
1
|
+
export { createSwrMutationApi } from "./swr-mutation/createSwrMutationApi";
|
package/swr-mutation.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import r from 'swr/mutation';
|
|
2
|
-
import { createApi } from './
|
|
3
|
-
import { createUseApi } from './
|
|
2
|
+
import { c as createApi } from './createApi.esm.js';
|
|
3
|
+
import { a as createUseApi } from './createSwrApi.esm.js';
|
|
4
4
|
import '@koine/utils';
|
|
5
5
|
import 'swr';
|
|
6
6
|
|
package/swr-mutation.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSwrMutationApi } from "./swr-mutation/createSwrMutationApi";
|
package/swr.cjs.js
CHANGED
|
@@ -2,15 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
var createSwrApi = require('./createSwrApi.cjs.js');
|
|
6
|
+
require('swr');
|
|
7
|
+
require('@koine/utils');
|
|
8
|
+
require('./createApi.cjs.js');
|
|
8
9
|
|
|
9
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
10
|
|
|
11
|
-
var e__default = /*#__PURE__*/_interopDefaultLegacy(e);
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
exports.createSwrApi = createSwrApi;
|
|
16
|
-
exports.createUseApi = createUseApi;
|
|
12
|
+
exports.createSwrApi = createSwrApi.createSwrApi;
|
package/swr.d.ts
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
|
|
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"> | undefined, config?: SWRConfigurationExtended<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, BareFetcher<any>> | undefined) => SWRResponse<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, any>;
|
|
7
|
-
export declare let createSwrApi: <TEndpoints extends Api.Endpoints>(apiName: string, baseUrl: string, options?: Api.ClientOptions | undefined) => Api.Client<TEndpoints> & {
|
|
8
|
-
use: <TEndpoint extends Extract<keyof TEndpoints, string>>(endpoint: TEndpoint, options?: Api.EndpointOptions<TEndpoints, TEndpoint, "get"> | undefined, config?: SWRConfigurationExtended<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, BareFetcher<any>> | undefined) => SWRResponse<Api.EndpointResponseOk<TEndpoints, TEndpoint, "get">, Api.EndpointResponseFail<TEndpoints, TEndpoint, "get">, any>;
|
|
9
|
-
};
|
|
10
|
-
export {};
|
|
1
|
+
export { createSwrApi } from "./swr/createSwrApi";
|
package/swr.esm.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
let createUseApi=t=>(i,n,o)=>{let l=async()=>{let{data:e}=await t.get(i,{...n||{},throwErr:!0});return e};return e(o?.when===!1||isFunction(o?.when)&&o?.when()===!1?null:n?[i,n]:[i],l,o)};let createSwrApi=(...e)=>{let r=createApi(...e);return r.use=createUseApi(r),r};
|
|
6
|
-
|
|
7
|
-
export { createSwrApi, createUseApi };
|
|
1
|
+
export { c as createSwrApi } from './createSwrApi.esm.js';
|
|
2
|
+
import 'swr';
|
|
3
|
+
import '@koine/utils';
|
|
4
|
+
import './createApi.esm.js';
|
package/swr.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createSwrApi } from "./swr/createSwrApi";
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|