@data-client/rest 0.4.1 → 0.6.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/dist/index.js +44 -5
  2. package/dist/index.umd.min.js +1 -1
  3. package/dist/next.js +44 -5
  4. package/legacy/createResource.js +39 -8
  5. package/legacy/index.js +1 -1
  6. package/legacy/next/index.js +1 -1
  7. package/legacy/resourceExtendable.js +2 -0
  8. package/legacy/resourceExtensionTypes.js +2 -0
  9. package/legacy/resourceTypes.js +1 -1
  10. package/lib/RestEndpoint.d.ts +1 -1
  11. package/lib/createResource.d.ts +2 -96
  12. package/lib/createResource.d.ts.map +1 -1
  13. package/lib/createResource.js +45 -6
  14. package/lib/index.d.ts +1 -1
  15. package/lib/index.d.ts.map +1 -1
  16. package/lib/index.js +1 -1
  17. package/lib/next/index.d.ts +1 -1
  18. package/lib/next/index.d.ts.map +1 -1
  19. package/lib/next/index.js +1 -1
  20. package/lib/resourceExtendable.d.ts +25 -0
  21. package/lib/resourceExtendable.d.ts.map +1 -0
  22. package/lib/resourceExtendable.js +2 -0
  23. package/lib/resourceExtensionTypes.d.ts +33 -0
  24. package/lib/resourceExtensionTypes.d.ts.map +1 -0
  25. package/lib/resourceExtensionTypes.js +2 -0
  26. package/lib/resourceTypes.d.ts +94 -4
  27. package/lib/resourceTypes.d.ts.map +1 -1
  28. package/lib/resourceTypes.js +1 -1
  29. package/package.json +1 -1
  30. package/src/RestEndpoint.d.ts +1 -1
  31. package/src/__tests__/createResource.test.ts +490 -19
  32. package/src/createResource.ts +60 -127
  33. package/src/index.ts +1 -1
  34. package/src/next/index.ts +1 -1
  35. package/src/resourceExtendable.ts +95 -0
  36. package/src/resourceExtensionTypes.ts +109 -0
  37. package/src/resourceTypes.ts +119 -4
  38. package/ts4.0/RestEndpoint.d.ts +1 -1
  39. package/ts4.0/createResource.d.ts +2 -108
  40. package/ts4.0/index.d.ts +1 -1
  41. package/ts4.0/next/index.d.ts +1 -1
  42. package/ts4.0/resourceExtendable.d.ts +21 -0
  43. package/ts4.0/resourceExtensionTypes.d.ts +33 -0
  44. package/ts4.0/resourceTypes.d.ts +102 -4
  45. package/ts4.1/RestEndpoint.d.ts +1 -1
  46. package/ts4.1/createResource.d.ts +2 -108
  47. package/ts4.1/index.d.ts +1 -1
  48. package/ts4.1/next/index.d.ts +1 -1
  49. package/ts4.1/resourceExtendable.d.ts +21 -0
  50. package/ts4.1/resourceExtensionTypes.d.ts +33 -0
  51. package/ts4.1/resourceTypes.d.ts +102 -4
  52. package/ts4.0/deleteme.d.ts +0 -0
  53. package/ts4.1/deleteme.d.ts +0 -0
