@deessejs/functions 0.0.45 → 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,14 +1,8 @@
1
- import { ExtensionBase } from "../extensions";
2
- export declare function defineContext<TContext>(): {
3
- withExtensions: <TExts extends readonly ExtensionBase[]>(config: {
4
- extensions: [...TExts];
5
- }) => MergeAllExtensions<TContext, TExts> & {
1
+ import { ExtensionBase, MergeExtensions } from "../extensions/types";
2
+ export declare function defineContext<TContext extends Record<string, any>>(): {
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 MergeAllExtensions<TContext, TExts extends readonly any[]> = {
10
- [K in keyof TExts]: TExts[K] extends {
11
- functions: infer F;
12
- } ? F extends (ctx: TContext) => infer R ? R : never : never;
13
- }[number];
14
- export {};
@@ -4,12 +4,18 @@ exports.defineContext = defineContext;
4
4
  function defineContext() {
5
5
  return {
6
6
  withExtensions: (config) => {
7
- const builder = {};
8
- for (const ext of config.extensions) {
9
- const result = ext();
10
- Object.assign(builder, result.functions);
11
- }
12
- 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
+ };
13
19
  },
14
20
  };
15
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
  }
@@ -1,29 +1,30 @@
1
1
  import { z } from "zod";
2
- import { Exception } from "../errors/types";
3
2
  import { AsyncResult } from "../types";
4
- export type QueryDef<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception = never> = {
5
- _type: "query";
6
- args: TArgs;
7
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
8
- };
9
- export type MutationDef<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception = never> = {
10
- _type: "mutation";
11
- args: TArgs;
12
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
13
- };
14
- export type EventHandler<TContext, TPayload> = {
15
- event: string;
16
- handler: (ctx: TContext, payload: TPayload) => AsyncResult<any, any>;
17
- };
18
- export declare function rpc<TContext>(): {
19
- query: <TArgs extends z.ZodType, TOutput, TError extends Exception = never>(def: QueryDef<TContext, TArgs, TOutput, TError>) => QueryDef<TContext, TArgs, TOutput, TError>;
20
- mutation: <TArgs extends z.ZodType, TOutput, TError extends Exception = never>(def: MutationDef<TContext, TArgs, TOutput, TError>) => MutationDef<TContext, TArgs, TOutput, TError>;
21
- group: <T extends Record<string, any>>(g: T) => T;
22
- on: <TPayload>(config: {
23
- event: string;
24
- handler: (ctx: TContext, payload: TPayload) => AsyncResult<any, any>;
25
- }) => {
26
- event: string;
27
- handler: (ctx: TContext, payload: TPayload) => AsyncResult<any, any>;
3
+ export declare const rpc: () => {
4
+ functions: <TContext>() => {
5
+ query: <TArgs extends z.ZodType, TOutput, TError = never>(def: {
6
+ args: TArgs;
7
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
8
+ }) => {
9
+ args: TArgs;
10
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
11
+ _type: "query";
12
+ };
13
+ mutation: <TArgs extends z.ZodType, TOutput_1, TError_1 = never>(def: {
14
+ args: TArgs;
15
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput_1, TError_1>;
16
+ }) => {
17
+ args: TArgs;
18
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput_1, TError_1>;
19
+ _type: "mutation";
20
+ };
21
+ group: <G>(group: G) => G;
22
+ on: <TPayload>(config: {
23
+ event: string;
24
+ handler: (ctx: TContext, payload: TPayload) => AsyncResult<any, any>;
25
+ }) => {
26
+ event: string;
27
+ handler: (ctx: TContext, payload: TPayload) => AsyncResult<any, any>;
28
+ };
28
29
  };
29
30
  };
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rpc = rpc;
4
- // Fonction générique : on passe le type du contexte ici !
5
- function rpc() {
6
- return {
7
- query: (def) => def,
8
- mutation: (def) => def,
9
- group: (g) => g,
3
+ exports.rpc = void 0;
4
+ const rpc = () => ({
5
+ functions: () => ({
6
+ query: (def) => ({ _type: "query", ...def }),
7
+ mutation: (def) => ({ _type: "mutation", ...def }),
8
+ group: (group) => group,
10
9
  on: (config) => config,
11
- };
12
- }
10
+ }),
11
+ });
12
+ exports.rpc = rpc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.45",
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",