@deessejs/functions 0.0.43 → 0.0.45

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,8 +1,14 @@
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> & {
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> & {
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,18 +4,12 @@ exports.defineContext = defineContext;
4
4
  function defineContext() {
5
5
  return {
6
6
  withExtensions: (config) => {
7
- // Créer un contexte qui préserve le type TContext
8
- const runtimeContext = {};
9
- // Appliquer les extensions en préservant les types
10
- const result = {
11
- context: runtimeContext,
12
- };
13
- for (const extension of config.extensions) {
14
- // Passer le contexte avec son type complet pour que l'inférence fonctionne
15
- const methods = extension.functions(runtimeContext);
16
- Object.assign(result, methods);
7
+ const builder = {};
8
+ for (const ext of config.extensions) {
9
+ const result = ext();
10
+ Object.assign(builder, result.functions);
17
11
  }
18
- return result;
12
+ return builder;
19
13
  },
20
14
  };
21
15
  }
@@ -6,19 +6,23 @@ 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 ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
9
+ export type QueryDefinition<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception> = {
10
10
  _type: "query";
11
+ args: TArgs;
12
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
11
13
  };
12
- export type MutationDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
14
+ export type MutationDefinition<TContext, TArgs extends z.ZodType, TOutput, TError extends Exception> = {
13
15
  _type: "mutation";
16
+ args: TArgs;
17
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
14
18
  };
15
19
  export type CommandsDefinition = QueryDefinition<any, any, any, any> | MutationDefinition<any, any, any, any>;
16
20
  export type FunctionsForContext<C> = {
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>;
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;
19
23
  on?: <TEvent extends string, TPayload>(config: {
20
24
  event: TEvent;
21
25
  handler: (ctx: C, payload: TPayload) => AsyncResult<any, any>;
22
26
  }) => any;
23
- group?: <TGroup extends Record<string, CommandsDefinition>>(defs: TGroup) => TGroup;
27
+ group?: <TGroup extends Record<string, CommandsDefinition>>(defs: TGroup) => any;
24
28
  };
@@ -1,9 +1,10 @@
1
- import { ZodType } from "zod";
2
- import { Extension, InferOptions } from "./types";
3
1
  import { FunctionsForContext } from "../context";
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: {
2
+ export type ExtensionBase = {
3
+ (): {
4
+ functions: FunctionsForContext<any>;
5
+ };
6
+ };
7
+ export declare function extensions<TFunctions extends FunctionsForContext<any>>(config: {
5
8
  name: string;
6
- schema?: TSchema;
7
- context?: (options: InferOptions<TSchema>) => TContextAddon;
8
- functions: (options: InferOptions<TSchema>) => TFunctionsFactory;
9
- }): Extension<TSchema, TContextAddon, TFunctionsFactory>;
9
+ functions: () => TFunctions;
10
+ }): ExtensionBase;
@@ -1,16 +1,8 @@
1
1
  "use strict";
2
- // extensions/index.ts (ou extensions.ts)
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.extensions = extensions;
5
4
  function extensions(config) {
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
- };
5
+ return () => ({
6
+ functions: config.functions(),
7
+ });
16
8
  }
@@ -1,20 +1,29 @@
1
- import z, { ZodType } from "zod";
1
+ import { z } from "zod";
2
2
  import { Exception } from "../errors/types";
3
3
  import { AsyncResult } from "../types";
4
- import { CommandsDefinition, MutationDefinition, QueryDefinition } from "../context/types";
5
- export type APINode = CommandsDefinition | {
6
- [key: string]: APINode;
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>;
28
+ };
7
29
  };