@@ -1,113 +1,7 @@
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';
1
+ import { ResourceGenerics, ResourceOptions, Resource } from './resourceTypes.js';
6
2
  /** Creates collection of Endpoints for common operations on a given data/schema.
7
3
  *
8
4
  * @see https://resthooks.io/rest/api/createResource
9
5
  */
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
- }
6
+ export default function createResource<O extends ResourceGenerics>({ path, schema, Endpoint, optimistic, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
113
7
  //# sourceMappingURL=createResource.d.ts.map
package/ts4.0/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { default as RestEndpoint } from './RestEndpoint.js';
2
2
  export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
3
3
  export { default as createResource } from './createResource.js';
4
- export type { Resource } from './createResource.js';
4
+ export type { Resource } from './resourceTypes.js';
5
5
  export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
6
6
  export { default as hookifyResource } from './hookifyResource.js';
7
7
  export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
@@ -1,6 +1,6 @@
1
1
  export * from '../RestEndpoint.js';
2
2
  export { default as RestEndpoint } from '../RestEndpoint.js';
3
3
  export { default as createResource } from '../createResource.js';
4
- export type { Resource } from '../createResource.js';
4
+ export type { Resource } from '../resourceTypes.js';
5
5
  export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,21 @@
1
+ import type { EndpointInterface, EndpointToFunction } from '@data-client/endpoint';
2
+ import type { ResourcePath } from './pathTypes.js';
3
+ import type { ResourceExtension, ResourceEndpointExtensions, CustomResource, ExtendedResource } from './resourceExtensionTypes.js';
4
+ import type { ResourceGenerics, ResourceInterface } from './resourceTypes.js';
5
+ import type { PartialRestGenerics, RestEndpointExtendOptions, RestExtendedEndpoint, RestInstanceBase } from './RestEndpoint.js';
6
+ export interface Extendable<O extends ResourceGenerics = {
7
+ path: ResourcePath;
8
+ schema: any;
9
+ }> {
10
+ extend<R extends {
11
+ [K in ExtendKey]: RestInstanceBase;
12
+ }, ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R[ExtendKey], EndpointToFunction<R[ExtendKey]>> & ExtendOptions>): ResourceExtension<R, ExtendKey, ExtendOptions>;
13
+ extend<R extends {
14
+ get: RestInstanceBase;
15
+ }, ExtendKey extends string, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R['get'], EndpointToFunction<R['get']>> & ExtendOptions>): R & {
16
+ [key in ExtendKey]: RestExtendedEndpoint<ExtendOptions, R['get']>;
17
+ };
18
+ extend<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
19
+ extend<R extends ResourceInterface, T extends Record<string, EndpointInterface>>(this: R, extender: (baseResource: R) => T): ExtendedResource<R, T>;
20
+ }
21
+ //# sourceMappingURL=resourceExtendable.d.ts.map
@@ -0,0 +1,33 @@
1
+ import type { EndpointInterface, EndpointToFunction } from '@data-client/endpoint';
2
+ import { OptionsToFunction } from './OptionsToFunction.js';
3
+ import type { ResourcePath } from './pathTypes.js';
4
+ import { Extendable } from './resourceExtendable.js';
5
+ import { ResourceGenerics, ResourceInterface } from './resourceTypes.js';
6
+ import type { PartialRestGenerics, RestExtendedEndpoint, RestInstanceBase, RestEndpointOptions } from './RestEndpoint.js';
7
+ export type ResourceExtension<R extends {
8
+ [K in ExtendKey]: RestInstanceBase;
9
+ }, ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>, O extends PartialRestGenerics | {}> = {
10
+ [K in keyof R]: K extends ExtendKey ? RestExtendedEndpoint<O, R[K]> : R[K];
11
+ };
12
+ /** Resource with individual endpoints customized
13
+ *
14
+ */
15
+ export interface CustomResource<R extends ResourceInterface, O extends ResourceGenerics = {
16
+ path: ResourcePath;
17
+ schema: any;
18
+ }, Get extends PartialRestGenerics | {} = any, GetList extends PartialRestGenerics | {} = any, Update extends PartialRestGenerics | {} = any, PartialUpdate extends PartialRestGenerics | {} = any, Delete extends PartialRestGenerics | {} = any> extends Extendable<O> {
19
+ get: unknown extends Get ? R['get'] : RestExtendedEndpoint<Get, R['get']>;
20
+ getList: unknown extends GetList ? R['getList'] : RestExtendedEndpoint<GetList, R['getList']>;
21
+ update: unknown extends Update ? R['update'] : RestExtendedEndpoint<Update, R['update']>;
22
+ partialUpdate: unknown extends PartialUpdate ? R['partialUpdate'] : RestExtendedEndpoint<PartialUpdate, R['partialUpdate']>;
23
+ delete: unknown extends Delete ? R['delete'] : RestExtendedEndpoint<Delete, R['delete']>;
24
+ }
25
+ export type ExtendedResource<R extends ResourceInterface, T extends Record<string, EndpointInterface>> = Omit<R, keyof T> & T;
26
+ export interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any> {
27
+ readonly get?: RestEndpointOptions<unknown extends Get ? EndpointToFunction<R['get']> : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>, R['get']['schema']> & Readonly<Get>;
28
+ readonly getList?: RestEndpointOptions<unknown extends GetList ? EndpointToFunction<R['getList']> : OptionsToFunction<GetList, R['getList'], EndpointToFunction<R['getList']>>, R['getList']['schema']> & Readonly<GetList>;
29
+ readonly update?: RestEndpointOptions<unknown extends Update ? EndpointToFunction<R['update']> : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>, R['update']['schema']> & Readonly<Update>;
30
+ readonly partialUpdate?: RestEndpointOptions<unknown extends PartialUpdate ? EndpointToFunction<R['partialUpdate']> : OptionsToFunction<PartialUpdate, R['partialUpdate'], EndpointToFunction<R['partialUpdate']>>, R['partialUpdate']['schema']> & Readonly<PartialUpdate>;
31
+ readonly delete?: RestEndpointOptions<unknown extends Delete ? EndpointToFunction<R['delete']> : OptionsToFunction<Delete, R['delete'], EndpointToFunction<R['delete']>>, R['delete']['schema']> & Readonly<Delete>;
32
+ }
33
+ //# sourceMappingURL=resourceExtensionTypes.d.ts.map
@@ -1,13 +1,15 @@
1
+ import type { schema } from '@data-client/endpoint';
1
2
  import type { Schema } from '@data-client/endpoint';
