@deessejs/functions 0.0.46 → 0.0.47

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,13 +1,8 @@
1
- import { Extension } from "../extensions/types";
1
+ import { ExtensionBase, MergeExtensions } from "../extensions/types";
2
2
  export declare function defineContext<TContext extends Record<string, any>>(): {
3
- withExtensions: <TExts extends readonly Extension<any>[]>(config: {
4
- extensions: [...TExts];
5
- }) => MergeExtensions<TContext, TExts> & {
3
+ withExtensions: <TExtensions extends readonly ExtensionBase[]>(config: {
4
+ extensions: readonly [...TExtensions];
5
+ }) => MergeExtensions<TContext, TExtensions> & {
6
6
  context: TContext;
7
7
  };
8
8
  };
9
- type MergeExtensions<TContext, TExts extends readonly Extension<any>[]> = UnionToIntersection<{
10
- [K in keyof TExts]: TExts[K] extends Extension<infer F> ? F extends (ctx: TContext) => infer R ? R : never : never;
11
- }[number]>;
12
- type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
13
- export {};
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineContext = defineContext;
4
- // defineContext.ts
5
4
  function defineContext() {
6
5
  return {
7
6
  withExtensions: (config) => {
8
- const builder = {};
9
- for (const ext of config.extensions) {
10
- const { functions } = ext(); // ← chaque extension sait maintenant recevoir TContext
11
- Object.assign(builder, functions);
12
- }
13
- // On ajoute la propriété context (runtime value = null, mais type correct)
14
- Object.defineProperty(builder, "context", {
15
- get() { return null; },
16
- enumerable: true,
17
- });
18
- return builder;
7
+ // runtime builder (inchangé)
8
+ const runtimeBuilder = {};
9
+ const dummyContext = {};
10
+ for (const extension of config.extensions) {
11
+ // appele runtime, pour construire les méthodes effectives
12
+ const extensionMethods = extension.functions(dummyContext);
13
+ Object.assign(runtimeBuilder, extensionMethods);
14
+ } // Typage public : MergeExtensions instancié avec le TContext exact
15
+ return {
16
+ ...runtimeBuilder,
17
+ context: {},
18
+ };
19
19
  },
20
20
  };
21
21
  }
@@ -6,23 +6,19 @@ export type CommandDefinition<TContext, TArgs extends ZodType, TOutput, TError e
6
6
  args: TArgs;
7
7
  handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
8
8
  };
9
- export type QueryDefinition<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception> = {
9
+ export type QueryDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
10
10
  _type: "query";
11
- args: TArgs;
12
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
13
11
  };
14
- export type MutationDefinition<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception> = {
12
+ export type MutationDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
15
13
  _type: "mutation";
16
- args: TArgs;
17
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
18
14
  };
19
15
  export type CommandsDefinition = QueryDefinition<any, any, any, any> | MutationDefinition<any, any, any, any>;
20
16
  export type FunctionsForContext<C> = {
21
- query: <TArgs extends ZodType, TOutput, TError extends Exception>(def: QueryDefinition<C, TArgs, TOutput, TError>) => any;
22
- mutation: <TArgs extends ZodType, TOutput, TError extends Exception>(def: MutationDefinition<C, TArgs, TOutput, TError>) => any;
17
+ query: <TArgs extends ZodType, TOutput, TError extends Exception>(def: QueryDefinition<C, TArgs, TOutput, TError>) => QueryDefinition<C, TArgs, TOutput, TError>;
18
+ mutation: <TArgs extends ZodType, TOutput, TError extends Exception>(def: MutationDefinition<C, TArgs, TOutput, TError>) => MutationDefinition<C, TArgs, TOutput, TError>;
23
19
  on?: <TEvent extends string, TPayload>(config: {
24
20
  event: TEvent;
25
21
  handler: (ctx: C, payload: TPayload) => AsyncResult<any, any>;
26
22
  }) => any;
27
- group?: <TGroup extends Record<string, CommandsDefinition>>(defs: TGroup) => any;
23
+ group?: <TGroup extends Record<string, CommandsDefinition>>(defs: TGroup) => TGroup;
28
24
  };
@@ -1,10 +1,9 @@
1
- import { FunctionsForContext } from "../context";
2
- export type ExtensionBase = {
3
- (): {
4
- functions: FunctionsForContext<any>;
5
- };
6
- };
7
- export declare function extensions<TFunctions extends FunctionsForContext<any>>(config: {
1
+ import { ZodType } from "zod";
2
+ import { Extension, InferOptions } from "./types";
3
+ import { FunctionsForContext } from "..";
4
+ export declare function extensions<TSchema extends ZodType | undefined = undefined, TContextAddon extends Record<string, unknown> = Record<string, unknown>, TFunctionsFactory extends (<C>(context: C) => FunctionsForContext<C>) = (<C>(context: C) => FunctionsForContext<C>)>(config: {
8
5
  name: string;
9
- functions: () => TFunctions;
10
- }): ExtensionBase;
6
+ schema?: TSchema;
7
+ context?: (options: InferOptions<TSchema>) => TContextAddon;
8
+ functions: (options: InferOptions<TSchema>) => TFunctionsFactory;
9
+ }): Extension<TSchema, TContextAddon, TFunctionsFactory>;
@@ -1,8 +1,16 @@
1
1
  "use strict";
2
+ // extensions/index.ts (ou extensions.ts)
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.extensions = extensions;
4
5
  function extensions(config) {
5
- return () => ({
6
- functions: config.functions(),
7
- });
6
+ return (...args) => {
7
+ const optionsInput = args[0];
8
+ const options = (config.schema ? config.schema.parse(optionsInput) : undefined);
9
+ const contextPart = config.context ? config.context(options) : {};
10
+ const functionsBuilder = config.functions(options);
11
+ return {
12
+ context: contextPart,
13
+ functions: functionsBuilder,
14
+ };
15
+ };
8
16
  }
@@ -21,9 +21,10 @@ export type ExtensionConfig<TSchema extends ZodType | undefined, TContextAddon e
21
21
  name: string;
22
22
  schema?: TSchema;
23
23
  context?: (options: InferOptions<TSchema>) => TContextAddon;
24
- functions: (options: InferOptions<TSchema>) => <C>(context: C) => any;
24
+ functions: (options: InferOptions<TSchema>) => (<C>(context: C) => any);
25
25
  };
26
- export type Extension<F extends (ctx: any) => any> = () => {
27
- functions: F;
26
+ 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]) => {
27
+ context: TContextAddon;
28
+ functions: TFunctionsFactory;
28
29
  };
29
30
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.46",
3
+ "version": "0.0.47",
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",