@data-client/rest 0.9.5 → 0.9.9

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 (51) hide show
  1. package/README.md +1 -1
  2. package/dist/index.umd.min.js +1 -1
  3. package/legacy/OptionsToFunction.js +1 -1
  4. package/legacy/RestEndpoint.js +1 -1
  5. package/legacy/RestEndpointTypeHelp.js +1 -1
  6. package/legacy/RestEndpointTypes.js +1 -1
  7. package/legacy/createResource.js +1 -1
  8. package/legacy/extractCollection.js +1 -1
  9. package/legacy/mapCollection.js +1 -1
  10. package/legacy/pathTypes.js +1 -1
  11. package/legacy/resourceExtensionTypes.js +1 -1
  12. package/legacy/resourceTypes.js +1 -1
  13. package/legacy/utiltypes.js +1 -1
  14. package/lib/OptionsToFunction.d.ts.map +1 -1
  15. package/lib/OptionsToFunction.js +1 -1
  16. package/lib/RestEndpoint.js +1 -1
  17. package/lib/RestEndpointTypeHelp.d.ts.map +1 -1
  18. package/lib/RestEndpointTypeHelp.js +1 -1
  19. package/lib/RestEndpointTypes.d.ts.map +1 -1
  20. package/lib/RestEndpointTypes.js +1 -1
  21. package/lib/createResource.js +1 -1
  22. package/lib/extractCollection.d.ts +2 -2
  23. package/lib/extractCollection.d.ts.map +1 -1
  24. package/lib/extractCollection.js +1 -1
  25. package/lib/mapCollection.d.ts.map +1 -1
  26. package/lib/mapCollection.js +1 -1
  27. package/lib/pathTypes.d.ts.map +1 -1
  28. package/lib/pathTypes.js +1 -1
  29. package/lib/resourceExtensionTypes.d.ts.map +1 -1
  30. package/lib/resourceExtensionTypes.js +1 -1
  31. package/lib/resourceTypes.d.ts +10 -2
  32. package/lib/resourceTypes.d.ts.map +1 -1
  33. package/lib/resourceTypes.js +1 -1
  34. package/lib/utiltypes.d.ts.map +1 -1
  35. package/lib/utiltypes.js +1 -1
  36. package/package.json +4 -2
  37. package/src/OptionsToFunction.ts +42 -42
  38. package/src/RestEndpoint.js +3 -3
  39. package/src/RestEndpointTypeHelp.ts +2 -3
  40. package/src/RestEndpointTypes.ts +187 -220
  41. package/src/createResource.ts +2 -2
  42. package/src/extractCollection.ts +13 -13
  43. package/src/mapCollection.ts +14 -22
  44. package/src/pathTypes.ts +27 -36
  45. package/src/resourceExtensionTypes.ts +33 -48
  46. package/src/resourceTypes.ts +76 -62
  47. package/src/utiltypes.ts +3 -3
  48. package/ts4.0/extractCollection.d.ts +2 -2
  49. package/ts4.0/resourceTypes.d.ts +4 -0
  50. package/ts4.1/extractCollection.d.ts +2 -2
  51. package/ts4.1/resourceTypes.d.ts +4 -0
package/src/pathTypes.ts CHANGED
@@ -2,59 +2,50 @@ type OnlyOptional<S extends string> = S extends `${infer K}?` ? K : never;
2
2
  type OnlyRequired<S extends string> = S extends `${string}?` ? never : S;
3
3
 
4
4
  /** Computes the union of keys for a path string */
5
- export type PathKeys<S extends string> = string extends S
6
- ? string
7
- : S extends `${infer A}\\:${infer B}`
8
- ? PathKeys<A> | PathKeys<B>
9
- : S extends `${infer A}\\?${infer B}`
10
- ? PathKeys<A> | PathKeys<B>
5
+ export type PathKeys<S extends string> =
6
+ string extends S ? string
7
+ : S extends `${infer A}\\:${infer B}` ? PathKeys<A> | PathKeys<B>
8
+ : S extends `${infer A}\\?${infer B}` ? PathKeys<A> | PathKeys<B>
11
9
  : PathSplits<S>;
12
10
 
13
- type PathSplits<S extends string> = S extends `${string}:${infer K}${
14
- | '/'
15
- | ','
16
- | '%'
17
- | '&'}${infer R}`
18
- ? PathSplits<`:${K}`> | PathSplits<R>
19
- : S extends `${string}:${infer K}:${infer R}`
20
- ? PathSplits<`:${K}`> | PathSplits<`:${R}`>
21
- : S extends `${string}:${infer K}`
22
- ? K
11
+ type PathSplits<S extends string> =
12
+ S extends `${string}:${infer K}${'/' | ',' | '%' | '&'}${infer R}` ?
13
+ PathSplits<`:${K}`> | PathSplits<R>
14
+ : S extends `${string}:${infer K}:${infer R}` ?
15
+ PathSplits<`:${K}`> | PathSplits<`:${R}`>
16
+ : S extends `${string}:${infer K}` ? K
23
17
  : never;