2
- import type { ResourcePath } from './pathTypes.js';
3
- import RestEndpoint from './RestEndpoint.js';
3
+ import type { Denormalize } from '@data-client/endpoint';
4
+ import type { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
5
+ import { Extendable } from './resourceExtendable.js';
6
+ import RestEndpoint, { GetEndpoint, MutateEndpoint, RestInstanceBase, RestTypeNoBody } from './RestEndpoint.js';
7
+ /** The typed (generic) options for a Resource */
4
8
  export interface ResourceGenerics {
5
9
  /** @see https://resthooks.io/rest/api/createResource#path */
6
10
  readonly path: ResourcePath;
7
11
  /** @see https://resthooks.io/rest/api/createResource#schema */
8
12
  readonly schema: Schema;
9
- /** @see https://resthooks.io/rest/api/createResource#paginationfield */
10
- readonly paginationField?: string;
11
13
  /** Only used for types */
12
14
  /** @see https://dataclient.io/rest/api/createResource#body */
13
15
  readonly body?: any;
@@ -15,6 +17,7 @@ export interface ResourceGenerics {
15
17
  /** @see https://resthooks.io/rest/api/createResource#searchParams */
16
18
  readonly searchParams?: any;
17
19
  }
20
+ /** The untyped options for createResource() */
18
21
  export interface ResourceOptions {
19
22
  /** @see https://resthooks.io/rest/api/createResource#endpoint */
20
23
  Endpoint?: typeof RestEndpoint;
@@ -38,4 +41,99 @@ export interface ResourceOptions {
38
41
  /** Determines whether to throw or fallback to */
39
42
  errorPolicy?(error: any): 'hard' | 'soft' | undefined;
40
43
  }
44
+ /** Resources are a collection of methods for a given data model.
45
+ * @see https://resthooks.io/rest/api/createResource
46
+ */
47
+ export interface Resource<O extends ResourceGenerics = {
48
+ path: ResourcePath;
49
+ schema: any;
50
+ }> extends Extendable<O> {
51
+ /** Get a singular item
52
+ *
53
+ * @see https://resthooks.io/rest/api/createResource#get
54
+ */
55
+ get: GetEndpoint<{
56
+ path: O['path'];
57
+ schema: O['schema'];
58
+ }>;
59
+ /** Get a list of item
60
+ *
61
+ * @see https://resthooks.io/rest/api/createResource#getlist
62
+ */
63
+ getList: 'searchParams' extends keyof O ? GetEndpoint<{
64
+ path: ShortenPath<O['path']>;
65
+ schema: schema.Collection<[
66
+ O['schema']
67
+ ]>;
68
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
69
+ searchParams: O['searchParams'];
70
+ }> : GetEndpoint<{
71
+ path: ShortenPath<O['path']>;
72
+ schema: schema.Collection<[
73
+ O['schema']
74
+ ]>;
75
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
76
+ searchParams: Record<string, number | string | boolean> | undefined;
77
+ }>;
78
+ /** Create a new item (POST)
79
+ *
80
+ * @deprecated use Resource.getList.push instead
81
+ */
82
+ create: 'searchParams' extends keyof O ? MutateEndpoint<{
83
+ path: ShortenPath<O['path']>;
84
+ schema: schema.Collection<[
85
+ O['schema']
86
+ ]>['push'];
87
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
88
+ searchParams: O['searchParams'];
89
+ }> : MutateEndpoint<{
90
+ path: ShortenPath<O['path']>;
91
+ schema: schema.Collection<[
92
+ O['schema']
93
+ ]>['push'];
94
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
95
+ }>;
96
+ /** Update an item (PUT)
97
+ *
98
+ * @see https://resthooks.io/rest/api/createResource#update
99
+ */
100
+ update: 'body' extends keyof O ? MutateEndpoint<{
101
+ path: O['path'];
102
+ body: O['body'];
103
+ schema: O['schema'];
104
+ }> : MutateEndpoint<{
105
+ path: O['path'];
106
+ body: Partial<Denormalize<O['schema']>> | FormData;
107
+ schema: O['schema'];
108
+ }>;
109
+ /** Update an item (PATCH)
110
+ *
111
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
112
+ */
113
+ partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
114
+ path: O['path'];
115
+ body: Partial<O['body']>;
116
+ schema: O['schema'];
117
+ }> : MutateEndpoint<{
118
+ path: O['path'];
119
+ body: Partial<Denormalize<O['schema']>> | FormData;
120
+ schema: O['schema'];
121
+ }>;
122
+ /** Delete an item (DELETE)
123
+ *
124
+ * @see https://resthooks.io/rest/api/createResource#delete
125
+ */
126
+ delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
127
+ process: any;
128
+ } ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
129
+ path: O['path'];
130
+ }>;
131
+ }
132
+ export interface ResourceInterface {
133
+ get: RestInstanceBase;
134
+ getList: RestInstanceBase;
135
+ update: RestInstanceBase;
136
+ partialUpdate: RestInstanceBase;
137
+ delete: RestInstanceBase;
138
+ }
41
139
  //# sourceMappingURL=resourceTypes.d.ts.map
