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.
Files changed (264) hide show
  1. package/dist/errors/RequestError.d.ts +3 -3
  2. package/dist/errors.d.ts +1 -1
  3. package/dist/errors.js +1 -1
  4. package/dist/errors.js.map +1 -1
  5. package/dist/middlewares/aggregate.d.ts +3 -3
  6. package/dist/middlewares/aggregate.js +1 -1
  7. package/dist/middlewares/aggregate.js.map +1 -1
  8. package/dist/middlewares/cache.d.ts +4 -4
  9. package/dist/middlewares/cache.js +3 -3
  10. package/dist/middlewares/cache.js.map +1 -1
  11. package/dist/middlewares.d.ts +9 -9
  12. package/dist/middlewares.js +9 -9
  13. package/dist/middlewares.js.map +1 -1
  14. package/dist/tools/cork.d.ts +5 -0
  15. package/dist/tools/cork.js +5 -0
  16. package/dist/tools/cork.js.map +1 -0
  17. package/dist/tools/defineCheckQuery.d.ts +12 -7
  18. package/dist/tools/defineCheckQuery.js +5 -0
  19. package/dist/tools/defineCheckQuery.js.map +1 -1
  20. package/dist/tools/defineCustomFetch.d.ts +26 -18
  21. package/dist/tools/defineCustomFetch.js +9 -3
  22. package/dist/tools/defineCustomFetch.js.map +1 -1
  23. package/dist/tools/defineGenericFetch.d.ts +14 -0
  24. package/dist/tools/defineGenericFetch.js +12 -0
  25. package/dist/tools/defineGenericFetch.js.map +1 -0
  26. package/dist/tools/queryItemList.js +3 -3
  27. package/dist/tools/queryItemList.js.map +1 -1
  28. package/dist/tools/sortItemList.js +3 -3
  29. package/dist/tools/sortItemList.js.map +1 -1
  30. package/dist/tools.d.ts +9 -8
  31. package/dist/tools.js +9 -8
  32. package/dist/tools.js.map +1 -1
  33. package/dist/types/AggregateFunction.d.ts +9 -0
  34. package/dist/types/AggregateFunction.js +2 -0
  35. package/dist/types/AggregateFunction.js.map +1 -0
  36. package/dist/types/AggregateFunctionOperator.d.ts +2 -0
  37. package/dist/types/AggregateFunctionOperator.js +2 -0
  38. package/dist/types/AggregateFunctionOperator.js.map +1 -0
  39. package/dist/types/Any.d.ts +1 -0
  40. package/dist/types/Any.js +2 -0
  41. package/dist/types/Any.js.map +1 -0
  42. package/dist/types/Context.d.ts +3 -0
  43. package/dist/types/Context.js +2 -0
  44. package/dist/types/Context.js.map +1 -0
  45. package/dist/types/CustomFieldAggregateMap.d.ts +2 -0
  46. package/dist/types/CustomFieldAggregateMap.js +2 -0
  47. package/dist/types/CustomFieldAggregateMap.js.map +1 -0
  48. package/dist/types/CustomFieldMap.d.ts +2 -0
  49. package/dist/types/CustomFieldMap.js +2 -0
  50. package/dist/types/CustomFieldMap.js.map +1 -0
  51. package/dist/types/CustomRequest.d.ts +16 -0
  52. package/dist/types/CustomRequest.js +2 -0
  53. package/dist/types/CustomRequest.js.map +1 -0
  54. package/dist/types/FieldFunction.d.ts +3 -0
  55. package/dist/types/FieldFunction.js +2 -0
  56. package/dist/types/FieldFunction.js.map +1 -0
  57. package/dist/types/FieldFunctionCustom.d.ts +7 -0
  58. package/dist/types/FieldFunctionCustom.js +2 -0
  59. package/dist/types/FieldFunctionCustom.js.map +1 -0
  60. package/dist/types/FieldFunctionFormatDate.d.ts +9 -0
  61. package/dist/types/FieldFunctionFormatDate.js +2 -0
  62. package/dist/types/FieldFunctionFormatDate.js.map +1 -0
  63. package/dist/types/FieldFunctionReturn.d.ts +6 -0
  64. package/dist/types/FieldFunctionReturn.js +2 -0
  65. package/dist/types/FieldFunctionReturn.js.map +1 -0
  66. package/dist/types/Filter.d.ts +12 -0
  67. package/dist/types/Filter.js +2 -0
  68. package/dist/types/Filter.js.map +1 -0
  69. package/dist/types/FilterArray.d.ts +10 -0
  70. package/dist/types/FilterArray.js +2 -0
  71. package/dist/types/FilterArray.js.map +1 -0
  72. package/dist/types/FilterBoolean.d.ts +9 -0
  73. package/dist/types/FilterBoolean.js +2 -0
  74. package/dist/types/FilterBoolean.js.map +1 -0
  75. package/dist/types/FilterField.d.ts +7 -0
  76. package/dist/types/FilterField.js +2 -0
  77. package/dist/types/FilterField.js.map +1 -0
  78. package/dist/types/FilterKeys.d.ts +6 -0
  79. package/dist/types/FilterKeys.js +2 -0
  80. package/dist/types/FilterKeys.js.map +1 -0
  81. package/dist/types/FilterNumber.d.ts +9 -0
  82. package/dist/types/FilterNumber.js +2 -0
  83. package/dist/types/FilterNumber.js.map +1 -0
  84. package/dist/types/FilterOperator.d.ts +2 -0
  85. package/dist/types/FilterOperator.js +2 -0
  86. package/dist/types/FilterOperator.js.map +1 -0
  87. package/dist/types/FilterSequence.d.ts +23 -0
  88. package/dist/types/FilterSequence.js +2 -0
  89. package/dist/types/FilterSequence.js.map +1 -0
  90. package/dist/types/FilterString.d.ts +9 -0
  91. package/dist/types/FilterString.js +2 -0
  92. package/dist/types/FilterString.js.map +1 -0
  93. package/dist/types/FilterStringIntersect.d.ts +9 -0
  94. package/dist/types/FilterStringIntersect.js +2 -0
  95. package/dist/types/FilterStringIntersect.js.map +1 -0
  96. package/dist/types/FilterStringMatch.d.ts +29 -0
  97. package/dist/types/FilterStringMatch.js +2 -0
  98. package/dist/types/FilterStringMatch.js.map +1 -0
  99. package/dist/types/Get.d.ts +6 -0
  100. package/dist/types/Get.js +2 -0
  101. package/dist/types/Get.js.map +1 -0
  102. package/dist/types/Group.d.ts +8 -0
  103. package/dist/types/Group.js +2 -0
  104. package/dist/types/Group.js.map +1 -0
  105. package/dist/types/Handler.d.ts +5 -0
  106. package/dist/types/Handler.js +2 -0
  107. package/dist/types/Handler.js.map +1 -0
  108. package/dist/types/Immutable.d.ts +3 -0
  109. package/dist/types/Immutable.js +2 -0
  110. package/dist/types/Immutable.js.map +1 -0
  111. package/dist/types/InjectCustomFields.d.ts +9 -0
  112. package/dist/types/InjectCustomFields.js +2 -0
  113. package/dist/types/InjectCustomFields.js.map +1 -0
  114. package/dist/types/Item.d.ts +4 -0
  115. package/dist/types/Item.js +2 -0
  116. package/dist/types/Item.js.map +1 -0
  117. package/dist/types/Key.d.ts +1 -0
  118. package/dist/types/Key.js +2 -0
  119. package/dist/types/Key.js.map +1 -0
  120. package/dist/types/Mutable.d.ts +3 -0
  121. package/dist/types/Mutable.js +2 -0
  122. package/dist/types/Mutable.js.map +1 -0
  123. package/dist/types/NextHandler.d.ts +4 -0
  124. package/dist/types/NextHandler.js +2 -0
  125. package/dist/types/NextHandler.js.map +1 -0
  126. package/dist/types/Order.d.ts +7 -0
  127. package/dist/types/Order.js +2 -0
  128. package/dist/types/Order.js.map +1 -0
  129. package/dist/types/Query.d.ts +23 -0
  130. package/dist/types/Query.js +2 -0
  131. package/dist/types/Query.js.map +1 -0
  132. package/dist/types/QueryAggregate.d.ts +10 -0
  133. package/dist/types/QueryAggregate.js +2 -0
  134. package/dist/types/QueryAggregate.js.map +1 -0
  135. package/dist/types/QueryAny.d.ts +7 -0
  136. package/dist/types/QueryAny.js +2 -0
  137. package/dist/types/QueryAny.js.map +1 -0
  138. package/dist/types/QueryCreate.d.ts +7 -0
  139. package/dist/types/QueryCreate.js +2 -0
  140. package/dist/types/QueryCreate.js.map +1 -0
  141. package/dist/types/QueryCreateMultiple.d.ts +8 -0
  142. package/dist/types/QueryCreateMultiple.js +2 -0
  143. package/dist/types/QueryCreateMultiple.js.map +1 -0
  144. package/dist/types/QueryDelete.d.ts +10 -0
  145. package/dist/types/QueryDelete.js +2 -0
  146. package/dist/types/QueryDelete.js.map +1 -0
  147. package/dist/types/QueryDeleteMultiple.d.ts +11 -0
  148. package/dist/types/QueryDeleteMultiple.js +2 -0
  149. package/dist/types/QueryDeleteMultiple.js.map +1 -0
  150. package/dist/types/QueryMethod.d.ts +5 -0
  151. package/dist/types/QueryMethod.js +2 -0
  152. package/dist/types/QueryMethod.js.map +1 -0
  153. package/dist/types/QueryRead.d.ts +23 -0
  154. package/dist/types/QueryRead.js +2 -0
  155. package/dist/types/QueryRead.js.map +1 -0
  156. package/dist/types/QueryReadMultiple.d.ts +34 -0
  157. package/dist/types/QueryReadMultiple.js +2 -0
  158. package/dist/types/QueryReadMultiple.js.map +1 -0
  159. package/dist/types/QueryUpdate.d.ts +18 -0
  160. package/dist/types/QueryUpdate.js +2 -0
  161. package/dist/types/QueryUpdate.js.map +1 -0
  162. package/dist/types/QueryUpdateMultiple.d.ts +26 -0
  163. package/dist/types/QueryUpdateMultiple.js +2 -0
  164. package/dist/types/QueryUpdateMultiple.js.map +1 -0
  165. package/dist/types/Result.d.ts +25 -0
  166. package/dist/types/Result.js +2 -0
  167. package/dist/types/Result.js.map +1 -0
  168. package/dist/types/Store.d.ts +6 -0
  169. package/dist/types/Store.js +2 -0
  170. package/dist/types/Store.js.map +1 -0
  171. package/dist/types.d.ts +46 -396
  172. package/doc/README.md +543 -211
  173. package/lib/errors/RequestError.ts +2 -2
  174. package/lib/errors.ts +1 -1
  175. package/lib/middlewares/aggregate.ts +5 -5
  176. package/lib/middlewares/cache.ts +7 -12
  177. package/lib/middlewares.ts +9 -9
  178. package/lib/tools/cork.ts +6 -0
  179. package/lib/tools/defineCheckQuery.ts +20 -15
  180. package/lib/tools/defineCustomFetch.ts +40 -62
  181. package/lib/tools/defineGenericFetch.ts +33 -0
  182. package/lib/tools/queryItemList.test.ts +6 -6
  183. package/lib/tools/queryItemList.ts +3 -3
  184. package/lib/tools/sortItemList.ts +3 -3
  185. package/lib/tools.ts +9 -8
  186. package/lib/types/AggregateFunction.ts +17 -0
  187. package/lib/types/AggregateFunctionOperator.ts +6 -0
  188. package/lib/types/Any.ts +10 -0
  189. package/lib/types/Context.ts +3 -0
  190. package/lib/types/CustomFieldAggregateMap.ts +6 -0
  191. package/lib/types/CustomFieldMap.ts +3 -0
  192. package/lib/types/CustomRequest.ts +21 -0
  193. package/lib/types/FieldFunction.ts +6 -0
  194. package/lib/types/FieldFunctionCustom.ts +7 -0
  195. package/lib/types/FieldFunctionFormatDate.ts +10 -0
  196. package/lib/types/FieldFunctionReturn.ts +6 -0
  197. package/lib/types/Filter.ts +20 -0
  198. package/lib/types/FilterArray.ts +11 -0
  199. package/lib/types/FilterBoolean.ts +10 -0
  200. package/lib/types/FilterField.ts +7 -0
  201. package/lib/types/FilterKeys.ts +6 -0
  202. package/lib/types/FilterNumber.ts +16 -0
  203. package/lib/types/FilterOperator.ts +3 -0
  204. package/lib/types/FilterSequence.ts +26 -0
  205. package/lib/types/FilterString.ts +19 -0
  206. package/lib/types/FilterStringIntersect.ts +10 -0
  207. package/lib/types/FilterStringMatch.ts +30 -0
  208. package/lib/types/Get.ts +8 -0
  209. package/lib/types/Group.ts +11 -0
  210. package/lib/types/Handler.ts +9 -0
  211. package/lib/types/Immutable.ts +3 -0
  212. package/lib/types/InjectCustomFields.ts +23 -0
  213. package/lib/types/Item.ts +8 -0
  214. package/lib/types/Key.ts +1 -0
  215. package/lib/types/Mutable.ts +3 -0
  216. package/lib/types/NextHandler.ts +4 -0
  217. package/lib/types/Order.ts +9 -0
  218. package/lib/types/Query.ts +36 -0
  219. package/lib/types/QueryAggregate.ts +11 -0
  220. package/lib/types/QueryAny.ts +8 -0
  221. package/lib/types/QueryCreate.ts +7 -0
  222. package/lib/types/QueryCreateMultiple.ts +8 -0
  223. package/lib/types/QueryDelete.ts +11 -0
  224. package/lib/types/QueryDeleteMultiple.ts +12 -0
  225. package/lib/types/QueryMethod.ts +6 -0
  226. package/lib/types/QueryRead.ts +24 -0
  227. package/lib/types/QueryReadMultiple.ts +36 -0
  228. package/lib/types/QueryUpdate.ts +19 -0
  229. package/lib/types/QueryUpdateMultiple.ts +27 -0
  230. package/lib/types/Result.ts +38 -0
  231. package/lib/types/Store.ts +7 -0
  232. package/lib/types.ts +47 -554
  233. package/package.json +3 -3
  234. package/dist/errors/QueryError.d.ts +0 -5
  235. package/dist/errors/QueryError.js +0 -23
  236. package/dist/errors/QueryError.js.map +0 -1
  237. package/dist/middlewares/concurrent.d.ts +0 -2
  238. package/dist/middlewares/concurrent.js +0 -20
  239. package/dist/middlewares/concurrent.js.map +0 -1
  240. package/dist/middlewares/fetch.d.ts +0 -2
  241. package/dist/middlewares/fetch.js +0 -20
  242. package/dist/middlewares/fetch.js.map +0 -1
  243. package/dist/middlewares/logQuery.d.ts +0 -2
  244. package/dist/middlewares/logQuery.js +0 -25
  245. package/dist/middlewares/logQuery.js.map +0 -1
  246. package/dist/tools/add.d.ts +0 -8
  247. package/dist/tools/add.js +0 -11
  248. package/dist/tools/add.js.map +0 -1
  249. package/dist/tools/add.test.d.ts +0 -1
  250. package/dist/tools/add.test.js +0 -6
  251. package/dist/tools/add.test.js.map +0 -1
  252. package/dist/tools/filterItem.test.d.ts +0 -1
  253. package/dist/tools/filterItem.test.js +0 -86
  254. package/dist/tools/filterItem.test.js.map +0 -1
  255. package/dist/tools/impasse.d.ts +0 -2
  256. package/dist/tools/impasse.js +0 -2
  257. package/dist/tools/impasse.js.map +0 -1
  258. package/dist/tools/queryItemList.test.d.ts +0 -1
  259. package/dist/tools/queryItemList.test.js +0 -181
  260. package/dist/tools/queryItemList.test.js.map +0 -1
  261. package/dist/tools/sortItemList.test.d.ts +0 -1
  262. package/dist/tools/sortItemList.test.js +0 -47
  263. package/dist/tools/sortItemList.test.js.map +0 -1
  264. package/lib/tools/impasse.ts +0 -3
