@data-client/rest 0.3.1 → 0.4.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 (53) hide show
  1. package/legacy/OptionsToFunction.js +1 -1
  2. package/legacy/createResource.js +1 -1
  3. package/legacy/extractCollection.js +1 -1
  4. package/legacy/extractObject.js +2 -0
  5. package/lib/OptionsToFunction.d.ts +8 -3
  6. package/lib/OptionsToFunction.d.ts.map +1 -1
  7. package/lib/OptionsToFunction.js +1 -1
  8. package/lib/RestEndpoint.d.ts +91 -31
  9. package/lib/createResource.d.ts +2 -0
  10. package/lib/createResource.d.ts.map +1 -1
  11. package/lib/createResource.js +1 -1
  12. package/lib/extractCollection.d.ts +7 -4
  13. package/lib/extractCollection.d.ts.map +1 -1
  14. package/lib/extractCollection.js +1 -1
  15. package/lib/extractObject.d.ts +6 -0
  16. package/lib/extractObject.d.ts.map +1 -0
  17. package/lib/extractObject.js +2 -0
  18. package/package.json +17 -5
  19. package/src/OptionsToFunction.ts +23 -9
  20. package/src/RestEndpoint.d.ts +91 -31
  21. package/src/__tests__/RestEndpoint.ts +10 -1
  22. package/src/__tests__/types.test.ts +6 -2
  23. package/src/createResource.ts +6 -0
  24. package/src/extractCollection.ts +16 -14
  25. package/src/extractObject.ts +7 -0
  26. package/ts4.0/OptionsToFunction.d.ts +7 -4
  27. package/ts4.0/RestEndpoint.d.ts +59 -22
  28. package/ts4.0/createResource.d.ts +2 -0
  29. package/ts4.0/deleteme.d.ts +0 -0
  30. package/ts4.0/extractCollection.d.ts +7 -4
  31. package/ts4.0/extractObject.d.ts +1 -0
  32. package/ts4.1/NetworkError.d.ts +11 -0
  33. package/ts4.1/OptionsToFunction.d.ts +12 -0
  34. package/ts4.1/RestEndpoint.d.ts +263 -0
  35. package/ts4.1/RestEndpointTypeHelp.d.ts +10 -0
  36. package/ts4.1/RestHelpers.d.ts +7 -0
  37. package/ts4.1/createResource.d.ts +113 -0
  38. package/ts4.1/deleteme.d.ts +0 -0
  39. package/ts4.1/extractCollection.d.ts +14 -0
  40. package/ts4.1/extractObject.d.ts +6 -0
  41. package/ts4.1/getArrayPath.d.ts +3 -0
  42. package/ts4.1/hookifyResource.d.ts +14 -0
  43. package/ts4.1/index.d.ts +14 -0
  44. package/ts4.1/mapCollection.d.ts +16 -0
  45. package/ts4.1/next/index.d.ts +6 -0
  46. package/ts4.1/paginatedCollections.d.ts +3 -0
  47. package/ts4.1/paginationUpdate.d.ts +10 -0
  48. package/ts4.1/paramsToString.d.ts +2 -0
  49. package/ts4.1/paramsToString.native.d.ts +2 -0
  50. package/ts4.1/pathTypes.d.ts +21 -0
  51. package/ts4.1/resourceTypes.d.ts +41 -0
  52. package/ts4.1/test-fixtures.d.ts +141 -0
  53. package/ts4.1/utiltypes.d.ts +21 -0
