@deessejs/functions 0.0.45 → 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,14 +1,13 @@
1
- import { ExtensionBase } from "../extensions";
2
- export declare function defineContext<TContext>(): {
3
- withExtensions: <TExts extends readonly ExtensionBase[]>(config: {
1
+ import { Extension } from "../extensions/types";
2
+ export declare function defineContext<TContext extends Record<string, any>>(): {
3
+ withExtensions: <TExts extends readonly Extension<any>[]>(config: {
4
4
  extensions: [...TExts];
5
- }) => MergeAllExtensions<TContext, TExts> & {
5
+ }) => MergeExtensions<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];
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;
14
13
  export {};
@@ -1,14 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineContext = defineContext;
4
+ // defineContext.ts
4
5
  function defineContext() {
5
6
  return {
6
7
  withExtensions: (config) => {
7
8
  const builder = {};
8
9
  for (const ext of config.extensions) {
9
- const result = ext();
10
- Object.assign(builder, result.functions);
10
+ const { functions } = ext(); // ← chaque extension sait maintenant recevoir TContext
11
+ Object.assign(builder, functions);
11
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
+ });
12
18
  return builder;
13
19
  },
14
20
  };
@@ -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;
@@ -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.45",
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",