@deessejs/functions 0.0.58 → 0.0.60
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,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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 {
|
|
4
|
+
functions: (ctx: TContext) => any;
|
|
5
|
+
}[]>(config: {
|
|
4
6
|
extensions: readonly [...TExtensions];
|
|
5
7
|
}) => MergeExtensions<TContext, TExtensions> & {
|
|
6
8
|
context: TContext;
|
package/dist/context/define.js
CHANGED
|
@@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.defineContext = defineContext;
|
|
4
4
|
function defineContext() {
|
|
5
5
|
return {
|
|
6
|
-
//
|
|
7
|
-
//
|
|
6
|
+
// FIX CRUCIAL :
|
|
7
|
+
// On contraint TExtensions pour que chaque extension ait une méthode 'functions'
|
|
8
|
+
// qui accepte TContext. Cela force TS à instancier le générique <C> de rpc immédiatement.
|
|
8
9
|
withExtensions: (config) => {
|
|
9
10
|
const runtimeBuilder = {};
|
|
10
11
|
const dummyContext = {};
|
|
11
12
|
for (const extension of config.extensions) {
|
|
12
|
-
// Au runtime, on
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const extensionMethods = ext.functions(dummyContext);
|
|
13
|
+
// Au runtime, on vérifie juste que c'est une fonction
|
|
14
|
+
if (extension && typeof extension.functions === "function") {
|
|
15
|
+
const extensionMethods = extension.functions(dummyContext);
|
|
16
16
|
Object.assign(runtimeBuilder, extensionMethods);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
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
|
-
*/
|
|
15
|
-
export type ExtensionBase = {
|
|
2
|
+
export type ExtensionBase<C = any> = {
|
|
16
3
|
context: any;
|
|
17
|
-
functions: any;
|
|
4
|
+
functions: (context: C) => any;
|
|
18
5
|
};
|
|
19
|
-
|
|
20
|
-
|
|
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> : {};
|
|
6
|
+
export type MergeExtensions<TContext, TExtensions extends readonly any[]> = TExtensions extends readonly [infer Head, ...infer Tail] ? (Head extends {
|
|
7
|
+
functions: (ctx: any) => infer R;
|
|
8
|
+
} ? R : {}) & MergeExtensions<TContext, Tail> : {};
|
|
23
9
|
export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
|
|
24
10
|
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]) => {
|
|
25
11
|
context: TContextAddon;
|
|
26
12
|
functions: TFunctionsFactory;
|
|
27
13
|
};
|
|
28
|
-
export {};
|
package/package.json
CHANGED