@deessejs/functions 0.0.11 → 0.0.13

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.
package/README.md CHANGED
Binary file
@@ -1,8 +1,5 @@
1
- import type { AsyncResult } from "../types";
2
- export type QueryFn<TArgs, TOutput, TError> = (input: TArgs) => AsyncResult<TOutput, TError>;
3
- export declare const createAPI: <TContext extends Record<string, unknown>>(config: {
4
- context: TContext;
5
- }) => {
6
- [key: string]: TContext | QueryFn<any, any, any>;
7
- context: TContext;
8
- } & Record<string, QueryFn<any, any, any>>;
1
+ import { ApiRouter } from "./types";
2
+ export declare function createAPI<TCtx extends Record<string, unknown>, TRoot extends Record<string, any>>(config: {
3
+ context: TCtx;
4
+ root: TRoot;
5
+ }): ApiRouter<TRoot>;
package/dist/api/index.js CHANGED
@@ -1,20 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAPI = void 0;
4
- const query_1 = require("../functions/query");
5
- const createAPI = (config) => {
6
- const ctx = { ...config.context };
7
- const api = {
8
- context: ctx,
9
- };
10
- const query = (options) => {
11
- const result = (0, query_1.query)(options);
12
- api[options.name] = result;
13
- return result;
14
- };
15
- return {
16
- ...api,
17
- query,
18
- };
19
- };
20
3
  exports.createAPI = createAPI;
4
+ const errors_1 = require("../errors");
5
+ const parse_1 = require("../functions/parse");
6
+ const types_1 = require("../types");
7
+ function createAPI(config) {
8
+ const hydrate = (node) => {
9
+ if (node && typeof node === "object" && node._type === "query") {
10
+ const def = node;
11
+ return (input) => {
12
+ const parsed = (0, parse_1.parseArgs)(def.args, input);
13
+ return parsed.match({
14
+ onSuccess: (data) => {
15
+ return def.handler(data, config.context);
16
+ },
17
+ onFailure: (error) => {
18
+ const ValidationError = (0, errors_1.exception)({
19
+ name: "ValidationError",
20
+ message: error.message,
21
+ });
22
+ return Promise.resolve((0, types_1.failure)(ValidationError));
23
+ },
24
+ });
25
+ };
26
+ }
27
+ if (node && typeof node === "object") {
28
+ const group = {};
29
+ for (const key in node) {
30
+ group[key] = hydrate(node[key]);
31
+ }
32
+ return group;
33
+ }
34
+ return node;
35
+ };
36
+ return hydrate(config.root);
37
+ }
@@ -1,14 +1,8 @@
1
- import { Command } from "../functions/types";
2
- import { Event } from "../events/types";
3
- import { Context } from "../context/types";
4
- export type APIConfig<TContext extends Context = Context> = {
5
- context: TContext;
6
- commands: Command[];
7
- events: Event[];
8
- };
9
- export type API<TContext extends Context = Context> = {
10
- context: TContext;
11
- addContext: <K extends string, V>(key: K, value: V) => API<TContext & Record<K, V>>;
12
- commands: Command[];
13
- events: Event[];
1
+ import z, { ZodType } from "zod";
2
+ import { QueryDefinition } from "../context/define";
3
+ import { AsyncResult } from "../types";
4
+ type InferQueryFn<T> = T extends QueryDefinition<any, infer TArgs extends ZodType<any, any, any>, infer TOutput, infer TError> ? (input: z.input<TArgs>) => AsyncResult<TOutput, TError> : never;
5
+ export type ApiRouter<T> = {
6
+ [K in keyof T]: T[K] extends QueryDefinition<any, any, any, any> ? InferQueryFn<T[K]> : T[K] extends Record<string, any> ? ApiRouter<T[K]> : never;
14
7
  };
8
+ export {};
@@ -0,0 +1,20 @@
1
+ import z, { ZodType } from "zod";
2
+ import { Exception } from "../errors/types";
3
+ import { AsyncResult } from "../types";
4
+ export type QueryDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = {
5
+ _type: "query";
6
+ name: string;
7
+ args: TArgs;
8
+ handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
9
+ };
10
+ export type APINode = QueryDefinition<any, any, any, any> | {
11
+ [key: string]: APINode;
12
+ };
13
+ export declare function defineContext<TContext extends Record<string, unknown>>(): {
14
+ query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
15
+ name: string;
16
+ args: TArgs;
17
+ handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
18
+ }) => QueryDefinition<TContext, TArgs, TOutput, TError>;
19
+ group: <T extends Record<string, APINode>>(definitions: T) => T;
20
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineContext = defineContext;
4
+ function defineContext() {
5
+ return {
6
+ query: (options) => {
7
+ return {
8
+ _type: "query",
9
+ name: options.name,
10
+ args: options.args,
11
+ handler: options.handler,
12
+ };
13
+ },
14
+ group: (definitions) => {
15
+ return definitions;
16
+ },
17
+ };
18
+ }
@@ -1,8 +1 @@
1
- import { Context } from "./types";
2
- export declare const createContext: (ctx: Context) => void;
3
- export declare const addContext: <K extends string, V>(key: K, value: V) => void;
4
- export declare const getContext: () => Context;
5
- export declare const getCurrentContext: () => Context;
6
- export declare const clearContext: () => void;
7
- export declare const setContextProperty: <K extends string, V>(key: K, value: V) => void;
8
- export declare const getContextProperty: <K extends string>(key: K) => unknown;
1
+ export * from "./define";
@@ -1,30 +1,17 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getContextProperty = exports.setContextProperty = exports.clearContext = exports.getCurrentContext = exports.getContext = exports.addContext = exports.createContext = void 0;
4
- let currentContext = {};
5
- const createContext = (ctx) => {
6
- currentContext = { ...ctx };
7
- };
8
- exports.createContext = createContext;
9
- const addContext = (key, value) => {
10
- currentContext = { ...currentContext, [key]: value };
11
- };
12
- exports.addContext = addContext;
13
- const getContext = () => currentContext;
14
- exports.getContext = getContext;
15
- const getCurrentContext = () => {
16
- return { ...currentContext };
17
- };
18
- exports.getCurrentContext = getCurrentContext;
19
- const clearContext = () => {
20
- currentContext = {};
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
15
  };
