@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.
@@ -1,6 +1,6 @@
1
- import { ExtensionBase, MergeExtensions } from "../extensions/types";
1
+ import { MergeExtensions } from "../extensions/types";
2
2
  export declare function defineContext<TContext extends Record<string, any>>(): {
3
- withExtensions: <TExtensions extends readonly ExtensionBase[]>(config: {
3
+ withExtensions: <TExtensions extends readonly any[]>(config: {
4
4
  extensions: readonly [...TExtensions];
5
5
  }) => MergeExtensions<TContext, TExtensions> & {
6
6
  context: TContext;
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineContext = defineContext;
4
4
  function defineContext() {
5
5
  return {
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).
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
- // Au runtime, on s'en fiche des types, on exécute juste
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
- * 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.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.58",
3
+ "version": "0.0.59",
4
4
  "description": "A powerful utility library for building type-safe APIs and functions with context management",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",