@koine/api 2.0.0-beta.13 → 2.0.0-beta.130
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.d.ts +2 -0
- 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.d.ts +2 -0
- package/createApi.esm.js +22 -0
- package/createApiResultFail.cjs.d.ts +2 -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.d.ts +2 -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 +2 -1
- package/createApiResultOk.esm.d.ts +2 -0
- package/createApiResultOk.esm.js +3 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +13 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -2
- package/index.esm.d.ts +1 -0
- 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.d.ts +1 -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.d.ts +1 -0
- package/next.esm.js +2 -0
- package/package.json +81 -9
- package/swr/createSwrApi.cjs.js +48 -0
- package/swr/createSwrApi.d.ts +10 -0
- package/swr/createSwrApi.esm.js +42 -0
- package/swr-mutation/createSwrMutationApi.cjs.js +20 -0
- package/swr-mutation/createSwrMutationApi.d.ts +15 -0
- package/swr-mutation/createSwrMutationApi.esm.js +15 -0
- package/swr-mutation.cjs.d.ts +1 -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.d.ts +1 -0
- package/swr-mutation.esm.js +1 -0
- package/swr.cjs.d.ts +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.d.ts +1 -0
- package/swr.esm.js +1 -0
- package/types.cjs.d.ts +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 +126 -0
- package/types.esm.d.ts +1 -0
- package/types.esm.js +1 -0
- package/typings.d.ts +11 -532
- package/ApiError.mjs +0 -14
- package/createApi.mjs +0 -128
- package/createApiResultFail.mjs +0 -7
- package/createApiResultOk.mjs +0 -8
- package/createSwrApi.d.ts +0 -34
- package/createSwrApi.mjs +0 -82
- package/index.mjs +0 -6
- package/nextApiResponse.d.ts +0 -5
- package/nextApiResponse.mjs +0 -12
- package/nextApiResponse12.d.ts +0 -6
- package/nextApiResponse12.mjs +0 -13
package/createApiResultFail.mjs
DELETED
package/createApiResultOk.mjs
DELETED
package/createSwrApi.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type BareFetcher, // type Fetcher,
|
|
2
|
-
type SWRConfiguration, type SWRResponse } from "swr";
|
|
3
|
-
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
4
|
-
type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
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.useGet("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
|
-
when?: boolean | (() => boolean);
|
|
22
|
-
};
|
|
23
|
-
type KoineApiMethodHookSWR<THookName extends keyof Koine.Api.HooksMapsByName, TEndpoints extends Koine.Api.Endpoints> = <TEndpoint extends Koine.Api.EndpointUrl<TEndpoints>, TMethod extends Koine.Api.RequestMethod = Koine.Api.HooksMapsByName[THookName]>(endpoint: TEndpoint, options?: Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, config?: THookName extends "useGet" ? SWRConfigurationExtended<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationConfiguration<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>) => THookName extends "useGet" ? SWRResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>> : SWRMutationResponse<Koine.Api.EndpointResponseOk<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointResponseFail<TEndpoints, TEndpoint, TMethod>, Koine.Api.EndpointOptions<TEndpoints, TEndpoint, TMethod>, TEndpoint>;
|
|
24
|
-
/**
|
|
25
|
-
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
26
|
-
*/
|
|
27
|
-
export declare const createSwrApi: <TEndpoints extends Koine.Api.Endpoints>(apiName: string, baseUrl: string, options?: Koine.Api.ClientOptions | undefined) => Koine.Api.Client<TEndpoints> & {
|
|
28
|
-
useGet: KoineApiMethodHookSWR<"useGet", TEndpoints>;
|
|
29
|
-
usePost: KoineApiMethodHookSWR<"usePost", TEndpoints>;
|
|
30
|
-
usePut: KoineApiMethodHookSWR<"usePut", TEndpoints>;
|
|
31
|
-
usePatch: KoineApiMethodHookSWR<"usePatch", TEndpoints>;
|
|
32
|
-
useDelete: KoineApiMethodHookSWR<"useDelete", TEndpoints>;
|
|
33
|
-
};
|
|
34
|
-
export 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.mjs
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { ApiError } from "./ApiError";
|
|
2
|
-
export { createApi } from "./createApi";
|
|
3
|
-
export { createApiResultFail } from "./createApiResultFail";
|
|
4
|
-
export { createApiResultOk } from "./createApiResultOk";
|
|
5
|
-
export { createSwrApi } from "./createSwrApi";
|
|
6
|
-
export { nextApiResponse12 } from "./nextApiResponse12";
|
package/nextApiResponse.d.ts
DELETED
package/nextApiResponse.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
|
-
import createApiResultFail from "./createApiResultFail";
|
|
3
|
-
import createApiResultOk from "./createApiResultOk";
|
|
4
|
-
export const nextApiResponse = {
|
|
5
|
-
ok (data, msg) {
|
|
6
|
-
NextResponse.json(createApiResultOk(data, msg));
|
|
7
|
-
},
|
|
8
|
-
fail (data, msg, status) {
|
|
9
|
-
NextResponse.json(createApiResultFail(data, msg, status));
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
export default nextApiResponse;
|
package/nextApiResponse12.d.ts
DELETED
package/nextApiResponse12.mjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import createApiResultFail from "./createApiResultFail";
|
|
2
|
-
import createApiResultOk from "./createApiResultOk";
|
|
3
|
-
export const nextApiResponse12 = (nextRes)=>{
|
|
4
|
-
return {
|
|
5
|
-
ok (data, msg) {
|
|
6
|
-
nextRes.status(200).json(createApiResultOk(data, msg));
|
|
7
|
-
},
|
|
8
|
-
fail (data, msg, status) {
|
|
9
|
-
nextRes.status(status || 404).json(createApiResultFail(data, msg, status));
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
export default nextApiResponse12;
|