@kunk/api 3.0.0
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/.turbo/turbo-build.log +12 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.d.mts +93 -0
- package/dist/index.mjs +1 -0
- package/package.json +17 -0
- package/src/contracts/+.ts +4 -0
- package/src/contracts/ContextData.ts +6 -0
- package/src/contracts/Generator.ts +8 -0
- package/src/contracts/Logger.ts +13 -0
- package/src/contracts/Validator.ts +17 -0
- package/src/index.ts +4 -0
- package/src/interfaces/+.ts +1 -0
- package/src/interfaces/ICommand.ts +8 -0
- package/src/utils/+.ts +5 -0
- package/src/utils/deep.ts +56 -0
- package/src/utils/error.ts +29 -0
- package/src/utils/infer.ts +31 -0
- package/tsconfig.json +11 -0
- package/tsdown.config.ts +11 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
$ tsdown
|
|
2
|
+
[34mℹ[39m tsdown [2mv0.20.3[22m powered by rolldown [2mv1.0.0-rc.3[22m
|
|
3
|
+
[34mℹ[39m config file: [4m/home/runner/work/kunk/kunk/packages/api/tsdown.config.ts[24m (unrun)
|
|
4
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
5
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
6
|
+
[34mℹ[39m Build start
|
|
7
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m0.65 kB[22m [2m│ gzip: 0.40 kB[22m
|
|
8
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m4.09 kB[22m [2m│ gzip: 1.28 kB[22m
|
|
9
|
+
[34mℹ[39m 2 files, total: 4.74 kB
|
|
10
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
11
|
+
|
|
12
|
+
[32m✔[39m Build complete in [32m4280ms[39m
|
package/CHANGELOG.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { StandardSchemaV1, StandardSchemaV1 as StandardSchemaV1$1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/ContextData.d.ts
|
|
4
|
+
declare class ContextData {
|
|
5
|
+
readonly tenantId?: string;
|
|
6
|
+
constructor(tenantId?: string);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/contracts/Generator.d.ts
|
|
10
|
+
declare abstract class Generator {
|
|
11
|
+
abstract get id(): string;
|
|
12
|
+
abstract code(length?: number): string;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/contracts/Logger.d.ts
|
|
16
|
+
declare abstract class Logger {
|
|
17
|
+
abstract info(message: string): void;
|
|
18
|
+
abstract info(message: string): void;
|
|
19
|
+
abstract warn(message: string): void;
|
|
20
|
+
abstract error(error: any): void;
|
|
21
|
+
abstract fork(name: string): Logger;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/contracts/Validator.d.ts
|
|
25
|
+
type IError = string | undefined | null;
|
|
26
|
+
declare class Validator {
|
|
27
|
+
messages: string[];
|
|
28
|
+
validate(...asserts: Array<Promise<IError> | IError>): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/interfaces/ICommand.d.ts
|
|
32
|
+
interface ICommand<IN extends StandardSchemaV1$1 = StandardSchemaV1$1, OUT extends StandardSchemaV1$1 = StandardSchemaV1$1> {
|
|
33
|
+
"~type": "kunk:command";
|
|
34
|
+
name: string;
|
|
35
|
+
input: IN;
|
|
36
|
+
output: OUT;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/utils/deep.d.ts
|
|
40
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
41
|
+
/**
|
|
42
|
+
* Tipos primitivos e built-in que não devem ser decompostos em paths
|
|
43
|
+
*/
|
|
44
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Tipos built-in que devem ser tratados como valores atômicos
|
|
47
|
+
*/
|
|
48
|
+
type BuiltIn = Primitive | Date | RegExp | Error | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove undefined de um tipo union
|
|
51
|
+
*/
|
|
52
|
+
type RemoveUndefined<T> = T extends undefined ? never : T;
|
|
53
|
+
type DeepKey<T, D extends number = 10> = T extends StandardSchemaV1$1 ? DeepKey<StandardSchemaV1$1.InferOutput<T>, D> : [D] extends [never] ? never : T extends BuiltIn ? never : T extends object ? { [K in keyof T]-?: K extends string | number ? RemoveUndefined<T[K]> extends BuiltIn ? `${K}` : RemoveUndefined<T[K]> extends Array<infer U> ? `${K}` | (U extends BuiltIn ? never : `${K}.${number}` | `${K}.${DeepKey<U, Prev[D]>}`) : RemoveUndefined<T[K]> extends object ? `${K}` | `${K}.${DeepKey<RemoveUndefined<T[K]>, Prev[D]>}` : `${K}` : never }[keyof T] : any;
|
|
54
|
+
type DeepValue<T, P extends string> = P extends keyof T ? T[P] : P extends `${infer First}.${infer Rest}` ? First extends keyof T ? Rest extends `${number}` ? RemoveUndefined<T[First]> extends Array<infer U> ? U : never : Rest extends `${number}.${infer After}` ? RemoveUndefined<T[First]> extends Array<infer U> ? DeepValue<U, After> : never : DeepValue<RemoveUndefined<T[First]>, Rest> : any : any;
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/utils/error.d.ts
|
|
57
|
+
interface IThrowable {
|
|
58
|
+
readonly type: "kunk:error";
|
|
59
|
+
readonly code: string;
|
|
60
|
+
readonly status: number;
|
|
61
|
+
readonly args: Record<string, any>;
|
|
62
|
+
}
|
|
63
|
+
declare namespace Throwable {
|
|
64
|
+
function create(code: string, status: number, args: Record<string, any>): IThrowable;
|
|
65
|
+
function is(error: any): error is IThrowable;
|
|
66
|
+
function forward(error: any): IThrowable;
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/utils/infer.d.ts
|
|
70
|
+
declare namespace I {
|
|
71
|
+
type Input<T> = T extends {
|
|
72
|
+
"~type": "kunk:aggregate";
|
|
73
|
+
schema: infer SCHEMA;
|
|
74
|
+
} ? Input<SCHEMA> : T extends {
|
|
75
|
+
"~type": "kunk:command";
|
|
76
|
+
input: infer INPUT;
|
|
77
|
+
} ? Input<INPUT> : T extends {
|
|
78
|
+
"~type": "kunk:schema";
|
|
79
|
+
input: infer INPUT;
|
|
80
|
+
} ? Input<INPUT> : T extends StandardSchemaV1$1 ? StandardSchemaV1$1.InferInput<T> : T extends object ? { [K in keyof T]: I.Input<T[K]> } : never;
|
|
81
|
+
type Output<T> = T extends {
|
|
82
|
+
"~type": "kunk:aggregate";
|
|
83
|
+
schema: infer SCHEMA;
|
|
84
|
+
} ? Output<SCHEMA> : T extends {
|
|
85
|
+
"~type": "kunk:command";
|
|
86
|
+
output: infer OUTPUT;
|
|
87
|
+
} ? Output<OUTPUT> : T extends {
|
|
88
|
+
"~type": "kunk:schema";
|
|
89
|
+
output: infer OUTPUT;
|
|
90
|
+
} ? Output<OUTPUT> : T extends StandardSchemaV1$1 ? StandardSchemaV1$1.InferOutput<T> : T extends object ? { [K in keyof T]: I.Output<T[K]> } : never;
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
export { ContextData, DeepKey, DeepValue, Generator, I, ICommand, IThrowable, Logger, type StandardSchemaV1, Throwable, Validator };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ContextData=class{constructor(e){this.tenantId=e}},Generator=class{},Logger=class{};let Throwable;(function(e){function t(e,t,n){return{type:`kunk:error`,code:e,status:t,args:n}}e.create=t;function n(e){return e.type===`kunk:error`}e.is=n;function r(e){return Throwable.is(e)?e:Throwable.create(e.code||`INTERNAL_SERVER_ERROR`,e.status||500,e.args||{message:e.message})}e.forward=r})(Throwable||={});var Validator=class{constructor(){this.messages=[]}async validate(...e){let t=(await Promise.all(e)).filter(Boolean);if(t.length>0)throw Throwable.create(`VALIDATION_ERROR`,422,{issues:t})}};export{ContextData,Generator,Logger,Throwable,Validator};
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kunk/api",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"module": "./src/index.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsdown"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@inversifyjs/common": "^1.5.2"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Throwable } from "@/utils/error"
|
|
2
|
+
|
|
3
|
+
type IError = string | undefined | null
|
|
4
|
+
|
|
5
|
+
export class Validator {
|
|
6
|
+
messages: string[] = []
|
|
7
|
+
|
|
8
|
+
async validate(
|
|
9
|
+
...asserts: Array<Promise<IError> | IError>
|
|
10
|
+
) {
|
|
11
|
+
const issues = await Promise.all(asserts) as string[]
|
|
12
|
+
const errors = issues.filter(Boolean)
|
|
13
|
+
if (errors.length > 0) {
|
|
14
|
+
throw Throwable.create("VALIDATION_ERROR", 422, { issues: errors })
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ICommand"
|
package/src/utils/+.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec"
|
|
2
|
+
|
|
3
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Tipos primitivos e built-in que não devem ser decompostos em paths
|
|
7
|
+
*/
|
|
8
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Tipos built-in que devem ser tratados como valores atômicos
|
|
12
|
+
*/
|
|
13
|
+
type BuiltIn = Primitive | Date | RegExp | Error | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Remove undefined de um tipo union
|
|
17
|
+
*/
|
|
18
|
+
type RemoveUndefined<T> = T extends undefined ? never : T
|
|
19
|
+
|
|
20
|
+
export type DeepKey<T, D extends number = 10> = T extends StandardSchemaV1
|
|
21
|
+
? DeepKey<StandardSchemaV1.InferOutput<T>, D>
|
|
22
|
+
: [D] extends [never]
|
|
23
|
+
? never
|
|
24
|
+
: T extends BuiltIn
|
|
25
|
+
? never
|
|
26
|
+
: T extends object
|
|
27
|
+
? {
|
|
28
|
+
[K in keyof T]-?: K extends string | number
|
|
29
|
+
? RemoveUndefined<T[K]> extends BuiltIn
|
|
30
|
+
? `${K}`
|
|
31
|
+
: RemoveUndefined<T[K]> extends Array<infer U>
|
|
32
|
+
?
|
|
33
|
+
| `${K}`
|
|
34
|
+
| (U extends BuiltIn ? never : `${K}.${number}` | `${K}.${DeepKey<U, Prev[D]>}`)
|
|
35
|
+
: RemoveUndefined<T[K]> extends object
|
|
36
|
+
? `${K}` | `${K}.${DeepKey<RemoveUndefined<T[K]>, Prev[D]>}`
|
|
37
|
+
: `${K}`
|
|
38
|
+
: never
|
|
39
|
+
}[keyof T]
|
|
40
|
+
: any
|
|
41
|
+
|
|
42
|
+
export type DeepValue<T, P extends string> = P extends keyof T
|
|
43
|
+
? T[P]
|
|
44
|
+
: P extends `${infer First}.${infer Rest}`
|
|
45
|
+
? First extends keyof T
|
|
46
|
+
? Rest extends `${number}`
|
|
47
|
+
? RemoveUndefined<T[First]> extends Array<infer U>
|
|
48
|
+
? U
|
|
49
|
+
: never
|
|
50
|
+
: Rest extends `${number}.${infer After}`
|
|
51
|
+
? RemoveUndefined<T[First]> extends Array<infer U>
|
|
52
|
+
? DeepValue<U, After>
|
|
53
|
+
: never
|
|
54
|
+
: DeepValue<RemoveUndefined<T[First]>, Rest>
|
|
55
|
+
: any
|
|
56
|
+
: any
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface IThrowable {
|
|
2
|
+
readonly type: "kunk:error",
|
|
3
|
+
readonly code: string,
|
|
4
|
+
readonly status: number,
|
|
5
|
+
readonly args: Record<string, any>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export namespace Throwable {
|
|
9
|
+
|
|
10
|
+
export function create(code: string, status: number, args: Record<string, any>): IThrowable {
|
|
11
|
+
return {
|
|
12
|
+
type: "kunk:error",
|
|
13
|
+
code,
|
|
14
|
+
status,
|
|
15
|
+
args
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function is(error: any): error is IThrowable {
|
|
20
|
+
return error.type === "kunk:error"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function forward(error: any): IThrowable {
|
|
24
|
+
if (Throwable.is(error)) {
|
|
25
|
+
return error
|
|
26
|
+
}
|
|
27
|
+
return Throwable.create(error.code || "INTERNAL_SERVER_ERROR", error.status || 500, error.args || { message: error.message })
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec"
|
|
2
|
+
|
|
3
|
+
export namespace I {
|
|
4
|
+
|
|
5
|
+
export type Input<T> =
|
|
6
|
+
T extends { "~type": "kunk:aggregate", schema: infer SCHEMA }
|
|
7
|
+
? Input<SCHEMA>
|
|
8
|
+
: T extends { "~type": "kunk:command", input: infer INPUT }
|
|
9
|
+
? Input<INPUT>
|
|
10
|
+
: T extends { "~type": "kunk:schema", input: infer INPUT }
|
|
11
|
+
? Input<INPUT>
|
|
12
|
+
: T extends StandardSchemaV1
|
|
13
|
+
? StandardSchemaV1.InferInput<T>
|
|
14
|
+
: T extends object
|
|
15
|
+
? { [K in keyof T]: I.Input<T[K]> }
|
|
16
|
+
: never
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export type Output<T> =
|
|
20
|
+
T extends { "~type": "kunk:aggregate", schema: infer SCHEMA }
|
|
21
|
+
? Output<SCHEMA>
|
|
22
|
+
: T extends { "~type": "kunk:command", output: infer OUTPUT }
|
|
23
|
+
? Output<OUTPUT>
|
|
24
|
+
: T extends { "~type": "kunk:schema", output: infer OUTPUT }
|
|
25
|
+
? Output<OUTPUT>
|
|
26
|
+
: T extends StandardSchemaV1
|
|
27
|
+
? StandardSchemaV1.InferOutput<T>
|
|
28
|
+
: T extends object
|
|
29
|
+
? { [K in keyof T]: I.Output<T[K]> }
|
|
30
|
+
: never
|
|
31
|
+
}
|
package/tsconfig.json
ADDED