@deessejs/functions 0.0.11 → 0.0.12

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,6 @@
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 type { AppContext } from "../context/typing";
2
+ import { ApiRouter } from "./types";
3
+ export declare function createAPI<TCtx extends AppContext, TRoot extends Record<string, any>>(config: {
4
+ context: TCtx;
5
+ root: TRoot;
6
+ }): 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,21 @@
1
+ import z, { ZodType } from "zod";
2
+ import { Exception } from "../errors/types";
3
+ import { AsyncResult } from "../types";
4
+ import { AppContext } from "./typing";
5
+ export type QueryDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = {
6
+ _type: "query";
7
+ name: string;
8
+ args: TArgs;
9
+ handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
10
+ };
11
+ export type APINode = QueryDefinition<any, any, any, any> | {
12
+ [key: string]: APINode;
13
+ };
14
+ export declare function defineContext<TContext extends AppContext>(): {
15
+ query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
16
+ name: string;
17
+ args: TArgs;
18
+ handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
19
+ }) => QueryDefinition<TContext, TArgs, TOutput, TError>;
20
+ group: <T extends Record<string, APINode>>(definitions: T) => T;
21
+ };
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deessejs/functions",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
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",