@deessejs/functions 0.0.58 → 0.0.59
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 +2 -2
- package/dist/context/define.js +3 -3
- package/dist/extensions/types.d.ts +13 -15
- package/package.json +1 -1
package/dist/context/define.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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 any[]>(config: {
|
|
4
4
|
extensions: readonly [...TExtensions];
|
|
5
5
|
}) => MergeExtensions<TContext, TExtensions> & {
|
|
6
6
|
context: TContext;
|
package/dist/context/define.js
CHANGED
|
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.defineContext = defineContext;
|
|
4
4
|
function defineContext() {
|
|
5
5
|
return {
|
|
6
|
-
// ON
|
|
7
|
-
// TExtensions
|
|
6
|
+
// ON NE MET AUCUNE CONTRAINTE ICI.
|
|
7
|
+
// TExtensions capture le type brut et exact (ex: le générique <C> de rpc).
|
|
8
8
|
withExtensions: (config) => {
|
|
9
9
|
const runtimeBuilder = {};
|
|
10
10
|
const dummyContext = {};
|
|
11
11
|
for (const extension of config.extensions) {
|
|
12
|
-
//
|
|
12
|
+
// Cast runtime uniquement pour éviter les erreurs TS dans la boucle
|
|
13
13
|
const ext = extension;
|
|
14
14
|
if (ext && typeof ext.functions === "function") {
|
|
15
15
|
const extensionMethods = ext.functions(dummyContext);
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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.
|
|
3
|
+
* LA SOLUTION MATHÉMATIQUE.
|
|
4
|
+
* On ne demande pas "Est-ce que ça marche ?" (extends), on dit "Fais-le marcher !" (&).
|
|
5
|
+
*
|
|
6
|
+
* On crée une intersection entre :
|
|
7
|
+
* 1. Ta fonction générique F: <C>(c: C) => API<C>
|
|
8
|
+
* 2. Une fonction factice: (c: TContext) => unknown
|
|
9
|
+
*
|
|
10
|
+
* Pour calculer le ReturnType de cette intersection, TypeScript DOIT instancier
|
|
11
|
+
* le générique <C> de F avec TContext.
|
|
14
12
|
*/
|
|
13
|
+
type ApplyContext<F, C> = F extends (c: any) => any ? ReturnType<F & ((c: C) => unknown)> : never;
|
|
14
|
+
export type MergeExtensions<TContext, TExtensions extends readonly any[]> = TExtensions extends readonly [infer Head, ...infer Tail] ? (Head extends {
|
|
15
|
+
functions: infer F;
|
|
16
|
+
} ? ApplyContext<F, TContext> : {}) & MergeExtensions<TContext, Tail> : {};
|
|
15
17
|
export type ExtensionBase = {
|
|
16
18
|
context: any;
|
|
17
19
|
functions: any;
|
|
18
20
|
};
|
|
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> : {};
|
|
23
21
|
export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
|
|
24
22
|
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
23
|
context: TContextAddon;
|
package/package.json
CHANGED