@zajno/common 2.6.5 → 2.6.7
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/cjs/api/builder.js +13 -8
- package/cjs/api/builder.js.map +1 -1
- package/cjs/api/call.types.js +1 -0
- package/cjs/api/call.types.js.map +1 -1
- package/cjs/api/endpoint.js +10 -0
- package/cjs/api/endpoint.js.map +1 -1
- package/cjs/api/helpers.js.map +1 -1
- package/cjs/api/index.js +4 -1
- package/cjs/api/index.js.map +1 -1
- package/cjs/structures/path/builder.helpers.js +180 -0
- package/cjs/structures/path/builder.helpers.js.map +1 -0
- package/cjs/structures/path/builder.js +21 -106
- package/cjs/structures/path/builder.js.map +1 -1
- package/cjs/structures/path/types.helpers.js +3 -0
- package/cjs/structures/path/types.helpers.js.map +1 -0
- package/esm/api/builder.js +12 -8
- package/esm/api/builder.js.map +1 -1
- package/esm/api/call.types.js +1 -0
- package/esm/api/call.types.js.map +1 -1
- package/esm/api/endpoint.js +10 -0
- package/esm/api/endpoint.js.map +1 -1
- package/esm/api/helpers.js.map +1 -1
- package/esm/api/index.js +1 -0
- package/esm/api/index.js.map +1 -1
- package/esm/structures/path/builder.helpers.js +172 -0
- package/esm/structures/path/builder.helpers.js.map +1 -0
- package/esm/structures/path/builder.js +20 -104
- package/esm/structures/path/builder.js.map +1 -1
- package/esm/structures/path/types.helpers.js +2 -0
- package/esm/structures/path/types.helpers.js.map +1 -0
- package/package.json +1 -1
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/tsconfig.esm.tsbuildinfo +1 -1
- package/tsconfig.types.tsbuildinfo +1 -1
- package/types/api/builder.d.ts +5 -0
- package/types/api/call.types.d.ts +7 -6
- package/types/api/endpoint.d.ts +2 -0
- package/types/api/endpoint.types.d.ts +4 -1
- package/types/api/extensions/contentType.d.ts +1 -1
- package/types/api/helpers.d.ts +3 -1
- package/types/api/index.d.ts +1 -0
- package/types/structures/path/builder.d.ts +5 -4
- package/types/structures/path/builder.helpers.d.ts +8 -0
- package/types/structures/path/types.d.ts +32 -27
- package/types/structures/path/types.helpers.d.ts +23 -0
- package/types/types/misc.d.ts +4 -0
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type { EmptyObject, LengthArray, Nullable } from '../../types/misc.js';
|
|
1
|
+
import type { EmptyObject, Expand, IsNever, LengthArray, Nullable } from '../../types/misc.js';
|
|
2
|
+
import type { ArgValue, IsOptional, ReadonlyArrayNormalized, TemplateTransform, ToOptionalArray, ToRequired, ToRequiredArray, TransformMap } from './types.helpers.js';
|
|
2
3
|
import type { CombineOptions } from './utils.js';
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
[
|
|
9
|
-
}
|
|
4
|
+
export type ObjectBuilderArgs<TArgs extends string, TFallback = EmptyObject> = string extends TArgs ? TFallback : ArgRecord<TArgs>;
|
|
5
|
+
export type BuilderArgs<TArgs extends string, L extends number = number> = LengthArray<ArgValue | null, L> | ArgRecord<TArgs>;
|
|
6
|
+
export type ArgRecord<T extends string> = Expand<{
|
|
7
|
+
[K in T as true extends IsOptional<K> ? never : K]: ArgValue;
|
|
8
|
+
} & {
|
|
9
|
+
[K in T as true extends IsOptional<K> ? ToRequired<K> : never]?: ArgValue | null;
|
|
10
|
+
}>;
|
|
10
11
|
/** Internal, Don't use it directly */
|
|
11
|
-
interface BaseBuilder<A extends readonly string[], B
|
|
12
|
+
interface BaseBuilder<A extends readonly string[], B> {
|
|
12
13
|
/**
|
|
13
14
|
* Builds the path using provided args.
|
|
14
15
|
*
|
|
@@ -20,14 +21,15 @@ interface BaseBuilder<A extends readonly string[], B, P = TemplatePrefixing> {
|
|
|
20
21
|
/**
|
|
21
22
|
* Template builder that will build the path using args names but also can:
|
|
22
23
|
* - keep args as they were in the template
|
|
23
|
-
* - add a prefix for each key in case `
|
|
24
|
-
* - format each key in case `
|
|
24
|
+
* - add a prefix for each key in case `transform` is provided as string
|
|
25
|
+
* - format each key in case `transform` is provided as function
|
|
26
|
+
* - append prefix/suffix/optional suffix for each key in case `transform` is provided as object
|
|
25
27
|
*
|
|
26
28
|
* For now it's recommended to cache the result, internally it doesn't care about it.
|
|
27
29
|
*/
|
|
28
|
-
template: (
|
|
30
|
+
template: (transform?: Nullable<TemplateTransform>, options?: CombineOptions) => string;
|
|
29
31
|
/** args as they were in the template */
|
|
30
|
-
readonly args: A
|
|
32
|
+
readonly args: ReadonlyArrayNormalized<ToRequiredArray<A>>;
|
|
31
33
|
/**
|
|
32
34
|
* Types helper method to easily cast all kinds of builders to generic IBuilder or others.
|
|
33
35
|
* It just always returns the passed object, so it's the implementation's responsibility to ensure the compatibility.
|
|
@@ -44,29 +46,32 @@ interface BaseBuilder<A extends readonly string[], B, P = TemplatePrefixing> {
|
|
|
44
46
|
/** Allows to specify **template** transformation per key for the current builder instance. */
|
|
45
47
|
withTemplateTransform: (transforms: TransformMap<A>) => this;
|
|
46
48
|
}
|
|
47
|
-
export type TransformMap<A extends readonly string[]> = {
|
|
48
|
-
[K in A[number]]?: (v: ArgValue) => string;
|
|
49
|
-
};
|
|
50
49
|
type EmptyBuilderArgs = [] | Record<PropertyKey, never>;
|
|
51
|
-
|
|
52
|
-
export interface Builder<TArgs extends readonly string[], TOptional = false> extends BaseBuilder<ReadonlyArr<TArgs>, true extends TOptional ? MakeOptional<BuilderArgs<TArgs[number], TArgs['length']>> : BuilderArgs<TArgs[number], TArgs['length']>> {
|
|
50
|
+
export interface Builder<TArgs extends readonly string[]> extends BaseBuilder<ReadonlyArrayNormalized<TArgs>, BuilderArgs<TArgs[number], TArgs['length']>> {
|
|
53
51
|
/** Marks input type for `build` to be `Partial`, so any/all arguments can be omitted.
|
|
54
52
|
*
|
|
55
53
|
* **Limitation**: will convert to optional ALL parameters if >=2 Builders combined via CombineBuilders
|
|
56
54
|
*/
|
|
57
|
-
asOptional(): Builder<TArgs
|
|
55
|
+
asOptional(): Builder<ToOptionalArray<TArgs>>;
|
|
58
56
|
}
|
|
59
57
|
export interface StaticBuilder extends BaseBuilder<readonly [], EmptyBuilderArgs> {
|
|
60
58
|
}
|
|
61
59
|
export interface IBuilder extends BaseBuilder<readonly string[], any> {
|
|
62
60
|
}
|
|
63
61
|
export type StaticInput = string | readonly string[];
|
|
64
|
-
export type BaseInput = StaticInput | BaseBuilder<any, any
|
|
65
|
-
export type
|
|
66
|
-
|
|
67
|
-
export type ExtractArgs<T> = T extends Builder<infer TArgs> ? TArgs : readonly string[];
|
|
68
|
-
export type ExtractObjectArgs<T, F = EmptyObject> = T extends Builder<infer TArgs
|
|
69
|
-
|
|
70
|
-
type
|
|
71
|
-
|
|
62
|
+
export type BaseInput = StaticInput | BaseBuilder<any, any>;
|
|
63
|
+
export type SwitchBuilder<TArg extends readonly string[]> = IsNever<TArg, StaticBuilder, [
|
|
64
|
+
] extends TArg ? StaticBuilder : (readonly string[] extends TArg ? IBuilder : Builder<TArg>)>;
|
|
65
|
+
export type ExtractArgs<T> = T extends Builder<infer TArgs> ? ToRequiredArray<TArgs> : readonly string[];
|
|
66
|
+
export type ExtractObjectArgs<T, F = EmptyObject> = T extends Builder<infer TArgs> ? ObjectBuilderArgs<TArgs[number], F> : F;
|
|
67
|
+
/** Normalizes builder, makes sure dynamic args are inferrable. If input is not a builder (i.e. StaticInput), return StaticBuilder */
|
|
68
|
+
type Output<TInput> = TInput extends StaticBuilder ? StaticBuilder : (TInput extends Builder<infer TArgs> ? Builder<TArgs> : (TInput extends StaticInput ? DynamicStringToBuilder<TInput> : StaticBuilder));
|
|
69
|
+
/** Turns two inputs into one builder, merging static and dynamic args */
|
|
70
|
+
type CombineTwo<T1 extends BaseInput, T2 extends BaseInput> = Output<T1> extends StaticBuilder ? Output<T2> : (Output<T2> extends StaticBuilder ? Output<T1> : (Output<T1> extends Builder<infer Arr1> ? (Output<T2> extends Builder<infer Arr2> ? Builder<[...Arr1, ...Arr2]> : never) : never));
|
|
71
|
+
export type CombineBuilders<T extends readonly BaseInput[]> = T extends readonly [infer T1 extends BaseInput, infer T2 extends BaseInput, ...infer Rest] ? (CombineTwo<T1, T2> extends infer C extends BaseBuilder<infer _A extends readonly string[], any> & BaseInput ? (Rest extends readonly BaseInput[] ? CombineBuilders<readonly [C, ...Rest]> : never) : never) : (T extends readonly [infer T1] ? Output<T1> : StaticBuilder);
|
|
72
|
+
type Splitter<T extends string> = T extends `${infer Head extends string}/${infer Rest}` ? [Head, ...Splitter<Rest>] : [T];
|
|
73
|
+
type ExtractDynamic<T extends string> = T extends `:${infer Arg}` ? [Arg] : T;
|
|
74
|
+
type SkipStaticParts<T extends readonly string[]> = T extends readonly [infer Head extends string, ...infer Rest extends readonly string[]] ? ExtractDynamic<Head> extends [infer Arg] ? [Arg, ...SkipStaticParts<Rest>] : SkipStaticParts<Rest> : [];
|
|
75
|
+
type ExtractDynamicParts<T extends StaticInput> = T extends readonly string[] ? SkipStaticParts<T> : (T extends string ? SkipStaticParts<Splitter<T>> : []);
|
|
76
|
+
type DynamicStringToBuilder<T extends StaticInput> = [] extends ExtractDynamicParts<T> ? StaticBuilder : Builder<ExtractDynamicParts<T>>;
|
|
72
77
|
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type IsOptional<T extends string> = T extends `${string}?` ? true : false;
|
|
2
|
+
export type ToOptional<T extends string> = T extends `${infer Arg}?` ? `${Arg}?` : `${T}?`;
|
|
3
|
+
export type ToOptionalArray<T extends readonly string[]> = T extends readonly [infer Head extends string, ...infer Rest extends readonly string[]] ? [ToOptional<Head>, ...ToOptionalArray<Rest>] : [];
|
|
4
|
+
export type ToRequired<T extends string> = T extends `${infer Arg}?` ? Arg : T;
|
|
5
|
+
export type ToRequiredArray<T extends readonly string[]> = T extends readonly [infer Head extends string, ...infer Rest extends readonly string[]] ? [ToRequired<Head>, ...ToRequiredArray<Rest>] : T;
|
|
6
|
+
export type ReadonlyArrayNormalized<TArr extends ReadonlyArray<unknown>> = TArr extends ReadonlyArray<infer T> ? ReadonlyArray<T> : never;
|
|
7
|
+
export type ArgValue = string | number | boolean;
|
|
8
|
+
export type ArgumentInfo = {
|
|
9
|
+
raw: string;
|
|
10
|
+
name: string;
|
|
11
|
+
index: number;
|
|
12
|
+
isOptional: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type TemplateTransformOptions = {
|
|
15
|
+
prefix: string;
|
|
16
|
+
suffix: string;
|
|
17
|
+
optionalSuffix: string;
|
|
18
|
+
};
|
|
19
|
+
export type TransformFunction<TValue extends ArgValue = ArgValue> = (value: TValue, info: ArgumentInfo) => string;
|
|
20
|
+
export type TemplateTransform = string | Partial<TemplateTransformOptions> | TransformFunction<string>;
|
|
21
|
+
export type TransformMap<A extends readonly string[]> = {
|
|
22
|
+
[K in A[number]]?: TransformFunction;
|
|
23
|
+
};
|
package/types/types/misc.d.ts
CHANGED
|
@@ -36,3 +36,7 @@ export type PickNullable<T, K extends keyof T> = {
|
|
|
36
36
|
export type EmptyObjectNullable = (EmptyObject | null | undefined);
|
|
37
37
|
export type IsNever<T, Y, N> = [T] extends [never] ? Y : N;
|
|
38
38
|
export type Coalesce<T, TReplace = EmptyObjectNullable> = [T] extends [never] | [null] | [undefined] ? TReplace : T;
|
|
39
|
+
/** Intellisense helper to show type unions/intersections as a whole type */
|
|
40
|
+
export type Expand<T> = T extends infer O ? {
|
|
41
|
+
[K in keyof O]: O[K];
|
|
42
|
+
} : never;
|