@@ -156,7 +156,7 @@ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schem
156
156
  } = {
157
157
  path: string;
158
158
  body: any;
159
- }> = 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>>, O['body'], ResolveType<F>>, S, true, O & {
159
+ }> = 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>>, O['body'], ResolveType<F>>, S, true, Omit<O, 'method'> & {
160
160
  method: 'POST';
161
161
  }>;
162
162
  type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
@@ -1,113 +1,7 @@
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';
1
+ import { ResourceGenerics, ResourceOptions, Resource } from './resourceTypes.js';
6
2
  /** Creates collection of Endpoints for common operations on a given data/schema.
7
3
  *
8
4
  * @see https://resthooks.io/rest/api/createResource
9
5
  */
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
- }
6
+ export default function createResource<O extends ResourceGenerics>({ path, schema, Endpoint, optimistic, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
113
7
  //# sourceMappingURL=createResource.d.ts.map
package/ts4.1/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { default as RestEndpoint } from './RestEndpoint.js';
2
2
  export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
3
3
  export { default as createResource } from './createResource.js';
4
- export type { Resource } from './createResource.js';
4
+ export type { Resource } from './resourceTypes.js';
5
5
  export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
6
6
  export { default as hookifyResource } from './hookifyResource.js';
7
7
  export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
@@ -1,6 +1,6 @@
1
1
  export * from '../RestEndpoint.js';
2
2
  export { default as RestEndpoint } from '../RestEndpoint.js';
3
3
  export { default as createResource } from '../createResource.js';
4
- export type { Resource } from '../createResource.js';
4
+ export type { Resource } from '../resourceTypes.js';
5
5
  export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,21 @@
1
+ import type { EndpointInterface, EndpointToFunction } from '@data-client/endpoint';
2
+ import type { ResourcePath } from './pathTypes.js';
3
+ import type { ResourceExtension, ResourceEndpointExtensions, CustomResource, ExtendedResource } from './resourceExtensionTypes.js';
4
+ import type { ResourceGenerics, ResourceInterface } from './resourceTypes.js';
5
+ import type { PartialRestGenerics, RestEndpointExtendOptions, RestExtendedEndpoint, RestInstanceBase } from './RestEndpoint.js';
6
+ export interface Extendable<O extends ResourceGenerics = {
7
+ path: ResourcePath;
8
+ schema: any;
9
+ }> {
10
+ extend<R extends {
11
+ [K in ExtendKey]: RestInstanceBase;
12
+ }, ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R[ExtendKey], EndpointToFunction<R[ExtendKey]>> & ExtendOptions>): ResourceExtension<R, ExtendKey, ExtendOptions>;
13
+ extend<R extends {
14
+ get: RestInstanceBase;
15
+ }, ExtendKey extends string, ExtendOptions extends PartialRestGenerics | {}>(this: R, key: ExtendKey, options: Readonly<RestEndpointExtendOptions<ExtendOptions, R['get'], EndpointToFunction<R['get']>> & ExtendOptions>): R & {
16
+ [key in ExtendKey]: RestExtendedEndpoint<ExtendOptions, R['get']>;
17
+ };
18
+ extend<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any>(this: R, options: ResourceEndpointExtensions<R, Get, GetList, Update, PartialUpdate, Delete>): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
19
+ extend<R extends ResourceInterface, T extends Record<string, EndpointInterface>>(this: R, extender: (baseResource: R) => T): ExtendedResource<R, T>;
20
+ }
21
+ //# sourceMappingURL=resourceExtendable.d.ts.map
@@ -0,0 +1,33 @@
1
+ import type { EndpointInterface, EndpointToFunction } from '@data-client/endpoint';
2
+ import { OptionsToFunction } from './OptionsToFunction.js';
3
+ import type { ResourcePath } from './pathTypes.js';
4
+ import { Extendable } from './resourceExtendable.js';
5
+ import { ResourceGenerics, ResourceInterface } from './resourceTypes.js';
6
+ import type { PartialRestGenerics, RestExtendedEndpoint, RestInstanceBase, RestEndpointOptions } from './RestEndpoint.js';
7
+ export type ResourceExtension<R extends {
8
+ [K in ExtendKey]: RestInstanceBase;
9
+ }, ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>, O extends PartialRestGenerics | {}> = {
10
+ [K in keyof R]: K extends ExtendKey ? RestExtendedEndpoint<O, R[K]> : R[K];
11
+ };
12
+ /** Resource with individual endpoints customized
13
+ *
14
+ */
15
+ export interface CustomResource<R extends ResourceInterface, O extends ResourceGenerics = {
16
+ path: ResourcePath;
17
+ schema: any;
18
+ }, Get extends PartialRestGenerics | {} = any, GetList extends PartialRestGenerics | {} = any, Update extends PartialRestGenerics | {} = any, PartialUpdate extends PartialRestGenerics | {} = any, Delete extends PartialRestGenerics | {} = any> extends Extendable<O> {
19
+ get: unknown extends Get ? R['get'] : RestExtendedEndpoint<Get, R['get']>;
20
+ getList: unknown extends GetList ? R['getList'] : RestExtendedEndpoint<GetList, R['getList']>;
21
+ update: unknown extends Update ? R['update'] : RestExtendedEndpoint<Update, R['update']>;
22
+ partialUpdate: unknown extends PartialUpdate ? R['partialUpdate'] : RestExtendedEndpoint<PartialUpdate, R['partialUpdate']>;
23
+ delete: unknown extends Delete ? R['delete'] : RestExtendedEndpoint<Delete, R['delete']>;
24
+ }
25
+ export type ExtendedResource<R extends ResourceInterface, T extends Record<string, EndpointInterface>> = Omit<R, keyof T> & T;
26
+ export interface ResourceEndpointExtensions<R extends ResourceInterface, Get extends PartialRestGenerics = any, GetList extends PartialRestGenerics = any, Update extends PartialRestGenerics = any, PartialUpdate extends PartialRestGenerics = any, Delete extends PartialRestGenerics = any> {
27
+ readonly get?: RestEndpointOptions<unknown extends Get ? EndpointToFunction<R['get']> : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>, R['get']['schema']> & Readonly<Get>;
28
+ readonly getList?: RestEndpointOptions<unknown extends GetList ? EndpointToFunction<R['getList']> : OptionsToFunction<GetList, R['getList'], EndpointToFunction<R['getList']>>, R['getList']['schema']> & Readonly<GetList>;
29
+ readonly update?: RestEndpointOptions<unknown extends Update ? EndpointToFunction<R['update']> : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>, R['update']['schema']> & Readonly<Update>;
30
+ readonly partialUpdate?: RestEndpointOptions<unknown extends PartialUpdate ? EndpointToFunction<R['partialUpdate']> : OptionsToFunction<PartialUpdate, R['partialUpdate'], EndpointToFunction<R['partialUpdate']>>, R['partialUpdate']['schema']> & Readonly<PartialUpdate>;
31
+ readonly delete?: RestEndpointOptions<unknown extends Delete ? EndpointToFunction<R['delete']> : OptionsToFunction<Delete, R['delete'], EndpointToFunction<R['delete']>>, R['delete']['schema']> & Readonly<Delete>;
32
+ }
33
+ //# sourceMappingURL=resourceExtensionTypes.d.ts.map
@@ -1,13 +1,15 @@
1
+ import type { schema } from '@data-client/endpoint';
1
2
  import type { Schema } from '@data-client/endpoint';
2
- import type { ResourcePath } from './pathTypes.js';
3
- import RestEndpoint from './RestEndpoint.js';
3
+ import type { Denormalize } from '@data-client/endpoint';
4
+ import type { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
5
+ import { Extendable } from './resourceExtendable.js';
6
+ import RestEndpoint, { GetEndpoint, MutateEndpoint, RestInstanceBase, RestTypeNoBody } from './RestEndpoint.js';
7
+ /** The typed (generic) options for a Resource */
4
8
  export interface ResourceGenerics {
5
9
  /** @see https://resthooks.io/rest/api/createResource#path */
6
10
  readonly path: ResourcePath;
7
11
  /** @see https://resthooks.io/rest/api/createResource#schema */
8
12
  readonly schema: Schema;
9
- /** @see https://resthooks.io/rest/api/createResource#paginationfield */
10
- readonly paginationField?: string;
11
13
  /** Only used for types */
12
14
  /** @see https://dataclient.io/rest/api/createResource#body */
13
15
  readonly body?: any;
@@ -15,6 +17,7 @@ export interface ResourceGenerics {
15
17
  /** @see https://resthooks.io/rest/api/createResource#searchParams */
16
18
  readonly searchParams?: any;
17
19
  }
20
+ /** The untyped options for createResource() */
18
21
  export interface ResourceOptions {
19
22
  /** @see https://resthooks.io/rest/api/createResource#endpoint */
20
23
  Endpoint?: typeof RestEndpoint;
@@ -38,4 +41,99 @@ export interface ResourceOptions {
38
41
  /** Determines whether to throw or fallback to */
39
42
  errorPolicy?(error: any): 'hard' | 'soft' | undefined;
40
43
  }
44
+ /** Resources are a collection of methods for a given data model.
45
+ * @see https://resthooks.io/rest/api/createResource
46
+ */
47
+ export interface Resource<O extends ResourceGenerics = {
48
+ path: ResourcePath;
49
+ schema: any;
50
+ }> extends Extendable<O> {
51
+ /** Get a singular item
52
+ *
53
+ * @see https://resthooks.io/rest/api/createResource#get
54
+ */
55
+ get: GetEndpoint<{
56
+ path: O['path'];
57
+ schema: O['schema'];
58
+ }>;
59
+ /** Get a list of item
60
+ *
61
+ * @see https://resthooks.io/rest/api/createResource#getlist
62
+ */
63
+ getList: 'searchParams' extends keyof O ? GetEndpoint<{
64
+ path: ShortenPath<O['path']>;
65
+ schema: schema.Collection<[
66
+ O['schema']
67
+ ]>;
68
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
69
+ searchParams: O['searchParams'];
70
+ }> : GetEndpoint<{
71
+ path: ShortenPath<O['path']>;
72
+ schema: schema.Collection<[
73
+ O['schema']
74
+ ]>;
75
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
76
+ searchParams: Record<string, number | string | boolean> | undefined;
77
+ }>;
78
+ /** Create a new item (POST)
79
+ *
80
+ * @deprecated use Resource.getList.push instead
81
+ */
82
+ create: 'searchParams' extends keyof O ? MutateEndpoint<{
83
+ path: ShortenPath<O['path']>;
84
+ schema: schema.Collection<[
85
+ O['schema']
86
+ ]>['push'];
87
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
88
+ searchParams: O['searchParams'];
89
+ }> : MutateEndpoint<{
90
+ path: ShortenPath<O['path']>;
91
+ schema: schema.Collection<[
92
+ O['schema']
93
+ ]>['push'];
94
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
95
+ }>;
96
+ /** Update an item (PUT)
97
+ *
98
+ * @see https://resthooks.io/rest/api/createResource#update
99
+ */
100
+ update: 'body' extends keyof O ? MutateEndpoint<{
101
+ path: O['path'];
102
+ body: O['body'];
103
+ schema: O['schema'];
104
+ }> : MutateEndpoint<{
105
+ path: O['path'];
106
+ body: Partial<Denormalize<O['schema']>> | FormData;
107
+ schema: O['schema'];
108
+ }>;
109
+ /** Update an item (PATCH)
110
+ *
111
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
112
+ */
113
+ partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
114
+ path: O['path'];
115
+ body: Partial<O['body']>;
116
+ schema: O['schema'];
117
+ }> : MutateEndpoint<{
118
+ path: O['path'];
119
+ body: Partial<Denormalize<O['schema']>> | FormData;
120
+ schema: O['schema'];
121
+ }>;
122
+ /** Delete an item (DELETE)
123
+ *
124
+ * @see https://resthooks.io/rest/api/createResource#delete
125
+ */
126
+ delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
127
+ process: any;
128
+ } ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
129
+ path: O['path'];
130
+ }>;
131
+ }
132
+ export interface ResourceInterface {
133
+ get: RestInstanceBase;
134
+ getList: RestInstanceBase;
135
+ update: RestInstanceBase;
136
+ partialUpdate: RestInstanceBase;
137
+ delete: RestInstanceBase;
138
+ }
41
139
  //# sourceMappingURL=resourceTypes.d.ts.map