@zajno/common 2.6.3 → 2.6.5

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 (66) hide show
  1. package/cjs/api/builder.js +6 -2
  2. package/cjs/api/builder.js.map +1 -1
  3. package/cjs/api/call.js +1 -1
  4. package/cjs/api/call.js.map +1 -1
  5. package/cjs/api/call.types.js +3 -0
  6. package/cjs/api/call.types.js.map +1 -0
  7. package/cjs/api/endpoint.js +4 -6
  8. package/cjs/api/endpoint.js.map +1 -1
  9. package/cjs/api/extensions/validation.js.map +1 -1
  10. package/cjs/api/helpers.js +4 -2
  11. package/cjs/api/helpers.js.map +1 -1
  12. package/cjs/async/arrays.js.map +1 -1
  13. package/cjs/dates/tz.js +19 -13
  14. package/cjs/dates/tz.js.map +1 -1
  15. package/cjs/observing/event.js +4 -4
  16. package/cjs/observing/event.js.map +1 -1
  17. package/cjs/observing/event.late.js +8 -0
  18. package/cjs/observing/event.late.js.map +1 -1
  19. package/cjs/observing/timeout.js +49 -0
  20. package/cjs/observing/timeout.js.map +1 -0
  21. package/cjs/observing/timer.js +5 -0
  22. package/cjs/observing/timer.js.map +1 -1
  23. package/cjs/structures/path/builder.js +2 -0
  24. package/cjs/structures/path/builder.js.map +1 -1
  25. package/esm/api/builder.js +6 -2
  26. package/esm/api/builder.js.map +1 -1
  27. package/esm/api/call.js +1 -1
  28. package/esm/api/call.js.map +1 -1
  29. package/esm/api/call.types.js +2 -0
  30. package/esm/api/call.types.js.map +1 -0
  31. package/esm/api/endpoint.js +4 -6
  32. package/esm/api/endpoint.js.map +1 -1
  33. package/esm/api/extensions/validation.js.map +1 -1
  34. package/esm/api/helpers.js +4 -2
  35. package/esm/api/helpers.js.map +1 -1
  36. package/esm/async/arrays.js.map +1 -1
  37. package/esm/dates/tz.js +19 -13
  38. package/esm/dates/tz.js.map +1 -1
  39. package/esm/observing/event.js +4 -4
  40. package/esm/observing/event.js.map +1 -1
  41. package/esm/observing/event.late.js +8 -0
  42. package/esm/observing/event.late.js.map +1 -1
  43. package/esm/observing/timeout.js +45 -0
  44. package/esm/observing/timeout.js.map +1 -0
  45. package/esm/observing/timer.js +5 -0
  46. package/esm/observing/timer.js.map +1 -1
  47. package/esm/structures/path/builder.js +2 -0
  48. package/esm/structures/path/builder.js.map +1 -1
  49. package/package.json +1 -1
  50. package/tsconfig.cjs.tsbuildinfo +1 -1
  51. package/tsconfig.esm.tsbuildinfo +1 -1
  52. package/tsconfig.types.tsbuildinfo +1 -1
  53. package/types/api/builder.d.ts +3 -4
  54. package/types/api/call.d.ts +3 -48
  55. package/types/api/call.types.d.ts +52 -0
  56. package/types/api/endpoint.d.ts +10 -16
  57. package/types/api/endpoint.types.d.ts +15 -6
  58. package/types/api/extensions/validation.d.ts +3 -2
  59. package/types/api/helpers.d.ts +1 -1
  60. package/types/async/arrays.d.ts +4 -4
  61. package/types/dates/tz.d.ts +14 -2
  62. package/types/observing/event.late.d.ts +8 -0
  63. package/types/observing/timeout.d.ts +25 -0
  64. package/types/observing/timer.d.ts +5 -0
  65. package/types/structures/path/types.d.ts +18 -7
  66. package/types/types/misc.d.ts +1 -0
@@ -1,12 +1,13 @@
1
+ import { AnyObject } from '../../types/misc.js';
1
2
  import type { ApiEndpoint } from '../endpoint.js';
2
3
  import type { IEndpointInfo } from '../endpoint.types.js';
3
4
  export interface IEndpointInputValidation {
4
- readonly validate?: IEndpointInputValidation.Validator<IEndpointInfo.ExtractIn<this>>;
5
+ readonly validate?: IEndpointInputValidation.Validator<IEndpointInfo.ExtractIn<this, AnyObject>>;
5
6
  withValidation(validator: IEndpointInputValidation.Validator<IEndpointInfo.ExtractIn<this>>): this;
6
7
  }
7
8
  export declare namespace IEndpointInputValidation {
8
9
  type Validator<TIn> = (input: TIn) => Promise<void> | void;
9
10
  const extender: ApiEndpoint.IBuilderExtender<IEndpointInputValidation>;
10
11
  function guard(api: IEndpointInfo): api is (IEndpointInfo & IEndpointInputValidation);
11
- function tryValidate(api: IEndpointInfo, input: IEndpointInfo.ExtractIn<IEndpointInfo>): void | Promise<void>;
12
+ function tryValidate(api: IEndpointInfo, input: AnyObject): void | Promise<void>;
12
13
  }
