@data-client/rest 0.16.1 → 0.16.3
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/CHANGELOG.md +63 -0
- package/dist/index.js +8 -1
- package/dist/index.umd.min.js +1 -1
- package/legacy/RestEndpoint.js +3 -4
- package/legacy/RestEndpointTypes.js +1 -1
- package/legacy/RestHelpers.js +9 -2
- package/legacy/pathTypes.js +1 -1
- package/lib/RestEndpoint.js +3 -4
- package/lib/RestEndpointTypes.d.ts +13 -12
- package/lib/RestEndpointTypes.d.ts.map +1 -1
- package/lib/RestEndpointTypes.js +1 -1
- package/lib/RestHelpers.d.ts +1 -0
- package/lib/RestHelpers.d.ts.map +1 -1
- package/lib/RestHelpers.js +9 -2
- package/lib/pathTypes.d.ts +3 -0
- package/lib/pathTypes.d.ts.map +1 -1
- package/lib/pathTypes.js +1 -1
- package/package.json +2 -2
- package/src/RestEndpoint.js +7 -3
- package/src/RestEndpointTypes.ts +29 -20
- package/src/RestHelpers.ts +16 -1
- package/src/__tests__/RestEndpoint.ts +14 -0
- package/src/pathTypes.ts +7 -0
- package/ts4.0/RestEndpointTypes.d.ts +15 -12
- package/ts4.0/RestHelpers.d.ts +1 -0
- package/ts4.0/pathTypes.d.ts +6 -0
- package/ts4.1/RestEndpointTypes.d.ts +15 -12
- package/ts4.1/RestHelpers.d.ts +1 -0
- package/ts4.1/pathTypes.d.ts +3 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
2
|
import type { ExtractCollection } from './extractCollection.js';
|
|
3
3
|
import { OptionsToBodyArgument, OptionsToFunction } from './OptionsToFunction.js';
|
|
4
|
-
import { PathArgs } from './pathTypes.js';
|
|
4
|
+
import { PathArgs, SoftPathArgs } from './pathTypes.js';
|
|
5
5
|
import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
|
|
6
6
|
export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends boolean | undefined = boolean | undefined, O extends {
|
|
7
7
|
path: string;
|
|
@@ -280,6 +280,15 @@ export type MoveEndpoint<F extends FetchFunction = FetchFunction, S extends Sche
|
|
|
280
280
|
}> = RestInstanceBase<RestFetch<PathArgs<Exclude<O['path'], undefined>>, O['body'], ResolveType<F>>, S, true, Omit<O, 'method' | 'searchParams'> & {
|
|
281
281
|
method: 'PATCH';
|
|
282
282
|
}>;
|
|
283
|
+
/** When `method` is omitted from `O`, infer it (must stay aligned with `OptionsToBodyArgument`). */
|
|
284
|
+
type InferRestMethodWhenOmitted<O extends RestGenerics> = O extends {
|
|
285
|
+
sideEffect: true;
|
|
286
|
+
} ? 'POST' : 'body' extends keyof O ? [
|
|
287
|
+
O['body']
|
|
288
|
+
] extends [
|
|
289
|
+
undefined
|
|
290
|
+
] ? 'GET' : 'POST' : 'GET';
|
|
291
|
+
type MethodArgForBodyInference<O extends RestGenerics> = 'method' extends keyof O ? O['method'] : InferRestMethodWhenOmitted<O>;
|
|
283
292
|
type OptionsToAdderBodyArgument<O extends {
|
|
284
293
|
body?: any;
|
|
285
294
|
}> = 'body' extends keyof O ? O['body'] : any;
|
|
@@ -313,27 +322,21 @@ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S
|
|
|
313
322
|
url?(...args: Parameters<F>): string;
|
|
314
323
|
update?: EndpointUpdateFunction<F, S>;
|
|
315
324
|
}
|
|
316
|
-
export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestEndpointOptions<RestFetch<'searchParams' extends keyof O ? [
|
|
325
|
+
export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestEndpointOptions<RestFetch<unknown extends O ? any : 'searchParams' extends keyof O ? [
|
|
317
326
|
O['searchParams']
|
|
318
327
|
] extends [
|
|
319
328
|
undefined
|
|
320
|
-
] ?
|
|
321
|
-
sideEffect: true;
|
|
322
|
-
} ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any>, O['schema']>;
|
|
329
|
+
] ? SoftPathArgs<O['path']> : O['searchParams'] & SoftPathArgs<O['path']> : SoftPathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, O['schema']>;
|
|
323
330
|
/** Simplifies endpoint definitions that follow REST patterns
|
|
324
331
|
*
|
|
325
332
|
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
326
333
|
*/
|
|
327
|
-
export interface RestEndpoint<O extends RestGenerics = any> extends RestInstance<RestFetch<'searchParams' extends keyof O ? [
|
|
334
|
+
export interface RestEndpoint<O extends RestGenerics = any> extends RestInstance<RestFetch<unknown extends O ? any : 'searchParams' extends keyof O ? [
|
|
328
335
|
O['searchParams']
|
|
329
336
|
] extends [
|
|
330
337
|
undefined
|
|
331
|
-
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, '
|
|
332
|
-
|
|
333
|
-
} ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<O['method']>, 'method' extends keyof O ? O : O & {
|
|
334
|
-
method: O extends {
|
|
335
|
-
sideEffect: true;
|
|
336
|
-
} ? 'POST' : 'GET';
|
|
338
|
+
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<MethodArgForBodyInference<O>>, 'method' extends keyof O ? O : O & {
|
|
339
|
+
method: InferRestMethodWhenOmitted<O>;
|
|
337
340
|
}> {
|
|
338
341
|
}
|
|
339
342
|
export interface RestEndpointConstructor {
|
package/ts4.1/RestHelpers.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { PathFunction, ParamData } from 'path-to-regexp';
|
|
|
2
2
|
import { ShortenPath } from './pathTypes.js';
|
|
3
3
|
export declare function getUrlBase(path: string): PathFunction<ParamData>;
|
|
4
4
|
export declare function getUrlTokens(path: string): Set<string>;
|
|
5
|
+
export declare function getPathRegex(path: string): RegExp;
|
|
5
6
|
export declare function isPojo(obj: unknown): obj is Record<string, any>;
|
|
6
7
|
export declare function shortenPath<S extends string>(path: S): ShortenPath<S>;
|
|
7
8
|
//# sourceMappingURL=RestHelpers.d.ts.map
|
package/ts4.1/pathTypes.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ type KeyName<K extends string> = CleanKey<K extends `*${infer N}}` ? N : K exten
|
|
|
3
3
|
type KeyVal<K extends string> = K extends `*${string}` ? string[] : string | number;
|
|
4
4
|
/** Parameters for a given path */
|
|
5
5
|
export type PathArgs<S extends string> = unknown extends S ? any : PathKeys<S> extends never ? unknown : KeysToArgs<PathKeys<S>>;
|
|
6
|
+
/** Like {@link PathArgs} but widened `path: string` collapses to `unknown`,
|
|
7
|
+
* preventing `(params, body) | (body)` union overloads in ParamFetchWithBody. */
|
|
8
|
+
export type SoftPathArgs<P extends string> = unknown extends P ? any : string extends P ? unknown : PathArgs<P>;
|
|
6
9
|
/** Computes the union of keys for a path string */
|
|
7
10
|
export type PathKeys<S extends string> = string extends S ? string : S extends `${infer A}\\${':' | '*' | '}'}${infer B}` ? PathKeys<A> | PathKeys<B> : Splits<S, ':'> | Splits<S, '*'>;
|
|
8
11
|
type Splits<S extends string, M extends ':' | '*'> = S extends `${string}${M}${infer K}${M}${infer R}` ? Splits<`${M}${K}`, M> | Splits<`${M}${R}`, M> : S extends (`${string}${M}${infer K}${'/' | '\\' | '%' | '&' | '*' | ':' | '{' | ';' | ',' | '!' | '@'}${infer R}`) ? Splits<`${M}${K}`, M> | Splits<R, M> : S extends `${string}${M}${infer K}` ? M extends '*' ? `*${K}` : K : never;
|