@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.
- package/LICENSE +201 -0
- package/README.md +110 -0
- package/dist/index.js +561 -0
- package/dist/index.umd.min.js +1 -0
- package/dist/next.js +520 -0
- package/dist/package.json +1 -0
- package/legacy/NetworkError.js +13 -0
- package/legacy/OptionsToFunction.js +2 -0
- package/legacy/RestEndpoint.js +225 -0
- package/legacy/RestEndpointTypeHelp.js +2 -0
- package/legacy/RestHelpers.js +33 -0
- package/legacy/createResource.js +88 -0
- package/legacy/extractCollection.js +14 -0
- package/legacy/getArrayPath.js +19 -0
- package/legacy/hookifyResource.js +24 -0
- package/legacy/index.js +10 -0
- package/legacy/mapCollection.js +14 -0
- package/legacy/next/OptionsToFunction.js +2 -0
- package/legacy/next/RestEndpoint.js +225 -0
- package/legacy/next/createResource.js +86 -0
- package/legacy/next/index.js +4 -0
- package/legacy/next/resourceTypes.js +2 -0
- package/legacy/paginatedCollections.js +29 -0
- package/legacy/paginationUpdate.js +36 -0
- package/legacy/paramsToString.js +6 -0
- package/legacy/paramsToString.native.js +9 -0
- package/legacy/pathTypes.js +2 -0
- package/legacy/test-fixtures.js +111 -0
- package/legacy/utiltypes.js +2 -0
- package/lib/NetworkError.d.ts +11 -0
- package/lib/NetworkError.d.ts.map +1 -0
- package/lib/NetworkError.js +13 -0
- package/lib/OptionsToFunction.d.ts +7 -0
- package/lib/OptionsToFunction.d.ts.map +1 -0
- package/lib/OptionsToFunction.js +2 -0
- package/lib/RestEndpoint.d.ts +548 -0
- package/lib/RestEndpoint.js +229 -0
- package/lib/RestEndpointTypeHelp.d.ts +10 -0
- package/lib/RestEndpointTypeHelp.d.ts.map +1 -0
- package/lib/RestEndpointTypeHelp.js +2 -0
- package/lib/RestHelpers.d.ts +7 -0
- package/lib/RestHelpers.d.ts.map +1 -0
- package/lib/RestHelpers.js +33 -0
- package/lib/createResource.d.ts +66 -0
- package/lib/createResource.d.ts.map +1 -0
- package/lib/createResource.js +104 -0
- package/lib/extractCollection.d.ts +11 -0
- package/lib/extractCollection.d.ts.map +1 -0
- package/lib/extractCollection.js +16 -0
- package/lib/getArrayPath.d.ts +3 -0
- package/lib/getArrayPath.d.ts.map +1 -0
- package/lib/getArrayPath.js +19 -0
- package/lib/hookifyResource.d.ts +14 -0
- package/lib/hookifyResource.d.ts.map +1 -0
- package/lib/hookifyResource.js +24 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +10 -0
- package/lib/mapCollection.d.ts +16 -0
- package/lib/mapCollection.d.ts.map +1 -0
- package/lib/mapCollection.js +16 -0
- package/lib/next/OptionsToFunction.d.ts +7 -0
- package/lib/next/OptionsToFunction.d.ts.map +1 -0
- package/lib/next/OptionsToFunction.js +2 -0
- package/lib/next/RestEndpoint.d.ts +542 -0
- package/lib/next/RestEndpoint.js +229 -0
- package/lib/next/createResource.d.ts +86 -0
- package/lib/next/createResource.d.ts.map +1 -0
- package/lib/next/createResource.js +100 -0
- package/lib/next/index.d.ts +6 -0
- package/lib/next/index.d.ts.map +1 -0
- package/lib/next/index.js +4 -0
- package/lib/next/resourceTypes.d.ts +31 -0
- package/lib/next/resourceTypes.d.ts.map +1 -0
- package/lib/next/resourceTypes.js +2 -0
- package/lib/paginatedCollections.d.ts +3 -0
- package/lib/paginatedCollections.d.ts.map +1 -0
- package/lib/paginatedCollections.js +29 -0
- package/lib/paginationUpdate.d.ts +8 -0
- package/lib/paginationUpdate.d.ts.map +1 -0
- package/lib/paginationUpdate.js +40 -0
- package/lib/paramsToString.d.ts +2 -0
- package/lib/paramsToString.d.ts.map +1 -0
- package/lib/paramsToString.js +6 -0
- package/lib/paramsToString.native.d.ts +2 -0
- package/lib/paramsToString.native.d.ts.map +1 -0
- package/lib/paramsToString.native.js +9 -0
- package/lib/pathTypes.d.ts +20 -0
- package/lib/pathTypes.d.ts.map +1 -0
- package/lib/pathTypes.js +2 -0
- package/lib/test-fixtures.d.ts +141 -0
- package/lib/test-fixtures.d.ts.map +1 -0
- package/lib/test-fixtures.js +111 -0
- package/lib/utiltypes.d.ts +21 -0
- package/lib/utiltypes.d.ts.map +1 -0
- package/lib/utiltypes.js +2 -0
- package/node.mjs +1 -0
- package/package.json +121 -0
- package/src/NetworkError.ts +17 -0
- package/src/OptionsToFunction.ts +42 -0
- package/src/RestEndpoint.d.ts +548 -0
- package/src/RestEndpoint.js +262 -0
- package/src/RestEndpointTypeHelp.ts +20 -0
- package/src/RestHelpers.ts +48 -0
- package/src/__tests__/Resource.test.ts +317 -0
- package/src/__tests__/RestEndpoint.ts +1304 -0
- package/src/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
- package/src/__tests__/__snapshots__/createResource.test.ts.snap +5 -0
- package/src/__tests__/createResource.test.ts +583 -0
- package/src/__tests__/getArrayPath.test.ts +30 -0
- package/src/__tests__/hookifyResource.ts +197 -0
- package/src/__tests__/types.test.ts +309 -0
- package/src/createResource.ts +172 -0
- package/src/extractCollection.ts +34 -0
- package/src/getArrayPath.ts +20 -0
- package/src/hookifyResource.ts +36 -0
- package/src/index.ts +42 -0
- package/src/mapCollection.ts +54 -0
- package/src/next/OptionsToFunction.ts +41 -0
- package/src/next/RestEndpoint.d.ts +542 -0
- package/src/next/RestEndpoint.js +262 -0
- package/src/next/__tests__/RestEndpoint.ts +1333 -0
- package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
- package/src/next/__tests__/types.test.ts +429 -0
- package/src/next/createResource.ts +194 -0
- package/src/next/index.ts +5 -0
- package/src/next/resourceTypes.ts +32 -0
- package/src/package.json +1 -0
- package/src/paginatedCollections.ts +32 -0
- package/src/paginationUpdate.ts +49 -0
- package/src/paramsToString.native.ts +10 -0
- package/src/paramsToString.ts +7 -0
- package/src/pathTypes.ts +58 -0
- package/src/test-fixtures.ts +128 -0
- package/src/utiltypes.ts +25 -0
- package/ts4.0/NetworkError.d.ts +11 -0
- package/ts4.0/OptionsToFunction.d.ts +16 -0
- package/ts4.0/RestEndpoint.d.ts +240 -0
- package/ts4.0/RestEndpointTypeHelp.d.ts +10 -0
- package/ts4.0/RestHelpers.d.ts +7 -0
- package/ts4.0/createResource.d.ts +66 -0
- package/ts4.0/extractCollection.d.ts +11 -0
- package/ts4.0/getArrayPath.d.ts +3 -0
- package/ts4.0/hookifyResource.d.ts +19 -0
- package/ts4.0/index.d.ts +12 -0
- package/ts4.0/mapCollection.d.ts +16 -0
- package/ts4.0/next/OptionsToFunction.d.ts +16 -0
- package/ts4.0/next/RestEndpoint.d.ts +226 -0
- package/ts4.0/next/createResource.d.ts +94 -0
- package/ts4.0/next/index.d.ts +6 -0
- package/ts4.0/next/resourceTypes.d.ts +31 -0
- package/ts4.0/paginatedCollections.d.ts +3 -0
- package/ts4.0/paginationUpdate.d.ts +10 -0
- package/ts4.0/paramsToString.d.ts +2 -0
- package/ts4.0/paramsToString.native.d.ts +2 -0
- package/ts4.0/pathTypes.d.ts +20 -0
- package/ts4.0/test-fixtures.d.ts +141 -0
- 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
|
+
>;
|