@data-client/rest 0.1.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 (158) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +110 -0
  3. package/dist/index.js +561 -0
  4. package/dist/index.umd.min.js +1 -0
  5. package/dist/next.js +520 -0
  6. package/dist/package.json +1 -0
  7. package/legacy/NetworkError.js +13 -0
  8. package/legacy/OptionsToFunction.js +2 -0
  9. package/legacy/RestEndpoint.js +225 -0
  10. package/legacy/RestEndpointTypeHelp.js +2 -0
  11. package/legacy/RestHelpers.js +33 -0
  12. package/legacy/createResource.js +88 -0
  13. package/legacy/extractCollection.js +14 -0
  14. package/legacy/getArrayPath.js +19 -0
  15. package/legacy/hookifyResource.js +24 -0
  16. package/legacy/index.js +10 -0
  17. package/legacy/mapCollection.js +14 -0
  18. package/legacy/next/OptionsToFunction.js +2 -0
  19. package/legacy/next/RestEndpoint.js +225 -0
  20. package/legacy/next/createResource.js +86 -0
  21. package/legacy/next/index.js +4 -0
  22. package/legacy/next/resourceTypes.js +2 -0
  23. package/legacy/paginatedCollections.js +29 -0
  24. package/legacy/paginationUpdate.js +36 -0
  25. package/legacy/paramsToString.js +6 -0
  26. package/legacy/paramsToString.native.js +9 -0
  27. package/legacy/pathTypes.js +2 -0
  28. package/legacy/test-fixtures.js +111 -0
  29. package/legacy/utiltypes.js +2 -0
  30. package/lib/NetworkError.d.ts +11 -0
  31. package/lib/NetworkError.d.ts.map +1 -0
  32. package/lib/NetworkError.js +13 -0
  33. package/lib/OptionsToFunction.d.ts +7 -0
  34. package/lib/OptionsToFunction.d.ts.map +1 -0
  35. package/lib/OptionsToFunction.js +2 -0
  36. package/lib/RestEndpoint.d.ts +548 -0
  37. package/lib/RestEndpoint.js +229 -0
  38. package/lib/RestEndpointTypeHelp.d.ts +10 -0
  39. package/lib/RestEndpointTypeHelp.d.ts.map +1 -0
  40. package/lib/RestEndpointTypeHelp.js +2 -0
  41. package/lib/RestHelpers.d.ts +7 -0
  42. package/lib/RestHelpers.d.ts.map +1 -0
  43. package/lib/RestHelpers.js +33 -0
  44. package/lib/createResource.d.ts +66 -0
  45. package/lib/createResource.d.ts.map +1 -0
  46. package/lib/createResource.js +104 -0
  47. package/lib/extractCollection.d.ts +11 -0
  48. package/lib/extractCollection.d.ts.map +1 -0
  49. package/lib/extractCollection.js +16 -0
  50. package/lib/getArrayPath.d.ts +3 -0
  51. package/lib/getArrayPath.d.ts.map +1 -0
  52. package/lib/getArrayPath.js +19 -0
  53. package/lib/hookifyResource.d.ts +14 -0
  54. package/lib/hookifyResource.d.ts.map +1 -0
  55. package/lib/hookifyResource.js +24 -0
  56. package/lib/index.d.ts +12 -0
  57. package/lib/index.d.ts.map +1 -0
  58. package/lib/index.js +10 -0
  59. package/lib/mapCollection.d.ts +16 -0
  60. package/lib/mapCollection.d.ts.map +1 -0
  61. package/lib/mapCollection.js +16 -0
  62. package/lib/next/OptionsToFunction.d.ts +7 -0
  63. package/lib/next/OptionsToFunction.d.ts.map +1 -0
  64. package/lib/next/OptionsToFunction.js +2 -0
  65. package/lib/next/RestEndpoint.d.ts +542 -0
  66. package/lib/next/RestEndpoint.js +229 -0
  67. package/lib/next/createResource.d.ts +86 -0
  68. package/lib/next/createResource.d.ts.map +1 -0
  69. package/lib/next/createResource.js +100 -0
  70. package/lib/next/index.d.ts +6 -0
  71. package/lib/next/index.d.ts.map +1 -0
  72. package/lib/next/index.js +4 -0
  73. package/lib/next/resourceTypes.d.ts +31 -0
  74. package/lib/next/resourceTypes.d.ts.map +1 -0
  75. package/lib/next/resourceTypes.js +2 -0
  76. package/lib/paginatedCollections.d.ts +3 -0
  77. package/lib/paginatedCollections.d.ts.map +1 -0
  78. package/lib/paginatedCollections.js +29 -0
  79. package/lib/paginationUpdate.d.ts +8 -0
  80. package/lib/paginationUpdate.d.ts.map +1 -0
  81. package/lib/paginationUpdate.js +40 -0
  82. package/lib/paramsToString.d.ts +2 -0
  83. package/lib/paramsToString.d.ts.map +1 -0
  84. package/lib/paramsToString.js +6 -0
  85. package/lib/paramsToString.native.d.ts +2 -0
  86. package/lib/paramsToString.native.d.ts.map +1 -0
  87. package/lib/paramsToString.native.js +9 -0
  88. package/lib/pathTypes.d.ts +20 -0
  89. package/lib/pathTypes.d.ts.map +1 -0
  90. package/lib/pathTypes.js +2 -0
  91. package/lib/test-fixtures.d.ts +141 -0
  92. package/lib/test-fixtures.d.ts.map +1 -0
  93. package/lib/test-fixtures.js +111 -0
  94. package/lib/utiltypes.d.ts +21 -0
  95. package/lib/utiltypes.d.ts.map +1 -0
  96. package/lib/utiltypes.js +2 -0
  97. package/node.mjs +1 -0
  98. package/package.json +121 -0
  99. package/src/NetworkError.ts +17 -0
  100. package/src/OptionsToFunction.ts +42 -0
  101. package/src/RestEndpoint.d.ts +548 -0
  102. package/src/RestEndpoint.js +262 -0
  103. package/src/RestEndpointTypeHelp.ts +20 -0
  104. package/src/RestHelpers.ts +48 -0
  105. package/src/__tests__/Resource.test.ts +317 -0
  106. package/src/__tests__/RestEndpoint.ts +1304 -0
  107. package/src/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
  108. package/src/__tests__/__snapshots__/createResource.test.ts.snap +5 -0
  109. package/src/__tests__/createResource.test.ts +583 -0
  110. package/src/__tests__/getArrayPath.test.ts +30 -0
  111. package/src/__tests__/hookifyResource.ts +197 -0
  112. package/src/__tests__/types.test.ts +309 -0
  113. package/src/createResource.ts +172 -0
  114. package/src/extractCollection.ts +34 -0
  115. package/src/getArrayPath.ts +20 -0
  116. package/src/hookifyResource.ts +36 -0
  117. package/src/index.ts +42 -0
  118. package/src/mapCollection.ts +54 -0
  119. package/src/next/OptionsToFunction.ts +41 -0
  120. package/src/next/RestEndpoint.d.ts +542 -0
  121. package/src/next/RestEndpoint.js +262 -0
  122. package/src/next/__tests__/RestEndpoint.ts +1333 -0
  123. package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
  124. package/src/next/__tests__/types.test.ts +429 -0
  125. package/src/next/createResource.ts +194 -0
  126. package/src/next/index.ts +5 -0
  127. package/src/next/resourceTypes.ts +32 -0
  128. package/src/package.json +1 -0
  129. package/src/paginatedCollections.ts +32 -0
  130. package/src/paginationUpdate.ts +49 -0
  131. package/src/paramsToString.native.ts +10 -0
  132. package/src/paramsToString.ts +7 -0
  133. package/src/pathTypes.ts +58 -0
  134. package/src/test-fixtures.ts +128 -0
  135. package/src/utiltypes.ts +25 -0
  136. package/ts4.0/NetworkError.d.ts +11 -0
  137. package/ts4.0/OptionsToFunction.d.ts +16 -0
  138. package/ts4.0/RestEndpoint.d.ts +240 -0
  139. package/ts4.0/RestEndpointTypeHelp.d.ts +10 -0
  140. package/ts4.0/RestHelpers.d.ts +7 -0
  141. package/ts4.0/createResource.d.ts +66 -0
  142. package/ts4.0/extractCollection.d.ts +11 -0
  143. package/ts4.0/getArrayPath.d.ts +3 -0
  144. package/ts4.0/hookifyResource.d.ts +19 -0
  145. package/ts4.0/index.d.ts +12 -0
  146. package/ts4.0/mapCollection.d.ts +16 -0
  147. package/ts4.0/next/OptionsToFunction.d.ts +16 -0
  148. package/ts4.0/next/RestEndpoint.d.ts +226 -0
  149. package/ts4.0/next/createResource.d.ts +94 -0
  150. package/ts4.0/next/index.d.ts +6 -0
  151. package/ts4.0/next/resourceTypes.d.ts +31 -0
  152. package/ts4.0/paginatedCollections.d.ts +3 -0
  153. package/ts4.0/paginationUpdate.d.ts +10 -0
  154. package/ts4.0/paramsToString.d.ts +2 -0
  155. package/ts4.0/paramsToString.native.d.ts +2 -0
  156. package/ts4.0/pathTypes.d.ts +20 -0
  157. package/ts4.0/test-fixtures.d.ts +141 -0
  158. package/ts4.0/utiltypes.d.ts +5 -0
