@deessejs/functions 0.0.44 → 0.0.46

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,13 @@
1
- import { ExtensionBase, MergeExtensions } from "../extensions/types";
1
+ import { Extension } from "../extensions/types";
2
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> & {
3
+ withExtensions: <TExts extends readonly Extension<any>[]>(config: {
4
+ extensions: [...TExts];
5
+ }) => MergeExtensions<TContext, TExts> & {
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,23 +1,21 @@
1
1
  "use strict";
2
- // defineContext.ts
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.defineContext = defineContext;
4
+ // defineContext.ts
5
5
  function defineContext() {
6
6
  return {
7
7
  withExtensions: (config) => {
8
- // runtime builder (inchangé)
9
- const runtimeBuilder = {};
10
- const dummyContext = {};
11
- for (const extension of config.extensions) {
12
- // appele runtime, pour construire les méthodes effectives
13
- const extensionMethods = extension.functions(dummyContext);
14
- Object.assign(runtimeBuilder, extensionMethods);
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);
15
12
  }
16
- // On retourne runtimeBuilder mais on force le type public attendu.
17
- return {
18
- ...runtimeBuilder,
19
- context: {},
20
- };
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;
21
19
  },
22
20
  };
23
21
  }
@@ -6,11 +6,15 @@ 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> = {
@@ -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,19 +1,30 @@
1
- import z, { ZodType } from "zod";
2
- import { Exception } from "../errors/types";
1
+ import { z } from "zod";
3
2
  import { AsyncResult } from "../types";
4
- import { CommandsDefinition, MutationDefinition, QueryDefinition } from "../context/types";
5
- export type APINode = CommandsDefinition | {
6
- [key: string]: APINode;
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
+ };
29
+ };
7
30
  };
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
- }>;
@@ -1,19 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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,
4
+ const rpc = () => ({
5
+ functions: () => ({
6
+ query: (def) => ({ _type: "query", ...def }),
7
+ mutation: (def) => ({ _type: "mutation", ...def }),
8
+ group: (group) => group,
9
+ on: (config) => config,
18
10
  }),
19
11
  });
12
+ exports.rpc = rpc;
@@ -21,10 +21,9 @@ 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<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;
26
+ export type Extension<F extends (ctx: any) => any> = () => {
27
+ functions: F;
29
28
  };
30
29
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
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
  }