24
18
 
25
19
  /** Parameters for a given path */
26
- export type PathArgs<S extends string> = PathKeys<S> extends never
27
- ? // unknown is identity for intersection ('&')
20
+ export type PathArgs<S extends string> =
21
+ PathKeys<S> extends never ?
22
+ // unknown is identity for intersection ('&')
28
23
  unknown
29
24
  : KeysToArgs<PathKeys<S>>;
30
25
 
31
26
  export type KeysToArgs<Key extends string> = {
32
27
  [K in Key as OnlyOptional<K>]?: string | number;
33
- } & (OnlyRequired<Key> extends never
34
- ? unknown
35
- : {
36
- [K in Key as OnlyRequired<K>]: string | number;
37
- });
38
-
39
- export type PathArgsAndSearch<S extends string> = OnlyRequired<
40
- PathKeys<S>
41
- > extends never
42
- ? Record<string, number | string | boolean> | undefined
28
+ } & (OnlyRequired<Key> extends never ? unknown
29
+ : {
30
+ [K in Key as OnlyRequired<K>]: string | number;
31
+ });
32
+
33
+ export type PathArgsAndSearch<S extends string> =
34
+ OnlyRequired<PathKeys<S>> extends never ?
35
+ Record<string, number | string | boolean> | undefined
43
36
  : {
44
37
  [K in PathKeys<S> as OnlyRequired<K>]: string | number;
45
38
  } & Record<string, number | string>;
46
39
 
47
40
  /** Removes the last :token */
48
- export type ShortenPath<S extends string> = string extends S
49
- ? string
50
- : S extends `${infer B}:${infer R}`
51
- ? TrimColon<`${B}:${ShortenPath<R>}`>
41
+ export type ShortenPath<S extends string> =
42
+ string extends S ? string
43
+ : S extends `${infer B}:${infer R}` ? TrimColon<`${B}:${ShortenPath<R>}`>
52
44
  : '';
53
45
 
54
- type TrimColon<S extends string> = string extends S
55
- ? string
56
- : S extends `${infer R}:`
57
- ? R
46
+ type TrimColon<S extends string> =
47
+ string extends S ? string
48
+ : S extends `${infer R}:` ? R
58
49
  : S;
59
50
 
60
51
  export type ResourcePath = string; // `${string}:${string}`; TODO: Maybe do this in the future? Seems to hard to understand for now
@@ -35,31 +35,21 @@ export interface CustomResource<
35
35
  Delete extends PartialRestGenerics | {} = any,
36
36
  > extends Extendable<O> {
37
37
  // unknown only extends any. this allows us to match exclusively on members not set
38
- get: unknown extends Get
39
- ? R['get']
40
- : PartialRestGenerics extends Get
41
- ? R['get']
42
- : RestExtendedEndpoint<Get, R['get']>;
43
- getList: unknown extends GetList
44
- ? R['getList']
45
- : PartialRestGenerics extends GetList
46
- ? R['getList']
47
- : RestExtendedEndpoint<GetList, R['getList']>;
48
- update: unknown extends Update
49
- ? R['update']
50
- : PartialRestGenerics extends Update
51
- ? R['update']
52
- : RestExtendedEndpoint<Update, R['update']>;
53
- partialUpdate: unknown extends PartialUpdate
54
- ? R['partialUpdate']
55
- : PartialRestGenerics extends PartialUpdate
56
- ? R['partialUpdate']
57
- : RestExtendedEndpoint<PartialUpdate, R['partialUpdate']>;
58
- delete: unknown extends Delete
59
- ? R['delete']
60
- : PartialRestGenerics extends Delete
61
- ? R['delete']
62
- : RestExtendedEndpoint<Delete, R['delete']>;
38
+ get: unknown extends Get ? R['get']
39
+ : PartialRestGenerics extends Get ? R['get']
40
+ : RestExtendedEndpoint<Get, R['get']>;
41
+ getList: unknown extends GetList ? R['getList']
42
+ : PartialRestGenerics extends GetList ? R['getList']
43
+ : RestExtendedEndpoint<GetList, R['getList']>;
44
+ update: unknown extends Update ? R['update']
45
+ : PartialRestGenerics extends Update ? R['update']
46
+ : RestExtendedEndpoint<Update, R['update']>;
47
+ partialUpdate: unknown extends PartialUpdate ? R['partialUpdate']
48
+ : PartialRestGenerics extends PartialUpdate ? R['partialUpdate']
49
+ : RestExtendedEndpoint<PartialUpdate, R['partialUpdate']>;
50
+ delete: unknown extends Delete ? R['delete']
51
+ : PartialRestGenerics extends Delete ? R['delete']
52
+ : RestExtendedEndpoint<Delete, R['delete']>;
63
53
  }
