@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.
Files changed (46) hide show
  1. package/cjs/api/builder.js +13 -8
  2. package/cjs/api/builder.js.map +1 -1
  3. package/cjs/api/call.types.js +1 -0
  4. package/cjs/api/call.types.js.map +1 -1
  5. package/cjs/api/endpoint.js +10 -0
  6. package/cjs/api/endpoint.js.map +1 -1
  7. package/cjs/api/helpers.js.map +1 -1
  8. package/cjs/api/index.js +4 -1
  9. package/cjs/api/index.js.map +1 -1
  10. package/cjs/structures/path/builder.helpers.js +180 -0
  11. package/cjs/structures/path/builder.helpers.js.map +1 -0
  12. package/cjs/structures/path/builder.js +21 -106
  13. package/cjs/structures/path/builder.js.map +1 -1
  14. package/cjs/structures/path/types.helpers.js +3 -0
  15. package/cjs/structures/path/types.helpers.js.map +1 -0
  16. package/esm/api/builder.js +12 -8
  17. package/esm/api/builder.js.map +1 -1
  18. package/esm/api/call.types.js +1 -0
  19. package/esm/api/call.types.js.map +1 -1
  20. package/esm/api/endpoint.js +10 -0
  21. package/esm/api/endpoint.js.map +1 -1
  22. package/esm/api/helpers.js.map +1 -1
  23. package/esm/api/index.js +1 -0
  24. package/esm/api/index.js.map +1 -1
  25. package/esm/structures/path/builder.helpers.js +172 -0
  26. package/esm/structures/path/builder.helpers.js.map +1 -0
  27. package/esm/structures/path/builder.js +20 -104
  28. package/esm/structures/path/builder.js.map +1 -1
  29. package/esm/structures/path/types.helpers.js +2 -0
  30. package/esm/structures/path/types.helpers.js.map +1 -0
  31. package/package.json +1 -1
  32. package/tsconfig.cjs.tsbuildinfo +1 -1
  33. package/tsconfig.esm.tsbuildinfo +1 -1
  34. package/tsconfig.types.tsbuildinfo +1 -1
  35. package/types/api/builder.d.ts +5 -0
  36. package/types/api/call.types.d.ts +7 -6
  37. package/types/api/endpoint.d.ts +2 -0
  38. package/types/api/endpoint.types.d.ts +4 -1
  39. package/types/api/extensions/contentType.d.ts +1 -1
  40. package/types/api/helpers.d.ts +3 -1
  41. package/types/api/index.d.ts +1 -0
  42. package/types/structures/path/builder.d.ts +5 -4
  43. package/types/structures/path/builder.helpers.d.ts +8 -0
  44. package/types/structures/path/types.d.ts +32 -27
  45. package/types/structures/path/types.helpers.d.ts +23 -0
  46. 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 ArgValue = string | number;
4
- export type ObjectBuilderArgs<TArgs extends string, TOptional = false, TFallback = EmptyObject> = string extends TArgs ? TFallback : true extends TOptional ? MakeOptional<Record<TArgs, ArgValue>> : Record<TArgs, ArgValue>;
5
- export type BuilderArgs<TArgs extends string, L extends number = number> = LengthArray<ArgValue, L> | Record<TArgs, ArgValue>;
6
- export type TemplatePrefixing = Nullable<string | ((key: string, index: number) => string)>;
7
- type MakeOptional<T> = {
8
- [P in keyof T]?: Nullable<T[P]>;
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, P = TemplatePrefixing> {
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 `prefix` is provided as string
24
- * - format each key in case `prefix` is provided as function
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: (prefix?: P, options?: CombineOptions) => string;
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
- type ReadonlyArr<TArr extends ReadonlyArray<any>> = TArr extends ReadonlyArray<infer T> ? ReadonlyArray<T> : never;
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, true>;
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, any>;
65
- export type Output<TInput> = TInput extends StaticBuilder ? StaticBuilder : (TInput extends Builder<infer TArgs, infer TOptional> ? Builder<TArgs, TOptional> : StaticBuilder);
66
- export type SwitchBuilder<TArg extends readonly string[]> = [TArg] extends [never] ? StaticBuilder : ([] extends TArg ? StaticBuilder : (readonly string[] extends TArg ? IBuilder : Builder<TArg>));
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, infer O> ? ObjectBuilderArgs<TArgs[number], O, F> : F;
69
- type LogicalOr<O1, O2> = true extends O1 ? true : true extends O2 ? true : false;
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, infer O1> ? (Output<T2> extends Builder<infer Arr2, infer O2> ? Builder<[...Arr1, ...Arr2], LogicalOr<O1, O2>> : 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<any, any, any> & BaseInput ? (Rest extends readonly BaseInput[] ? CombineBuilders<readonly [C, ...Rest]> : never) : never) : (T extends readonly [infer T1] ? Output<T1> : StaticBuilder);
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
+ };
@@ -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;