22
- exports.clearContext = clearContext;
23
- const setContextProperty = (key, value) => {
24
- currentContext = { ...currentContext, [key]: value };
25
- };
26
- exports.setContextProperty = setContextProperty;
27
- const getContextProperty = (key) => {
28
- return currentContext[key];
29
- };
30
- exports.getContextProperty = getContextProperty;
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./define"), exports);
@@ -1,16 +1,15 @@
1
1
  import { z, ZodType } from "zod";
2
2
  import { Exception } from "../errors/types";
3
3
  import { AsyncResult } from "../types";
4
- import type { AppContext } from "../context/typing";
5
4
  export declare function mutation<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
6
5
  args: TArgs;
7
6
  handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
8
7
  }): (input: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
9
- export declare const createMutation: <TContext extends AppContext = AppContext>() => <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
8
+ export declare const createMutation: <TContext extends Record<string, unknown> = Record<string, unknown>>() => <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
10
9
  args: TArgs;
11
10
  handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
12
11
  }) => (input: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
13
- export declare function mutationWithContext<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception, TContext extends AppContext = AppContext>(options: {
12
+ export declare function mutationWithContext<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception, TContext extends Record<string, unknown> = Record<string, unknown>>(options: {
14
13
  args: TArgs;
15
14
  handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
16
15
  }): (input: z.core.output<TArgs>, context?: TContext) => AsyncResult<TOutput, TError>;
@@ -1,9 +1,8 @@
1
1
  import { z, ZodType } from "zod";
2
- import type { AppContext } from "../context/typing";
3
2
  import { Exception } from "../errors/types";
4
3
  import { AsyncResult } from "../types";
5
- export declare function query<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception, TContext extends AppContext = AppContext>(options: {
4
+ export declare function query<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception, TContext extends Record<string, unknown> = Record<string, unknown>>(options: {
6
5
  name: string;
7
6
  args: TArgs;
8
7
  handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
9
- }): (input: z.core.output<TArgs>, context?: TContext) => AsyncResult<TOutput, TError>;
8
+ }): (input: z.core.output<TArgs>, context: TContext) => AsyncResult<TOutput, TError>;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.query = query;
4
- const typing_1 = require("../context/typing");
5
4
  const errors_1 = require("../errors");
6
5
  const types_1 = require("../types");
7
6
  const parse_1 = require("./parse");
@@ -10,7 +9,7 @@ function query(options) {
10
9
  const parsed = (0, parse_1.parseArgs)(options.args, input);
11
10
  return parsed.match({
12
11
  onSuccess: (data) => {
13
- const ctx = context ?? (0, typing_1.getTypedContext)();
12
+ const ctx = context;
14
13
  return options.handler(data, ctx);
15
14
  },
16
15
  onFailure: (error) => {
package/dist/index.d.ts CHANGED
@@ -3,4 +3,3 @@ export * from "./api/types";
3
3
  export * from "./functions";
4
4
  export * from "./types";
5
5
  export * from "./context";
6
- export * from "./context/typing";
package/dist/index.js CHANGED
@@ -20,4 +20,3 @@ __exportStar(require("./functions"), exports);
20
20
  __exportStar(require("./types"), exports);
21
21
  // Context and typing exports
22
22
  __exportStar(require("./context"), exports);
23
- __exportStar(require("./context/typing"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
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",