@@ -4,6 +4,6 @@ export declare const DefaultSettings: {
4
4
  basePrefix: string;
5
5
  };
6
6
  export declare function setDefaults(settings: Partial<typeof DefaultSettings>): void;
7
- export declare function getPath<T extends IEndpointInfo.IPath<K>, K extends readonly string[]>(endpoint: T, pathArgs: IEndpointInfo.ExtractPath<T>, prefix?: string | boolean): string;
7
+ export declare function getPath<T extends IEndpointInfo.Base & IEndpointInfo.IPathAbstract>(endpoint: T, pathArgs: IEndpointInfo.ExtractPath<T>, prefix?: string | boolean): string;
8
8
  export declare function getTemplate<T extends IEndpointInfo>(endpoint: T, prefix?: string | boolean): string;
9
9
  export declare function getFormattedDisplayName(endpoint: IEndpointInfo): string;
@@ -1,4 +1,4 @@
1
- export declare function someAsync<T>(target: ReadonlyArray<T>, condition: (v: T, index?: number, arr?: ReadonlyArray<T>) => Promise<boolean>): Promise<boolean>;
2
- export declare function everyAsync<T>(target: ReadonlyArray<T>, condition: (v: T, index?: number, arr?: ReadonlyArray<T>) => Promise<boolean>): Promise<boolean>;
3
- export declare function forEachAsync<T>(target: ReadonlyArray<T>, cb: (v: T, index?: number, arr?: ReadonlyArray<T>) => Promise<void>): Promise<void>;
4
- export declare function mapAsync<T, R>(target: ReadonlyArray<T>, cb: (v: T, index?: number, arr?: ReadonlyArray<T>) => Promise<R>): Promise<R[]>;
1
+ export declare function someAsync<T>(target: ReadonlyArray<T>, condition: (v: T, index: number, arr: ReadonlyArray<T>) => Promise<boolean>): Promise<boolean>;
2
+ export declare function everyAsync<T>(target: ReadonlyArray<T>, condition: (v: T, index: number, arr: ReadonlyArray<T>) => Promise<boolean>): Promise<boolean>;
3
+ export declare function forEachAsync<T>(target: ReadonlyArray<T>, cb: (v: T, index: number, arr: ReadonlyArray<T>) => Promise<void>): Promise<void>;
4
+ export declare function mapAsync<T, R>(target: ReadonlyArray<T>, cb: (v: T, index: number, arr: ReadonlyArray<T>) => Promise<R>): Promise<R[]>;
@@ -1,7 +1,19 @@
1
- /** This is experimental stuff, so no tests provided, see comments in the code */
1
+ /** Helpers for time zones, based on `Intl.DateTimeFormat` */
2
2
  export declare namespace Timezones {
3
3
  const Current: string;
4
4
  const CurrentOffset: number;
5
- const getOffset: (timeZone: string) => number;
5
+ /**
6
+ * Get the offset in minutes for a given time zone and date. Uses Intl.DateTimeFormat
7
+ * to get the time zone name, then parses it to get the offset.
8
+ * @param timeZone time zone name, e.g. 'America/New_York', 'Europe/Kyiv', etc.
9
+ * @param date date to get the offset for. If not provided, uses the current date.
10
+ * @returns offset in minutes. Positive values are west of UTC, negative values are east of UTC.
11
+ */
12
+ function getOffset(timeZone: string, date?: Date | number): number;
13
+ /** Returns a new date with the specified timezone offset applied.
14
+ *
15
+ * Usage case can be imagined like this: if your local wall clock shows time X,
16
+ * what your clock will show when another local wall clock in the specified timezone will be showing time X.
17
+ */
6
18
  function shiftToTimeZone(date: Date, tz: string | number): Date;
7
19
  }
@@ -1,4 +1,12 @@
1
1
  import { Event, EventHandler } from './event.js';