@@ -0,0 +1,20 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+
3
+ export default function getArrayPath(s: Schema | undefined): string[] | false {
4
+ if (typeof s !== 'object') return false;
5
+ if (s === undefined || s instanceof schema.Array || Array.isArray(s)) {
6
+ return [];
7
+ }
8
+ const o: Record<string, Schema> =
9
+ s instanceof schema.Object ? (s as any).schema : s;
10
+ for (const k in o) {
11
+ if (!o[k]) continue;
12
+ const path = getArrayPath(o[k]);
13
+ if (path !== false) {
14
+ // mutation is ok because there is only one path
15
+ path.unshift(k);
16
+ return path;
17
+ }
18
+ }
19
+ return false;
20
+ }
@@ -0,0 +1,36 @@
1
+ import { EndpointInterface } from '@data-client/endpoint';
2
+
3
+ export interface HookableEndpointInterface extends EndpointInterface {
4
+ extend(...args: any): HookableEndpointInterface;
5
+ }
6
+
7
+ /** Turns a collection of Endpoints (Resource) into a collection of hooks.
8
+ * This is useful for Endpoints that need hooks to prepare their fetch requests.
9
+ *
10
+ * @see https://resthooks.io/rest/api/hookifyResource
11
+ */
12
+ export default function hookifyResource<R extends {}>(
13
+ resource: R,
14
+ useRequestInit: () => RequestInit,
15
+ ): HookResource<R> {
16
+ const usingResource: Record<string, () => HookableEndpointInterface> = {};
17
+ Object.keys(resource).forEach(key => {
18
+ const endpoint = (resource as any)[key];
19
+ if (endpoint.extend !== undefined)
20
+ usingResource[`use${capitalizeFirstLetter(key)}`] = () => {
21
+ // this is false positive due to the dynamic nature of assignment
22
+ // eslint-disable-next-line react-hooks/rules-of-hooks
23
+ const requestInit = useRequestInit();
24
+ return endpoint.extend({ requestInit });
25
+ };
26
+ });
27
+ return usingResource as any;
28
+ }
29
+
30
+ export type HookResource<R extends {}> = {
31
+ [K in Extract<keyof R, string> as `use${Capitalize<K>}`]: () => R[K];
32
+ };
33
+
34
+ function capitalizeFirstLetter(s: string) {
35
+ return s.charAt(0).toUpperCase() + s.slice(1);
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ Object.hasOwn =
2
+ Object.hasOwn ||
3
+ /* istanbul ignore next */ function hasOwn(it, key) {
4
+ return Object.prototype.hasOwnProperty.call(it, key);
5
+ };
6
+ export { default as RestEndpoint } from './RestEndpoint.js';
7
+ export type {
8
+ GetEndpoint,
9
+ MutateEndpoint,
10
+ NewGetEndpoint,
11
+ NewMutateEndpoint,
12
+ Defaults,
13
+ RestGenerics,
14
+ FetchGet,
15
+ FetchMutate,
16
+ RestFetch,
17
+ RestType,
18
+ RestInstance,
19
+ KeyofRestEndpoint,
20
+ RestEndpointConstructorOptions,
21
+ AddEndpoint,
22
+ PaginationFieldEndpoint,
23
+ } from './RestEndpoint.js';
24
+ export { default as createResource } from './createResource.js';
25
+ export type { Resource } from './createResource.js';
26
+ export { default as hookifyResource } from './hookifyResource.js';
27
+ export type {
28
+ HookResource,
29
+ HookableEndpointInterface,
30
+ } from './hookifyResource.js';
31
+ export { default as NetworkError } from './NetworkError.js';
32
+ export { default as paginationUpdate } from './paginationUpdate.js';
33
+ export type { OptionsToFunction } from './OptionsToFunction.js';
34
+ export type {
35
+ ShortenPath,
36
+ PathArgs,
37
+ PathKeys,
38
+ KeysToArgs,
39
+ PathArgsAndSearch,
40
+ } from './pathTypes.js';
41
+
42
+ export * from '@data-client/endpoint';
@@ -0,0 +1,54 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+
3
+ export default function mapCollection<
4
+ M extends <C extends schema.Collection>(collection: C) => any,
5
+ S extends Schema | undefined,
6
+ >(
7
+ s: S,
8
+ mapper: M,
9
+ ): S extends schema.Collection
10
+ ? ReturnType<typeof mapper<S>>
11
+ : S extends schema.Object<infer T>
12
+ ? {
13
+ [K in keyof T]: T[K] extends Schema
14
+ ? typeof mapCollection<M, T[K]>
15
+ : T[K];
16
+ }
17
+ : S extends { [K: string]: any }
18
+ ? {
19
+ [K in keyof S]: S[K] extends Schema
20
+ ? typeof mapCollection<M, S[K]>
21
+ : S[K];
22
+ }
23
+ : S {
24
+ if (typeof s !== 'object' || s === undefined) return s as any;
25
+ if (s instanceof schema.Collection) {
26
+ return mapper(s as any);
27
+ }
28
+ const objCopy: Record<string, Schema> = {
29
+ ...(s instanceof schema.Object ? (s as any).schema : s),
30
+ };
31
+ for (const k in objCopy) {
32
+ if (!objCopy[k]) continue;
33
+ objCopy[k] = mapCollection(objCopy[k], mapper) as any;
34
+ }
35
+ return objCopy as any;
36
+ }
37
+
38
+ type MapCollection<
39
+ M extends <C extends schema.Collection>(collection: C) => any,
40
+ S extends Schema | undefined,
41
+ > = S extends schema.Collection
42
+ ? ReturnType<M>
43
+ : S extends schema.Object<infer T>
44
+ ? MapCollection<M, T>
45
+ : S extends { [K: string]: any }
46
+ ? MapObject<M, S>
47
+ : never;
48
+
49
+ export type MapObject<
50
+ M extends (collection: schema.Collection) => any,
51
+ S extends Record<string, any>,
52
+ > = {
53
+ [K in keyof S]: S[K] extends Schema ? MapCollection<M, S[K]> : S[K];
54
+ };
@@ -0,0 +1,41 @@
1
+ import type { FetchFunction, ResolveType } from '@data-client/endpoint';
2
+
3
+ import {
4
+ PartialRestGenerics,
5
+ RestInstanceBase,
6
+ RestFetch,
7
+ } from './RestEndpoint.js';
8
+ import { PathArgs } from '../pathTypes.js';
9
+
10
+ export type OptionsToFunction<
11
+ O extends PartialRestGenerics,
12
+ E extends RestInstanceBase & { body?: any },
13
+ F extends FetchFunction,
14
+ > = 'path' extends keyof O
15
+ ? RestFetch<
16
+ 'searchParams' extends keyof O
17
+ ? O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
18
+ : PathArgs<Exclude<O['path'], undefined>>,
19
+ 'body' extends keyof O ? O['body'] : E['body'],
20
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
21
+ >
22
+ : 'body' extends keyof O
23
+ ? RestFetch<
24
+ 'searchParams' extends keyof O
25
+ ? O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
26
+ : PathArgs<Exclude<E['path'], undefined>>,
27
+ O['body'],
28
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
29
+ >
30
+ : 'searchParams' extends keyof O
31
+ ? RestFetch<
32
+ O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
33
+ E['body'],
34
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
35
+ >
36
+ : (
37
+ this: ThisParameterType<F>,
38
+ ...args: Parameters<F>
39
+ ) => Promise<
40
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
41
+ >;