8
- export declare const rpc: import("./types").Extension<undefined, Record<string, unknown>, <C>(context: C) => {
9
- query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception>(options: {
10
- args: TArgs;
11
- handler: (ctx: C, // C sera ici { user: ... }
12
- args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
13
- }) => QueryDefinition<C, TArgs, TOutput, TError>;
14
- mutation: <TArgs extends ZodType<any, any, any>, TOutput_1, TError_1 extends Exception>(options: {
15
- args: TArgs;
16
- handler: (ctx: C, args: z.infer<TArgs>) => AsyncResult<TOutput_1, TError_1>;
17
- }) => MutationDefinition<C, TArgs, TOutput_1, TError_1>;
18
- group: <T extends Record<string, APINode>>(definitions: T) => T;
19
- _contextType: C;
20
- }>;
@@ -1,21 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rpc = void 0;
4
- const _1 = require(".");
5
- exports.rpc = (0, _1.extensions)({
6
- name: "rpc",
7
- schema: undefined, // Pas d'options pour le moment
8
- context: undefined,
9
- // On garde une signature ultra-clean : <C>(context: C) => ...
10
- functions: (_options) => (context) => ({
11
- query: (options) => {
12
- return { _type: "query", ...options };
13
- },
14
- mutation: (options) => {
15
- return { _type: "mutation", ...options };
16
- },
17
- group: (definitions) => definitions,
18
- // Exposer le type de contexte pour l'inférence
19
- _contextType: context,
20
- }),
21
- });
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,
10
+ on: (config) => config,
11
+ };
12
+ }
@@ -10,10 +10,10 @@ type FnCallSignatureMatchesWith<C, F> = F extends {
10
10
  export type MergeExtensions<TContext, TExtensions extends readonly any[]> = TExtensions extends readonly [infer Head, ...infer Tail] ? (Head extends {
11
11
  functions: infer F;
12
12
  } ? FnCallSignatureMatchesWith<TContext, F> : {}) & MergeExtensions<TContext, Tail> : {};
13
- /** Ici : functions est une fonction qui retourne les méthodes pour un contexte donné */
13
+ /** Ici : functions est *explicitement* une call signature générique */
14
14
  export type ExtensionBase = {
15
15
  context: any;
16
- functions: <TOptions>(options: TOptions) => <C>(context: C) => FunctionsForContext<C>;
16
+ functions: <C>(context: C) => FunctionsForContext<C>;
17
17
  };
18
18
  /** Helpers pour extensions/InferOptions et définition d'Extension */
19
19
  export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
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",
@@ -56,7 +56,11 @@
56
56
  "build": "tsc",
57
57
  "build:watch": "tsc --watch",
58
58
  "prepublishOnly": "npm run build",
59
- "test": "echo \"Error: no test specified\" && exit 1",
59
+ "test": "vitest",
60
+ "test:ui": "vitest --ui",
61
+ "test:run": "vitest run",
62
+ "test:coverage": "vitest run --coverage",
63
+ "test:watch": "vitest watch",
60
64
  "lint": "eslint src --ext .ts",
61
65
  "lint:fix": "eslint src --ext .ts --fix"
62
66
  },
@@ -89,10 +93,19 @@
89
93
  "zod": "^4.1.12"
90
94
  },
91
95
  "devDependencies": {
96
+ "@testing-library/dom": "^10.4.1",
97
+ "@testing-library/user-event": "^14.6.1",
98
+ "@types/jsdom": "^27.0.0",
92
99
  "@types/node": "^20.0.0",
93
- "typescript": "^5.0.0",
94
- "eslint": "^8.0.0",
100
+ "@types/sinon": "^21.0.0",
95
101
  "@typescript-eslint/eslint-plugin": "^6.0.0",
96
- "@typescript-eslint/parser": "^6.0.0"
102
+ "@typescript-eslint/parser": "^6.0.0",
103
+ "@vitest/coverage-v8": "^4.0.13",
104
+ "@vitest/ui": "^4.0.13",
105
+ "eslint": "^8.0.0",
106
+ "jsdom": "^27.2.0",
107
+ "sinon": "^21.0.0",
108
+ "typescript": "^5.0.0",
109
+ "vitest": "^4.0.13"
97
110
  }
98
111
  }