@deessejs/functions 0.0.56 → 0.0.58
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/context/define.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { MergeExtensions } from "../extensions/types";
|
|
1
|
+
import { ExtensionBase, MergeExtensions } from "../extensions/types";
|
|
2
2
|
export declare function defineContext<TContext extends Record<string, any>>(): {
|
|
3
|
-
withExtensions: <TExtensions extends readonly
|
|
3
|
+
withExtensions: <TExtensions extends readonly ExtensionBase[]>(config: {
|
|
4
4
|
extensions: readonly [...TExtensions];
|
|
5
5
|
}) => MergeExtensions<TContext, TExtensions> & {
|
|
6
6
|
context: TContext;
|
package/dist/context/define.js
CHANGED
|
@@ -3,16 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.defineContext = defineContext;
|
|
4
4
|
function defineContext() {
|
|
5
5
|
return {
|
|
6
|
-
// ON UTILISE
|
|
7
|
-
//
|
|
8
|
-
// Si on met 'ExtensionBase[]', TS peut simplifier le générique trop tôt.
|
|
6
|
+
// ON UTILISE ExtensionBase (la version simple définie ci-dessus).
|
|
7
|
+
// TExtensions va capturer le type EXACT et BRUT de tes extensions (ex: rpc avec ses génériques <C> intacts).
|
|
9
8
|
withExtensions: (config) => {
|
|
10
9
|
const runtimeBuilder = {};
|
|
11
10
|
const dummyContext = {};
|
|
12
11
|
for (const extension of config.extensions) {
|
|
13
|
-
//
|
|
12
|
+
// Au runtime, on s'en fiche des types, on exécute juste
|
|
14
13
|
const ext = extension;
|
|
15
|
-
if (ext && typeof ext.functions ===
|
|
14
|
+
if (ext && typeof ext.functions === "function") {
|
|
16
15
|
const extensionMethods = ext.functions(dummyContext);
|
|
17
16
|
Object.assign(runtimeBuilder, extensionMethods);
|
|
18
17
|
}
|
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
/**
|
|
3
|
+
* C'est le seul mécanisme fiable.
|
|
4
|
+
* F est ta fonction générique <C>(c: C) => API<C>
|
|
5
|
+
* On lui dit : "Si je te donne TContext, que me renvoies-tu ?"
|
|
6
|
+
* TypeScript est obligé de calculer le résultat avec TContext.
|
|
7
|
+
*/
|
|
8
|
+
type ApplyContext<F, C> = F extends (context: C) => infer R ? R : never;
|
|
9
|
+
/**
|
|
10
|
+
* ICI EST LA CLÉ :
|
|
11
|
+
* On définit 'functions' comme 'any'.
|
|
12
|
+
* Pourquoi ? Pour que TypeScript ne transforme PAS ta fonction générique <C>
|
|
13
|
+
* en une fonction générique "morte" ou "unknown" quand elle passe dans un tableau.
|
|
14
|
+
*/
|
|
6
15
|
export type ExtensionBase = {
|
|
7
16
|
context: any;
|
|
8
|
-
functions:
|
|
17
|
+
functions: any;
|
|
9
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* On itère, et on applique ApplyContext sur la fonction brute.
|
|
21
|
+
*/
|
|
22
|
+
export type MergeExtensions<TContext, TExtensions extends readonly any[]> = TExtensions extends readonly [infer Head, ...infer Tail] ? (Head extends ExtensionBase ? ApplyContext<Head["functions"], TContext> : {}) & MergeExtensions<TContext, Tail> : {};
|
|
10
23
|
export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
|
|
11
|
-
export type Extension<TSchema extends ZodType | undefined, TContextAddon extends Record<string, unknown>, TFunctionsFactory extends <C>(context: C) => any> = (...args: TSchema extends ZodType ? [options: z.input<TSchema>] : [options?: undefined]) => {
|
|
24
|
+
export type Extension<TSchema extends ZodType | undefined, TContextAddon extends Record<string, unknown>, TFunctionsFactory extends (<C>(context: C) => any)> = (...args: TSchema extends ZodType ? [options: z.input<TSchema>] : [options?: undefined]) => {
|
|
12
25
|
context: TContextAddon;
|
|
13
26
|
functions: TFunctionsFactory;
|
|
14
27
|
};
|
package/package.json
CHANGED