@koine/api 1.0.57 → 1.0.60
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/createSwrApi.d.ts +21 -2
- package/createSwrApi.js +5 -2
- package/node/createSwrApi.js +5 -2
- package/package.json +2 -2
package/createSwrApi.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
-
import { type SWRConfiguration, type SWRResponse } from "swr";
|
|
1
|
+
import { type SWRConfiguration, type SWRResponse, type BareFetcher } from "swr";
|
|
2
2
|
import { type SWRMutationConfiguration, type SWRMutationResponse } from "swr/mutation";
|
|
3
|
-
declare type
|
|
3
|
+
declare type SWRConfigurationExtended<Data = any, Error = any, Fn extends BareFetcher<any> = BareFetcher<any>> = SWRConfiguration<Data, Error, Fn> & {
|
|
4
|
+
/**
|
|
5
|
+
* Conditional fetching as option
|
|
6
|
+
*
|
|
7
|
+
* Moving this to an option allows us to keep the endpoints typed dictionary,
|
|
8
|
+
* e.g. we can write:
|
|
9
|
+
*
|
|
10
|
+
* ```js
|
|
11
|
+
* const { data, mutate } = myApi.useGet("User/{id}",
|
|
12
|
+
* { params: { id: aVariableMaybeContainingAnId || "" }, },
|
|
13
|
+
* { when: !!aVariableMaybeContainingAnId }
|
|
14
|
+
* );
|
|
15
|
+
*
|
|
16
|
+
* // we still have typed `data`, `mutate`
|
|
17
|
+
* ```
|
|
18
|
+
* @see https://swr.vercel.app/docs/conditional-fetching
|
|
19
|
+
*/
|
|
20
|
+
when?: boolean | (() => boolean);
|
|
21
|
+
};
|
|
22
|
+
declare 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>;
|
|
4
23
|
/**
|
|
5
24
|
* It creates an api client extended with auto-generated SWR wrapper hooks
|
|
6
25
|
*/
|
package/createSwrApi.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { __assign, __awaiter, __generator } from "tslib";
|
|
2
|
+
import { isFunction } from "@koine/utils";
|
|
2
3
|
import useSWR from "swr";
|
|
3
4
|
import useSWRMutation from "swr/mutation";
|
|
4
5
|
import { createApi } from "./createApi";
|
|
@@ -33,9 +34,11 @@ function createUseApi(api, method) {
|
|
|
33
34
|
});
|
|
34
35
|
}); };
|
|
35
36
|
var config_1 = _config;
|
|
36
|
-
|
|
37
|
+
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
38
|
+
(isFunction(config_1 === null || config_1 === void 0 ? void 0 : config_1.when) && (config_1 === null || config_1 === void 0 ? void 0 : config_1.when()) === false);
|
|
39
|
+
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
37
40
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
38
|
-
return useSWR(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
41
|
+
return useSWR(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
39
42
|
}
|
|
40
43
|
var config = _config;
|
|
41
44
|
var sender = function (
|
package/node/createSwrApi.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSwrApi = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
+
var utils_1 = require("@koine/utils");
|
|
5
6
|
var swr_1 = require("swr");
|
|
6
7
|
var mutation_1 = require("swr/mutation");
|
|
7
8
|
var createApi_1 = require("./createApi");
|
|
@@ -36,9 +37,11 @@ function createUseApi(api, method) {
|
|
|
36
37
|
});
|
|
37
38
|
}); };
|
|
38
39
|
var config_1 = _config;
|
|
39
|
-
|
|
40
|
+
var shouldNotFetch = (config_1 === null || config_1 === void 0 ? void 0 : config_1.when) === false ||
|
|
41
|
+
((0, utils_1.isFunction)(config_1 === null || config_1 === void 0 ? void 0 : config_1.when) && (config_1 === null || config_1 === void 0 ? void 0 : config_1.when()) === false);
|
|
42
|
+
// <Data = any, Error = any>(key: Key, config: SWRConfigurationExtended<Data, Error, Fetcher<Data>> | undefined): SWRResponse<Data, Error>;
|
|
40
43
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
41
|
-
return (0, swr_1.default)(options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
44
|
+
return (0, swr_1.default)(shouldNotFetch ? null : options ? [endpoint, options] : [endpoint], fetcher, config_1);
|
|
42
45
|
}
|
|
43
46
|
var config = _config;
|
|
44
47
|
var sender = function (
|
package/package.json
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
"typings": "./index.d.ts",
|
|
6
6
|
"dependencies": {},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@koine/utils": "1.0.
|
|
8
|
+
"@koine/utils": "1.0.60",
|
|
9
9
|
"swr": "^2.0.0-beta.6",
|
|
10
10
|
"next": "^12.2.0",
|
|
11
11
|
"tslib": "^2.4.0"
|
|
12
12
|
},
|
|
13
|
-
"version": "1.0.
|
|
13
|
+
"version": "1.0.60",
|
|
14
14
|
"module": "./index.js",
|
|
15
15
|
"types": "./index.d.ts"
|
|
16
16
|
}
|