@deessejs/functions 0.0.63 → 0.0.65
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/dist/api/index.js +7 -26
- package/dist/context/define.d.ts +11 -6
- package/dist/context/define.js +43 -20
- package/dist/extensions/index.d.ts +7 -10
- package/dist/extensions/index.js +6 -16
- package/dist/extensions/rpc.d.ts +18 -16
- package/dist/extensions/rpc.js +15 -13
- package/dist/extensions/types.d.ts +23 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/utils/hkt.d.ts +7 -0
- package/dist/utils/hkt.js +2 -0
- package/package.json +111 -111
- package/dist/context/generated.d.ts +0 -2
- package/dist/context/generated.js +0 -40
- package/dist/context/generator.d.ts +0 -9
- package/dist/context/generator.js +0 -116
- package/dist/context/typing.d.ts +0 -10
- package/dist/context/typing.js +0 -60
package/dist/api/index.js
CHANGED
|
@@ -1,39 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createAPI = createAPI;
|
|
4
|
-
const
|
|
5
|
-
const parse_1 = require("../functions/parse");
|
|
6
|
-
const types_1 = require("../types");
|
|
4
|
+
const query_1 = require("../functions/query");
|
|
7
5
|
function createAPI(config) {
|
|
8
6
|
const hydrate = (node) => {
|
|
9
7
|
if (node &&
|
|
10
8
|
typeof node === "object" &&
|
|
11
9
|
(node._type === "query" || node._type === "mutation")) {
|
|
12
10
|
const def = node;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
},
|
|
19
|
-
onFailure: (error) => {
|
|
20
|
-
const ValidationError = (0, errors_1.exception)({
|
|
21
|
-
name: "ValidationError",
|
|
22
|
-
message: error.message,
|
|
23
|
-
});
|
|
24
|
-
return Promise.resolve((0, types_1.failure)(ValidationError));
|
|
25
|
-
},
|
|
26
|
-
});
|
|
11
|
+
const api = { context: ctx };
|
|
12
|
+
const query = () => {
|
|
13
|
+
return (options) => {
|
|
14
|
+
return (0, query_1.query)(options);
|
|
15
|
+
};
|
|
27
16
|
};
|
|
17
|
+
return hydrate(config.root);
|
|
28
18
|
}
|
|
29
|
-
if (node && typeof node === "object") {
|
|
30
|
-
const group = {};
|
|
31
|
-
for (const key in node) {
|
|
32
|
-
group[key] = hydrate(node[key]);
|
|
33
|
-
}
|
|
34
|
-
return group;
|
|
35
|
-
}
|
|
36
|
-
return node;
|
|
37
19
|
};
|
|
38
|
-
return hydrate(config.root);
|
|
39
20
|
}
|
package/dist/context/define.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { ExtensionWithHKT, ExtractContext, MergeAPIs, TransformAPI } from "../extensions/types";
|
|
2
|
+
import { UnionToIntersection } from "../utils";
|
|
3
|
+
export declare const defineContext: <const InitCtx = {}>(defaultContext?: InitCtx) => {
|
|
4
|
+
withExtensions: <const Exts extends readonly ExtensionWithHKT<any>[]>(extensions: Exts) => {
|
|
5
|
+
t: MergeAPIs<Exts, InitCtx & UnionToIntersection<ExtractContext<Exts[number]>>> & {
|
|
6
|
+
router: <R>(routes: R) => R;
|
|
7
|
+
};
|
|
8
|
+
createAPI: <Root>(options: {
|
|
9
|
+
root: Root;
|
|
10
|
+
context?: InitCtx | (() => Promise<InitCtx> | InitCtx);
|
|
11
|
+
}) => TransformAPI<Root>;
|
|
7
12
|
};
|
|
8
13
|
};
|
package/dist/context/define.js
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defineContext =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
exports.defineContext = void 0;
|
|
4
|
+
const defineContext = (defaultContext = {}) => ({
|
|
5
|
+
withExtensions: (extensions) => {
|
|
6
|
+
// --- Runtime t ---
|
|
7
|
+
const t = extensions.reduce((acc, ext) => ({
|
|
8
|
+
...acc,
|
|
9
|
+
...(ext.functions ? ext.functions() : {})
|
|
10
|
+
}), { router: (r) => r });
|
|
11
|
+
// --- Runtime createAPI ---
|
|
12
|
+
const createAPI = (options) => {
|
|
13
|
+
const states = new Map();
|
|
14
|
+
extensions.forEach((e) => e.init && states.set(e.name, e.init()));
|
|
15
|
+
const getContext = async () => {
|
|
16
|
+
let base = defaultContext;
|
|
17
|
+
if (options.context) {
|
|
18
|
+
base = typeof options.context === 'function'
|
|
19
|
+
? await options.context()
|
|
20
|
+
: options.context;
|
|
21
|
+
}
|
|
22
|
+
let ctx = { ...base };
|
|
23
|
+
for (const ext of extensions) {
|
|
24
|
+
if (ext.request) {
|
|
25
|
+
const part = await ext.request(states.get(ext.name), ctx);
|
|
26
|
+
ctx = { ...ctx, ...part };
|
|
27
|
+
}
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
...runtimeBuilder,
|
|
20
|
-
context: {},
|
|
29
|
+
return ctx;
|
|
21
30
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
const activate = (node) => {
|
|
32
|
+
if (typeof node === "function")
|
|
33
|
+
return node(getContext);
|
|
34
|
+
if (typeof node === "object" && node !== null) {
|
|
35
|
+
const res = {};
|
|
36
|
+
for (const k in node)
|
|
37
|
+
res[k] = activate(node[k]);
|
|
38
|
+
return res;
|
|
39
|
+
}
|
|
40
|
+
return node;
|
|
41
|
+
};
|
|
42
|
+
return activate(options.root);
|
|
43
|
+
};
|
|
44
|
+
return { t, createAPI };
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
exports.defineContext = defineContext;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}): (...args: any[]) => {
|
|
9
|
-
context: TContextAddon;
|
|
10
|
-
functions: TFunctionsFactory;
|
|
1
|
+
import { HKT } from "../utils/hkt";
|
|
2
|
+
import { DefaultHKT, ExtensionConfig } from "./types";
|
|
3
|
+
export declare const extension: <const C extends ExtensionConfig>(config: C) => C & {
|
|
4
|
+
readonly _HKT: DefaultHKT;
|
|
5
|
+
};
|
|
6
|
+
export declare const withKind: <Kind extends HKT>() => <Config extends ExtensionConfig>(config: Config) => Config & {
|
|
7
|
+
readonly _HKT: Kind;
|
|
11
8
|
};
|
package/dist/extensions/index.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const optionsInput = args[0];
|
|
10
|
-
const options = (config.schema ? config.schema.parse(optionsInput) : undefined);
|
|
11
|
-
const contextPart = config.context ? config.context(options) : {};
|
|
12
|
-
const functionsBuilder = config.functions(options);
|
|
13
|
-
return {
|
|
14
|
-
context: contextPart,
|
|
15
|
-
functions: functionsBuilder,
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
}
|
|
3
|
+
exports.withKind = exports.extension = void 0;
|
|
4
|
+
const extension = (config) => config;
|
|
5
|
+
exports.extension = extension;
|
|
6
|
+
// 4. Helper 'withKind'
|
|
7
|
+
const withKind = () => (config) => config;
|
|
8
|
+
exports.withKind = withKind;
|
package/dist/extensions/rpc.d.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
2
2
|
import { Exception } from "../errors/types";
|
|
3
3
|
import { AsyncResult } from "../types";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import { HKT } from "../utils/hkt";
|
|
5
|
+
type CoreAPI<C> = {
|
|
6
|
+
mutation: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception>(options: {
|
|
7
|
+
args: TArgs;
|
|
8
|
+
handler: (ctx: C, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
|
9
|
+
}) => (contextProvider: () => Promise<C>) => (input: z.input<TArgs>) => AsyncResult<TOutput, TError>;
|
|
7
10
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}) => QueryDefinition<C, TArgs, TOutput, TError>;
|
|
16
|
-
mutation: <TArgs extends ZodType<any, any, any>, TOutput_1, TError_1 extends Exception>(options: {
|
|
17
|
-
args: TArgs;
|
|
18
|
-
handler: (ctx: C, args: z.core.output<TArgs>) => AsyncResult<TOutput_1, TError_1>;
|
|
19
|
-
}) => MutationDefinition<C, TArgs, TOutput_1, TError_1>;
|
|
20
|
-
group: <T extends Record<string, APINode>>(definitions: T) => T;
|
|
11
|
+
interface MutationHKT extends HKT {
|
|
12
|
+
new: CoreAPI<this["_C"]>;
|
|
13
|
+
}
|
|
14
|
+
export declare const rpc: {
|
|
15
|
+
readonly name: "core";
|
|
16
|
+
readonly functions: <C>() => {
|
|
17
|
+
mutation: (options: any) => (contextProvider: () => Promise<C>) => (input: any) => Promise<any>;
|
|
21
18
|
};
|
|
19
|
+
} & {
|
|
20
|
+
readonly _HKT: import("./types").DefaultHKT;
|
|
21
|
+
} & {
|
|
22
|
+
readonly _HKT: MutationHKT;
|
|
22
23
|
};
|
|
24
|
+
export {};
|
package/dist/extensions/rpc.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.rpc = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
4
5
|
const _1 = require(".");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
const parse_1 = require("../functions/parse");
|
|
7
|
+
exports.rpc = (0, _1.withKind)()((0, _1.extension)({
|
|
8
|
+
name: "core",
|
|
9
|
+
functions: () => ({
|
|
10
|
+
mutation: (options) => (contextProvider) => async (input) => {
|
|
11
|
+
const parsed = (0, parse_1.parseArgs)(options.args, input);
|
|
12
|
+
return parsed.match({
|
|
13
|
+
onSuccess: async (data) => {
|
|
14
|
+
const ctx = await contextProvider();
|
|
15
|
+
return options.handler(ctx, data);
|
|
16
|
+
},
|
|
17
|
+
onFailure: (error) => Promise.resolve((0, types_1.failure)(error)),
|
|
18
|
+
});
|
|
13
19
|
},
|
|
14
|
-
mutation: (options) => {
|
|
15
|
-
return { _type: "mutation", ...options };
|
|
16
|
-
},
|
|
17
|
-
group: (definitions) => definitions,
|
|
18
20
|
}),
|
|
19
|
-
});
|
|
21
|
+
}));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
2
|
+
import { Apply, HKT } from "../utils/hkt";
|
|
2
3
|
/**
|
|
3
4
|
* Maintenant que F est une fonction générique "propre" (grâce à l'étape 1),
|
|
4
5
|
* l'inférence directe fonctionne parfaitement.
|
|
@@ -14,4 +15,26 @@ export type ExtensionBase = {
|
|
|
14
15
|
functions: any;
|
|
15
16
|
};
|
|
16
17
|
export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
|
|
18
|
+
export type ExtensionConfig = {
|
|
19
|
+
name: string;
|
|
20
|
+
init?: () => any;
|
|
21
|
+
request?: (state: any, ctx: any) => any;
|
|
22
|
+
functions?: <C>() => any;
|
|
23
|
+
};
|
|
24
|
+
export interface DefaultHKT extends HKT {
|
|
25
|
+
readonly new: {};
|
|
26
|
+
}
|
|
27
|
+
export type ExtensionWithHKT<Kind extends HKT> = ExtensionConfig & {
|
|
28
|
+
readonly _HKT: Kind;
|
|
29
|
+
};
|
|
30
|
+
export type ExtractContext<T> = T extends {
|
|
31
|
+
request: (...args: any) => infer R;
|
|
32
|
+
} ? Awaited<R> : {};
|
|
33
|
+
export type GetHKT<T> = T extends {
|
|
34
|
+
_HKT: infer K;
|
|
35
|
+
} ? (K extends HKT ? K : DefaultHKT) : DefaultHKT;
|
|
36
|
+
export type TransformAPI<T> = T extends (ctxProvider: any) => infer Func ? Func : T extends object ? {
|
|
37
|
+
[K in keyof T]: TransformAPI<T[K]>;
|
|
38
|
+
} : T;
|
|
39
|
+
export type MergeAPIs<Exts extends readonly any[], Ctx> = Exts extends readonly [infer Head, ...infer Tail] ? Apply<GetHKT<Head>, Ctx> & MergeAPIs<Tail, Ctx> : {};
|
|
17
40
|
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,8 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./api"), exports);
|
|
18
|
-
__exportStar(require("./api/types"), exports);
|
|
19
17
|
__exportStar(require("./functions"), exports);
|
|
20
18
|
__exportStar(require("./types"), exports);
|
|
21
19
|
__exportStar(require("./extensions/rpc"), exports);
|
package/package.json
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@deessejs/functions",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "A powerful utility library for building type-safe APIs and functions with context management",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"module": "dist/index.esm.js",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.esm.js",
|
|
12
|
-
"require": "./dist/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./api": {
|
|
15
|
-
"types": "./dist/api.d.ts",
|
|
16
|
-
"import": "./dist/api.esm.js",
|
|
17
|
-
"require": "./dist/api.js"
|
|
18
|
-
},
|
|
19
|
-
"./checks": {
|
|
20
|
-
"types": "./dist/checks.d.ts",
|
|
21
|
-
"import": "./dist/checks.esm.js",
|
|
22
|
-
"require": "./dist/checks.js"
|
|
23
|
-
},
|
|
24
|
-
"./context": {
|
|
25
|
-
"types": "./dist/context.d.ts",
|
|
26
|
-
"import": "./dist/context.esm.js",
|
|
27
|
-
"require": "./dist/context.js"
|
|
28
|
-
},
|
|
29
|
-
"./errors": {
|
|
30
|
-
"types": "./dist/errors.d.ts",
|
|
31
|
-
"import": "./dist/errors.esm.js",
|
|
32
|
-
"require": "./dist/errors.js"
|
|
33
|
-
},
|
|
34
|
-
"./events": {
|
|
35
|
-
"types": "./dist/events.d.ts",
|
|
36
|
-
"import": "./dist/events.esm.js",
|
|
37
|
-
"require": "./dist/events.js"
|
|
38
|
-
},
|
|
39
|
-
"./functions": {
|
|
40
|
-
"types": "./dist/functions.d.ts",
|
|
41
|
-
"import": "./dist/functions.esm.js",
|
|
42
|
-
"require": "./dist/functions.js"
|
|
43
|
-
},
|
|
44
|
-
"./types": {
|
|
45
|
-
"types": "./dist/types.d.ts",
|
|
46
|
-
"import": "./dist/types.esm.js",
|
|
47
|
-
"require": "./dist/types.js"
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
"files": [
|
|
51
|
-
"dist/**/*",
|
|
52
|
-
"README.md",
|
|
53
|
-
"LICENSE"
|
|
54
|
-
],
|
|
55
|
-
"scripts": {
|
|
56
|
-
"build": "tsc",
|
|
57
|
-
"build:watch": "tsc --watch",
|
|
58
|
-
"prepublishOnly": "npm run build",
|
|
59
|
-
"test": "vitest",
|
|
60
|
-
"test:ui": "vitest --ui",
|
|
61
|
-
"test:run": "vitest run",
|
|
62
|
-
"test:coverage": "vitest run --coverage",
|
|
63
|
-
"test:watch": "vitest watch",
|
|
64
|
-
"lint": "eslint src --ext .ts",
|
|
65
|
-
"lint:fix": "eslint src --ext .ts --fix"
|
|
66
|
-
},
|
|
67
|
-
"keywords": [
|
|
68
|
-
"typescript",
|
|
69
|
-
"api",
|
|
70
|
-
"functions",
|
|
71
|
-
"context",
|
|
72
|
-
"validation",
|
|
73
|
-
"zod",
|
|
74
|
-
"type-safe",
|
|
75
|
-
"utility"
|
|
76
|
-
],
|
|
77
|
-
"author": "Votre Nom",
|
|
78
|
-
"license": "MIT",
|
|
79
|
-
"repository": {
|
|
80
|
-
"type": "git",
|
|
81
|
-
"url": "https://github.com/votre-organisation/deesse-api.git",
|
|
82
|
-
"directory": "packages/functions"
|
|
83
|
-
},
|
|
84
|
-
"bugs": {
|
|
85
|
-
"url": "https://github.com/votre-organisation/deesse-api/issues"
|
|
86
|
-
},
|
|
87
|
-
"homepage": "https://github.com/votre-organisation/deesse-api#readme",
|
|
88
|
-
"engines": {
|
|
89
|
-
"node": ">=16.0.0"
|
|
90
|
-
},
|
|
91
|
-
"packageManager": "pnpm@10.11.1",
|
|
92
|
-
"dependencies": {
|
|
93
|
-
"zod": "^4.1.12"
|
|
94
|
-
},
|
|
95
|
-
"devDependencies": {
|
|
96
|
-
"@testing-library/dom": "^10.4.1",
|
|
97
|
-
"@testing-library/user-event": "^14.6.1",
|
|
98
|
-
"@types/jsdom": "^27.0.0",
|
|
99
|
-
"@types/node": "^20.0.0",
|
|
100
|
-
"@types/sinon": "^21.0.0",
|
|
101
|
-
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
102
|
-
"@typescript-eslint/parser": "^6.0.0",
|
|
103
|
-
"@vitest/coverage-v8": "^4.0.13",
|
|
104
|
-
"@vitest/ui": "^4.0.13",
|
|
105
|
-
"eslint": "^8.0.0",
|
|
106
|
-
"jsdom": "^27.2.0",
|
|
107
|
-
"sinon": "^21.0.0",
|
|
108
|
-
"typescript": "^5.0.0",
|
|
109
|
-
"vitest": "^4.0.13"
|
|
110
|
-
}
|
|
111
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@deessejs/functions",
|
|
3
|
+
"version": "0.0.65",
|
|
4
|
+
"description": "A powerful utility library for building type-safe APIs and functions with context management",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.esm.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./api": {
|
|
15
|
+
"types": "./dist/api.d.ts",
|
|
16
|
+
"import": "./dist/api.esm.js",
|
|
17
|
+
"require": "./dist/api.js"
|
|
18
|
+
},
|
|
19
|
+
"./checks": {
|
|
20
|
+
"types": "./dist/checks.d.ts",
|
|
21
|
+
"import": "./dist/checks.esm.js",
|
|
22
|
+
"require": "./dist/checks.js"
|
|
23
|
+
},
|
|
24
|
+
"./context": {
|
|
25
|
+
"types": "./dist/context.d.ts",
|
|
26
|
+
"import": "./dist/context.esm.js",
|
|
27
|
+
"require": "./dist/context.js"
|
|
28
|
+
},
|
|
29
|
+
"./errors": {
|
|
30
|
+
"types": "./dist/errors.d.ts",
|
|
31
|
+
"import": "./dist/errors.esm.js",
|
|
32
|
+
"require": "./dist/errors.js"
|
|
33
|
+
},
|
|
34
|
+
"./events": {
|
|
35
|
+
"types": "./dist/events.d.ts",
|
|
36
|
+
"import": "./dist/events.esm.js",
|
|
37
|
+
"require": "./dist/events.js"
|
|
38
|
+
},
|
|
39
|
+
"./functions": {
|
|
40
|
+
"types": "./dist/functions.d.ts",
|
|
41
|
+
"import": "./dist/functions.esm.js",
|
|
42
|
+
"require": "./dist/functions.js"
|
|
43
|
+
},
|
|
44
|
+
"./types": {
|
|
45
|
+
"types": "./dist/types.d.ts",
|
|
46
|
+
"import": "./dist/types.esm.js",
|
|
47
|
+
"require": "./dist/types.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist/**/*",
|
|
52
|
+
"README.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc",
|
|
57
|
+
"build:watch": "tsc --watch",
|
|
58
|
+
"prepublishOnly": "npm run build",
|
|
59
|
+
"test": "vitest",
|
|
60
|
+
"test:ui": "vitest --ui",
|
|
61
|
+
"test:run": "vitest run",
|
|
62
|
+
"test:coverage": "vitest run --coverage",
|
|
63
|
+
"test:watch": "vitest watch",
|
|
64
|
+
"lint": "eslint src --ext .ts",
|
|
65
|
+
"lint:fix": "eslint src --ext .ts --fix"
|
|
66
|
+
},
|
|
67
|
+
"keywords": [
|
|
68
|
+
"typescript",
|
|
69
|
+
"api",
|
|
70
|
+
"functions",
|
|
71
|
+
"context",
|
|
72
|
+
"validation",
|
|
73
|
+
"zod",
|
|
74
|
+
"type-safe",
|
|
75
|
+
"utility"
|
|
76
|
+
],
|
|
77
|
+
"author": "Votre Nom",
|
|
78
|
+
"license": "MIT",
|
|
79
|
+
"repository": {
|
|
80
|
+
"type": "git",
|
|
81
|
+
"url": "https://github.com/votre-organisation/deesse-api.git",
|
|
82
|
+
"directory": "packages/functions"
|
|
83
|
+
},
|
|
84
|
+
"bugs": {
|
|
85
|
+
"url": "https://github.com/votre-organisation/deesse-api/issues"
|
|
86
|
+
},
|
|
87
|
+
"homepage": "https://github.com/votre-organisation/deesse-api#readme",
|
|
88
|
+
"engines": {
|
|
89
|
+
"node": ">=16.0.0"
|
|
90
|
+
},
|
|
91
|
+
"packageManager": "pnpm@10.11.1",
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"zod": "^4.1.12"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@testing-library/dom": "^10.4.1",
|
|
97
|
+
"@testing-library/user-event": "^14.6.1",
|
|
98
|
+
"@types/jsdom": "^27.0.0",
|
|
99
|
+
"@types/node": "^20.0.0",
|
|
100
|
+
"@types/sinon": "^21.0.0",
|
|
101
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
102
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
103
|
+
"@vitest/coverage-v8": "^4.0.13",
|
|
104
|
+
"@vitest/ui": "^4.0.13",
|
|
105
|
+
"eslint": "^8.0.0",
|
|
106
|
+
"jsdom": "^27.2.0",
|
|
107
|
+
"sinon": "^21.0.0",
|
|
108
|
+
"typescript": "^5.0.0",
|
|
109
|
+
"vitest": "^4.0.13"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// This file is automatically generated by the context type generator.
|
|
3
|
-
// Do not modify this file directly. It will be overwritten when new context types are generated.
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
16
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
17
|
-
};
|
|
18
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
// Import utilities for context access
|
|
20
|
-
__exportStar(require("./index"), exports);
|
|
21
|
-
// Generated context types will be added here by the generator
|
|
22
|
-
// Example:
|
|
23
|
-
// declare module "./generated" {
|
|
24
|
-
// export interface AppContext {
|
|
25
|
-
// user: null;
|
|
26
|
-
// permissions: string[];
|
|
27
|
-
// settings: {
|
|
28
|
-
// theme: string;
|
|
29
|
-
// language: string;
|
|
30
|
-
// };
|
|
31
|
-
// database: {
|
|
32
|
-
// host: string;
|
|
33
|
-
// port: number;
|
|
34
|
-
// };
|
|
35
|
-
// }
|
|
36
|
-
//
|
|
37
|
-
// export type AppAPI<T extends AppContext = AppContext> = T & {
|
|
38
|
-
// __contextName: "App";
|
|
39
|
-
// };
|
|
40
|
-
// }
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export interface GeneratorOptions {
|
|
2
|
-
outputPath: string;
|
|
3
|
-
moduleName: string;
|
|
4
|
-
contextName: string;
|
|
5
|
-
}
|
|
6
|
-
export declare const generateContextDeclaration: (contextName: string, context: Record<string, unknown>) => string;
|
|
7
|
-
export declare const generateContextDeclarationFile: (contextName: string, context: Record<string, unknown>) => Promise<void>;
|
|
8
|
-
export declare const clearGeneratedCache: () => void;
|
|
9
|
-
export declare const getGeneratedContextNames: () => string[];
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getGeneratedContextNames = exports.clearGeneratedCache = exports.generateContextDeclarationFile = exports.generateContextDeclaration = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const process_1 = __importDefault(require("process"));
|
|
10
|
-
// Cache pour éviter de générer plusieurs fois le même contexte
|
|
11
|
-
const generatedContexts = new Set();
|
|
12
|
-
const generateContextDeclaration = (contextName, context) => {
|
|
13
|
-
const properties = Object.entries(context)
|
|
14
|
-
.map(([key, value]) => {
|
|
15
|
-
const type = inferTypeFromValue(value);
|
|
16
|
-
return ` ${key}: ${type};`;
|
|
17
|
-
})
|
|
18
|
-
.join('\n');
|
|
19
|
-
return `
|
|
20
|
-
declare module "${process_1.default.cwd()}/packages/functions/src/context/generated" {
|
|
21
|
-
export interface ${contextName}Context {
|
|
22
|
-
${properties}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type ${contextName}API<T extends ${contextName}Context = ${contextName}Context> = T & {
|
|
26
|
-
__contextName: "${contextName}";
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
`;
|
|
30
|
-
};
|
|
31
|
-
exports.generateContextDeclaration = generateContextDeclaration;
|
|
32
|
-
const generateContextDeclarationFile = async (contextName, context) => {
|
|
33
|
-
// Vérifier si le contexte a déjà été généré
|
|
34
|
-
if (generatedContexts.has(contextName)) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const declaration = (0, exports.generateContextDeclaration)(contextName, context);
|
|
38
|
-
const outputPath = path_1.default.join(process_1.default.cwd(), 'packages', 'functions', 'src', 'context', 'generated.ts');
|
|
39
|
-
try {
|
|
40
|
-
// S'assurer que le répertoire existe
|
|
41
|
-
const dir = path_1.default.dirname(outputPath);
|
|
42
|
-
if (!fs_1.default.existsSync(dir)) {
|
|
43
|
-
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
44
|
-
}
|
|
45
|
-
// Lire le contenu existant s'il existe
|
|
46
|
-
let existingContent = '';
|
|
47
|
-
if (fs_1.default.existsSync(outputPath)) {
|
|
48
|
-
existingContent = fs_1.default.readFileSync(outputPath, 'utf8');
|
|
49
|
-
}
|
|
50
|
-
// Vérifier si le fichier contient déjà notre déclaration
|
|
51
|
-
if (existingContent.includes(`__contextName: "${contextName}"`)) {
|
|
52
|
-
generatedContexts.add(contextName);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
// Écrire ou ajouter au fichier généré
|
|
56
|
-
const finalContent = existingContent ?
|
|
57
|
-
`${existingContent}\n\n${declaration}` :
|
|
58
|
-
declaration;
|
|
59
|
-
fs_1.default.writeFileSync(outputPath, finalContent, 'utf8');
|
|
60
|
-
generatedContexts.add(contextName);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error(`Failed to generate context types for ${contextName}:`, error);
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
exports.generateContextDeclarationFile = generateContextDeclarationFile;
|
|
68
|
-
const clearGeneratedCache = () => {
|
|
69
|
-
generatedContexts.clear();
|
|
70
|
-
};
|
|
71
|
-
exports.clearGeneratedCache = clearGeneratedCache;
|
|
72
|
-
const getGeneratedContextNames = () => {
|
|
73
|
-
return Array.from(generatedContexts);
|
|
74
|
-
};
|
|
75
|
-
exports.getGeneratedContextNames = getGeneratedContextNames;
|
|
76
|
-
const inferTypeFromValue = (value) => {
|
|
77
|
-
if (value === null) {
|
|
78
|
-
return 'null';
|
|
79
|
-
}
|
|
80
|
-
if (Array.isArray(value)) {
|
|
81
|
-
if (value.length > 0) {
|
|
82
|
-
const elementType = inferTypeFromValue(value[0]);
|
|
83
|
-
return `${elementType}[]`;
|
|
84
|
-
}
|
|
85
|
-
return 'unknown[]';
|
|
86
|
-
}
|
|
87
|
-
switch (typeof value) {
|
|
88
|
-
case 'string':
|
|
89
|
-
return 'string';
|
|
90
|
-
case 'number':
|
|
91
|
-
return 'number';
|
|
92
|
-
case 'boolean':
|
|
93
|
-
return 'boolean';
|
|
94
|
-
case 'object': {
|
|
95
|
-
if (value !== null && !Array.isArray(value)) {
|
|
96
|
-
const obj = value;
|
|
97
|
-
const properties = Object.entries(obj)
|
|
98
|
-
.map(([key, val]) => {
|
|
99
|
-
const type = inferTypeFromValue(val);
|
|
100
|
-
return ` ${key}: ${type};`;
|
|
101
|
-
})
|
|
102
|
-
.join('\n');
|
|
103
|
-
return `{\n${properties}\n }`;
|
|
104
|
-
}
|
|
105
|
-
return 'Record<string, unknown>';
|
|
106
|
-
}
|
|
107
|
-
case 'bigint':
|
|
108
|
-
return 'bigint';
|
|
109
|
-
case 'symbol':
|
|
110
|
-
return 'symbol';
|
|
111
|
-
case 'undefined':
|
|
112
|
-
return 'undefined';
|
|
113
|
-
default:
|
|
114
|
-
return 'unknown';
|
|
115
|
-
}
|
|
116
|
-
};
|
package/dist/context/typing.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export type ContextValidator<TContext> = (context: unknown) => context is TContext;
|
|
2
|
-
export declare const createContextValidator: <TContext>(validator: ContextValidator<TContext>) => ContextValidator<TContext>;
|
|
3
|
-
export declare const getValidatedContext: <TContext>(validator: ContextValidator<TContext>) => TContext;
|
|
4
|
-
export declare const assertContextProperties: <TContext extends Record<string, unknown>, TKeys extends readonly (keyof TContext)[]>(keys: TKeys) => asserts keys is TKeys;
|
|
5
|
-
export type AppContext = Record<string, unknown>;
|
|
6
|
-
export declare const getTypedContext: <TContext extends AppContext = AppContext>() => TContext;
|
|
7
|
-
export declare const getSafeTypedContext: <TContext extends AppContext = AppContext>() => TContext | null;
|
|
8
|
-
export declare const assertContextProperty: <K extends keyof AppContext>(key: K) => asserts key is K;
|
|
9
|
-
export declare const getContextPropertyTyped: <K extends keyof AppContext, T>(key: K) => T | undefined;
|
|
10
|
-
export declare const setContextPropertyTyped: <K extends string, T>(key: K, value: T) => void;
|
package/dist/context/typing.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setContextPropertyTyped = exports.getContextPropertyTyped = exports.assertContextProperty = exports.getSafeTypedContext = exports.getTypedContext = exports.assertContextProperties = exports.getValidatedContext = exports.createContextValidator = void 0;
|
|
4
|
-
const index_1 = require("./index");
|
|
5
|
-
const createContextValidator = (validator) => {
|
|
6
|
-
return validator;
|
|
7
|
-
};
|
|
8
|
-
exports.createContextValidator = createContextValidator;
|
|
9
|
-
// Generic context access with validation
|
|
10
|
-
const getValidatedContext = (validator) => {
|
|
11
|
-
const context = (0, index_1.getContext)();
|
|
12
|
-
if (validator(context)) {
|
|
13
|
-
return context;
|
|
14
|
-
}
|
|
15
|
-
throw new Error("Context validation failed");
|
|
16
|
-
};
|
|
17
|
-
exports.getValidatedContext = getValidatedContext;
|
|
18
|
-
// Utility for partial context validation
|
|
19
|
-
const assertContextProperties = (keys) => {
|
|
20
|
-
const context = (0, index_1.getContext)();
|
|
21
|
-
for (const key of keys) {
|
|
22
|
-
if (!(key in context)) {
|
|
23
|
-
throw new Error(`Required context property '${String(key)}' is missing`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
exports.assertContextProperties = assertContextProperties;
|
|
28
|
-
const getTypedContext = () => {
|
|
29
|
-
return (0, index_1.getContext)();
|
|
30
|
-
};
|
|
31
|
-
exports.getTypedContext = getTypedContext;
|
|
32
|
-
const getSafeTypedContext = () => {
|
|
33
|
-
try {
|
|
34
|
-
const context = (0, index_1.getContext)();
|
|
35
|
-
return context;
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
exports.getSafeTypedContext = getSafeTypedContext;
|
|
42
|
-
const assertContextProperty = (key) => {
|
|
43
|
-
const context = (0, index_1.getContext)();
|
|
44
|
-
if (!(key in context)) {
|
|
45
|
-
throw new Error(`Context property '${key}' does not exist`);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
exports.assertContextProperty = assertContextProperty;
|
|
49
|
-
const getContextPropertyTyped = (key) => {
|
|
50
|
-
const context = (0, index_1.getContext)();
|
|
51
|
-
return context[key];
|
|
52
|
-
};
|
|
53
|
-
exports.getContextPropertyTyped = getContextPropertyTyped;
|
|
54
|
-
const setContextPropertyTyped = (key, value) => {
|
|
55
|
-
const context = (0, index_1.getContext)();
|
|
56
|
-
const updatedContext = { ...context, [key]: value };
|
|
57
|
-
// Note: Update global context as needed
|
|
58
|
-
};
|
|
59
|
-
exports.setContextPropertyTyped = setContextPropertyTyped;
|
|
60
|
-
// Other utilities (validator, etc.) remain unchanged...
|