@deessejs/functions 0.0.21 → 0.0.23

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,7 +1,11 @@
1
1
  import { UnionToIntersection } from "../utils";
2
- export type MergeExtensions<TContext, TExtensions extends Array<(...args: any) => any>> = UnionToIntersection<ReturnType<TExtensions[number]>>;
2
+ export type ExtensionInstance = {
3
+ context: Record<string, any>;
4
+ functions: (...args: any[]) => any;
5
+ };
6
+ export type MergeExtensions<TContext, TExtensions extends Array<ExtensionInstance>> = UnionToIntersection<ReturnType<TExtensions[number]["functions"]>>;
3
7
  export declare function defineContext<TContext extends Record<string, unknown>>(): {
4
- withExtensions: <TExtensions extends Array<(...args: any) => any>>(config: {
8
+ withExtensions: <TExtensions extends Array<ExtensionInstance>>(config: {
5
9
  extensions: TExtensions;
6
10
  }) => MergeExtensions<TContext, TExtensions> & {
7
11
  context: TContext;
@@ -5,9 +5,9 @@ function defineContext() {
5
5
  return {
6
6
  withExtensions: (config) => {
7
7
  const builder = {};
8
- for (const createExtension of config.extensions) {
9
- const extensionInstance = createExtension();
10
- Object.assign(builder, extensionInstance);
8
+ for (const extension of config.extensions) {
9
+ const extensionMethods = extension.functions();
10
+ Object.assign(builder, extensionMethods);
11
11
  }
12
12
  return {
13
13
  ...builder,
@@ -1,2 +1,3 @@
1
- import { ExtensionConfig } from "./types";
2
- export declare const extensions: (config: ExtensionConfig) => void;
1
+ import { ZodType } from "zod";
2
+ import { Extension, ExtensionConfig } from "./types";
3
+ export declare function extensions<TSchema extends ZodType | undefined = undefined, TContextAddon extends Record<string, unknown> = Record<string, unknown>, TFunctions extends Record<string, unknown> = Record<string, unknown>>(config: ExtensionConfig<TSchema, TContextAddon, TFunctions>): Extension<TSchema, TContextAddon, TFunctions>;
@@ -1,5 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extensions = void 0;
4
- const extensions = (config) => { };
5
3
  exports.extensions = extensions;
4
+ function extensions(config) {
5
+ return (...args) => {
6
+ const optionsInput = args[0];
7
+ const options = (config.schema ? config.schema.parse(optionsInput) : undefined);
8
+ const contextPart = config.context
9
+ ? config.context(options)
10
+ : {};
11
+ const functionsBuilder = config.functions(options);
12
+ return {
13
+ context: contextPart,
14
+ functions: functionsBuilder,
15
+ };
16
+ };
17
+ }
@@ -5,15 +5,14 @@ import { CommandsDefinition, MutationDefinition, QueryDefinition } from "../cont
5
5
  export type APINode = CommandsDefinition | {
6
6
  [key: string]: APINode;
7
7
  };
8
- export declare const rpc: <TContext>() => () => {
9
- query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
8
+ export declare const rpc: import("./types").Extension<undefined, Record<string, unknown>, {
9
+ query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception>(options: {
10
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: {
11
+ handler: (ctx: unknown, args: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
12
+ }) => QueryDefinition<unknown, TArgs, TOutput, TError>;
13
+ mutation: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception>(options: {
15
14
  args: TArgs;
16
- handler: (ctx: TContext, args: z.infer<TArgs>) => AsyncResult<TOutput_1, TError_1>;
17
- }) => MutationDefinition<TContext, TArgs, TOutput_1, TError_1>;
15
+ handler: (ctx: unknown, args: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
16
+ }) => MutationDefinition<unknown, TArgs, TOutput, TError>;
18
17
  group: <T extends Record<string, APINode>>(definitions: T) => T;
19
- };
18
+ }>;
@@ -1,26 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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;
4
+ const _1 = require(".");
5
+ exports.rpc = (0, _1.extensions)({
6
+ // Pas d'options, pas de contexte ajouté
7
+ schema: undefined,
8
+ context: undefined,
9
+ // Définition des fonctions
10
+ functions: (_options) => {
11
+ // On retourne la fonction générique
12
+ return () => ({
13
+ query: (options) => {
14
+ return { _type: "query", ...options };
15
+ },
16
+ mutation: (options) => {
17
+ return { _type: "mutation", ...options };
18
+ },
19
+ group: (definitions) => definitions,
20
+ });
21
+ },
22
+ });
@@ -1,4 +1,11 @@
1
- export type ExtensionConfig = {
2
- context: undefined;
3
- functions: undefined;
1
+ import z, { ZodType } from "zod";
2
+ export type InferOptions<T extends ZodType | undefined> = T extends ZodType ? z.infer<T> : undefined;
3
+ export type ExtensionConfig<TSchema extends ZodType | undefined, TContext extends Record<string, unknown>, TFunctions extends Record<string, unknown>> = {
4
+ schema: TSchema;
5
+ context?: (options: InferOptions<TSchema>) => TContext;
6
+ functions: (options: InferOptions<TSchema>) => <TAppContext extends TContext>() => TFunctions;
7
+ };
8
+ export type Extension<TSchema extends ZodType | undefined, TContextAddon extends Record<string, unknown>, TFunctions extends Record<string, unknown>> = (...args: TSchema extends ZodType ? [options: z.input<TSchema>] : [options?: undefined]) => {
9
+ context: TContextAddon;
10
+ functions: <TAppContext>() => TFunctions;
4
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
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",