quetch 0.2.0 → 0.3.0
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/dist/errors/RequestError.d.ts +3 -3
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/middlewares/aggregate.d.ts +3 -3
- package/dist/middlewares/aggregate.js +1 -1
- package/dist/middlewares/aggregate.js.map +1 -1
- package/dist/middlewares/cache.d.ts +4 -4
- package/dist/middlewares/cache.js +3 -3
- package/dist/middlewares/cache.js.map +1 -1
- package/dist/middlewares.d.ts +9 -9
- package/dist/middlewares.js +9 -9
- package/dist/middlewares.js.map +1 -1
- package/dist/tools/cork.d.ts +5 -0
- package/dist/tools/cork.js +5 -0
- package/dist/tools/cork.js.map +1 -0
- package/dist/tools/defineCheckQuery.d.ts +12 -7
- package/dist/tools/defineCheckQuery.js +5 -0
- package/dist/tools/defineCheckQuery.js.map +1 -1
- package/dist/tools/defineCustomFetch.d.ts +26 -18
- package/dist/tools/defineCustomFetch.js +9 -3
- package/dist/tools/defineCustomFetch.js.map +1 -1
- package/dist/tools/defineGenericFetch.d.ts +14 -0
- package/dist/tools/defineGenericFetch.js +12 -0
- package/dist/tools/defineGenericFetch.js.map +1 -0
- package/dist/tools/queryItemList.js +3 -3
- package/dist/tools/queryItemList.js.map +1 -1
- package/dist/tools/sortItemList.js +3 -3
- package/dist/tools/sortItemList.js.map +1 -1
- package/dist/tools.d.ts +9 -8
- package/dist/tools.js +9 -8
- package/dist/tools.js.map +1 -1
- package/dist/types/AggregateFunction.d.ts +9 -0
- package/dist/types/AggregateFunction.js +2 -0
- package/dist/types/AggregateFunction.js.map +1 -0
- package/dist/types/AggregateFunctionOperator.d.ts +2 -0
- package/dist/types/AggregateFunctionOperator.js +2 -0
- package/dist/types/AggregateFunctionOperator.js.map +1 -0
- package/dist/types/Any.d.ts +1 -0
- package/dist/types/Any.js +2 -0
- package/dist/types/Any.js.map +1 -0
- package/dist/types/Context.d.ts +3 -0
- package/dist/types/Context.js +2 -0
- package/dist/types/Context.js.map +1 -0
- package/dist/types/CustomFieldAggregateMap.d.ts +2 -0
- package/dist/types/CustomFieldAggregateMap.js +2 -0
- package/dist/types/CustomFieldAggregateMap.js.map +1 -0
- package/dist/types/CustomFieldMap.d.ts +2 -0
- package/dist/types/CustomFieldMap.js +2 -0
- package/dist/types/CustomFieldMap.js.map +1 -0
- package/dist/types/CustomRequest.d.ts +16 -0
- package/dist/types/CustomRequest.js +2 -0
- package/dist/types/CustomRequest.js.map +1 -0
- package/dist/types/FieldFunction.d.ts +3 -0
- package/dist/types/FieldFunction.js +2 -0
- package/dist/types/FieldFunction.js.map +1 -0
- package/dist/types/FieldFunctionCustom.d.ts +7 -0
- package/dist/types/FieldFunctionCustom.js +2 -0
- package/dist/types/FieldFunctionCustom.js.map +1 -0
- package/dist/types/FieldFunctionFormatDate.d.ts +9 -0
- package/dist/types/FieldFunctionFormatDate.js +2 -0
- package/dist/types/FieldFunctionFormatDate.js.map +1 -0
- package/dist/types/FieldFunctionReturn.d.ts +6 -0
- package/dist/types/FieldFunctionReturn.js +2 -0
- package/dist/types/FieldFunctionReturn.js.map +1 -0
- package/dist/types/Filter.d.ts +12 -0
- package/dist/types/Filter.js +2 -0
- package/dist/types/Filter.js.map +1 -0
- package/dist/types/FilterArray.d.ts +10 -0
- package/dist/types/FilterArray.js +2 -0
- package/dist/types/FilterArray.js.map +1 -0
- package/dist/types/FilterBoolean.d.ts +9 -0
- package/dist/types/FilterBoolean.js +2 -0
- package/dist/types/FilterBoolean.js.map +1 -0
- package/dist/types/FilterField.d.ts +7 -0
- package/dist/types/FilterField.js +2 -0
- package/dist/types/FilterField.js.map +1 -0
- package/dist/types/FilterKeys.d.ts +6 -0
- package/dist/types/FilterKeys.js +2 -0
- package/dist/types/FilterKeys.js.map +1 -0
- package/dist/types/FilterNumber.d.ts +9 -0
- package/dist/types/FilterNumber.js +2 -0
- package/dist/types/FilterNumber.js.map +1 -0
- package/dist/types/FilterOperator.d.ts +2 -0
- package/dist/types/FilterOperator.js +2 -0
- package/dist/types/FilterOperator.js.map +1 -0
- package/dist/types/FilterSequence.d.ts +23 -0
- package/dist/types/FilterSequence.js +2 -0
- package/dist/types/FilterSequence.js.map +1 -0
- package/dist/types/FilterString.d.ts +9 -0
- package/dist/types/FilterString.js +2 -0
- package/dist/types/FilterString.js.map +1 -0
- package/dist/types/FilterStringIntersect.d.ts +9 -0
- package/dist/types/FilterStringIntersect.js +2 -0
- package/dist/types/FilterStringIntersect.js.map +1 -0
- package/dist/types/FilterStringMatch.d.ts +29 -0
- package/dist/types/FilterStringMatch.js +2 -0
- package/dist/types/FilterStringMatch.js.map +1 -0
- package/dist/types/Get.d.ts +6 -0
- package/dist/types/Get.js +2 -0
- package/dist/types/Get.js.map +1 -0
- package/dist/types/Group.d.ts +8 -0
- package/dist/types/Group.js +2 -0
- package/dist/types/Group.js.map +1 -0
- package/dist/types/Handler.d.ts +5 -0
- package/dist/types/Handler.js +2 -0
- package/dist/types/Handler.js.map +1 -0
- package/dist/types/Immutable.d.ts +3 -0
- package/dist/types/Immutable.js +2 -0
- package/dist/types/Immutable.js.map +1 -0
- package/dist/types/InjectCustomFields.d.ts +9 -0
- package/dist/types/InjectCustomFields.js +2 -0
- package/dist/types/InjectCustomFields.js.map +1 -0
- package/dist/types/Item.d.ts +4 -0
- package/dist/types/Item.js +2 -0
- package/dist/types/Item.js.map +1 -0
- package/dist/types/Key.d.ts +1 -0
- package/dist/types/Key.js +2 -0
- package/dist/types/Key.js.map +1 -0
- package/dist/types/Mutable.d.ts +3 -0
- package/dist/types/Mutable.js +2 -0
- package/dist/types/Mutable.js.map +1 -0
- package/dist/types/NextHandler.d.ts +4 -0
- package/dist/types/NextHandler.js +2 -0
- package/dist/types/NextHandler.js.map +1 -0
- package/dist/types/Order.d.ts +7 -0
- package/dist/types/Order.js +2 -0
- package/dist/types/Order.js.map +1 -0
- package/dist/types/Query.d.ts +23 -0
- package/dist/types/Query.js +2 -0
- package/dist/types/Query.js.map +1 -0
- package/dist/types/QueryAggregate.d.ts +10 -0
- package/dist/types/QueryAggregate.js +2 -0
- package/dist/types/QueryAggregate.js.map +1 -0
- package/dist/types/QueryAny.d.ts +7 -0
- package/dist/types/QueryAny.js +2 -0
- package/dist/types/QueryAny.js.map +1 -0
- package/dist/types/QueryCreate.d.ts +7 -0
- package/dist/types/QueryCreate.js +2 -0
- package/dist/types/QueryCreate.js.map +1 -0
- package/dist/types/QueryCreateMultiple.d.ts +8 -0
- package/dist/types/QueryCreateMultiple.js +2 -0
- package/dist/types/QueryCreateMultiple.js.map +1 -0
- package/dist/types/QueryDelete.d.ts +10 -0
- package/dist/types/QueryDelete.js +2 -0
- package/dist/types/QueryDelete.js.map +1 -0
- package/dist/types/QueryDeleteMultiple.d.ts +11 -0
- package/dist/types/QueryDeleteMultiple.js +2 -0
- package/dist/types/QueryDeleteMultiple.js.map +1 -0
- package/dist/types/QueryMethod.d.ts +5 -0
- package/dist/types/QueryMethod.js +2 -0
- package/dist/types/QueryMethod.js.map +1 -0
- package/dist/types/QueryRead.d.ts +23 -0
- package/dist/types/QueryRead.js +2 -0
- package/dist/types/QueryRead.js.map +1 -0
- package/dist/types/QueryReadMultiple.d.ts +34 -0
- package/dist/types/QueryReadMultiple.js +2 -0
- package/dist/types/QueryReadMultiple.js.map +1 -0
- package/dist/types/QueryUpdate.d.ts +18 -0
- package/dist/types/QueryUpdate.js +2 -0
- package/dist/types/QueryUpdate.js.map +1 -0
- package/dist/types/QueryUpdateMultiple.d.ts +26 -0
- package/dist/types/QueryUpdateMultiple.js +2 -0
- package/dist/types/QueryUpdateMultiple.js.map +1 -0
- package/dist/types/Result.d.ts +25 -0
- package/dist/types/Result.js +2 -0
- package/dist/types/Result.js.map +1 -0
- package/dist/types/Store.d.ts +6 -0
- package/dist/types/Store.js +2 -0
- package/dist/types/Store.js.map +1 -0
- package/dist/types.d.ts +46 -396
- package/doc/README.md +543 -211
- package/lib/errors/RequestError.ts +2 -2
- package/lib/errors.ts +1 -1
- package/lib/middlewares/aggregate.ts +5 -5
- package/lib/middlewares/cache.ts +7 -12
- package/lib/middlewares.ts +9 -9
- package/lib/tools/cork.ts +6 -0
- package/lib/tools/defineCheckQuery.ts +20 -15
- package/lib/tools/defineCustomFetch.ts +40 -62
- package/lib/tools/defineGenericFetch.ts +33 -0
- package/lib/tools/queryItemList.test.ts +6 -6
- package/lib/tools/queryItemList.ts +3 -3
- package/lib/tools/sortItemList.ts +3 -3
- package/lib/tools.ts +9 -8
- package/lib/types/AggregateFunction.ts +17 -0
- package/lib/types/AggregateFunctionOperator.ts +6 -0
- package/lib/types/Any.ts +10 -0
- package/lib/types/Context.ts +3 -0
- package/lib/types/CustomFieldAggregateMap.ts +6 -0
- package/lib/types/CustomFieldMap.ts +3 -0
- package/lib/types/CustomRequest.ts +21 -0
- package/lib/types/FieldFunction.ts +6 -0
- package/lib/types/FieldFunctionCustom.ts +7 -0
- package/lib/types/FieldFunctionFormatDate.ts +10 -0
- package/lib/types/FieldFunctionReturn.ts +6 -0
- package/lib/types/Filter.ts +20 -0
- package/lib/types/FilterArray.ts +11 -0
- package/lib/types/FilterBoolean.ts +10 -0
- package/lib/types/FilterField.ts +7 -0
- package/lib/types/FilterKeys.ts +6 -0
- package/lib/types/FilterNumber.ts +16 -0
- package/lib/types/FilterOperator.ts +3 -0
- package/lib/types/FilterSequence.ts +26 -0
- package/lib/types/FilterString.ts +19 -0
- package/lib/types/FilterStringIntersect.ts +10 -0
- package/lib/types/FilterStringMatch.ts +30 -0
- package/lib/types/Get.ts +8 -0
- package/lib/types/Group.ts +11 -0
- package/lib/types/Handler.ts +9 -0
- package/lib/types/Immutable.ts +3 -0
- package/lib/types/InjectCustomFields.ts +23 -0
- package/lib/types/Item.ts +8 -0
- package/lib/types/Key.ts +1 -0
- package/lib/types/Mutable.ts +3 -0
- package/lib/types/NextHandler.ts +4 -0
- package/lib/types/Order.ts +9 -0
- package/lib/types/Query.ts +36 -0
- package/lib/types/QueryAggregate.ts +11 -0
- package/lib/types/QueryAny.ts +8 -0
- package/lib/types/QueryCreate.ts +7 -0
- package/lib/types/QueryCreateMultiple.ts +8 -0
- package/lib/types/QueryDelete.ts +11 -0
- package/lib/types/QueryDeleteMultiple.ts +12 -0
- package/lib/types/QueryMethod.ts +6 -0
- package/lib/types/QueryRead.ts +24 -0
- package/lib/types/QueryReadMultiple.ts +36 -0
- package/lib/types/QueryUpdate.ts +19 -0
- package/lib/types/QueryUpdateMultiple.ts +27 -0
- package/lib/types/Result.ts +38 -0
- package/lib/types/Store.ts +7 -0
- package/lib/types.ts +47 -554
- package/package.json +3 -3
- package/dist/errors/QueryError.d.ts +0 -5
- package/dist/errors/QueryError.js +0 -23
- package/dist/errors/QueryError.js.map +0 -1
- package/dist/middlewares/concurrent.d.ts +0 -2
- package/dist/middlewares/concurrent.js +0 -20
- package/dist/middlewares/concurrent.js.map +0 -1
- package/dist/middlewares/fetch.d.ts +0 -2
- package/dist/middlewares/fetch.js +0 -20
- package/dist/middlewares/fetch.js.map +0 -1
- package/dist/middlewares/logQuery.d.ts +0 -2
- package/dist/middlewares/logQuery.js +0 -25
- package/dist/middlewares/logQuery.js.map +0 -1
- package/dist/tools/add.d.ts +0 -8
- package/dist/tools/add.js +0 -11
- package/dist/tools/add.js.map +0 -1
- package/dist/tools/add.test.d.ts +0 -1
- package/dist/tools/add.test.js +0 -6
- package/dist/tools/add.test.js.map +0 -1
- package/dist/tools/filterItem.test.d.ts +0 -1
- package/dist/tools/filterItem.test.js +0 -86
- package/dist/tools/filterItem.test.js.map +0 -1
- package/dist/tools/impasse.d.ts +0 -2
- package/dist/tools/impasse.js +0 -2
- package/dist/tools/impasse.js.map +0 -1
- package/dist/tools/queryItemList.test.d.ts +0 -1
- package/dist/tools/queryItemList.test.js +0 -181
- package/dist/tools/queryItemList.test.js.map +0 -1
- package/dist/tools/sortItemList.test.d.ts +0 -1
- package/dist/tools/sortItemList.test.js +0 -47
- package/dist/tools/sortItemList.test.js.map +0 -1
- package/lib/tools/impasse.ts +0 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { QueryAny } from "../types";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Error to be thrown in case there is an issue with the query call. Only instances of this error will be caught by the `retry()` middleware.
|
|
@@ -7,7 +7,7 @@ export class RequestError extends Error {
|
|
|
7
7
|
constructor(
|
|
8
8
|
message: string,
|
|
9
9
|
public status: number,
|
|
10
|
-
public query?:
|
|
10
|
+
public query?: QueryAny,
|
|
11
11
|
public request?: Request,
|
|
12
12
|
public response?: Response,
|
|
13
13
|
) {
|
package/lib/errors.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// File automatically generated by `vite-plugin-module-list`
|
|
2
|
-
export { RequestError } from "./errors/RequestError";
|
|
2
|
+
export { RequestError } from "./errors/RequestError.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { sleep } from "futurise";
|
|
2
2
|
|
|
3
3
|
import { RequestError } from "../errors";
|
|
4
|
-
import type {
|
|
4
|
+
import type { Handler, QueryAny } from "../types";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Aggregates multiple incoming query calls into one query.
|
|
@@ -13,7 +13,7 @@ import type { AnyQuery, Handler } from "../types";
|
|
|
13
13
|
* @param options
|
|
14
14
|
* @returns
|
|
15
15
|
*/
|
|
16
|
-
export function aggregate<I extends
|
|
16
|
+
export function aggregate<I extends QueryAny, O, In extends QueryAny, On>({
|
|
17
17
|
queryGroupId = ({ type, method = "get" }) => {
|
|
18
18
|
if (method !== "get") {
|
|
19
19
|
return undefined;
|
|
@@ -31,9 +31,9 @@ export function aggregate<I extends AnyQuery, O, In extends AnyQuery, On>({
|
|
|
31
31
|
},
|
|
32
32
|
mergeQuery = (query, _currentQuery) => query,
|
|
33
33
|
delay = 200,
|
|
34
|
-
queryForGroup = (queryList, _):
|
|
34
|
+
queryForGroup = (queryList, _): QueryAny => ({
|
|
35
35
|
type: queryList[0].type,
|
|
36
|
-
method: "
|
|
36
|
+
method: "read",
|
|
37
37
|
multiple: true,
|
|
38
38
|
filter: {
|
|
39
39
|
operator: "include",
|
|
@@ -55,7 +55,7 @@ export function aggregate<I extends AnyQuery, O, In extends AnyQuery, On>({
|
|
|
55
55
|
queryId?: (query: I) => string | undefined;
|
|
56
56
|
mergeQuery?: (query: I, currentQuery: I) => I;
|
|
57
57
|
delay?: number;
|
|
58
|
-
queryForGroup: (queryList: I[], group: string) =>
|
|
58
|
+
queryForGroup: (queryList: I[], group: string) => QueryAny;
|
|
59
59
|
resultForQuery: (resultList: O[], query: I) => O | never;
|
|
60
60
|
}): Handler<I, O, In, On> {
|
|
61
61
|
const queryGroupMap = new Map<
|
package/lib/middlewares/cache.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Handler, QueryAny, Store } from "../types";
|
|
2
2
|
|
|
3
|
-
type CachedItem<I extends
|
|
3
|
+
type CachedItem<I extends QueryAny> = {
|
|
4
4
|
query: I;
|
|
5
5
|
value: any;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
type CacheOptions<I extends
|
|
8
|
+
type CacheOptions<I extends QueryAny> = {
|
|
9
9
|
/**
|
|
10
10
|
* Unique identifier for the item to cache.
|
|
11
11
|
* Returns `undefined` if the item should not be cached.
|
|
@@ -36,15 +36,10 @@ type CacheOptions<I extends AnyQueryExternal> = {
|
|
|
36
36
|
mergeItem: (value: any, cachedValue: any, query: I, cachedQuery: I) => any;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
export function cache<
|
|
40
|
-
I
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
On,
|
|
44
|
-
>({
|
|
45
|
-
itemId = ({ context = {}, method = "get", type }: I) => {
|
|
46
|
-
if (method === "get" && context.id) {
|
|
47
|
-
return `${type}/${context.id}`;
|
|
39
|
+
export function cache<I extends QueryAny, O, In extends QueryAny, On>({
|
|
40
|
+
itemId = ({ context = {}, method = "read", type }: I) => {
|
|
41
|
+
if (method === "read" && context.id) {
|
|
42
|
+
return `${String(type)}/${context.id}`;
|
|
48
43
|
}
|
|
49
44
|
return undefined;
|
|
50
45
|
},
|
package/lib/middlewares.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// File automatically generated by `vite-plugin-module-list`
|
|
2
|
-
export { aggregate } from "./middlewares/aggregate";
|
|
3
|
-
export { branch } from "./middlewares/branch";
|
|
4
|
-
export { cache } from "./middlewares/cache";
|
|
5
|
-
export { combine } from "./middlewares/combine";
|
|
6
|
-
export { fetchExternal } from "./middlewares/fetchExternal";
|
|
7
|
-
export { fetchLocal } from "./middlewares/fetchLocal";
|
|
8
|
-
export { identity } from "./middlewares/identity";
|
|
9
|
-
export { log } from "./middlewares/log";
|
|
10
|
-
export { retry } from "./middlewares/retry";
|
|
2
|
+
export { aggregate } from "./middlewares/aggregate.js";
|
|
3
|
+
export { branch } from "./middlewares/branch.js";
|
|
4
|
+
export { cache } from "./middlewares/cache.js";
|
|
5
|
+
export { combine } from "./middlewares/combine.js";
|
|
6
|
+
export { fetchExternal } from "./middlewares/fetchExternal.js";
|
|
7
|
+
export { fetchLocal } from "./middlewares/fetchLocal.js";
|
|
8
|
+
export { identity } from "./middlewares/identity.js";
|
|
9
|
+
export { log } from "./middlewares/log.js";
|
|
10
|
+
export { retry } from "./middlewares/retry.js";
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import type { CustomFieldMap, Query } from "../types";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Returns a function that checks queries. This is useful to prevent the query to have its type being narrowed if declared outside of a custom fetcher function argument.
|
|
5
|
+
*
|
|
6
|
+
* @returns Function that checks queries.
|
|
7
|
+
*/
|
|
8
|
+
export function defineCheckQuery<M extends Record<string, object>>() {
|
|
4
9
|
function checkQuery<
|
|
5
|
-
|
|
6
|
-
C extends CustomFieldMap<
|
|
7
|
-
const Q extends Query<
|
|
8
|
-
>(query: Q & { type:
|
|
10
|
+
T extends object,
|
|
11
|
+
C extends CustomFieldMap<T>,
|
|
12
|
+
const Q extends Query<T, C>,
|
|
13
|
+
>(query: Q & { type: T[]; customFields?: C }): typeof query;
|
|
9
14
|
function checkQuery<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
C extends CustomFieldMap<
|
|
13
|
-
const Q extends Query<
|
|
14
|
-
>(query: Q & { type:
|
|
15
|
+
K extends keyof M,
|
|
16
|
+
T extends M[K],
|
|
17
|
+
C extends CustomFieldMap<T>,
|
|
18
|
+
const Q extends Query<T, C>,
|
|
19
|
+
>(query: Q & { type: K; customFields?: C }): typeof query;
|
|
15
20
|
function checkQuery<
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
C extends CustomFieldMap<
|
|
19
|
-
const Q extends Query<
|
|
20
|
-
>(query: Q & { type:
|
|
21
|
+
K extends keyof M,
|
|
22
|
+
T extends M[K] | object,
|
|
23
|
+
C extends CustomFieldMap<T>,
|
|
24
|
+
const Q extends Query<T, C>,
|
|
25
|
+
>(query: Q & { type: K | T[]; customFields?: C }): typeof query {
|
|
21
26
|
return query;
|
|
22
27
|
}
|
|
23
28
|
return checkQuery;
|
|
@@ -1,70 +1,48 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyQuery,
|
|
3
|
-
CustomFieldMap,
|
|
4
|
-
EntityItem,
|
|
5
|
-
Handler,
|
|
6
|
-
Query,
|
|
7
|
-
} from "../types";
|
|
1
|
+
import type { CustomFieldMap, Handler, Key, Query, Result } from "../types";
|
|
8
2
|
|
|
9
|
-
import {
|
|
3
|
+
import { cork } from "./cork";
|
|
10
4
|
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @param handler
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
11
|
export function defineCustomFetch<M extends Record<string, object>>(
|
|
12
|
-
handler: Handler<
|
|
12
|
+
handler: Handler<
|
|
13
|
+
Query<any, any> & { type: Key | any[]; customFields?: CustomFieldMap<any> },
|
|
14
|
+
any,
|
|
15
|
+
never,
|
|
16
|
+
never
|
|
17
|
+
>,
|
|
13
18
|
) {
|
|
14
|
-
function customFetch<
|
|
15
|
-
E extends object,
|
|
16
|
-
C extends CustomFieldMap<E>,
|
|
17
|
-
const Q extends Query<E, C>,
|
|
18
|
-
>(
|
|
19
|
-
input: Q & { type: E[]; customFields?: C },
|
|
20
|
-
): Promise<
|
|
21
|
-
Q["method"] extends "get"
|
|
22
|
-
? Q extends { multiple: true }
|
|
23
|
-
? EntityItem<E, C, Q>[]
|
|
24
|
-
: EntityItem<E, C, Q>
|
|
25
|
-
: Q["method"] extends "aggregate"
|
|
26
|
-
? number
|
|
27
|
-
: Q extends { multiple: true }
|
|
28
|
-
? EntityItem<E, C, Q>[]
|
|
29
|
-
: EntityItem<E, C, Q>
|
|
30
|
-
>;
|
|
31
|
-
function customFetch<
|
|
32
|
-
T extends keyof M,
|
|
33
|
-
E extends M[T],
|
|
34
|
-
C extends CustomFieldMap<E>,
|
|
35
|
-
const Q extends Query<E, C>,
|
|
36
|
-
>(
|
|
37
|
-
input: Q & { type: T; customFields?: C },
|
|
38
|
-
): Promise<
|
|
39
|
-
Q["method"] extends "get"
|
|
40
|
-
? Q extends { multiple: true }
|
|
41
|
-
? EntityItem<E, C, Q>[]
|
|
42
|
-
: EntityItem<E, C, Q>
|
|
43
|
-
: Q["method"] extends "aggregate"
|
|
44
|
-
? number
|
|
45
|
-
: Q extends { multiple: true }
|
|
46
|
-
? EntityItem<E, C, Q>[]
|
|
47
|
-
: EntityItem<E, C, Q>
|
|
48
|
-
>;
|
|
49
19
|
async function customFetch<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
Q
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
20
|
+
K extends keyof M,
|
|
21
|
+
T extends M[K],
|
|
22
|
+
const Q extends Query<T, {}>,
|
|
23
|
+
>(query: Q & { type: K; customFields?: never }): Promise<Result<T, Q, {}>>;
|
|
24
|
+
async function customFetch<
|
|
25
|
+
K extends keyof M,
|
|
26
|
+
T extends M[K],
|
|
27
|
+
const Q extends Query<T, C>,
|
|
28
|
+
const C extends CustomFieldMap<T>,
|
|
29
|
+
>(query: Q & { type: K; customFields: C }): Promise<Result<T, Q, C>>;
|
|
30
|
+
async function customFetch<T extends object, const Q extends Query<T, {}>>(
|
|
31
|
+
type: T[],
|
|
32
|
+
query: Q & { customFields?: never },
|
|
33
|
+
): Promise<Result<T, Q, {}>>;
|
|
34
|
+
async function customFetch<
|
|
35
|
+
T extends object,
|
|
36
|
+
const Q extends Query<T, C>,
|
|
37
|
+
const C extends CustomFieldMap<T>,
|
|
38
|
+
>(query: Q & { type: T[]; customFields: C }): Promise<Result<T, Q, C>>;
|
|
39
|
+
async function customFetch<
|
|
40
|
+
K extends keyof M,
|
|
41
|
+
T extends M[K] | object,
|
|
42
|
+
const Q extends Query<T, C>,
|
|
43
|
+
const C extends CustomFieldMap<T>,
|
|
44
|
+
>(query: Q & { type: T[] | K; customFields?: C }): Promise<Result<T, Q, C>> {
|
|
45
|
+
return await handler(query, cork);
|
|
68
46
|
}
|
|
69
47
|
return customFetch;
|
|
70
48
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { CustomFieldMap, Handler, Key, Query, Result } from "../types";
|
|
2
|
+
|
|
3
|
+
import { cork } from "./cork";
|
|
4
|
+
|
|
5
|
+
export function defineGenericFetch<K extends string>(
|
|
6
|
+
handler: Handler<
|
|
7
|
+
Query<any, any> & { type: Key | any[]; customFields?: CustomFieldMap<any> },
|
|
8
|
+
any,
|
|
9
|
+
never,
|
|
10
|
+
never
|
|
11
|
+
>,
|
|
12
|
+
) {
|
|
13
|
+
// FIXME: Until https://github.com/microsoft/TypeScript/issues/26242 gets resolved, the fetcher needs to be curried
|
|
14
|
+
function genericFetch<T extends object>() {
|
|
15
|
+
async function customFetch<const Q extends Query<T, {}>>(
|
|
16
|
+
query: Q & { type: K | T[]; customFields?: never },
|
|
17
|
+
): Promise<Result<T, Q, {}>>;
|
|
18
|
+
async function customFetch<
|
|
19
|
+
const Q extends Query<T, C>,
|
|
20
|
+
const C extends CustomFieldMap<T>,
|
|
21
|
+
>(query: Q & { type: K | T[]; customFields: C }): Promise<Result<T, Q, C>>;
|
|
22
|
+
async function customFetch<
|
|
23
|
+
const Q extends Query<T, C>,
|
|
24
|
+
const C extends CustomFieldMap<T>,
|
|
25
|
+
>(
|
|
26
|
+
query: Q & { type: K | T[]; customFields?: C },
|
|
27
|
+
): Promise<Result<T, Q, C>> {
|
|
28
|
+
return await handler(query, cork);
|
|
29
|
+
}
|
|
30
|
+
return customFetch;
|
|
31
|
+
}
|
|
32
|
+
return genericFetch;
|
|
33
|
+
}
|
|
@@ -63,7 +63,7 @@ test("queries lists of items", () => {
|
|
|
63
63
|
{ a: 2, c: "a" },
|
|
64
64
|
{ a: 3, c: "b" },
|
|
65
65
|
],
|
|
66
|
-
method: "
|
|
66
|
+
method: "read",
|
|
67
67
|
multiple: true,
|
|
68
68
|
context: {
|
|
69
69
|
c: "a",
|
|
@@ -99,7 +99,7 @@ test("sorts items", () => {
|
|
|
99
99
|
{ a: 1, c: "b" },
|
|
100
100
|
],
|
|
101
101
|
multiple: true,
|
|
102
|
-
|
|
102
|
+
order: ["a", "c"],
|
|
103
103
|
}),
|
|
104
104
|
).toEqual([
|
|
105
105
|
{ a: 1, c: "a" },
|
|
@@ -118,7 +118,7 @@ test("sorts items", () => {
|
|
|
118
118
|
{ a: 1, c: "b" },
|
|
119
119
|
],
|
|
120
120
|
multiple: true,
|
|
121
|
-
|
|
121
|
+
order: [
|
|
122
122
|
{ field: "a", descending: true },
|
|
123
123
|
{ field: "c", descending: true },
|
|
124
124
|
],
|
|
@@ -176,7 +176,7 @@ test("slices items", () => {
|
|
|
176
176
|
{ a: 2, c: "a" },
|
|
177
177
|
{ a: 3, c: "b" },
|
|
178
178
|
],
|
|
179
|
-
method: "
|
|
179
|
+
method: "read",
|
|
180
180
|
multiple: true,
|
|
181
181
|
limit: 1,
|
|
182
182
|
}),
|
|
@@ -188,7 +188,7 @@ test("slices items", () => {
|
|
|
188
188
|
{ a: 2, c: "a" },
|
|
189
189
|
{ a: 3, c: "b" },
|
|
190
190
|
],
|
|
191
|
-
method: "
|
|
191
|
+
method: "read",
|
|
192
192
|
multiple: true,
|
|
193
193
|
context: {
|
|
194
194
|
c: "a",
|
|
@@ -204,7 +204,7 @@ test("slices items", () => {
|
|
|
204
204
|
{ a: 3, c: "b" },
|
|
205
205
|
{ a: 2, c: "b" },
|
|
206
206
|
],
|
|
207
|
-
method: "
|
|
207
|
+
method: "read",
|
|
208
208
|
multiple: true,
|
|
209
209
|
offset: 1,
|
|
210
210
|
limit: 2,
|
|
@@ -43,11 +43,11 @@ export function queryItemList<T extends object, C extends CustomFieldMap<T>>(
|
|
|
43
43
|
) {
|
|
44
44
|
const data = query.type as T[];
|
|
45
45
|
switch (query.method) {
|
|
46
|
-
case "
|
|
46
|
+
case "read":
|
|
47
47
|
case undefined: {
|
|
48
48
|
const { filter, context } = query;
|
|
49
49
|
if (query.multiple) {
|
|
50
|
-
const { offset = 0, limit = Infinity,
|
|
50
|
+
const { offset = 0, limit = Infinity, order } = query;
|
|
51
51
|
// Filter
|
|
52
52
|
let result = data;
|
|
53
53
|
if (context !== undefined || filter !== undefined) {
|
|
@@ -59,7 +59,7 @@ export function queryItemList<T extends object, C extends CustomFieldMap<T>>(
|
|
|
59
59
|
}
|
|
60
60
|
// Sort
|
|
61
61
|
result = sortItemList(
|
|
62
|
-
|
|
62
|
+
order as Order<T>[],
|
|
63
63
|
result === data ? [...data] : result,
|
|
64
64
|
);
|
|
65
65
|
// Slice
|
|
@@ -16,10 +16,10 @@ export function sortItemList<T extends object>(
|
|
|
16
16
|
if (orderList === undefined || orderList.length === 0) {
|
|
17
17
|
return value;
|
|
18
18
|
}
|
|
19
|
-
const
|
|
19
|
+
const normalizedorder = orderList.map(normalizeOrder);
|
|
20
20
|
return value.sort((a, b) => {
|
|
21
|
-
for (let index = 0; index <
|
|
22
|
-
const { field, descending } =
|
|
21
|
+
for (let index = 0; index < normalizedorder.length; index++) {
|
|
22
|
+
const { field, descending } = normalizedorder[index];
|
|
23
23
|
if (a[field] === b[field]) {
|
|
24
24
|
continue;
|
|
25
25
|
}
|
package/lib/tools.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// File automatically generated by `vite-plugin-module-list`
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
2
|
+
export { cork } from "./tools/cork.js";
|
|
3
|
+
export { defineCheckQuery } from "./tools/defineCheckQuery.js";
|
|
4
|
+
export { defineCustomFetch } from "./tools/defineCustomFetch.js";
|
|
5
|
+
export { defineGenericFetch } from "./tools/defineGenericFetch.js";
|
|
6
|
+
export { filterFromContext } from "./tools/filterFromContext.js";
|
|
7
|
+
export { filterItem } from "./tools/filterItem.js";
|
|
8
|
+
export { normalizeOrder } from "./tools/normalizeOrder.js";
|
|
9
|
+
export { queryItemList } from "./tools/queryItemList.js";
|
|
10
|
+
export { sortItemList } from "./tools/sortItemList.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aggregation function.
|
|
3
|
+
*/
|
|
4
|
+
export type AggregateFunction<T extends object> =
|
|
5
|
+
| "length"
|
|
6
|
+
| { operator: "length" }
|
|
7
|
+
| {
|
|
8
|
+
operator:
|
|
9
|
+
| "median"
|
|
10
|
+
| "standardDeviation"
|
|
11
|
+
| "mean"
|
|
12
|
+
| "minimum"
|
|
13
|
+
| "maximum"
|
|
14
|
+
| "variance"
|
|
15
|
+
| "mode";
|
|
16
|
+
field: keyof T;
|
|
17
|
+
};
|
package/lib/types/Any.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CustomFieldMap } from "./CustomFieldMap";
|
|
2
|
+
import type { Query } from "./Query";
|
|
3
|
+
|
|
4
|
+
export type CustomRequest<
|
|
5
|
+
T extends object,
|
|
6
|
+
Q extends Query<T, C>,
|
|
7
|
+
C extends CustomFieldMap<T>,
|
|
8
|
+
> = {
|
|
9
|
+
/**
|
|
10
|
+
* Entity being queried.
|
|
11
|
+
*/
|
|
12
|
+
data: T;
|
|
13
|
+
/**
|
|
14
|
+
* Query.
|
|
15
|
+
*/
|
|
16
|
+
query: Q;
|
|
17
|
+
/**
|
|
18
|
+
* Abort signal to abort the request.
|
|
19
|
+
*/
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FilterKeys } from "./FilterKeys";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Formats the date found in a given field, which can be an ISO string date or a timestamp.
|
|
5
|
+
*/
|
|
6
|
+
export type FieldFunctionFormatDate<T extends object> = {
|
|
7
|
+
operator: "formatDate";
|
|
8
|
+
field: FilterKeys<T, string | number>;
|
|
9
|
+
format: string;
|
|
10
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FilterArray } from "./FilterArray";
|
|
2
|
+
import type { FilterBoolean } from "./FilterBoolean";
|
|
3
|
+
import type { FilterField } from "./FilterField";
|
|
4
|
+
import type { FilterNumber } from "./FilterNumber";
|
|
5
|
+
import type { FilterSequence } from "./FilterSequence";
|
|
6
|
+
import type { FilterString } from "./FilterString";
|
|
7
|
+
import type { FilterStringIntersect } from "./FilterStringIntersect";
|
|
8
|
+
import type { FilterStringMatch } from "./FilterStringMatch";
|
|
9
|
+
/**
|
|
10
|
+
* Describes a predicate for filtering items.
|
|
11
|
+
*/
|
|
12
|
+
export type Filter<T extends object> =
|
|
13
|
+
| FilterSequence<T>
|
|
14
|
+
| FilterField<T>
|
|
15
|
+
| FilterBoolean<T>
|
|
16
|
+
| FilterString<T>
|
|
17
|
+
| FilterStringMatch<T>
|
|
18
|
+
| FilterStringIntersect<T>
|
|
19
|
+
| FilterNumber<T>
|
|
20
|
+
| FilterArray<T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Any } from "./Any";
|
|
2
|
+
import type { FilterKeys } from "./FilterKeys";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a given array field matches a given array value according to a given operator.
|
|
6
|
+
*/
|
|
7
|
+
export type FilterArray<T extends object, P = Any> = {
|
|
8
|
+
operator: "equal" | "include" | "intersect";
|
|
9
|
+
field: FilterKeys<T, P[]>;
|
|
10
|
+
value: P[];
|
|
11
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FilterKeys } from "./FilterKeys";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given number field matches a given number value according to a given operator.
|
|
5
|
+
*/
|
|
6
|
+
export type FilterNumber<T extends object> = {
|
|
7
|
+
operator:
|
|
8
|
+
| "equal"
|
|
9
|
+
| "notEqual"
|
|
10
|
+
| "greaterThan"
|
|
11
|
+
| "greaterThanOrEqual"
|
|
12
|
+
| "lowerThan"
|
|
13
|
+
| "lowerThanOrEqual";
|
|
14
|
+
field: FilterKeys<T, number>;
|
|
15
|
+
value: number;
|
|
16
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Filter } from "./Filter";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Joins a list of filters with a specific boolean operator.
|
|
5
|
+
*/
|
|
6
|
+
export type FilterSequence<T extends object> =
|
|
7
|
+
| {
|
|
8
|
+
/**
|
|
9
|
+
* Boolean operator to use for joining the filters.
|
|
10
|
+
*/
|
|
11
|
+
operator: "all";
|
|
12
|
+
/**
|
|
13
|
+
* Filters to join.
|
|
14
|
+
*/
|
|
15
|
+
value: Filter<T>[];
|
|
16
|
+
}
|
|
17
|
+
| {
|
|
18
|
+
/**
|
|
19
|
+
* Boolean operator to use for joining the filters.
|
|
20
|
+
*/
|
|
21
|
+
operator: "any" | "none";
|
|
22
|
+
/**
|
|
23
|
+
* Filters to join.
|
|
24
|
+
*/
|
|
25
|
+
value?: Filter<T>[];
|
|
26
|
+
};
|