@deessejs/functions 0.0.15 → 0.0.17

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,20 +1,8 @@
1
- import z, { ZodType } from "zod";
2
- import { Exception } from "../errors/types";
3
- import { AsyncResult } from "../types";
4
- import { CommandsDefinition, MutationDefinition, QueryDefinition } from "./types";
5
- export type APINode = CommandsDefinition | {
6
- [key: string]: APINode;
7
- };
8
- export declare function defineContext<TContext extends Record<string, unknown>>(): {
9
- query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
10
- name: string;
11
- args: TArgs;
12
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
13
- }) => QueryDefinition<TContext, TArgs, TOutput, TError>;
14
- mutation: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
15
- name: string;
16
- args: TArgs;
17
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
18
- }) => MutationDefinition<TContext, TArgs, TOutput, TError>;
19
- group: <T extends Record<string, APINode>>(definitions: T) => T;
1
+ import { UnionToIntersection } from "../utils";
2
+ type MergeExtensions<TContext, TExtensions extends any[]> = UnionToIntersection<ReturnType<TExtensions[number] extends (...args: any) => any ? (ctx: TContext) => ReturnType<ReturnType<TExtensions[number]>> : never>>;
3
+ export declare function defineContext<TBaseContext extends Record<string, unknown>, TExtensions extends Array<(...args: any) => any>>(config: {
4
+ extensions: TExtensions;
5
+ }): MergeExtensions<TBaseContext, TExtensions> & {
6
+ context: TBaseContext;
20
7
  };
8
+ export {};
@@ -1,26 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defineContext = defineContext;
4
- function defineContext() {
4
+ function defineContext(config) {
5
+ const builder = {};
6
+ for (const createExtension of config.extensions) {
7
+ const extensionInstance = createExtension();
8
+ Object.assign(builder, extensionInstance);
9
+ }
5
10
  return {
6
- query: (options) => {
7
- return {
8
- _type: "query",
9
- name: options.name,
10
- args: options.args,
11
- handler: options.handler,
12
- };
13
- },
14
- mutation: (options) => {
15
- return {
16
- _type: "mutation",
17
- name: options.name,
18
- args: options.args,
19
- handler: options.handler,
20
- };
21
- },
22
- group: (definitions) => {
23
- return definitions;
24
- },
11
+ ...builder,
12
+ context: {},
25
13
  };
26
14
  }
@@ -3,7 +3,6 @@ import { Exception } from "../errors/types";
3
3
  import { AsyncResult } from "../types";
4
4
  export type Context = Record<string, unknown>;
5
5
  export type CommandDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = {
6
- name: string;
7
6
  args: TArgs;
8
7
  handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
9
8
  };
@@ -0,0 +1,2 @@
1
+ import { ExtensionConfig } from "./types";
2
+ export declare const extensions: (config: ExtensionConfig) => void;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extensions = void 0;
4
+ const extensions = (config) => { };
5
+ exports.extensions = extensions;
@@ -0,0 +1,19 @@
1
+ import z, { ZodType } from "zod";
2
+ import { Exception } from "../errors/types";
3
+ import { AsyncResult } from "../types";
4
+ import { CommandsDefinition, MutationDefinition, QueryDefinition } from "../context/types";
5
+ export type APINode = CommandsDefinition | {
6
+ [key: string]: APINode;
7
+ };
8
+ export declare const rpc: () => <TContext>() => {
9
+ query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
10
+ args: TArgs;
11
+ handler: (ctx: TContext, // <--- Le contexte est injecté ici
12
+ args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
13
+ }) => QueryDefinition<TContext, TArgs, TOutput, TError>;
14
+ mutation: <TArgs extends ZodType<any, any, any>, TOutput_1, TError_1 extends Exception = Exception>(options: {
15
+ args: TArgs;
16
+ handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput_1, TError_1>;
17
+ }) => MutationDefinition<TContext, TArgs, TOutput_1, TError_1>;
18
+ group: <T extends Record<string, APINode>>(definitions: T) => T;
19
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rpc = void 0;
4
+ // Cette fonction retourne une "ExtensionFn"
5
+ const rpc = () => {
6
+ return () => ({
7
+ query: (options) => {
8
+ return {
9
+ _type: "query",
10
+ args: options.args,
11
+ handler: options.handler,
12
+ };
13
+ },
14
+ mutation: (options) => {
15
+ return {
16
+ _type: "mutation",
17
+ args: options.args,
18
+ handler: options.handler,
19
+ };
20
+ },
21
+ group: (definitions) => {
22
+ return definitions;
23
+ },
24
+ });
25
+ };
26
+ exports.rpc = rpc;
@@ -0,0 +1,4 @@
1
+ export type ExtensionConfig = {
2
+ context: undefined;
3
+ functions: undefined;
4
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
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",