@arkstack/common 0.12.6 → 0.12.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.
|
@@ -11,8 +11,29 @@ declare class Encryption {
|
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/utils/hash.d.ts
|
|
13
13
|
declare class Hash {
|
|
14
|
+
/**
|
|
15
|
+
* Hash a value using bcrypt
|
|
16
|
+
*
|
|
17
|
+
* @param value
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
14
20
|
static make(value: string): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Verify a value against a hashed value
|
|
23
|
+
*
|
|
24
|
+
* @param value
|
|
25
|
+
* @param hashedValue
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
15
28
|
static verify(value: string, hashedValue: string): Promise<boolean>;
|
|
29
|
+
/**
|
|
30
|
+
* Generate a one-time password (OTP) using TOTP algorithm
|
|
31
|
+
*
|
|
32
|
+
* @param digits The number of digits for the OTP, default is 6.
|
|
33
|
+
* @param label A label to identify the OTP, can be an email or phone number.
|
|
34
|
+
* @param period Interval of time for which a token is valid, in seconds.
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
16
37
|
static otp(digits?: number, label?: string, period?: number): TOTP;
|
|
17
38
|
static totp(secret: string, label: string, issuer?: string, period?: number): TOTP;
|
|
18
39
|
}
|
|
@@ -22,10 +43,24 @@ type AbstractModelConstructor<TModel = unknown> = abstract new (attributes?: Rec
|
|
|
22
43
|
type ModelConstructor<TModel extends Model = Model> = AbstractModelConstructor<TModel> & Pick<ModelStatic<TModel>, keyof ModelStatic<TModel>>;
|
|
23
44
|
interface ModelRegistry {}
|
|
24
45
|
type ModelName = Extract<keyof ModelRegistry, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Determine the number of items to return per page based on the provided query parameters.
|
|
48
|
+
*
|
|
49
|
+
* @param query
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
25
52
|
declare const perPage: (query: {
|
|
26
53
|
limit?: number;
|
|
27
54
|
perPage?: number;
|
|
28
55
|
}) => number;
|
|
56
|
+
/**
|
|
57
|
+
* Import an application model by name.
|
|
58
|
+
*
|
|
59
|
+
* Apps can augment `ModelRegistry` to make `getModel('User')` return `typeof User`.
|
|
60
|
+
* Without a registry entry, pass the class type explicitly: `getModel<typeof User>('User')`.
|
|
61
|
+
*
|
|
62
|
+
* @param modelName
|
|
63
|
+
*/
|
|
29
64
|
declare function getModel<TName extends ModelName>(modelName: TName): Promise<ModelRegistry[TName]>;
|
|
30
65
|
declare function getModel<TModel extends AbstractModelConstructor = ModelConstructor>(modelName: string): Promise<TModel>;
|
|
31
66
|
declare const initializeGlobalContext: ({
|
|
@@ -37,8 +72,31 @@ declare const initializeGlobalContext: ({
|
|
|
37
72
|
Response?: any;
|
|
38
73
|
Session?: any;
|
|
39
74
|
}) => Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Thows to abort the current request
|
|
77
|
+
*
|
|
78
|
+
* @param message
|
|
79
|
+
* @param code
|
|
80
|
+
* @throws {RequestException}
|
|
81
|
+
*/
|
|
40
82
|
declare const abort: (message?: string, code?: number) => void;
|
|
83
|
+
/**
|
|
84
|
+
* Asserts that a boolean condition is true.
|
|
85
|
+
*
|
|
86
|
+
* @param boolean
|
|
87
|
+
* @param message
|
|
88
|
+
* @param code
|
|
89
|
+
* @throws {RequestException} Throws if the boolean condition is true.
|
|
90
|
+
*/
|
|
41
91
|
declare const abortIf: <T>(boolean: T, message?: string, code?: number) => asserts boolean is T;
|
|
92
|
+
/**
|
|
93
|
+
* Asserts that a value is not null or undefined.
|
|
94
|
+
*
|
|
95
|
+
* @param value
|
|
96
|
+
* @param message
|
|
97
|
+
* @param code
|
|
98
|
+
* @throws {RequestException} Throws if the value is null or undefined.
|
|
99
|
+
*/
|
|
42
100
|
declare const assertFound: <T>(value: T | null | undefined, message: string, code?: number) => asserts value is T;
|
|
43
101
|
//#endregion
|
|
44
102
|
export { abortIf as a, initializeGlobalContext as c, Encryption as d, abort as i, perPage as l, ModelConstructor as n, assertFound as o, ModelRegistry as r, getModel as s, AbstractModelConstructor as t, Hash as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference path="./app.d.ts" />
|
|
2
|
-
import { a as abortIf, c as initializeGlobalContext, d as Encryption, i as abort, l as perPage, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as Hash } from "./helpers-
|
|
2
|
+
import { a as abortIf, c as initializeGlobalContext, d as Encryption, i as abort, l as perPage, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as Hash } from "./helpers-DALo42cp.js";
|
|
3
3
|
import { JitiOptions, JitiResolveOptions } from "jiti";
|
|
4
4
|
import { Arkstack } from "@arkstack/contract";
|
|
5
5
|
import pino from "pino";
|
|
@@ -9,6 +9,15 @@ import { ChalkInstance } from "chalk";
|
|
|
9
9
|
declare const bindGracefulShutdown: (shutdown: () => Promise<void> | void, defer?: boolean) => void;
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region src/network.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Boots the app using an available port close to the requested port
|
|
14
|
+
*when requested port is not available
|
|
15
|
+
*
|
|
16
|
+
* @param boot
|
|
17
|
+
* @param preferredPort
|
|
18
|
+
* @param app
|
|
19
|
+
* @param defer
|
|
20
|
+
*/
|
|
12
21
|
declare const bootWithDetectedPort: <TApp, TRoutes = unknown, THandler = unknown>(boot: (port: number) => Promise<void>, preferredPort?: number, app?: Arkstack<TApp, TRoutes, THandler>, defer?: boolean) => Promise<void>;
|
|
13
22
|
declare const renderError: ({
|
|
14
23
|
message,
|
|
@@ -34,31 +43,130 @@ declare class Console {
|
|
|
34
43
|
static error: (...args: any[]) => void[];
|
|
35
44
|
}
|
|
36
45
|
declare class Logger {
|
|
46
|
+
/**
|
|
47
|
+
* Global verbosity configuration
|
|
48
|
+
*/
|
|
37
49
|
private static verbosity;
|
|
38
50
|
private static isQuiet;
|
|
39
51
|
private static isSilent;
|
|
52
|
+
/**
|
|
53
|
+
* Configure global verbosity levels
|
|
54
|
+
*/
|
|
40
55
|
static configure(options?: {
|
|
41
56
|
verbosity?: number;
|
|
42
57
|
quiet?: boolean;
|
|
43
58
|
silent?: boolean;
|
|
44
59
|
}): void;
|
|
60
|
+
/**
|
|
61
|
+
* Check if output should be suppressed
|
|
62
|
+
*/
|
|
45
63
|
private static shouldSuppressOutput;
|
|
64
|
+
/**
|
|
65
|
+
* Logs the message in two columns
|
|
66
|
+
*
|
|
67
|
+
* @param name
|
|
68
|
+
* @param value
|
|
69
|
+
* @param log If set to false, array of [name, dots, value] output will be returned and not logged
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
46
72
|
static twoColumnDetail(name: string, value: string, log?: true, spacer?: string): void;
|
|
47
73
|
static twoColumnDetail(name: string, value: string, log?: false, spacer?: string): [string, string, string];
|
|
74
|
+
/**
|
|
75
|
+
* Logs the message in two columns
|
|
76
|
+
*
|
|
77
|
+
* @param name
|
|
78
|
+
* @param desc
|
|
79
|
+
* @param width
|
|
80
|
+
* @param log If set to false, array of [name, dots, value] output will be returned and not logged
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
48
83
|
static describe(name: string, desc: string, width?: number, log?: true): void;
|
|
49
84
|
static describe(name: string, desc: string, width?: number, log?: false): [string, string, string];
|
|
85
|
+
/**
|
|
86
|
+
* Logs the message in two columns but allways passing status
|
|
87
|
+
*
|
|
88
|
+
* @param name
|
|
89
|
+
* @param value
|
|
90
|
+
* @param status
|
|
91
|
+
* @param exit
|
|
92
|
+
* @param preserveCol
|
|
93
|
+
*/
|
|
50
94
|
static split(name: string, value: string, status?: 'success' | 'info' | 'error', exit?: boolean, preserveCol?: boolean, spacer?: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Wraps text with chalk
|
|
97
|
+
*
|
|
98
|
+
* @param txt
|
|
99
|
+
* @param color
|
|
100
|
+
* @param preserveCol
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
51
103
|
static textFormat(txt: unknown | unknown[], color: (...text: unknown[]) => string, preserveCol?: boolean): string;
|
|
104
|
+
/**
|
|
105
|
+
* Logs a success message
|
|
106
|
+
*
|
|
107
|
+
* @param msg
|
|
108
|
+
* @param exit
|
|
109
|
+
* @param preserveCol
|
|
110
|
+
*/
|
|
52
111
|
static success(msg: any, exit?: boolean, preserveCol?: boolean): void;
|
|
112
|
+
/**
|
|
113
|
+
* Logs an informational message
|
|
114
|
+
*
|
|
115
|
+
* @param msg
|
|
116
|
+
* @param exit
|
|
117
|
+
* @param preserveCol
|
|
118
|
+
*/
|
|
53
119
|
static info(msg: any, exit?: boolean, preserveCol?: boolean): void;
|
|
120
|
+
/**
|
|
121
|
+
* Logs an error message
|
|
122
|
+
*
|
|
123
|
+
* @param msg
|
|
124
|
+
* @param exit
|
|
125
|
+
* @param preserveCol
|
|
126
|
+
*/
|
|
54
127
|
static error(msg: any, exit?: boolean, preserveCol?: boolean): void;
|
|
128
|
+
/**
|
|
129
|
+
* Logs a warning message
|
|
130
|
+
*
|
|
131
|
+
* @param msg
|
|
132
|
+
* @param exit
|
|
133
|
+
* @param preserveCol
|
|
134
|
+
*/
|
|
55
135
|
static warn(msg: any, exit?: boolean, preserveCol?: boolean): void;
|
|
136
|
+
/**
|
|
137
|
+
* Logs a debug message (only shown with verbosity >= 3)
|
|
138
|
+
*
|
|
139
|
+
* @param msg
|
|
140
|
+
* @param exit
|
|
141
|
+
* @param preserveCol
|
|
142
|
+
*/
|
|
56
143
|
static debug<M = any>(msg: M | M[], exit?: boolean, preserveCol?: boolean): void;
|
|
144
|
+
/**
|
|
145
|
+
* Terminates the process
|
|
146
|
+
*/
|
|
57
147
|
static quiet(): void;
|
|
58
148
|
static chalker(styles: LoggerChalk[]): (input: any) => string;
|
|
149
|
+
/**
|
|
150
|
+
* Parse an array formated message and logs it
|
|
151
|
+
*
|
|
152
|
+
* @param config
|
|
153
|
+
* @param joiner
|
|
154
|
+
* @param log If set to false, string output will be returned and not logged
|
|
155
|
+
* @param sc color to use ue on split text if : is found
|
|
156
|
+
*/
|
|
59
157
|
static parse(config: LoggerParseSignature, joiner?: string, log?: true, sc?: LoggerChalk): void;
|
|
60
158
|
static parse(config: LoggerParseSignature, joiner?: string, log?: false, sc?: LoggerChalk): string;
|
|
159
|
+
/**
|
|
160
|
+
* Ouput formater object or format the output
|
|
161
|
+
*
|
|
162
|
+
* @returns
|
|
163
|
+
*/
|
|
61
164
|
static log: LoggerLog;
|
|
165
|
+
/**
|
|
166
|
+
* A simple console like output logger
|
|
167
|
+
*
|
|
168
|
+
* @returns
|
|
169
|
+
*/
|
|
62
170
|
static console(): typeof Console;
|
|
63
171
|
}
|
|
64
172
|
//#endregion
|
|
@@ -72,6 +180,16 @@ type LoggerChalk = keyof ChalkInstance | ChalkInstance | (keyof ChalkInstance)[]
|
|
|
72
180
|
type LoggerParseSignature = [string, LoggerChalk][];
|
|
73
181
|
type DotPathValue<T, P extends string> = P extends `${infer Head}.${infer Tail}` ? Head extends keyof T ? DotPathValue<T[Head], Tail> : never : P extends keyof T ? T[P] : never;
|
|
74
182
|
type DotPath<T> = T extends Primitive ? never : T extends any[] ? never : { [K in keyof T & string]: T[K] extends Primitive ? `${K}` : T[K] extends any[] ? `${K}` : `${K}` | `${K}.${DotPath<T[K]>}` }[keyof T & string];
|
|
183
|
+
/**
|
|
184
|
+
* Ouput formater object or format the output
|
|
185
|
+
*
|
|
186
|
+
* @param config
|
|
187
|
+
* @param joiner
|
|
188
|
+
* @param log If set to false, string output will be returned and not logged
|
|
189
|
+
* @param sc color to use ue on split text if : is found
|
|
190
|
+
*
|
|
191
|
+
* @returns
|
|
192
|
+
*/
|
|
75
193
|
interface LoggerLog {
|
|
76
194
|
(): typeof Logger;
|
|
77
195
|
<L extends boolean>(config: string, joiner: LoggerChalk, log?: L, sc?: LoggerChalk): L extends true ? void : string;
|
|
@@ -120,13 +238,63 @@ type HookPositions<N extends string> = N extends keyof HookRegistry ? keyof Hook
|
|
|
120
238
|
type HookArgs<N extends string, P extends string> = N extends keyof HookRegistry ? P extends keyof HookRegistry[N] ? HookRegistry[N][P] extends ((...args: infer A) => any) ? A : any[] : any[] : any[];
|
|
121
239
|
//#endregion
|
|
122
240
|
//#region src/system.d.ts
|
|
241
|
+
/**
|
|
242
|
+
* Read the .env file
|
|
243
|
+
*
|
|
244
|
+
* @param env
|
|
245
|
+
* @param def
|
|
246
|
+
* @returns
|
|
247
|
+
*/
|
|
123
248
|
declare const env: GlobalEnv;
|
|
249
|
+
/**
|
|
250
|
+
* Build the app url
|
|
251
|
+
*
|
|
252
|
+
* @param link
|
|
253
|
+
* @returns
|
|
254
|
+
*/
|
|
124
255
|
declare const appUrl: (link?: string) => string;
|
|
125
256
|
declare const CONFIG_KEY: unique symbol;
|
|
257
|
+
/**
|
|
258
|
+
* Gets the application configuration.
|
|
259
|
+
*
|
|
260
|
+
* @param key The configuration key to retrieve.
|
|
261
|
+
* @param defaultValue The default value to return if the key is not found.
|
|
262
|
+
* @returns The configuration value.
|
|
263
|
+
*/
|
|
126
264
|
declare const config: GlobalConfig;
|
|
265
|
+
/**
|
|
266
|
+
* Gets the current Node environment (development or production).
|
|
267
|
+
*
|
|
268
|
+
* @returns
|
|
269
|
+
*/
|
|
127
270
|
declare const nodeEnv: () => "prod" | "dev";
|
|
271
|
+
/**
|
|
272
|
+
* Gets the output directory for the application based on the current environment.
|
|
273
|
+
*
|
|
274
|
+
* @param cwd The current working directory (optional, defaults to Arkstack.rootDir()).
|
|
275
|
+
* @returns
|
|
276
|
+
*/
|
|
128
277
|
declare const outputDir: (cwd?: string) => string;
|
|
278
|
+
/**
|
|
279
|
+
*
|
|
280
|
+
* Dynamically imports a file at the given path with full TypeScript support,
|
|
281
|
+
* including `tsconfig.json` path aliases.
|
|
282
|
+
*
|
|
283
|
+
* @param filePath - The path to the file to import.
|
|
284
|
+
* @returns The imported module typed as `T`.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* const config = await importFile<AppConfig>('./config/app.ts')
|
|
288
|
+
*/
|
|
129
289
|
declare const importFile: FileImporter;
|
|
290
|
+
/**
|
|
291
|
+
* Resolves the default export from a module, handling both CJS and ESM interop.
|
|
292
|
+
* In CJS modules, the default export is often the module itself (a function or object),
|
|
293
|
+
* while in ESM the default is nested under the `default` property.
|
|
294
|
+
*
|
|
295
|
+
* @param imp - The imported module
|
|
296
|
+
* @returns The resolved default export
|
|
297
|
+
*/
|
|
130
298
|
declare const interopDefault: <T>(imp: T | {
|
|
131
299
|
default: T;
|
|
132
300
|
}) => T;
|
|
@@ -150,8 +318,33 @@ declare class AppException extends Exception {
|
|
|
150
318
|
declare class RequestException extends AppException {
|
|
151
319
|
statusCode: number;
|
|
152
320
|
constructor(message?: string, statusCode?: number, options?: ErrorOptions);
|
|
321
|
+
/**
|
|
322
|
+
* Asserts that a value is not null or undefined.
|
|
323
|
+
*
|
|
324
|
+
* @param value
|
|
325
|
+
* @param message
|
|
326
|
+
* @param code
|
|
327
|
+
* @throws {RequestException} Throws if the value is null or undefined.
|
|
328
|
+
*/
|
|
153
329
|
static assertFound<T>(value: T | null | undefined, message: string, code?: number): asserts value is T;
|
|
330
|
+
/**
|
|
331
|
+
* Asserts that a value is not null or undefined.
|
|
332
|
+
*
|
|
333
|
+
* @param value
|
|
334
|
+
* @param message
|
|
335
|
+
* @param code
|
|
336
|
+
* @throws {RequestException} Throws if the value is null or undefined.
|
|
337
|
+
* @deprecated Use assertFound instead
|
|
338
|
+
*/
|
|
154
339
|
static assertNotEmpty<T>(value: T | null | undefined, message: string, code?: number): asserts value is T;
|
|
340
|
+
/**
|
|
341
|
+
* Asserts that a boolean condition is true.
|
|
342
|
+
*
|
|
343
|
+
* @param boolean
|
|
344
|
+
* @param message
|
|
345
|
+
* @param code
|
|
346
|
+
* @throws {RequestException} Throws if the boolean condition is true.
|
|
347
|
+
*/
|
|
155
348
|
static abortIf<T>(boolean: T, message: string, code?: number): asserts boolean is T;
|
|
156
349
|
}
|
|
157
350
|
//#endregion
|
|
@@ -188,13 +381,54 @@ declare const logUnhandledError: typeof ErrorHandler.logUnhandledError;
|
|
|
188
381
|
//#region src/Hook.d.ts
|
|
189
382
|
declare class Hook {
|
|
190
383
|
private static hooks;
|
|
384
|
+
/**
|
|
385
|
+
* Hooks define code that should run within defined boundaries to add extra functionalities.
|
|
386
|
+
*
|
|
387
|
+
* @param name
|
|
388
|
+
* @param value
|
|
389
|
+
*/
|
|
191
390
|
static set<N extends HookName>(name: N | (string & {}), hook: HookFor<N>): void;
|
|
391
|
+
/**
|
|
392
|
+
* Check if a hook is defined by name
|
|
393
|
+
*
|
|
394
|
+
* @param name
|
|
395
|
+
*/
|
|
192
396
|
static has<N extends HookName, P extends HookPositions<N>>(name: N | (string & {}), pos?: P): boolean;
|
|
397
|
+
/**
|
|
398
|
+
* Retrieve a defined hook by name
|
|
399
|
+
*
|
|
400
|
+
* @param name
|
|
401
|
+
*/
|
|
193
402
|
static get<N extends HookName>(name: N): HookFor<N> | undefined;
|
|
403
|
+
/**
|
|
404
|
+
* Retrieve a defined hook by name and position
|
|
405
|
+
*
|
|
406
|
+
* @param name
|
|
407
|
+
* @param pos
|
|
408
|
+
*/
|
|
194
409
|
static get<N extends HookName, P extends HookPositions<N>>(name: N, pos: P): HookPos<N, P> | undefined;
|
|
410
|
+
/**
|
|
411
|
+
* Retrieve a defined hook by name and position the set args for callback
|
|
412
|
+
*
|
|
413
|
+
* @param name
|
|
414
|
+
* @param pos
|
|
415
|
+
*/
|
|
195
416
|
static get<N extends HookName, P extends HookPositions<N>>(name: N, pos: P, ...args: HookArgs<N, P>): void;
|
|
417
|
+
/**
|
|
418
|
+
* Retrieve all defined hooks
|
|
419
|
+
*
|
|
420
|
+
* @param name
|
|
421
|
+
*/
|
|
196
422
|
static getAll: () => Record<string, IHook> & HookRegistry;
|
|
423
|
+
/**
|
|
424
|
+
* Remove the defined hook
|
|
425
|
+
*
|
|
426
|
+
* @param name
|
|
427
|
+
*/
|
|
197
428
|
static unset<N extends HookName, P extends HookPositions<N>>(name?: N | (string & {}), pos?: P): undefined;
|
|
429
|
+
/**
|
|
430
|
+
* Clear all the defined hooks
|
|
431
|
+
*/
|
|
198
432
|
static clear: () => void;
|
|
199
433
|
}
|
|
200
434
|
//#endregion
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as abortIf, c as initializeGlobalContext, d as Encryption, i as abort, l as perPage, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as Hash } from "../helpers-
|
|
1
|
+
import { a as abortIf, c as initializeGlobalContext, d as Encryption, i as abort, l as perPage, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as Hash } from "../helpers-DALo42cp.js";
|
|
2
2
|
import { Model } from "arkormx";
|
|
3
3
|
|
|
4
4
|
//#region src/utils/traits.d.ts
|
|
@@ -17,14 +17,32 @@ type CombineClasses<T extends Array<(new () => any) & {
|
|
|
17
17
|
prototype: Combine<MapClassesToPrototypes<T>>;
|
|
18
18
|
};
|
|
19
19
|
type ResolveTraitLikeArray<T extends Array<Trait | TypeFactory<Trait>>> = CombineClasses<{ [K in keyof T]: ResolveTraitLike<T[K]> }>;
|
|
20
|
+
/**
|
|
21
|
+
* utility type and function: constructor (function)
|
|
22
|
+
*/
|
|
20
23
|
type Cons<T = any> = new (...args: any[]) => T;
|
|
21
24
|
type ArkormModelCons<T extends Model = Model> = abstract new (...args: any[]) => T;
|
|
22
25
|
type DirectCons<T = any> = Cons<T> | ArkormModelCons<Model>;
|
|
23
26
|
type DirectBase = DirectCons | Model;
|
|
27
|
+
/**
|
|
28
|
+
* utility type and function: constructor factory (function)
|
|
29
|
+
*/
|
|
24
30
|
type ConsFactory<T extends Cons = Cons, B = any> = (base: B) => T;
|
|
31
|
+
/**
|
|
32
|
+
* utility type and function: type factory (function)
|
|
33
|
+
*/
|
|
25
34
|
type TypeFactory<T = any> = () => T;
|
|
35
|
+
/**
|
|
36
|
+
* utility type: map an object type into a bare properties type
|
|
37
|
+
*/
|
|
26
38
|
type Explode<T = any> = { [P in keyof T]: T[P] };
|
|
39
|
+
/**
|
|
40
|
+
* utility type: convert two arrays of types into an array of union types
|
|
41
|
+
*/
|
|
27
42
|
type MixParams<T1 extends any[], T2 extends any[]> = T1 extends [] ? (T2 extends [] ? [] : T2) : (T2 extends [] ? T1 : (T1 extends [infer H1, ...infer R1] ? (T2 extends [infer H2, ...infer R2] ? [H1 & H2, ...MixParams<R1, R2>] : []) : []));
|
|
43
|
+
/**
|
|
44
|
+
* API: trait type
|
|
45
|
+
*/
|
|
28
46
|
type TraitDefTypeT = ConsFactory<Cons>;
|
|
29
47
|
type TraitDefTypeST = (Trait | TypeFactory<Trait>)[] | undefined;
|
|
30
48
|
type Trait<T extends TraitDefTypeT = TraitDefTypeT, ST extends TraitDefTypeST = TraitDefTypeST> = {
|
|
@@ -33,30 +51,111 @@ type Trait<T extends TraitDefTypeT = TraitDefTypeT, ST extends TraitDefTypeST =
|
|
|
33
51
|
factory: T;
|
|
34
52
|
superTraits: ST;
|
|
35
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* API: generate trait (regular variant)
|
|
56
|
+
*
|
|
57
|
+
* @param factory
|
|
58
|
+
*/
|
|
36
59
|
declare function trait<T extends ConsFactory<Cons>>(factory: T): Trait<T, undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* API: generate trait (super-trait variant)
|
|
62
|
+
*
|
|
63
|
+
* @param superTraits
|
|
64
|
+
* @param factory
|
|
65
|
+
*/
|
|
37
66
|
declare function trait<const ST extends (Trait | TypeFactory<Trait>)[], T extends ConsFactory<Cons, ResolveTraitLikeArray<ST>>>(superTraits: ST, factory: T): Trait<T, ST>;
|
|
67
|
+
/**
|
|
68
|
+
* utility types: extract factory from a trait
|
|
69
|
+
*/
|
|
38
70
|
type ExtractFactory<T extends Trait> = T extends Trait<ConsFactory<infer C>, TraitDefTypeST> ? C : never;
|
|
71
|
+
/**
|
|
72
|
+
* utility types: extract supertraits from a trait
|
|
73
|
+
*/
|
|
39
74
|
type ExtractSuperTrait<T extends Trait> = T extends Trait<TraitDefTypeT, infer ST extends TraitDefTypeST> ? ST : never;
|
|
75
|
+
/**
|
|
76
|
+
* utility type: derive type constructor: merge two constructors
|
|
77
|
+
*/
|
|
40
78
|
type DeriveTraitsConsConsMerge<A extends Cons, B extends Cons> = A extends (new (...args: infer ArgsA) => infer RetA) ? (B extends (new (...args: infer ArgsB) => infer RetB) ? (new (...args: MixParams<ArgsA, ArgsB>) => RetA & RetB) : never) : never;
|
|
79
|
+
/**
|
|
80
|
+
* utility type: derive type constructor: extract plain constructor
|
|
81
|
+
*/
|
|
41
82
|
type DeriveTraitsConsCons<T extends DirectCons> = new (...args: ConstructorParameters<T>) => InstanceType<T>;
|
|
42
83
|
type DeriveTraitsConsDirectBase<T extends DirectBase> = T extends Model ? new (...args: any[]) => T : T extends DirectCons ? DeriveTraitsConsCons<T> : never;
|
|
84
|
+
/**
|
|
85
|
+
* utility type: derive type constructor: from trait parts
|
|
86
|
+
*/
|
|
43
87
|
type DeriveTraitsConsTraitParts<C extends Cons, ST extends ((Trait | TypeFactory<Trait>)[] | undefined)> = ST extends undefined ? DeriveTraitsConsCons<C> : ST extends [] ? DeriveTraitsConsCons<C> : DeriveTraitsConsConsMerge<DeriveTraitsConsCons<C>, DeriveTraitsConsAll<ST>>;
|
|
88
|
+
/**
|
|
89
|
+
* utility type: derive type constructor: from single trait
|
|
90
|
+
*/
|
|
44
91
|
type DeriveTraitsConsTrait<T extends Trait> = DeriveTraitsConsTraitParts<ExtractFactory<T>, ExtractSuperTrait<T>>;
|
|
92
|
+
/**
|
|
93
|
+
* utility type: derive type constructor: from single trait or trait factory
|
|
94
|
+
*/
|
|
45
95
|
type DeriveTraitsConsOne<T extends (Trait | TypeFactory<Trait>)> = T extends Trait ? DeriveTraitsConsTrait<T> : T extends TypeFactory<Trait> ? DeriveTraitsConsTrait<ReturnType<T>> : never;
|
|
46
|
-
|
|
96
|
+
/**
|
|
97
|
+
* utility type: derive type constructor: from one or more traits or trait factories
|
|
98
|
+
*/
|
|
99
|
+
type DeriveTraitsConsAll<T extends (((Trait | TypeFactory<Trait>)[] | [...(Trait | TypeFactory<Trait>)[], DirectBase]) | undefined)> = T extends [infer Only extends DirectBase] ? DeriveTraitsConsDirectBase<Only> : T extends [...infer Others extends (Trait | TypeFactory<Trait>)[], infer Last extends DirectBase] ? (Others extends [] ? DeriveTraitsConsDirectBase<Last> : DeriveTraitsConsConsMerge<DeriveTraitsConsAll<Others>, /* RECURSION */DeriveTraitsConsDirectBase<Last>>) : T extends (Trait | TypeFactory<Trait>)[] ? (T extends [infer First extends (Trait | TypeFactory<Trait>)] ? (DeriveTraitsConsOne<First>) : (T extends [infer First extends (Trait | TypeFactory<Trait>), ...infer Rest extends (Trait | TypeFactory<Trait>)[]] ? (DeriveTraitsConsConsMerge<DeriveTraitsConsOne<First>, DeriveTraitsConsAll<Rest>>) : never)) : never;
|
|
100
|
+
/**
|
|
101
|
+
* utility type: derive type constructor
|
|
102
|
+
*/
|
|
47
103
|
type DeriveTraitsCons<T extends ((Trait | TypeFactory<Trait>)[] | [...(Trait | TypeFactory<Trait>)[], DirectBase])> = DeriveTraitsConsAll<T>;
|
|
104
|
+
/**
|
|
105
|
+
* utility type: derive type statics: merge two objects with statics
|
|
106
|
+
*/
|
|
48
107
|
type DeriveTraitsStatsConsMerge<T1 extends object, T2 extends object> = T1 & T2;
|
|
108
|
+
/**
|
|
109
|
+
* utility type: derive type statics: extract plain statics
|
|
110
|
+
*/
|
|
49
111
|
type DeriveTraitsStatsCons<T extends DirectCons> = Explode<T>;
|
|
50
112
|
type DeriveTraitsStatsDirectBase<T extends DirectBase> = T extends Model ? object : T extends DirectCons ? DeriveTraitsStatsCons<T> : never;
|
|
113
|
+
/**
|
|
114
|
+
* utility type: derive type statics: from trait parts
|
|
115
|
+
*/
|
|
51
116
|
type DeriveTraitsStatsTraitParts<C extends Cons, ST extends ((Trait | TypeFactory<Trait>)[] | undefined)> = ST extends undefined ? DeriveTraitsStatsCons<C> : ST extends [] ? DeriveTraitsStatsCons<C> : DeriveTraitsStatsConsMerge<DeriveTraitsStatsCons<C>, DeriveTraitsStatsAll<ST>>;
|
|
117
|
+
/**
|
|
118
|
+
* utility type: derive type statics: from single trait
|
|
119
|
+
*/
|
|
52
120
|
type DeriveTraitsStatsTrait<T extends Trait> = DeriveTraitsStatsTraitParts<ExtractFactory<T>, ExtractSuperTrait<T>>;
|
|
121
|
+
/**
|
|
122
|
+
* utility type: derive type statics: from single trait or trait factory
|
|
123
|
+
*/
|
|
53
124
|
type DeriveTraitsStatsOne<T extends (Trait | TypeFactory<Trait>)> = T extends Trait ? DeriveTraitsStatsTrait<T> : T extends TypeFactory<Trait> ? DeriveTraitsStatsTrait<ReturnType<T>> : never;
|
|
54
|
-
|
|
125
|
+
/**
|
|
126
|
+
* utility type: derive type statics: from one or more traits or trait factories
|
|
127
|
+
*/
|
|
128
|
+
type DeriveTraitsStatsAll<T extends (((Trait | TypeFactory<Trait>)[] | [...(Trait | TypeFactory<Trait>)[], DirectBase]) | undefined)> = T extends [infer Only extends DirectBase] ? DeriveTraitsStatsDirectBase<Only> : T extends [...infer Others extends (Trait | TypeFactory<Trait>)[], infer Last extends DirectBase] ? (Others extends [] ? DeriveTraitsStatsDirectBase<Last> : DeriveTraitsStatsConsMerge<DeriveTraitsStatsAll<Others>, /* RECURSION */DeriveTraitsStatsDirectBase<Last>>) : T extends (Trait | TypeFactory<Trait>)[] ? (T extends [infer First extends (Trait | TypeFactory<Trait>)] ? (DeriveTraitsStatsOne<First>) : (T extends [infer First extends (Trait | TypeFactory<Trait>), ...infer Rest extends (Trait | TypeFactory<Trait>)[]] ? (DeriveTraitsStatsConsMerge<DeriveTraitsStatsOne<First>, DeriveTraitsStatsAll<Rest>>) : never)) : never;
|
|
129
|
+
/**
|
|
130
|
+
* utility type: derive type statics
|
|
131
|
+
*/
|
|
55
132
|
type DeriveTraitsStats<T extends ((Trait | TypeFactory<Trait>)[] | [...(Trait | TypeFactory<Trait>)[], DirectBase])> = DeriveTraitsStatsAll<T>;
|
|
133
|
+
/**
|
|
134
|
+
* utility type: derive type from one or more traits or trait type factories
|
|
135
|
+
*/
|
|
56
136
|
type DeriveTraits<T extends ((Trait | TypeFactory<Trait>)[] | [...(Trait | TypeFactory<Trait>)[], DirectBase])> = DeriveTraitsCons<T> & DeriveTraitsStats<T>;
|
|
137
|
+
/**
|
|
138
|
+
* API: derive a class from one or more traits or trait type factories
|
|
139
|
+
*
|
|
140
|
+
* @param traits
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
57
143
|
declare function use<T extends ([Trait | TypeFactory<Trait>, ...(Trait | TypeFactory<Trait>)[]] | [...(Trait | TypeFactory<Trait>)[], DirectBase])>(...traits: T): DeriveTraits<T>;
|
|
144
|
+
/**
|
|
145
|
+
* internal type: implements trait type
|
|
146
|
+
*/
|
|
58
147
|
type DerivedType<T extends Trait> = InstanceType<ExtractFactory<T>>;
|
|
148
|
+
/**
|
|
149
|
+
* internal type: implements trait type or trait type factory
|
|
150
|
+
*/
|
|
59
151
|
type Derived<T extends (Trait | TypeFactory<Trait> | Cons)> = T extends TypeFactory<Trait> ? DerivedType<ReturnType<T>> : T extends Trait ? DerivedType<T> : T extends Cons ? T : never;
|
|
152
|
+
/**
|
|
153
|
+
* API: type guard for checking whether class instance is derived from a trait
|
|
154
|
+
*
|
|
155
|
+
* @param instance
|
|
156
|
+
* @param trait
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
60
159
|
declare function uses<T extends (Trait | TypeFactory<Trait> | Cons)>(instance: unknown, trait: T): instance is Derived<T>;
|
|
61
160
|
//#endregion
|
|
62
161
|
export { AbstractModelConstructor, Derived, Encryption, Hash, ModelConstructor, ModelRegistry, Trait, abort, abortIf, assertFound, crc32, getModel, initializeGlobalContext, perPage, trait, use, uses };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkstack/common",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Core utilities, primitives, and shared infrastructure for the Arkstack ecosystem.",
|
|
6
6
|
"homepage": "https://arkstack.toneflix.net",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"@h3ravel/support": "^0.15.11",
|
|
44
44
|
"arkormx": "^ 2.4.1",
|
|
45
|
-
"@arkstack/
|
|
46
|
-
"@arkstack/
|
|
45
|
+
"@arkstack/foundry": "^0.12.7",
|
|
46
|
+
"@arkstack/contract": "^0.12.7"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsdown --config-loader unrun",
|