@@ -0,0 +1,113 @@
1
+ import { schema } from '@data-client/endpoint';
2
+ import type { Denormalize } from '@data-client/endpoint';
3
+ import { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
4
+ import { ResourceGenerics, ResourceOptions } from './resourceTypes.js';
5
+ import { GetEndpoint, MutateEndpoint, PaginationFieldEndpoint, RestTypeNoBody } from './RestEndpoint.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, paginationField, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
11
+ export interface Resource<O extends ResourceGenerics = {
12
+ path: ResourcePath;
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
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
33
+ searchParams: O['searchParams'];
34
+ }> : GetEndpoint<{
35
+ path: ShortenPath<O['path']>;
36
+ schema: schema.Collection<[
37
+ O['schema']
38
+ ]>;
39
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
40
+ searchParams: Record<string, number | string | boolean> | undefined;
41
+ }>;
42
+ /** Get a list of item
43
+ *
44
+ * @see https://dataclient.io/rest/api/createResource#getNextPage
45
+ */
46
+ getNextPage: 'paginationField' extends keyof O ? PaginationFieldEndpoint<'searchParams' extends keyof O ? GetEndpoint<{
47
+ path: ShortenPath<O['path']>;
48
+ schema: schema.Collection<[
49
+ O['schema']
50
+ ]>;
51
+ searchParams: O['searchParams'];
52
+ }> : GetEndpoint<{
53
+ path: ShortenPath<O['path']>;
54
+ schema: schema.Collection<[
55
+ O['schema']
56
+ ]>;
57
+ searchParams: Record<string, number | string | boolean> | undefined;
58
+ }>, Exclude<O['paginationField'], undefined>> : undefined;
59
+ /** Create a new item (POST)
60
+ *
61
+ * @see https://resthooks.io/rest/api/createResource#create
62
+ */
63
+ create: 'searchParams' extends keyof O ? MutateEndpoint<{
64
+ path: ShortenPath<O['path']>;
65
+ schema: schema.Collection<[
66
+ O['schema']
67
+ ]>['push'];
68
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
69
+ searchParams: O['searchParams'];
70
+ }> : MutateEndpoint<{
71
+ path: ShortenPath<O['path']>;
72
+ schema: schema.Collection<[
73
+ O['schema']
74
+ ]>['push'];
75
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
76
+ }>;
77
+ /** Update an item (PUT)
78
+ *
79
+ * @see https://resthooks.io/rest/api/createResource#update
80
+ */
81
+ update: 'body' extends keyof O ? MutateEndpoint<{
82
+ path: O['path'];
83
+ body: O['body'];
84
+ schema: O['schema'];
85
+ }> : MutateEndpoint<{
86
+ path: O['path'];
87
+ body: Partial<Denormalize<O['schema']>>;
88
+ schema: O['schema'];
89
+ }>;
90
+ /** Update an item (PATCH)
91
+ *
92
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
93
+ */
94
+ partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
95
+ path: O['path'];
96
+ body: Partial<O['body']>;
97
+ schema: O['schema'];
98
+ }> : MutateEndpoint<{
99
+ path: O['path'];
100
+ body: Partial<Denormalize<O['schema']>>;
101
+ schema: O['schema'];
102
+ }>;
103
+ /** Delete an item (DELETE)
104
+ *
105
+ * @see https://resthooks.io/rest/api/createResource#delete
106
+ */
107
+ delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
108
+ process: any;
109
+ } ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
110
+ path: O['path'];
111
+ }>;
112
+ }
113
+ //# sourceMappingURL=createResource.d.ts.map
File without changes
@@ -0,0 +1,14 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+ import { ExtractObject } from './extractObject.js';
3
+ export default function extractCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): ExtractCollection<S> | undefined;
4
+ export type ExtractCollection<S extends Schema | undefined> = S extends {
5
+ push: any;
6
+ unshift: any;
7
+ assign: any;
8
+ schema: Schema;
9
+ } ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
10
+ [K: string]: any;
11
+ }> ? never : S extends {
12
+ [K: string]: Schema;
13
+ } ? ExtractObject<S> : never;
14
+ //# sourceMappingURL=extractCollection.d.ts.map
@@ -0,0 +1,6 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ import { ExtractCollection } from './extractCollection.js';
3
+ export type ExtractObject<S extends Record<string, any>> = {
4
+ [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
5
+ }[keyof S];
6
+ //# sourceMappingURL=extractObject.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,14 @@
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
+ * @see https://resthooks.io/rest/api/hookifyResource
9
+ */
10
+ export default function hookifyResource<R extends {}>(resource: R, useRequestInit: () => RequestInit): HookResource<R>;
11
+ export type HookResource<R extends {}> = {
12
+ [K in Extract<keyof R, string> as `use${Capitalize<K>}`]: () => R[K];
13
+ };
14
+ //# sourceMappingURL=hookifyResource.d.ts.map
@@ -0,0 +1,14 @@
1
+ export { default as RestEndpoint } from './RestEndpoint.js';
2
+ export type { GetEndpoint, MutateEndpoint, 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 type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
6
+ export { default as hookifyResource } from './hookifyResource.js';
7
+ export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
8
+ export { default as NetworkError } from './NetworkError.js';
9
+ export { default as paginationUpdate } from './paginationUpdate.js';
10
+ export type { OptionsToFunction } from './OptionsToFunction.js';
11
+ export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
12
+ export * from '@data-client/endpoint';
13
+ export * from './RestEndpoint.js';
14
+ //# 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,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,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,21 @@
1
+ type OnlyOptional<S extends string> = S extends `${infer K}?` ? K : never;
2
+ type OnlyRequired<S extends string> = S extends `${string}?` ? never : S;
3
+ /** Computes the union of keys for a path string */
4
+ export type PathKeys<S extends string> = string extends S ? string : S extends `${infer A}\\:${infer B}` ? PathKeys<A> | PathKeys<B> : S extends `${infer A}\\?${infer B}` ? PathKeys<A> | PathKeys<B> : PathSplits<S>;
5
+ type PathSplits<S extends string> = S extends `${string}:${infer K}${'/' | ',' | '%' | '&'}${infer R}` ? PathSplits<`:${K}`> | PathSplits<R> : S extends `${string}:${infer K}:${infer R}` ? PathSplits<`:${K}`> | PathSplits<`:${R}`> : S extends `${string}:${infer K}` ? K : never;
6
+ /** Parameters for a given path */
7
+ export type PathArgs<S extends string> = PathKeys<S> extends never ? unknown : KeysToArgs<PathKeys<S>>;
8
+ export type KeysToArgs<Key extends string> = {
9
+ [K in Key as OnlyOptional<K>]?: string | number;
10
+ } & (OnlyRequired<Key> extends never ? unknown : {
11
+ [K in Key as OnlyRequired<K>]: string | number;
12
+ });
13
+ export type PathArgsAndSearch<S extends string> = OnlyRequired<PathKeys<S>> extends never ? Record<string, number | string | boolean> | undefined : {
14
+ [K in PathKeys<S> as OnlyRequired<K>]: string | number;
15
+ } & Record<string, number | string>;
16
+ /** Removes the last :token */
17
+ export type ShortenPath<S extends string> = string extends S ? string : S extends `${infer B}:${infer R}` ? TrimColon<`${B}:${ShortenPath<R>}`> : '';
18
+ type TrimColon<S extends string> = string extends S ? string : S extends `${infer R}:` ? R : S;
19
+ export type ResourcePath = string;
20
+ export {};
21
+ //# sourceMappingURL=pathTypes.d.ts.map
@@ -0,0 +1,41 @@
1
+ import type { Schema } from '@data-client/endpoint';
2
+ import type { ResourcePath } from './pathTypes.js';
3
+ import RestEndpoint from './RestEndpoint.js';
4
+ export interface ResourceGenerics {
5
+ /** @see https://resthooks.io/rest/api/createResource#path */
6
+ readonly path: ResourcePath;
7
+ /** @see https://resthooks.io/rest/api/createResource#schema */
8
+ readonly schema: Schema;
9
+ /** @see https://resthooks.io/rest/api/createResource#paginationfield */
10
+ readonly paginationField?: string;
11
+ /** Only used for types */
12
+ /** @see https://dataclient.io/rest/api/createResource#body */
13
+ readonly body?: any;
14
+ /** Only used for types */
15
+ /** @see https://resthooks.io/rest/api/createResource#searchParams */
16
+ readonly searchParams?: any;
17
+ }
18
+ export interface ResourceOptions {
19
+ /** @see https://resthooks.io/rest/api/createResource#endpoint */
20
+ Endpoint?: typeof RestEndpoint;
21
+ /** @see https://resthooks.io/rest/api/createResource#optimistic */
22
+ optimistic?: boolean;
23
+ /** @see https://resthooks.io/rest/api/createResource#urlprefix */
24
+ urlPrefix?: string;
25
+ requestInit?: RequestInit;
26
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
27
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
28
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
29
+ parseResponse?(response: Response): Promise<any>;
30
+ /** Default data expiry length, will fall back to NetworkManager default if not defined */
31
+ readonly dataExpiryLength?: number;
32
+ /** Default error expiry length, will fall back to NetworkManager default if not defined */
33
+ readonly errorExpiryLength?: number;
34
+ /** Poll with at least this frequency in miliseconds */
35
+ readonly pollFrequency?: number;
36
+ /** Marks cached resources as invalid if they are stale */
37
+ readonly invalidIfStale?: boolean;
38
+ /** Determines whether to throw or fallback to */
39
+ errorPolicy?(error: any): 'hard' | 'soft' | undefined;
40
+ }
41
+ //# sourceMappingURL=resourceTypes.d.ts.map
@@ -0,0 +1,141 @@
1
+ export declare const payload: {
2
+ id: number;
3
+ title: string;
4
+ content: string;
5
+ tags: string[];
6
+ };
7
+ export declare const createPayload: {
8
+ id: number;
9
+ title: string;
10
+ content: string;
11
+ tags: string[];
12
+ };
13
+ export declare const articlesPages: {
14
+ prevPage: string;
15
+ nextPage: string;
16
+ results: ({
17
+ id: number;
18
+ title: string;
19
+ content: string;
20
+ tags: string[];
21
+ } | {
22
+ id: number;
23
+ title: string;
24
+ content: string;
25
+ tags?: undefined;
26
+ })[];
27
+ };
28
+ export declare const users: {
29
+ id: number;
30
+ username: string;
31
+ email: string;
32
+ isAdmin: boolean;
33
+ }[];
34
+ export declare const nested: ({
35
+ id: number;
36
+ title: string;
37
+ content: string;
38
+ tags: string[];
39
+ author: {
40
+ id: number;
41
+ username: string;
42
+ email?: undefined;
43
+ };
44
+ } | {
45
+ id: number;
46
+ title: string;
47
+ content: string;
48
+ author: {
49
+ id: number;
50
+ username: string;
51
+ email: string;
52
+ };
53
+ tags?: undefined;
54
+ })[];
55
+ export declare const moreNested: ({
56
+ id: number;
57
+ title: string;
58
+ content: string;
59
+ tags: string[];
60
+ author: {
61
+ id: number;
62
+ username: string;
63
+ email?: undefined;
64
+ };
65
+ } | {
66
+ id: number;
67
+ title: string;
68
+ content: string;
69
+ author: {
70
+ id: number;
71
+ username: string;
72
+ email: string;
73
+ };
74
+ tags?: undefined;
75
+ })[];
76
+ export declare const valuesFixture: {
77
+ first: {
78
+ id: number;
79
+ title: string;
80
+ content: string;
81
+ tags: never[];
82
+ };
83
+ second: {
84
+ id: number;
85
+ name: string;
86
+ content: string;
87
+ tags: never[];
88
+ };
89
+ };
90
+ export declare const paginatedFirstPage: {
91
+ nextPage: string;
92
+ data: {
93
+ results: ({
94
+ id: number;
95
+ title: string;
96
+ content: string;
97
+ tags: string[];
98
+ author: {
99
+ id: number;
100
+ username: string;
101
+ email?: undefined;
102
+ };
103
+ } | {
104
+ id: number;
105
+ title: string;
106
+ content: string;
107
+ author: {
108
+ id: number;
109
+ username: string;
110
+ email: string;
111
+ };
112
+ tags?: undefined;
113
+ })[];
114
+ };
115
+ };
116
+ export declare const paginatedSecondPage: {
117
+ data: {
118
+ results: ({
119
+ id: number;
120
+ title: string;
121
+ content: string;
122
+ tags: string[];
123
+ author: {
124
+ id: number;
125
+ username: string;
126
+ email?: undefined;
127
+ };
128
+ } | {
129
+ id: number;
130
+ title: string;
131
+ content: string;
132
+ author: {
133
+ id: number;
134
+ username: string;
135
+ email: string;
136
+ };
137
+ tags?: undefined;
138
+ })[];
139
+ };
140
+ };
141
+ //# sourceMappingURL=test-fixtures.d.ts.map
@@ -0,0 +1,21 @@
1
+ /** Extracts only the keys that will be required
2
+ *
3
+ * Removes optional, as well as unbounded (aka 'string')
4
+ *
5
+ * @example
6
+ ```
7
+ RequiredKeys<{
8
+ opt?: string;
9
+ bob: string;
10
+ alice: number;
11
+ [k: string]: string | number | undefined;
12
+ }> // = 'bob' | 'alice'
13
+ ```
14
+ */
15
+ export type RequiredKeys<T> = Values<OnlyRequired<T>>;
16
+ type OnlyRequired<T> = {
17
+ [K in keyof T as string extends K ? never : K]-?: {} extends Pick<T, K> ? never : K;
18
+ };
19
+ type Values<T> = T[Exclude<keyof T, number>];
20
+ export {};
21
+ //# sourceMappingURL=utiltypes.d.ts.map