2
+ /**
3
+ * One-time event that can be triggered only once, but listeners will be notified at the time of event or subscription, if it happens later.
4
+ *
5
+ * Useful for solving race conditions, when components initialization order is undetermined.
6
+ *
7
+ * Example usage: app initialization finished event, but some async components can subscribe later and be notified;
8
+ * So subscribers can be sure that the main initialization is finished.
9
+ */
2
10
  export declare class OneTimeLateEvent<T = any> extends Event<T> {
3
11
  private _triggeredWith;
4
12
  private _triggered;
@@ -0,0 +1,25 @@
1
+ import type { IDisposable } from '../functions/disposer.js';
2
+ import { type IEvent } from './event.js';
3
+ export interface ITimeout extends IDisposable {
4
+ /** Fired when the timeout has elapsed. Passes actual elapsed milliseconds. */
5
+ readonly event: IEvent<number>;
6
+ /** Whether the timeout is running */
7
+ readonly isRunning: boolean;
8
+ /** Starts the timeout */
9
+ start(delay: number, callback?: () => void): Promise<number>;
10
+ /** Stops the timeout, event will not be triggered. */
11
+ stop(): void;
12
+ }
13
+ /**
14
+ * Simple one-off `setTimeout` wrapper/abstraction.
15
+ * Handle timeout via event, callback or Promise.
16
+ */
17
+ export declare class Timeout implements ITimeout {
18
+ private _cancelCb;
19
+ private readonly _event;
20
+ get event(): IEvent<number>;
21
+ get isRunning(): boolean;
22
+ start(delay: number, callback?: () => void): Promise<number>;
23
+ stop(): void;
24
+ dispose(): void;
25
+ }
@@ -6,6 +6,11 @@ export type TimerState = {
6
6
  left: number;
7
7
  now: number;
8
8
  };
9
+ /**
10
+ * A timer wraps `setInterval` and provides a more convenient API.
11
+ * It will fire an event every `interval` milliseconds.
12
+ * It also may have duration which means it will stop when elapsed time is greater than or equal to the duration.
13
+ */
9
14
  export declare class Timer implements IDisposable, IEvent<TimerState> {
10
15
  readonly interval: number;
11
16
  private readonly _event;
@@ -1,9 +1,13 @@
1
- import type { LengthArray, Nullable } from '../../types/misc.js';
1
+ import type { EmptyObject, LengthArray, Nullable } from '../../types/misc.js';
2
2
  import type { CombineOptions } from './utils.js';
3
3
  export type ArgValue = string | number;
4
- export type ObjectBuilderArgs<TArgs extends string> = Record<TArgs, ArgValue>;
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
5
  export type BuilderArgs<TArgs extends string, L extends number = number> = LengthArray<ArgValue, L> | Record<TArgs, ArgValue>;
6
6
  export type TemplatePrefixing = Nullable<string | ((key: string, index: number) => string)>;
7
+ type MakeOptional<T> = {
8
+ [P in keyof T]?: Nullable<T[P]>;
9
+ };
10
+ /** Internal, Don't use it directly */
7
11
  interface BaseBuilder<A extends readonly string[], B, P = TemplatePrefixing> {
8
12
  /**
9
13
  * Builds the path using provided args.
@@ -45,17 +49,24 @@ export type TransformMap<A extends readonly string[]> = {
45
49
  };
46
50
  type EmptyBuilderArgs = [] | Record<PropertyKey, never>;
47
51
  type ReadonlyArr<TArr extends ReadonlyArray<any>> = TArr extends ReadonlyArray<infer T> ? ReadonlyArray<T> : never;
48
- export interface Builder<TArgs extends readonly string[]> extends BaseBuilder<ReadonlyArr<TArgs>, BuilderArgs<TArgs[number], TArgs['length']>> {
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']>> {
53
+ /** Marks input type for `build` to be `Partial`, so any/all arguments can be omitted.
54
+ *
55
+ * **Limitation**: will convert to optional ALL parameters if >=2 Builders combined via CombineBuilders
56
+ */
57
+ asOptional(): Builder<TArgs, true>;
49
58
  }
50
59
  export interface StaticBuilder extends BaseBuilder<readonly [], EmptyBuilderArgs> {
51
60
  }
52
- export interface IBuilder extends BaseBuilder<readonly string[], BuilderArgs<string>> {
61
+ export interface IBuilder extends BaseBuilder<readonly string[], any> {
53
62
  }
54
63
  export type StaticInput = string | readonly string[];
55
64
  export type BaseInput = StaticInput | BaseBuilder<any, any, any>;
56
- export type Output<TInput> = TInput extends StaticBuilder ? StaticBuilder : (TInput extends Builder<infer TArgs> ? Builder<TArgs> : StaticBuilder);
65
+ export type Output<TInput> = TInput extends StaticBuilder ? StaticBuilder : (TInput extends Builder<infer TArgs, infer TOptional> ? Builder<TArgs, TOptional> : StaticBuilder);
57
66
  export type SwitchBuilder<TArg extends readonly string[]> = [TArg] extends [never] ? StaticBuilder : ([] extends TArg ? StaticBuilder : (readonly string[] extends TArg ? IBuilder : Builder<TArg>));
58
67
  export type ExtractArgs<T> = T extends Builder<infer TArgs> ? TArgs : readonly string[];
59
- 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));
60
- export type CombineBuilders<T extends BaseInput[]> = T extends [infer T1 extends BaseInput, infer T2 extends BaseInput, ...infer Rest] ? (CombineTwo<T1, T2> extends infer C extends BaseBuilder<any, any> & BaseInput ? (Rest extends BaseInput[] ? CombineBuilders<[C, ...Rest]> : never) : never) : (T extends [infer T1] ? Output<T1> : StaticBuilder);
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);
61
72
  export {};
@@ -34,4 +34,5 @@ export type PickNullable<T, K extends keyof T> = {
34
34
  [P in K]?: Nullable<T[P]>;
35
35
  };
36
36
  export type EmptyObjectNullable = (EmptyObject | null | undefined);
37
+ export type IsNever<T, Y, N> = [T] extends [never] ? Y : N;
37
38
  export type Coalesce<T, TReplace = EmptyObjectNullable> = [T] extends [never] | [null] | [undefined] ? TReplace : T;