64
54
 
65
55
  export type ExtendedResource<
@@ -76,45 +66,40 @@ export interface ResourceEndpointExtensions<
76
66
  Delete extends PartialRestGenerics = any,
77
67
  > {
78
68
  readonly get?: RestEndpointOptions<
79
- unknown extends Get
80
- ? EndpointToFunction<R['get']>
81
- : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>,
69
+ unknown extends Get ? EndpointToFunction<R['get']>
70
+ : OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>,
82
71
  R['get']['schema']
83
72
  > &
84
73
  Readonly<Get>;
85
74
  readonly getList?: RestEndpointOptions<
86
- unknown extends GetList
87
- ? EndpointToFunction<R['getList']>
88
- : OptionsToFunction<
89
- GetList,
90
- R['getList'],
91
- EndpointToFunction<R['getList']>
92
- >,
75
+ unknown extends GetList ? EndpointToFunction<R['getList']>
76
+ : OptionsToFunction<
77
+ GetList,
78
+ R['getList'],
79
+ EndpointToFunction<R['getList']>
80
+ >,
93
81
  R['getList']['schema']
94
82
  > &
95
83
  Readonly<GetList>;
96
84
  readonly update?: RestEndpointOptions<
97
- unknown extends Update
98
- ? EndpointToFunction<R['update']>
99
- : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>,
85
+ unknown extends Update ? EndpointToFunction<R['update']>
86
+ : OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>,
100
87
  R['update']['schema']
101
88
  > &
102
89
  Readonly<Update>;
103
90
  readonly partialUpdate?: RestEndpointOptions<
104
- unknown extends PartialUpdate
105
- ? EndpointToFunction<R['partialUpdate']>
106
- : OptionsToFunction<
107
- PartialUpdate,
108
- R['partialUpdate'],
109
- EndpointToFunction<R['partialUpdate']>
110
- >,
91
+ unknown extends PartialUpdate ? EndpointToFunction<R['partialUpdate']>
92
+ : OptionsToFunction<
93
+ PartialUpdate,
94
+ R['partialUpdate'],
95
+ EndpointToFunction<R['partialUpdate']>
96
+ >,
111
97
  R['partialUpdate']['schema']
112
98
  > &
113
99
  Readonly<PartialUpdate>;
114
100
  readonly delete?: RestEndpointOptions<
115
- unknown extends Delete
116
- ? EndpointToFunction<R['delete']>
117
- : OptionsToFunction<Delete, R['delete'], EndpointToFunction<R['delete']>>,
101
+ unknown extends Delete ? EndpointToFunction<R['delete']>
102
+ : OptionsToFunction<Delete, R['delete'], EndpointToFunction<R['delete']>>,
118
103
  R['delete']['schema']
119
104
  > &
120
105
  Readonly<Delete>;
@@ -68,86 +68,100 @@ export interface Resource<
68
68
  *
69
69
  * @see https://dataclient.io/rest/api/createResource#getlist
70
70
  */
71
- getList: 'searchParams' extends keyof O
72
- ? GetEndpoint<
73
- {
74
- path: ShortenPath<O['path']>;
75
- schema: schema.Collection<[O['schema']]>;
76
- body: 'body' extends keyof O
77
- ? O['body']
78
- : Partial<Denormalize<O['schema']>>;
79
- searchParams: O['searchParams'];
80
- } & Pick<O, 'paginationField'>
81
- >
82
- : GetEndpoint<
83
- {
84
- path: ShortenPath<O['path']>;
85
- schema: schema.Collection<[O['schema']]>;
86
- body: 'body' extends keyof O
87
- ? O['body']
88
- : Partial<Denormalize<O['schema']>>;
89
- searchParams: Record<string, number | string | boolean> | undefined;
90
- } & Pick<O, 'paginationField'>
91
- >;
71
+ getList: 'searchParams' extends keyof O ?
72
+ GetEndpoint<
73
+ {
74
+ path: ShortenPath<O['path']>;
75
+ schema: schema.Collection<
76
+ [O['schema']],
77
+ [
78
+ 'searchParams' extends keyof O ?
79
+ O['searchParams'] extends undefined ?
80
+ PathArgs<ShortenPath<O['path']>>
81
+ : O['searchParams'] & PathArgs<ShortenPath<O['path']>>
82
+ : PathArgs<ShortenPath<O['path']>>,
83
+ ]
84
+ >;
85
+ body: 'body' extends keyof O ? O['body']
86
+ : Partial<Denormalize<O['schema']>>;
87
+ searchParams: O['searchParams'];
88
+ } & Pick<O, 'paginationField'>
89
+ >
90
+ : GetEndpoint<
91
+ {
92
+ path: ShortenPath<O['path']>;
93
+ schema: schema.Collection<
94
+ [O['schema']],
95
+ [
96
+ 'searchParams' extends keyof O ?
97
+ O['searchParams'] extends undefined ?
98
+ PathArgs<ShortenPath<O['path']>>
99
+ : O['searchParams'] & PathArgs<ShortenPath<O['path']>>
100
+ : PathArgs<ShortenPath<O['path']>>,
101
+ ]
102
+ >;
103
+ body: 'body' extends keyof O ? O['body']
104
+ : Partial<Denormalize<O['schema']>>;
105
+ searchParams: Record<string, number | string | boolean> | undefined;
106
+ } & Pick<O, 'paginationField'>
107
+ >;
92
108
  /** Create a new item (POST)
93
109
  *
94
110
  * @deprecated use Resource.getList.push instead
95
111
  */
96
- create: 'searchParams' extends keyof O
97
- ? MutateEndpoint<{
98
- path: ShortenPath<O['path']>;
99
- schema: schema.Collection<[O['schema']]>['push'];
100
- body: 'body' extends keyof O
101
- ? O['body']
102
- : Partial<Denormalize<O['schema']>>;
103
- searchParams: O['searchParams'];
104
- }>
105
- : MutateEndpoint<{
106
- path: ShortenPath<O['path']>;
107
- schema: schema.Collection<[O['schema']]>['push'];
108
- body: 'body' extends keyof O
109
- ? O['body']
110
- : Partial<Denormalize<O['schema']>>;
111
- }>;
112
+ create: 'searchParams' extends keyof O ?
113
+ MutateEndpoint<{
114
+ path: ShortenPath<O['path']>;
115
+ schema: schema.Collection<[O['schema']]>['push'];
116
+ body: 'body' extends keyof O ? O['body']
117
+ : Partial<Denormalize<O['schema']>>;
118
+ searchParams: O['searchParams'];
119
+ }>
120
+ : MutateEndpoint<{
121
+ path: ShortenPath<O['path']>;
122
+ schema: schema.Collection<[O['schema']]>['push'];
123
+ body: 'body' extends keyof O ? O['body']
124
+ : Partial<Denormalize<O['schema']>>;
125
+ }>;
112
126
  /** Update an item (PUT)
113
127
  *
114
128
  * @see https://dataclient.io/rest/api/createResource#update
115
129
  */
116
- update: 'body' extends keyof O
117
- ? MutateEndpoint<{
118
- path: O['path'];
119
- body: O['body'];
120
- schema: O['schema'];
121
- }>
122
- : MutateEndpoint<{
123
- path: O['path'];
124
- body: Partial<Denormalize<O['schema']>> | FormData;
125
- schema: O['schema'];
126
- }>;
130
+ update: 'body' extends keyof O ?
131
+ MutateEndpoint<{
132
+ path: O['path'];
133
+ body: O['body'];
134
+ schema: O['schema'];
135
+ }>
136
+ : MutateEndpoint<{
137
+ path: O['path'];
138
+ body: Partial<Denormalize<O['schema']>> | FormData;
139
+ schema: O['schema'];
140
+ }>;
127
141
  /** Update an item (PATCH)
128
142
  *
129
143
  * @see https://dataclient.io/rest/api/createResource#partialupdate
130
144
  */
131
- partialUpdate: 'body' extends keyof O
132
- ? MutateEndpoint<{
133
- path: O['path'];
134
- body: Partial<O['body']>;
135
- schema: O['schema'];
136
- }>
137
- : MutateEndpoint<{
138
- path: O['path'];
139
- body: Partial<Denormalize<O['schema']>> | FormData;
140
- schema: O['schema'];
141
- }>;
145
+ partialUpdate: 'body' extends keyof O ?
146
+ MutateEndpoint<{
147
+ path: O['path'];
148
+ body: Partial<O['body']>;
149
+ schema: O['schema'];
150
+ }>
151
+ : MutateEndpoint<{
152
+ path: O['path'];
153
+ body: Partial<Denormalize<O['schema']>> | FormData;
154
+ schema: O['schema'];
155
+ }>;
142
156
  /** Delete an item (DELETE)
143
157
  *
144
158
  * @see https://dataclient.io/rest/api/createResource#delete
145
159
  */
146
160
  delete: RestTypeNoBody<
147
161
  PathArgs<O['path']>,
148
- O['schema'] extends schema.EntityInterface & { process: any }
149
- ? schema.Invalidate<O['schema']>
150
- : O['schema'],
162
+ O['schema'] extends schema.EntityInterface & { process: any } ?
163
+ schema.Invalidate<O['schema']>
164
+ : O['schema'],
151
165
  undefined,
152
166
  Partial<PathArgs<O['path']>>,
153
167
  {
package/src/utiltypes.ts CHANGED
@@ -15,9 +15,9 @@ RequiredKeys<{
15
15
  export type RequiredKeys<T> = Values<OnlyRequired<T>>;
16
16
 
17
17
  type OnlyRequired<T> = {
18
- [K in keyof T as string extends K ? never : K]-?: {} extends Pick<T, K>
19
- ? never
20
- : K;
18
+ [K in keyof T as string extends K ? never : K]-?: {} extends Pick<T, K> ?
19
+ never
20
+ : K;
21
21
  };
22
22
 
23
23
  // exclude number only required for typescript 4.4 and below
@@ -1,12 +1,12 @@
1
1
  import { Schema, schema } from '@data-client/endpoint';
2
2
  import { ExtractObject } from './extractObject.js';
3
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 {
4
+ export type ExtractCollection<S extends Schema | undefined> = S extends ({
5
5
  push: any;
6
6
  unshift: any;
7
7
  assign: any;
8
8
  schema: Schema;
9
- } ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
9
+ }) ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
10
10
  [K: string]: any;
11
11
  }> ? never : S extends {
12
12
  [K: string]: Schema;
@@ -68,6 +68,8 @@ export interface Resource<O extends ResourceGenerics = {
68
68
  path: ShortenPath<O['path']>;
69
69
  schema: schema.Collection<[
70
70
  O['schema']
71
+ ], [
72
+ 'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<ShortenPath<O['path']>> : O['searchParams'] & PathArgs<ShortenPath<O['path']>> : PathArgs<ShortenPath<O['path']>>
71
73
  ]>;
72
74
  body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
73
75
  searchParams: O['searchParams'];
@@ -75,6 +77,8 @@ export interface Resource<O extends ResourceGenerics = {
75
77
  path: ShortenPath<O['path']>;
76
78
  schema: schema.Collection<[
77
79
  O['schema']
80
+ ], [
81
+ 'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<ShortenPath<O['path']>> : O['searchParams'] & PathArgs<ShortenPath<O['path']>> : PathArgs<ShortenPath<O['path']>>
78
82
  ]>;
79
83
  body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
80
84
  searchParams: Record<string, number | string | boolean> | undefined;
@@ -1,12 +1,12 @@
1
1
  import { Schema, schema } from '@data-client/endpoint';
2
2
  import { ExtractObject } from './extractObject.js';
3
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 {
4
+ export type ExtractCollection<S extends Schema | undefined> = S extends ({
5
5
  push: any;
6
6
  unshift: any;
7
7
  assign: any;
8
8
  schema: Schema;
9
- } ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
9
+ }) ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
10
10
  [K: string]: any;
11
11
  }> ? never : S extends {
12
12
  [K: string]: Schema;
@@ -68,6 +68,8 @@ export interface Resource<O extends ResourceGenerics = {
68
68
  path: ShortenPath<O['path']>;
69
69
  schema: schema.Collection<[
70
70
  O['schema']
71
+ ], [
72
+ 'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<ShortenPath<O['path']>> : O['searchParams'] & PathArgs<ShortenPath<O['path']>> : PathArgs<ShortenPath<O['path']>>
71
73
  ]>;
72
74
  body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
73
75
  searchParams: O['searchParams'];
@@ -75,6 +77,8 @@ export interface Resource<O extends ResourceGenerics = {
75
77
  path: ShortenPath<O['path']>;
76
78
  schema: schema.Collection<[
77
79
  O['schema']
80
+ ], [
81
+ 'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<ShortenPath<O['path']>> : O['searchParams'] & PathArgs<ShortenPath<O['path']>> : PathArgs<ShortenPath<O['path']>>
78
82
  ]>;
79
83
  body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
80
84
  searchParams: Record<string, number | string | boolean> | undefined;