@@ -1,4 +1,4 @@
1
- import type { AnyQuery } from "../types";
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?: AnyQuery,
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 { AnyQuery, Handler } from "../types";
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 AnyQuery, O, In extends AnyQuery, On>({
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, _): AnyQuery => ({
34
+ queryForGroup = (queryList, _): QueryAny => ({
35
35
  type: queryList[0].type,
36
- method: "get",
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) => AnyQuery;
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<
@@ -1,11 +1,11 @@
1
- import type { AnyQueryExternal, Handler, Store } from "../types";
1
+ import type { Handler, QueryAny, Store } from "../types";
2
2
 
3
- type CachedItem<I extends AnyQueryExternal> = {
3
+ type CachedItem<I extends QueryAny> = {
4
4
  query: I;
5
5
  value: any;
6
6
  };
7
7
 
8
- type CacheOptions<I extends AnyQueryExternal> = {
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 extends AnyQueryExternal,
41
- O,
42
- In extends AnyQueryExternal,
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
  },
@@ -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";
@@ -0,0 +1,6 @@
1
+ import type { NextHandler } from "../types";
2
+
3
+ /**
4
+ * Uncallable handler used to terminate a sequence of handlers combined with `combine`.
5
+ */
6
+ export const cork = undefined as unknown as NextHandler<never, never>;
@@ -1,23 +1,28 @@
1
1
  import type { CustomFieldMap, Query } from "../types";
2
2
 
3
- export function defineCheckQuery<Entities extends Record<string, object>>() {
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
- E extends object,
6
- C extends CustomFieldMap<E>,
7
- const Q extends Query<E, C>,
8
- >(query: Q & { type: E[]; customFields?: C }): typeof query;
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
- T extends keyof Entities,
11
- E extends Entities[T],
12
- C extends CustomFieldMap<E>,
13
- const Q extends Query<E, C>,
14
- >(query: Q & { type: T; customFields?: C }): typeof query;
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
- T extends keyof Entities,
17
- E extends Entities[T] | object,
18
- C extends CustomFieldMap<E>,
19
- const Q extends Query<E, C>,
20
- >(query: Q & { type: T | E[]; customFields?: C }): typeof query {
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 { impasse } from "./impasse";
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<AnyQuery, any, never, never>,
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
- T extends keyof M,
51
- E extends M[T] | object,
52
- C extends CustomFieldMap<E>,
53
- const Q extends Query<E, C>,
54
- >(
55
- input: Q & { type: T | E[]; customFields?: C },
56
- ): Promise<
57
- Q["method"] extends "get"
58
- ? Q extends { multiple: true }
59
- ? EntityItem<E, C, Q>[]
60
- : EntityItem<E, C, Q>
61
- : Q["method"] extends "aggregate"
62
- ? number
63
- : Q extends { multiple: true }
64
- ? EntityItem<E, C, Q>[]
65
- : EntityItem<E, C, Q>
66
- > {
67
- return await handler(input, impasse);
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: "get",
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
- orderBy: ["a", "c"],
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
- orderBy: [
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: "get",
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: "get",
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: "get",
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 "get":
46
+ case "read":
47
47
  case undefined: {
48
48
  const { filter, context } = query;
49
49
  if (query.multiple) {
50
- const { offset = 0, limit = Infinity, orderBy } = query;
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
- orderBy as Order<T>[],
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 normalizedOrderBy = orderList.map(normalizeOrder);
19
+ const normalizedorder = orderList.map(normalizeOrder);
20
20
  return value.sort((a, b) => {
21
- for (let index = 0; index < normalizedOrderBy.length; index++) {
22
- const { field, descending } = normalizedOrderBy[index];
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 { defineCheckQuery } from "./tools/defineCheckQuery";
3
- export { defineCustomFetch } from "./tools/defineCustomFetch";
4
- export { filterFromContext } from "./tools/filterFromContext";
5
- export { filterItem } from "./tools/filterItem";
6
- export { impasse } from "./tools/impasse";
7
- export { normalizeOrder } from "./tools/normalizeOrder";
8
- export { queryItemList } from "./tools/queryItemList";
9
- export { sortItemList } from "./tools/sortItemList";
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
+ };
@@ -0,0 +1,6 @@
1
+ import type { AggregateFunction } from "./AggregateFunction";
2
+
3
+ export type AggregateFunctionOperator = Exclude<
4
+ AggregateFunction<{}>,
5
+ string
6
+ >["operator"];
@@ -0,0 +1,10 @@
1
+ export type Any =
2
+ | boolean
3
+ | string
4
+ | number
5
+ | object
6
+ | symbol
7
+ | null
8
+ | undefined
9
+ | Array<any>
10
+ | ((...args: any[]) => any);
@@ -0,0 +1,3 @@
1
+ export type Context<T extends object> = {
2
+ [K in keyof T]?: T[K];
3
+ };
@@ -0,0 +1,6 @@
1
+ import type { AggregateFunction } from "./AggregateFunction";
2
+
3
+ export type CustomFieldAggregateMap<T extends object> = Record<
4
+ string,
5
+ AggregateFunction<T>
6
+ >;
@@ -0,0 +1,3 @@
1
+ import type { FieldFunction } from "./FieldFunction";
2
+
3
+ export type CustomFieldMap<T extends object> = Record<string, FieldFunction<T>>;
@@ -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,6 @@
1
+ import type { FieldFunctionCustom } from "./FieldFunctionCustom";
2
+ import type { FieldFunctionFormatDate } from "./FieldFunctionFormatDate";
3
+
4
+ export type FieldFunction<T extends object> =
5
+ | FieldFunctionCustom<T>
6
+ | FieldFunctionFormatDate<T>;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Applies a custom field transform function.
3
+ */
4
+ export type FieldFunctionCustom<T extends object> = {
5
+ operator: "custom";
6
+ value: (item: T) => any;
7
+ };
@@ -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,6 @@
1
+ /**
2
+ * Return types of custom field functions.
3
+ */
4
+ export type FieldFunctionReturn = {
5
+ formatDate: string;
6
+ };
@@ -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,10 @@
1
+ import type { FilterKeys } from "./FilterKeys";
2
+
3
+ /**
4
+ * Checks if a given boolean field is `true` or `false`.
5
+ */
6
+ export type FilterBoolean<T extends object> = {
7
+ operator: "equal" | "notEqual";
8
+ field: FilterKeys<T, boolean>;
9
+ value: boolean;
10
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Checks if a given field exists.
3
+ */
4
+ export type FilterField<T extends object> = {
5
+ operator: "exist";
6
+ field: keyof T;
7
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Returns object type with field extending the provided `P` type.
3
+ */
4
+ export type FilterKeys<T extends object, P> = {
5
+ [K in keyof T]-?: T[K] extends P ? K : never;
6
+ }[keyof T];
@@ -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,3 @@
1
+ import type { Filter } from "./Filter";
2
+
3
+ export type FilterOperator = Filter<never>["operator"];
@@ -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
+ };