@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,66 @@
1
+ import { schema } from '@data-client/endpoint';
2
+ import type { Schema, Denormalize, EndpointExtraOptions } from '@data-client/endpoint';
3
+ import { PathArgs, ShortenPath } from './pathTypes.js';
4
+ import RestEndpoint, { NewGetEndpoint, NewMutateEndpoint, RestTypeNoBody } from './RestEndpoint.js';
5
+ /** Creates collection of Endpoints for common operations on a given data/schema.
6
+ *
7
+ * @see https://resthooks.io/rest/api/createResource
8
+ */
9
+ export default function createResource<U extends string, S extends Schema>({ path, schema, Endpoint, optimistic, ...extraOptions }: {
10
+ readonly path: U;
11
+ readonly schema: S;
12
+ readonly Endpoint?: typeof RestEndpoint;
13
+ urlPrefix?: string;
14
+ optimistic?: boolean;
15
+ } & EndpointExtraOptions): Resource<U, S>;
16
+ export interface Resource<U extends string, S extends Schema> {
17
+ /** Get a singular item
18
+ *
19
+ * @see https://resthooks.io/rest/api/createResource#get
20
+ */
21
+ get: NewGetEndpoint<{
22
+ path: U;
23
+ }, S>;
24
+ /** Get a list of item
25
+ *
26
+ * @see https://resthooks.io/rest/api/createResource#getlist
27
+ */
28
+ getList: NewGetEndpoint<{
29
+ path: ShortenPath<U>;
30
+ searchParams: Record<string, number | string | boolean> | undefined;
31
+ }, S[]>;
32
+ /** Create a new item (POST)
33
+ *
34
+ * @see https://resthooks.io/rest/api/createResource#create
35
+ */
36
+ create: NewMutateEndpoint<{
37
+ path: ShortenPath<U>;
38
+ body: Partial<Denormalize<S>>;
39
+ }, S>;
40
+ /** Update an item (PUT)
41
+ *
42
+ * @see https://resthooks.io/rest/api/createResource#update
43
+ */
44
+ update: NewMutateEndpoint<{
45
+ path: U;
46
+ body: Partial<Denormalize<S>>;
47
+ }, S>;
48
+ /** Update an item (PATCH)
49
+ *
50
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
51
+ */
52
+ partialUpdate: NewMutateEndpoint<{
53
+ path: U;
54
+ body: Partial<Denormalize<S>>;
55
+ }, S>;
56
+ /** Delete an item (DELETE)
57
+ *
58
+ * @see https://resthooks.io/rest/api/createResource#delete
59
+ */
60
+ delete: RestTypeNoBody<PathArgs<U>, S extends schema.EntityInterface & {
61
+ process: any;
62
+ } ? schema.Delete<S> : S, undefined, Partial<PathArgs<U>>, {
63
+ path: U;
64
+ }>;
65
+ }
66
+ //# sourceMappingURL=createResource.d.ts.map
@@ -0,0 +1,11 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+ export default function extractCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): ExtractCollection<S> | undefined;
3
+ export type ExtractCollection<S extends Schema | undefined> = S extends schema.CollectionInterface ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
4
+ [K: string]: any;
5
+ }> ? never : S extends {
6
+ [K: string]: Schema;
7
+ } ? ExtractObject<S> : never;
8
+ export type ExtractObject<S extends Record<string, any>> = {
9
+ [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
10
+ }[keyof S];
11
+ //# sourceMappingURL=extractCollection.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ export default function getArrayPath(s: Schema | undefined): string[] | false;
3
+ //# sourceMappingURL=getArrayPath.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { EndpointInterface } from '@data-client/endpoint';
2
+ export interface HookableEndpointInterface extends EndpointInterface {
3
+ extend(...args: any): HookableEndpointInterface;
4
+ }
5
+ /** Turns a collection of Endpoints (Resource) into a collection of hooks.
6
+ * This is useful for Endpoints that need hooks to prepare their fetch requests.
7
+ *
8
+ * Requires TypeScript 4.1 to be typed correctly
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
+ export type HookResource<R extends Record<string, unknown>> = {
17
+ [K: string]: () => R[string];
18
+ };
19
+ //# sourceMappingURL=hookifyResource.d.ts.map
@@ -0,0 +1,12 @@
1
+ export { default as RestEndpoint } from './RestEndpoint.js';
2
+ export type { GetEndpoint, MutateEndpoint, NewGetEndpoint, NewMutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, } from './RestEndpoint.js';
3
+ export { default as createResource } from './createResource.js';
4
+ export type { Resource } from './createResource.js';
5
+ export { default as hookifyResource } from './hookifyResource.js';
6
+ export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
7
+ export { default as NetworkError } from './NetworkError.js';
8
+ export { default as paginationUpdate } from './paginationUpdate.js';
9
+ export type { OptionsToFunction } from './OptionsToFunction.js';
10
+ export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
11
+ export * from '@data-client/endpoint';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,16 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+ export default function mapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): S extends schema.Collection ? ReturnType<typeof mapper<S>> : S extends schema.Object<infer T> ? {
3
+ [K in keyof T]: T[K] extends Schema ? typeof mapCollection<M, T[K]> : T[K];
4
+ } : S extends {
5
+ [K: string]: any;
6
+ } ? {
7
+ [K in keyof S]: S[K] extends Schema ? typeof mapCollection<M, S[K]> : S[K];
8
+ } : S;
9
+ type MapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined> = S extends schema.Collection ? ReturnType<M> : S extends schema.Object<infer T> ? MapCollection<M, T> : S extends {
10
+ [K: string]: any;
11
+ } ? MapObject<M, S> : never;
12
+ export type MapObject<M extends (collection: schema.Collection) => any, S extends Record<string, any>> = {
13
+ [K in keyof S]: S[K] extends Schema ? MapCollection<M, S[K]> : S[K];
14
+ };
15
+ export {};
16
+ //# sourceMappingURL=mapCollection.d.ts.map
@@ -0,0 +1,16 @@
1
+ import type { FetchFunction, ResolveType } from '@data-client/endpoint';
2
+
3
+ import { PartialRestGenerics, RestInstance } from './RestEndpoint.js';
4
+
5
+ export type OptionsToFunction<
6
+ O extends PartialRestGenerics,
7
+ E extends RestInstance & {
8
+ body?: any;
9
+ },
10
+ F extends FetchFunction,
11
+ > = (
12
+ this: ThisParameterType<F>,
13
+ ...args: any
14
+ ) => Promise<
15
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
16
+ >;
@@ -0,0 +1,226 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, } from '@data-client/endpoint';
3
+ import { OptionsToFunction } from './OptionsToFunction.js';
4
+ import type { ExtractCollection } from '../extractCollection.js';
5
+ import { PathArgs } from '../pathTypes.js';
6
+ import { EndpointUpdateFunction } from '../RestEndpointTypeHelp.js';
7
+ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
8
+ path: string;
9
+ body?: any;
10
+ searchParams?: any;
11
+ } = {
12
+ path: string;
13
+ }> extends EndpointInstanceInterface<F, S, M> {
14
+ readonly body?: 'body' extends keyof O ? O['body'] : any;
15
+ readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
16
+ /** Pattern to construct url based on Url Parameters
17
+ * @see https://resthooks.io/rest/api/RestEndpoint#path
18
+ */
19
+ readonly path: O['path'];
20
+ /** Prepended to all urls
21
+ * @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
22
+ */
23
+ readonly urlPrefix: string;
24
+ readonly requestInit: RequestInit;
25
+ readonly method: string;
26
+ readonly signal: AbortSignal | undefined;
27
+ /* fetch lifecycles */
28
+ /* before-fetch */
29
+ url(...args: Parameters<F>): string;
30
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
31
+ getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
32
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
33
+ getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
34
+ /* after-fetch */
35
+ /** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
36
+ fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
37
+ /** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
38
+ parseResponse(response: Response): Promise<any>;
39
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
40
+ process(value: any, ...args: Parameters<F>): ResolveType<F>;
41
+ /* utilities */
42
+ /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
43
+ testKey(key: string): boolean;
44
+ /* extenders */
45
+ // TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
46
+ // this is just a hack to handle when no members of PartialRestGenerics are present
47
+ // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
48
+ // should be used from the other (due to same problem with every member being partial)
49
+ extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<O, E, F> & O>): RestExtendedEndpoint<O, E>;
50
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
51
+ ...Parameters<E>
52
+ ]): PaginationEndpoint<E, A>;
53
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
54
+ }
55
+ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
56
+ path: string;
57
+ body?: any;
58
+ searchParams?: any;
59
+ } = {
60
+ path: string;
61
+ }> extends RestInstanceBase<F, S, M, O> {
62
+ push: AddEndpoint<F, S, O>;
63
+ unshift: AddEndpoint<F, S, O>;
64
+ assign: AddEndpoint<F, S, O>;
65
+ }
66
+ export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends RestInstanceBase, F extends FetchFunction> = RestEndpointOptions<OptionsToFunction<O, E, F>, 'schema' extends keyof O ? Extract<O['schema'], Schema | undefined> : E['schema']> & Partial<Omit<E, KeyofRestEndpoint | keyof PartialRestGenerics>>;
67
+ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
68
+ body?: any;
69
+ }, F extends FetchFunction> = 'path' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, 'body' extends keyof O ? O['body'] : E['body'], 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
70
+ path: Exclude<O['path'], undefined>;
71
+ body: 'body' extends keyof O ? O['body'] : E['body'];
72
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
73
+ }> : 'body' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, O['body'], 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
74
+ path: E['path'];
75
+ body: O['body'];
76
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
77
+ }> : 'searchParams' extends keyof O ? RestType<O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, E['body'], 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
78
+ path: E['path'];
79
+ body: E['body'];
80
+ searchParams: O['searchParams'];
81
+ }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
82
+ path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
83
+ body: 'body' extends keyof O ? O['body'] : E['body'];
84
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
85
+ }>;
86
+ export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase> = OptionsToRestEndpoint<O, E, RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
87
+ export interface PartialRestGenerics {
88
+ readonly path?: string;
89
+ readonly schema?: Schema | undefined;
90
+ readonly method?: string;
91
+ /** Only used for types */
92
+ readonly body?: any;
93
+ /** Only used for types */
94
+ readonly searchParams?: any;
95
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
96
+ process?(value: any, ...args: any): any;
97
+ }
98
+ export interface RestGenerics extends PartialRestGenerics {
99
+ readonly path: string;
100
+ }
101
+ export type PaginationEndpoint<E extends RestInstanceBase, A extends any[]> = RestInstance<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
102
+ searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
103
+ }>;
104
+ export type PaginationFieldEndpoint<E extends RestInstanceBase, C extends string> = RestInstance<ParamFetchNoBody<{
105
+ [K in C]: string | number | boolean;
106
+ } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
107
+ searchParams: {
108
+ [K in C]: string | number | boolean;
109
+ } & E['searchParams'];
110
+ }>;
111
+ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
112
+ path: string;
113
+ body?: any;
114
+ searchParams?: any;
115
+ } = {
116
+ path: string;
117
+ }> = RestInstanceBase<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, any, ReturnType<F>>, ExtractCollection<S>, true, Omit<O, 'body'>> & {
118
+ method: 'POST';
119
+ };
120
+ export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O['body'] : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? Record<string, unknown> | FormData : undefined;
121
+ type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
122
+ body: any;
123
+ } : O & {
124
+ body: undefined;
125
+ };
126
+ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
127
+ urlPrefix?: string;
128
+ requestInit?: RequestInit;
129
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
130
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
131
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
132
+ parseResponse?(response: Response): Promise<any>;
133
+ sideEffect?: true | undefined;
134
+ name?: string;
135
+ signal?: AbortSignal;
136
+ fetch?: F;
137
+ key?(...args: Parameters<F>): string;
138
+ url?(...args: Parameters<F>): string;
139
+ update?: EndpointUpdateFunction<F, S>;
140
+ }
141
+ export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestEndpointOptions<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, BodyDefault<O>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
142
+ export interface RestEndpointConstructor {
143
+ new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O>): RestInstance<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, BodyDefault<O>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, 'schema' extends keyof O ? O['schema'] : undefined, MethodToSide<O['method']>, OptionsBodyDefault<O>>;
144
+ readonly prototype: RestInstanceBase;
145
+ }
146
+ /** Simplifies endpoint definitions that follow REST patterns
147
+ *
148
+ * @see https://resthooks.io/rest/api/RestEndpoint
149
+ */
150
+ export declare let RestEndpoint: RestEndpointConstructor;
151
+ export default RestEndpoint;
152
+ export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
153
+ /** RestEndpoint types simplified */
154
+ export type RestType<UrlParams = any, Body = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
155
+ path: string;
156
+ body?: any;
157
+ searchParams?: any;
158
+ } = {
159
+ path: string;
160
+ }> = IfTypeScriptLooseNull<RestInstance<keyof UrlParams extends never ? (this: EndpointInstanceInterface, body?: Body) => Promise<R> : {} extends UrlParams ? ((this: EndpointInstanceInterface, body?: Body) => Promise<R>) | ((this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>) : (this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>, S, M, O>, Body extends {} ? RestTypeWithBody<UrlParams, S, M, Body, R, O> : RestTypeNoBody<UrlParams, S, M, R, O>>;
161
+ export type RestTypeWithBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, Body = any, R = any /*Denormalize<S>*/, O extends {
162
+ path: string;
163
+ body?: any;
164
+ searchParams?: any;
165
+ } = {
166
+ path: string;
167
+ body: any;
168
+ }> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
169
+ export type RestTypeNoBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any /*Denormalize<S>*/, O extends {
170
+ path: string;
171
+ body?: undefined;
172
+ searchParams?: any;
173
+ } = {
174
+ path: string;
175
+ body: undefined;
176
+ }> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
177
+ /** Simple parameters, and body fetch functions */
178
+ export type RestFetch<UrlParams,
179
+ // eslint-disable-next-line @typescript-eslint/ban-types
180
+ Body = {}, Resolve = any> = IfTypeScriptLooseNull<ParamFetchNoBody<UrlParams, Resolve> | ParamFetchWithBody<UrlParams, Body, Resolve>, Body extends {} ? ParamFetchWithBody<UrlParams, Body, Resolve> : ParamFetchNoBody<UrlParams, Resolve>>;
181
+ export type ParamFetchWithBody<P, B = {}, R = any> =
182
+ // we must always allow undefined in a union and give it a type without params
183
+ P extends undefined ? (this: EndpointInstanceInterface, body: B) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface, body: B) => Promise<R> : ((this: EndpointInstanceInterface, params: P, body: B) => Promise<R>) | ((this: EndpointInstanceInterface, body: B) => Promise<R>) : (this: EndpointInstanceInterface, params: P, body: B) => Promise<R>;
184
+ export type ParamFetchNoBody<P, R = any> =
185
+ // we must always allow undefined in a union and give it a type without params
186
+ P extends undefined ? (this: EndpointInstanceInterface) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface) => Promise<R> : ((this: EndpointInstanceInterface, params: P) => Promise<R>) | ((this: EndpointInstanceInterface) => Promise<R>) : (this: EndpointInstanceInterface, params: P) => Promise<R>;
187
+ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
188
+ export type KeyofRestEndpoint = keyof RestInstance;
189
+ export type FromFallBack<K extends keyof E, O, E> = K extends keyof O ? O[K] : E[K];
190
+ export type FetchMutate<A extends readonly any[] = [
191
+ any,
192
+ {}
193
+ ]
194
+ // eslint-disable-next-line @typescript-eslint/ban-types
195
+ | [
196
+ {}
197
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
198
+ export type FetchGet<A extends readonly any[] = [
199
+ any
200
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
201
+ export type Defaults<O, D> = {
202
+ [K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
203
+ };
204
+ export type GetEndpoint<O extends {
205
+ readonly path: string;
206
+ readonly schema: Schema;
207
+ /** Only used for types */
208
+ readonly searchParams?: any;
209
+ } = {
210
+ path: string;
211
+ schema: Schema;
212
+ }> = RestTypeNoBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], undefined, any, O & {
213
+ body: undefined;
214
+ }>;
215
+ export type MutateEndpoint<O extends {
216
+ readonly path: string;
217
+ readonly schema: Schema;
218
+ /** Only used for types */
219
+ readonly searchParams?: any;
220
+ /** Only used for types */
221
+ readonly body?: any;
222
+ } = {
223
+ path: string;
224
+ body: any;
225
+ schema: Schema;
226
+ }> = RestTypeWithBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], true, O['body'], any, O>;
@@ -0,0 +1,94 @@
1
+ import { schema } from '@data-client/endpoint';
2
+ import type { Denormalize } from '@data-client/endpoint';
3
+ import { ResourceGenerics, ResourceOptions } from './resourceTypes.js';
4
+ import { GetEndpoint, MutateEndpoint, RestTypeNoBody } from './RestEndpoint.js';
5
+ import { PathArgs, ShortenPath } from '../pathTypes.js';
6
+ /** Creates collection of Endpoints for common operations on a given data/schema.
7
+ *
8
+ * @see https://resthooks.io/rest/api/createResource
9
+ */
10
+ export default function createResource<O extends ResourceGenerics>({ path, schema, Endpoint, optimistic, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
11
+ export interface Resource<O extends ResourceGenerics = {
12
+ path: string;
13
+ schema: any;
14
+ }> {
15
+ /** Get a singular item
16
+ *
17
+ * @see https://resthooks.io/rest/api/createResource#get
18
+ */
19
+ get: GetEndpoint<{
20
+ path: O['path'];
21
+ schema: O['schema'];
22
+ }>;
23
+ /** Get a list of item
24
+ *
25
+ * @see https://resthooks.io/rest/api/createResource#getlist
26
+ */
27
+ getList: 'searchParams' extends keyof O ? GetEndpoint<{
28
+ path: ShortenPath<O['path']>;
29
+ schema: schema.Collection<[
30
+ O['schema']
31
+ ]>;
32
+ searchParams: O['searchParams'];
33
+ }> : GetEndpoint<{
34
+ path: ShortenPath<O['path']>;
35
+ schema: schema.Collection<[
36
+ O['schema']
37
+ ]>;
38
+ searchParams: Record<string, number | string | boolean> | undefined;
39
+ }>;
40
+ /** Create a new item (POST)
41
+ *
42
+ * @see https://resthooks.io/rest/api/createResource#create
43
+ */
44
+ create: 'searchParams' extends keyof O ? MutateEndpoint<{
45
+ path: ShortenPath<O['path']>;
46
+ schema: schema.Collection<[
47
+ O['schema']
48
+ ]>['push'];
49
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
50
+ searchParams: O['searchParams'];
51
+ }> : MutateEndpoint<{
52
+ path: ShortenPath<O['path']>;
53
+ schema: schema.Collection<[
54
+ O['schema']
55
+ ]>['push'];
56
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
57
+ }>;
58
+ /** Update an item (PUT)
59
+ *
60
+ * @see https://resthooks.io/rest/api/createResource#update
61
+ */
62
+ update: 'body' extends keyof O ? MutateEndpoint<{
63
+ path: O['path'];
64
+ body: O['body'];
65
+ schema: O['schema'];
66
+ }> : MutateEndpoint<{
67
+ path: O['path'];
68
+ body: Partial<Denormalize<O['schema']>>;
69
+ schema: O['schema'];
70
+ }>;
71
+ /** Update an item (PATCH)
72
+ *
73
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
74
+ */
75
+ partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
76
+ path: O['path'];
77
+ body: Partial<O['body']>;
78
+ schema: O['schema'];
79
+ }> : MutateEndpoint<{
80
+ path: O['path'];
81
+ body: Partial<Denormalize<O['schema']>>;
82
+ schema: O['schema'];
83
+ }>;
84
+ /** Delete an item (DELETE)
85
+ *
86
+ * @see https://resthooks.io/rest/api/createResource#delete
87
+ */
88
+ delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
89
+ process: any;
90
+ } ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
91
+ path: O['path'];
92
+ }>;
93
+ }
94
+ //# sourceMappingURL=createResource.d.ts.map
@@ -0,0 +1,6 @@
1
+ export * from './RestEndpoint.js';
2
+ export { default as RestEndpoint } from './RestEndpoint.js';
3
+ export { default as createResource } from './createResource.js';
4
+ export type { Resource } from './createResource.js';
5
+ export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,31 @@
1
+ import type { Schema } from '@data-client/endpoint';
2
+ import RestEndpoint from './RestEndpoint.js';
3
+ export interface ResourceGenerics {
4
+ readonly path: string;
5
+ readonly schema: Schema;
6
+ /** Only used for types */
7
+ readonly body?: any;
8
+ /** Only used for types */
9
+ readonly searchParams?: any;
10
+ }
11
+ export interface ResourceOptions {
12
+ Endpoint?: typeof RestEndpoint;
13
+ urlPrefix?: string;
14
+ requestInit?: RequestInit;
15
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
16
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
17
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
18
+ parseResponse?(response: Response): Promise<any>;
19
+ /** Default data expiry length, will fall back to NetworkManager default if not defined */
20
+ readonly dataExpiryLength?: number;
21
+ /** Default error expiry length, will fall back to NetworkManager default if not defined */
22
+ readonly errorExpiryLength?: number;
23
+ /** Poll with at least this frequency in miliseconds */
24
+ readonly pollFrequency?: number;
25
+ /** Marks cached resources as invalid if they are stale */
26
+ readonly invalidIfStale?: boolean;
27
+ /** Determines whether to throw or fallback to */
28
+ errorPolicy?(error: any): 'hard' | 'soft' | undefined;
29
+ optimistic?: boolean;
30
+ }
31
+ //# sourceMappingURL=resourceTypes.d.ts.map
@@ -0,0 +1,3 @@
1
+ export declare function paginatedMerge(existing: any[], incoming: any[]): any[];
2
+ export declare const paginatedFilter: <C extends (...args: readonly any[]) => readonly any[]>(removeCursor: C) => (...args: Parameters<C>) => (collectionKey: Record<string, string>) => boolean;
3
+ //# sourceMappingURL=paginatedCollections.d.ts.map
@@ -0,0 +1,10 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ export default function paginationUpdate<E extends {
3
+ schema: Schema;
4
+ key: any;
5
+ } & ((...args: any) => Promise<any>), A extends any[]>(endpoint: E, removeCursor: (...args: A) => readonly [
6
+ ...Parameters<typeof endpoint>
7
+ ]): (newPage: any, ...args: A) => {
8
+ [x: number]: (existing: any) => any;
9
+ };
10
+ //# sourceMappingURL=paginationUpdate.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default function paramsToString(searchParams: Readonly<Record<string, string | number | boolean>>): string;
2
+ //# sourceMappingURL=paramsToString.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default function paramsToString(searchParams: Readonly<Record<string, string | number>>): string;
2
+ //# sourceMappingURL=paramsToString.native.d.ts.map
@@ -0,0 +1,20 @@
1
+ /** Computes the union of keys for a path string */
2
+ export declare type PathKeys<S extends string> = string;
3
+
4
+ /** Parameters for a given path */
5
+ export declare type PathArgs<S extends string> = PathKeys<S> extends never
6
+ ? unknown
7
+ : KeysToArgs<PathKeys<S>>;
8
+
9
+ export declare type KeysToArgs<Key extends string> = {
10
+ [K in Key]?: string | number;
11
+ };
12
+ export declare type PathArgsAndSearch<S extends string> =
13
+ PathKeys<S> extends never
14
+ ? Record<string, number | string> | undefined
15
+ : {
16
+ [K in PathKeys<S>]: string | number;
17
+ } & Record<string, number | string>;
18
+
19
+ /** Removes the last :token */
20
+ export declare type ShortenPath<S extends string> = S;