@deessejs/functions 0.0.12 → 0.0.14
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/dist/api/index.d.ts +1 -2
- package/dist/api/index.js +3 -1
- package/dist/api/types.d.ts +4 -4
- package/dist/context/define.d.ts +8 -9
- package/dist/context/define.js +8 -0
- package/dist/context/index.d.ts +2 -8
- package/dist/context/index.js +16 -28
- package/dist/context/types.d.ts +15 -0
- package/dist/functions/mutation.d.ts +3 -11
- package/dist/functions/mutation.js +1 -29
- package/dist/functions/query.d.ts +2 -3
- package/dist/functions/query.js +1 -2
- package/dist/functions/types.d.ts +4 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +1 -1
package/dist/api/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { AppContext } from "../context/typing";
|
|
2
1
|
import { ApiRouter } from "./types";
|
|
3
|
-
export declare function createAPI<TCtx extends
|
|
2
|
+
export declare function createAPI<TCtx extends Record<string, unknown>, TRoot extends Record<string, any>>(config: {
|
|
4
3
|
context: TCtx;
|
|
5
4
|
root: TRoot;
|
|
6
5
|
}): ApiRouter<TRoot>;
|
package/dist/api/index.js
CHANGED
|
@@ -6,7 +6,9 @@ const parse_1 = require("../functions/parse");
|
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
function createAPI(config) {
|
|
8
8
|
const hydrate = (node) => {
|
|
9
|
-
if (node &&
|
|
9
|
+
if (node &&
|
|
10
|
+
typeof node === "object" &&
|
|
11
|
+
(node._type === "query" || node._type === "mutation")) {
|
|
10
12
|
const def = node;
|
|
11
13
|
return (input) => {
|
|
12
14
|
const parsed = (0, parse_1.parseArgs)(def.args, input);
|
package/dist/api/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import z
|
|
2
|
-
import { QueryDefinition } from "../context
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import { MutationDefinition, QueryDefinition } from "../context";
|
|
3
3
|
import { AsyncResult } from "../types";
|
|
4
|
-
type
|
|
4
|
+
type InferOperationFn<T> = T extends QueryDefinition<any, infer TArgs, infer TOutput, infer TError> | MutationDefinition<any, infer TArgs, infer TOutput, infer TError> ? (input: z.input<TArgs>) => AsyncResult<TOutput, TError> : never;
|
|
5
5
|
export type ApiRouter<T> = {
|
|
6
|
-
[K in keyof T]: T[K] extends QueryDefinition<any, any, any, any> ?
|
|
6
|
+
[K in keyof T]: T[K] extends QueryDefinition<any, any, any, any> | MutationDefinition<any, any, any, any> ? InferOperationFn<T[K]> : T[K] extends Record<string, any> ? ApiRouter<T[K]> : never;
|
|
7
7
|
};
|
|
8
8
|
export {};
|
package/dist/context/define.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import z, { ZodType } from "zod";
|
|
2
2
|
import { Exception } from "../errors/types";
|
|
3
3
|
import { AsyncResult } from "../types";
|
|
4
|
-
import {
|
|
5
|
-
export type
|
|
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> | {
|
|
4
|
+
import { CommandsDefinition, MutationDefinition, QueryDefinition } from "./types";
|
|
5
|
+
export type APINode = CommandsDefinition | {
|
|
12
6
|
[key: string]: APINode;
|
|
13
7
|
};
|
|
14
|
-
export declare function defineContext<TContext extends
|
|
8
|
+
export declare function defineContext<TContext extends Record<string, unknown>>(): {
|
|
15
9
|
query: <TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
|
|
16
10
|
name: string;
|
|
17
11
|
args: TArgs;
|
|
18
12
|
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
19
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: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
18
|
+
}) => MutationDefinition<TContext, TArgs, TOutput, TError>;
|
|
20
19
|
group: <T extends Record<string, APINode>>(definitions: T) => T;
|
|
21
20
|
};
|
package/dist/context/define.js
CHANGED
|
@@ -11,6 +11,14 @@ function defineContext() {
|
|
|
11
11
|
handler: options.handler,
|
|
12
12
|
};
|
|
13
13
|
},
|
|
14
|
+
mutation: (options) => {
|
|
15
|
+
return {
|
|
16
|
+
_type: "mutation",
|
|
17
|
+
name: options.name,
|
|
18
|
+
args: options.args,
|
|
19
|
+
handler: options.handler,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
14
22
|
group: (definitions) => {
|
|
15
23
|
return definitions;
|
|
16
24
|
},
|
package/dist/context/index.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
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";
|
|
2
|
+
export * from "./types";
|
package/dist/context/index.js
CHANGED
|
@@ -1,30 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
exports
|
|
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
|
|
23
|
-
|
|
24
|
-
|
|
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);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
package/dist/context/types.d.ts
CHANGED
|
@@ -1 +1,16 @@
|
|
|
1
|
+
import z, { ZodType } from "zod";
|
|
2
|
+
import { Exception } from "../errors/types";
|
|
3
|
+
import { AsyncResult } from "../types";
|
|
1
4
|
export type Context = Record<string, unknown>;
|
|
5
|
+
export type CommandDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = {
|
|
6
|
+
name: string;
|
|
7
|
+
args: TArgs;
|
|
8
|
+
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
9
|
+
};
|
|
10
|
+
export type QueryDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
|
|
11
|
+
_type: "query";
|
|
12
|
+
};
|
|
13
|
+
export type MutationDefinition<TContext, TArgs extends ZodType, TOutput, TError extends Exception> = CommandDefinition<TContext, TArgs, TOutput, TError> & {
|
|
14
|
+
_type: "mutation";
|
|
15
|
+
};
|
|
16
|
+
export type CommandsDefinition = QueryDefinition<any, any, any, any> | MutationDefinition<any, any, any, any>;
|
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import { z, ZodType } from "zod";
|
|
2
2
|
import { Exception } from "../errors/types";
|
|
3
3
|
import { AsyncResult } from "../types";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
args: TArgs;
|
|
7
|
-
handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
|
8
|
-
}): (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: {
|
|
10
|
-
args: TArgs;
|
|
11
|
-
handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
|
12
|
-
}) => (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: {
|
|
4
|
+
export declare function mutation<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception, TContext extends Record<string, unknown> = Record<string, unknown>>(options: {
|
|
5
|
+
name: string;
|
|
14
6
|
args: TArgs;
|
|
15
7
|
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
16
|
-
}): (input: z.core.output<TArgs>, context
|
|
8
|
+
}): (input: z.core.output<TArgs>, context: TContext) => AsyncResult<TOutput, TError>;
|
|
@@ -1,43 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createMutation = void 0;
|
|
4
3
|
exports.mutation = mutation;
|
|
5
|
-
exports.mutationWithContext = mutationWithContext;
|
|
6
4
|
const errors_1 = require("../errors");
|
|
7
5
|
const types_1 = require("../types");
|
|
8
6
|
const parse_1 = require("./parse");
|
|
9
|
-
// Mutation functions do not accept context to avoid side effects
|
|
10
|
-
// They remain pure and focused on state modification
|
|
11
7
|
function mutation(options) {
|
|
12
|
-
return (input) => {
|
|
13
|
-
const parsed = (0, parse_1.parseArgs)(options.args, input);
|
|
14
|
-
return parsed.match({
|
|
15
|
-
onSuccess: (data) => options.handler(data),
|
|
16
|
-
onFailure: (error) => {
|
|
17
|
-
const ValidationError = (0, errors_1.exception)({
|
|
18
|
-
name: "ValidationError",
|
|
19
|
-
message: error.message,
|
|
20
|
-
});
|
|
21
|
-
return Promise.resolve((0, types_1.failure)(ValidationError));
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
// Mutation builder for consistency with query builder
|
|
27
|
-
const createMutation = () => {
|
|
28
|
-
return (options) => {
|
|
29
|
-
return mutation(options);
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
exports.createMutation = createMutation;
|
|
33
|
-
// Optional: Mutation that can access context but should not modify it
|
|
34
|
-
function mutationWithContext(options) {
|
|
35
8
|
return (input, context) => {
|
|
36
9
|
const parsed = (0, parse_1.parseArgs)(options.args, input);
|
|
37
10
|
return parsed.match({
|
|
38
11
|
onSuccess: (data) => {
|
|
39
|
-
|
|
40
|
-
const ctx = context ?? globalThis.__context;
|
|
12
|
+
const ctx = context;
|
|
41
13
|
return options.handler(data, ctx);
|
|
42
14
|
},
|
|
43
15
|
onFailure: (error) => {
|
|
@@ -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
|
|
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
|
|
8
|
+
}): (input: z.core.output<TArgs>, context: TContext) => AsyncResult<TOutput, TError>;
|
package/dist/functions/query.js
CHANGED
|
@@ -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
|
|
12
|
+
const ctx = context;
|
|
14
13
|
return options.handler(data, ctx);
|
|
15
14
|
},
|
|
16
15
|
onFailure: (error) => {
|
|
@@ -27,7 +27,8 @@ export type Query<TArgs extends ZodType = ZodType, TError extends Exception = Ex
|
|
|
27
27
|
args: TArgs;
|
|
28
28
|
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
29
29
|
}) => (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
30
|
-
export type Mutation<TArgs extends ZodType = ZodType, TError extends Exception = Exception, TOutput = Unit> = (options: {
|
|
30
|
+
export type Mutation<TArgs extends ZodType = ZodType, TError extends Exception = Exception, TOutput = Unit, TContext = {}> = (options: {
|
|
31
|
+
name: string;
|
|
31
32
|
args: TArgs;
|
|
32
|
-
handler: (args: z.infer<TArgs
|
|
33
|
-
}) => (args: z.infer<TArgs
|
|
33
|
+
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
34
|
+
}) => (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED