@deessejs/functions 0.0.1
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 +0 -0
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.js +24 -0
- package/dist/api/types.d.ts +14 -0
- package/dist/api/types.js +2 -0
- package/dist/checks/errors.d.ts +9 -0
- package/dist/checks/errors.js +11 -0
- package/dist/checks/index.d.ts +2 -0
- package/dist/checks/index.js +28 -0
- package/dist/checks/types.d.ts +9 -0
- package/dist/checks/types.js +2 -0
- package/dist/context/index.d.ts +4 -0
- package/dist/context/index.js +14 -0
- package/dist/context/types.d.ts +1 -0
- package/dist/context/types.js +2 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.js +52 -0
- package/dist/errors/types.d.ts +26 -0
- package/dist/errors/types.js +2 -0
- package/dist/events/index.d.ts +1 -0
- package/dist/events/index.js +4 -0
- package/dist/events/types.d.ts +1 -0
- package/dist/events/types.js +2 -0
- package/dist/functions/index.d.ts +3 -0
- package/dist/functions/index.js +19 -0
- package/dist/functions/mutation.d.ts +7 -0
- package/dist/functions/mutation.js +21 -0
- package/dist/functions/parse.d.ts +4 -0
- package/dist/functions/parse.js +15 -0
- package/dist/functions/query.d.ts +8 -0
- package/dist/functions/query.js +26 -0
- package/dist/functions/types.d.ts +25 -0
- package/dist/functions/types.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/types/array.d.ts +1 -0
- package/dist/types/array.js +2 -0
- package/dist/types/async-result.d.ts +3 -0
- package/dist/types/async-result.js +6 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +14 -0
- package/dist/types/maybe.d.ts +22 -0
- package/dist/types/maybe.js +30 -0
- package/dist/types/result.d.ts +24 -0
- package/dist/types/result.js +31 -0
- package/dist/types/try.d.ts +5 -0
- package/dist/types/try.js +23 -0
- package/dist/types/unit.d.ts +2 -0
- package/dist/types/unit.js +4 -0
- package/package.json +98 -0
package/README.md
ADDED
|
Binary file
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAPI = void 0;
|
|
4
|
+
const createAPI = (config) => {
|
|
5
|
+
let context = { ...config.context };
|
|
6
|
+
const addContext = (key, value) => {
|
|
7
|
+
context = { ...context, [key]: value };
|
|
8
|
+
return api;
|
|
9
|
+
};
|
|
10
|
+
const api = {
|
|
11
|
+
context,
|
|
12
|
+
addContext,
|
|
13
|
+
commands: config.commands,
|
|
14
|
+
events: config.events,
|
|
15
|
+
};
|
|
16
|
+
return api;
|
|
17
|
+
};
|
|
18
|
+
exports.createAPI = createAPI;
|
|
19
|
+
const api = (0, exports.createAPI)({
|
|
20
|
+
context: { user: "John Doe" },
|
|
21
|
+
commands: [],
|
|
22
|
+
events: [],
|
|
23
|
+
});
|
|
24
|
+
exports.default = api;
|
|
@@ -0,0 +1,14 @@
|
|
|
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[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CheckError = void 0;
|
|
4
|
+
class CheckError extends Error {
|
|
5
|
+
constructor({ code, message, details }) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.details = details;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.CheckError = CheckError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.check = void 0;
|
|
4
|
+
const result_1 = require("../types/result");
|
|
5
|
+
const errors_1 = require("./errors");
|
|
6
|
+
const check = ({ args, handler }) => {
|
|
7
|
+
return async (input) => {
|
|
8
|
+
const parsed = args.safeParse(input);
|
|
9
|
+
if (!parsed.success) {
|
|
10
|
+
return (0, result_1.failure)(new errors_1.CheckError({
|
|
11
|
+
code: "INVALID_ARGS",
|
|
12
|
+
message: "Arguments invalides pour le check",
|
|
13
|
+
details: parsed.error.issues,
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
return await handler(parsed.data);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
return (0, result_1.failure)(new errors_1.CheckError({
|
|
21
|
+
code: "INTERNAL_ERROR",
|
|
22
|
+
message: "Erreur interne lors de l’exécution du check",
|
|
23
|
+
details: error,
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
exports.check = check;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AsyncResult } from "../types/async-result";
|
|
2
|
+
import { Unit } from "../types/unit";
|
|
3
|
+
import { CheckError } from "./errors";
|
|
4
|
+
import { z, ZodType } from "zod";
|
|
5
|
+
export type CheckResult = AsyncResult<Unit, CheckError>;
|
|
6
|
+
export type Check<TArgs extends ZodType = ZodType, TError extends CheckError = CheckError> = (options: {
|
|
7
|
+
args: TArgs;
|
|
8
|
+
handler: (args: z.infer<TArgs>) => AsyncResult<Unit, TError>;
|
|
9
|
+
}) => (args: z.infer<TArgs>) => AsyncResult<Unit, TError>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Context = Record<string, unknown>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Exception, ExceptionConfig, ExceptionSpaceConfig, ExceptionGroup } from "./types";
|
|
2
|
+
export declare const exception: (config: ExceptionConfig) => Exception;
|
|
3
|
+
export declare const exceptionSpace: (space: ExceptionSpaceConfig) => {
|
|
4
|
+
define: (config: Omit<ExceptionConfig, "namespace">) => Exception;
|
|
5
|
+
severity: "error" | "info" | "warning" | "critical";
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const raise: (exception: Exception) => Exception;
|
|
9
|
+
export declare const group: (name: string, exceptions: [Exception, ...Exception[]]) => ExceptionGroup;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.group = exports.raise = exports.exceptionSpace = exports.exception = void 0;
|
|
4
|
+
const exception = (config) => {
|
|
5
|
+
const base = {
|
|
6
|
+
...config,
|
|
7
|
+
stack: config.stack ?? [],
|
|
8
|
+
cause: config.cause,
|
|
9
|
+
notes: config.notes ?? [],
|
|
10
|
+
from(cause) {
|
|
11
|
+
return (0, exports.exception)({
|
|
12
|
+
...config,
|
|
13
|
+
cause,
|
|
14
|
+
stack: [...base.stack, cause],
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
is(other) {
|
|
18
|
+
return (other.name === config.name &&
|
|
19
|
+
(config.namespace ? other.namespace === config.namespace : true));
|
|
20
|
+
},
|
|
21
|
+
addNote(note) {
|
|
22
|
+
return (0, exports.exception)({
|
|
23
|
+
...config,
|
|
24
|
+
notes: [...base.notes, note],
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
return base;
|
|
29
|
+
};
|
|
30
|
+
exports.exception = exception;
|
|
31
|
+
const exceptionSpace = (space) => {
|
|
32
|
+
return {
|
|
33
|
+
define: (config) => {
|
|
34
|
+
return (0, exports.exception)({
|
|
35
|
+
...config,
|
|
36
|
+
namespace: space.name,
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
severity: space.severity,
|
|
40
|
+
name: space.name,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.exceptionSpace = exceptionSpace;
|
|
44
|
+
const raise = (exception) => {
|
|
45
|
+
return exception;
|
|
46
|
+
};
|
|
47
|
+
exports.raise = raise;
|
|
48
|
+
const group = (name, exceptions) => ({
|
|
49
|
+
name,
|
|
50
|
+
exceptions,
|
|
51
|
+
});
|
|
52
|
+
exports.group = group;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { NonEmptyArray } from "../types";
|
|
2
|
+
export type ExceptionSpaceConfig = {
|
|
3
|
+
name: string;
|
|
4
|
+
severity: "info" | "warning" | "error" | "critical";
|
|
5
|
+
};
|
|
6
|
+
export type ExceptionConfig = {
|
|
7
|
+
name: string;
|
|
8
|
+
namespace?: string;
|
|
9
|
+
code?: string;
|
|
10
|
+
message?: string;
|
|
11
|
+
cause?: Exception;
|
|
12
|
+
stack?: Exception[];
|
|
13
|
+
notes?: string[];
|
|
14
|
+
};
|
|
15
|
+
export type Exception = ExceptionConfig & {
|
|
16
|
+
stack: Exception[];
|
|
17
|
+
cause?: Exception;
|
|
18
|
+
notes: string[];
|
|
19
|
+
from: (cause: Exception) => Exception;
|
|
20
|
+
is: (exception: Exception) => boolean;
|
|
21
|
+
addNote: (note: string) => Exception;
|
|
22
|
+
};
|
|
23
|
+
export type ExceptionGroup = {
|
|
24
|
+
name: string;
|
|
25
|
+
exceptions: NonEmptyArray<Exception>;
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const listener: undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Event = {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
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);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./query"), exports);
|
|
18
|
+
__exportStar(require("./mutation"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { z, ZodType } from "zod";
|
|
2
|
+
import { Exception } from "../errors/types";
|
|
3
|
+
import { AsyncResult } from "../types";
|
|
4
|
+
export declare function mutation<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
|
|
5
|
+
args: TArgs;
|
|
6
|
+
handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
|
7
|
+
}): (input: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mutation = mutation;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
const parse_1 = require("./parse");
|
|
7
|
+
function mutation(options) {
|
|
8
|
+
return (input) => {
|
|
9
|
+
const parsed = (0, parse_1.parseArgs)(options.args, input);
|
|
10
|
+
return parsed.match({
|
|
11
|
+
onSuccess: (data) => options.handler(data),
|
|
12
|
+
onFailure: (error) => {
|
|
13
|
+
const ValidationError = (0, errors_1.exception)({
|
|
14
|
+
name: "ValidationError",
|
|
15
|
+
message: error.message,
|
|
16
|
+
});
|
|
17
|
+
return Promise.resolve((0, types_1.failure)(ValidationError));
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseArgs = parseArgs;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
function parseArgs(schema, input) {
|
|
7
|
+
const parsed = schema.safeParse(input);
|
|
8
|
+
if (!parsed.success) {
|
|
9
|
+
return (0, types_1.failure)((0, errors_1.exception)({
|
|
10
|
+
name: "ValidatedArgsError",
|
|
11
|
+
message: parsed.error.message,
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
return (0, types_1.success)(parsed.data);
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { z, ZodType } from "zod";
|
|
2
|
+
import { Exception } from "../errors/types";
|
|
3
|
+
import { AsyncResult } from "../types";
|
|
4
|
+
import { Context } from "../context/types";
|
|
5
|
+
export declare function query<TArgs extends ZodType<any, any, any>, TOutput, TError extends Exception = Exception>(options: {
|
|
6
|
+
args: TArgs;
|
|
7
|
+
handler: (args: z.infer<TArgs>, ctx: Context) => AsyncResult<TOutput, TError>;
|
|
8
|
+
}): (input: z.core.output<TArgs>) => AsyncResult<TOutput, TError>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.query = query;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
const parse_1 = require("./parse");
|
|
7
|
+
const context_1 = require("../context");
|
|
8
|
+
// Query finale
|
|
9
|
+
function query(options) {
|
|
10
|
+
return (input) => {
|
|
11
|
+
const parsed = (0, parse_1.parseArgs)(options.args, input);
|
|
12
|
+
return parsed.match({
|
|
13
|
+
onSuccess: (data) => {
|
|
14
|
+
const ctx = (0, context_1.getContext)();
|
|
15
|
+
return options.handler(data, ctx);
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { z, ZodType } from "zod";
|
|
2
|
+
import { Exception } from "../errors/types";
|
|
3
|
+
import { AsyncResult, Unit } from "../types";
|
|
4
|
+
export type Command = {
|
|
5
|
+
beforeInvoke: () => Promise<Unit>;
|
|
6
|
+
afterInvoke: () => Promise<Unit>;
|
|
7
|
+
onSuccess: () => Promise<Unit>;
|
|
8
|
+
onError: (config: {
|
|
9
|
+
exception: Exception;
|
|
10
|
+
}) => Promise<Unit>;
|
|
11
|
+
};
|
|
12
|
+
export type CommandGroup = {
|
|
13
|
+
name: string;
|
|
14
|
+
children: (Command | CommandGroup)[];
|
|
15
|
+
add: (child: Command | CommandGroup) => Unit;
|
|
16
|
+
remove: (child: Command | CommandGroup) => Unit;
|
|
17
|
+
};
|
|
18
|
+
export type Query<TArgs extends ZodType = ZodType, TError extends Exception = Exception, TOutput = Unit, TContext = {}> = (options: {
|
|
19
|
+
args: TArgs;
|
|
20
|
+
handler: (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
21
|
+
}) => (args: z.infer<TArgs>, ctx: TContext) => AsyncResult<TOutput, TError>;
|
|
22
|
+
export type Mutation<TArgs extends ZodType = ZodType, TError extends Exception = Exception, TOutput = Unit> = (options: {
|
|
23
|
+
args: TArgs;
|
|
24
|
+
handler: (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
|
25
|
+
}) => (args: z.infer<TArgs>) => AsyncResult<TOutput, TError>;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
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);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./functions"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type NonEmptyArray<T> = [T, ...T[]];
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { NonEmptyArray } from "../types/array";
|
|
2
|
+
export { AsyncResult } from "../types/async-result";
|
|
3
|
+
export { Result, success, failure } from "../types/result";
|
|
4
|
+
export { Maybe, some, none } from "../types/maybe";
|
|
5
|
+
export { Unit, unit } from "../types/unit";
|
|
6
|
+
export { Try, tryCatch, tryCatchAsync } from "../types/try";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryCatchAsync = exports.tryCatch = exports.unit = exports.none = exports.some = exports.failure = exports.success = void 0;
|
|
4
|
+
var result_1 = require("../types/result");
|
|
5
|
+
Object.defineProperty(exports, "success", { enumerable: true, get: function () { return result_1.success; } });
|
|
6
|
+
Object.defineProperty(exports, "failure", { enumerable: true, get: function () { return result_1.failure; } });
|
|
7
|
+
var maybe_1 = require("../types/maybe");
|
|
8
|
+
Object.defineProperty(exports, "some", { enumerable: true, get: function () { return maybe_1.some; } });
|
|
9
|
+
Object.defineProperty(exports, "none", { enumerable: true, get: function () { return maybe_1.none; } });
|
|
10
|
+
var unit_1 = require("../types/unit");
|
|
11
|
+
Object.defineProperty(exports, "unit", { enumerable: true, get: function () { return unit_1.unit; } });
|
|
12
|
+
var try_1 = require("../types/try");
|
|
13
|
+
Object.defineProperty(exports, "tryCatch", { enumerable: true, get: function () { return try_1.tryCatch; } });
|
|
14
|
+
Object.defineProperty(exports, "tryCatchAsync", { enumerable: true, get: function () { return try_1.tryCatchAsync; } });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type Maybe<T> = Some<T> | None;
|
|
2
|
+
export type Some<T> = {
|
|
3
|
+
readonly _tag: "Some";
|
|
4
|
+
readonly value: T;
|
|
5
|
+
isSome(): this is Some<T>;
|
|
6
|
+
isNone(): this is None;
|
|
7
|
+
match<U>(handlers: {
|
|
8
|
+
onSome: (value: T) => U;
|
|
9
|
+
onNone: () => U;
|
|
10
|
+
}): U;
|
|
11
|
+
};
|
|
12
|
+
export type None = {
|
|
13
|
+
readonly _tag: "None";
|
|
14
|
+
isSome(): this is Some<never>;
|
|
15
|
+
isNone(): this is None;
|
|
16
|
+
match<U>(handlers: {
|
|
17
|
+
onSome: (value: never) => U;
|
|
18
|
+
onNone: () => U;
|
|
19
|
+
}): U;
|
|
20
|
+
};
|
|
21
|
+
export declare const some: <T>(value: T) => Some<T>;
|
|
22
|
+
export declare const none: () => None;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.none = exports.some = void 0;
|
|
4
|
+
const some = (value) => ({
|
|
5
|
+
_tag: "Some",
|
|
6
|
+
value,
|
|
7
|
+
isSome() {
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
isNone() {
|
|
11
|
+
return false;
|
|
12
|
+
},
|
|
13
|
+
match({ onSome }) {
|
|
14
|
+
return onSome(value);
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
exports.some = some;
|
|
18
|
+
const none = () => ({
|
|
19
|
+
_tag: "None",
|
|
20
|
+
isSome() {
|
|
21
|
+
return false;
|
|
22
|
+
},
|
|
23
|
+
isNone() {
|
|
24
|
+
return true;
|
|
25
|
+
},
|
|
26
|
+
match({ onNone }) {
|
|
27
|
+
return onNone();
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
exports.none = none;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Exception } from "../errors/types";
|
|
2
|
+
export type Result<T, E = Exception> = Success<T> | Failure<E>;
|
|
3
|
+
export type Success<T> = {
|
|
4
|
+
readonly _tag: "Success";
|
|
5
|
+
readonly value: T;
|
|
6
|
+
isSuccess(): this is Success<T>;
|
|
7
|
+
isFailure(): this is Failure<never>;
|
|
8
|
+
match<U>(handlers: {
|
|
9
|
+
onSuccess: (value: T) => U;
|
|
10
|
+
onFailure: (error: never) => U;
|
|
11
|
+
}): U;
|
|
12
|
+
};
|
|
13
|
+
export type Failure<E> = {
|
|
14
|
+
readonly _tag: "Failure";
|
|
15
|
+
readonly error: E;
|
|
16
|
+
isSuccess(): this is Success<never>;
|
|
17
|
+
isFailure(): this is Failure<E>;
|
|
18
|
+
match<U>(handlers: {
|
|
19
|
+
onSuccess: (value: never) => U;
|
|
20
|
+
onFailure: (error: E) => U;
|
|
21
|
+
}): U;
|
|
22
|
+
};
|
|
23
|
+
export declare const success: <T>(value: T) => Success<T>;
|
|
24
|
+
export declare const failure: <E>(error: E) => Failure<E>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.failure = exports.success = void 0;
|
|
4
|
+
const success = (value) => ({
|
|
5
|
+
_tag: "Success",
|
|
6
|
+
value,
|
|
7
|
+
isSuccess() {
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
isFailure() {
|
|
11
|
+
return false;
|
|
12
|
+
},
|
|
13
|
+
match(handlers) {
|
|
14
|
+
return handlers.onSuccess(value);
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
exports.success = success;
|
|
18
|
+
const failure = (error) => ({
|
|
19
|
+
_tag: "Failure",
|
|
20
|
+
error,
|
|
21
|
+
isSuccess() {
|
|
22
|
+
return false;
|
|
23
|
+
},
|
|
24
|
+
isFailure() {
|
|
25
|
+
return true;
|
|
26
|
+
},
|
|
27
|
+
match(handlers) {
|
|
28
|
+
return handlers.onFailure(error);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
exports.failure = failure;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AsyncResult } from "./async-result";
|
|
2
|
+
import { Result } from "./result";
|
|
3
|
+
export type Try<T> = () => T;
|
|
4
|
+
export declare const tryCatch: <T, E = unknown>(fn: Try<T>) => Result<T, E>;
|
|
5
|
+
export declare const tryCatchAsync: <T, E = unknown>(fn: () => Promise<T> | T) => AsyncResult<T, E>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryCatchAsync = exports.tryCatch = void 0;
|
|
4
|
+
const result_1 = require("./result");
|
|
5
|
+
const tryCatch = (fn) => {
|
|
6
|
+
try {
|
|
7
|
+
return (0, result_1.success)(fn());
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
return (0, result_1.failure)(error);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
exports.tryCatch = tryCatch;
|
|
14
|
+
const tryCatchAsync = async (fn) => {
|
|
15
|
+
try {
|
|
16
|
+
const value = await Promise.resolve(fn());
|
|
17
|
+
return (0, result_1.success)(value);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
return (0, result_1.failure)(error);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
exports.tryCatchAsync = tryCatchAsync;
|
package/package.json
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deessejs/functions",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A powerful utility library for building type-safe APIs and functions with context management",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.esm.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./api": {
|
|
15
|
+
"import": "./dist/api.esm.js",
|
|
16
|
+
"require": "./dist/api.js",
|
|
17
|
+
"types": "./dist/api.d.ts"
|
|
18
|
+
},
|
|
19
|
+
"./checks": {
|
|
20
|
+
"import": "./dist/checks.esm.js",
|
|
21
|
+
"require": "./dist/checks.js",
|
|
22
|
+
"types": "./dist/checks.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./context": {
|
|
25
|
+
"import": "./dist/context.esm.js",
|
|
26
|
+
"require": "./dist/context.js",
|
|
27
|
+
"types": "./dist/context.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./errors": {
|
|
30
|
+
"import": "./dist/errors.esm.js",
|
|
31
|
+
"require": "./dist/errors.js",
|
|
32
|
+
"types": "./dist/errors.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./events": {
|
|
35
|
+
"import": "./dist/events.esm.js",
|
|
36
|
+
"require": "./dist/events.js",
|
|
37
|
+
"types": "./dist/events.d.ts"
|
|
38
|
+
},
|
|
39
|
+
"./functions": {
|
|
40
|
+
"import": "./dist/functions.esm.js",
|
|
41
|
+
"require": "./dist/functions.js",
|
|
42
|
+
"types": "./dist/functions.d.ts"
|
|
43
|
+
},
|
|
44
|
+
"./types": {
|
|
45
|
+
"import": "./dist/types.esm.js",
|
|
46
|
+
"require": "./dist/types.js",
|
|
47
|
+
"types": "./dist/types.d.ts"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist/**/*",
|
|
52
|
+
"README.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc",
|
|
57
|
+
"build:watch": "tsc --watch",
|
|
58
|
+
"prepublishOnly": "npm run build",
|
|
59
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
60
|
+
"lint": "eslint src --ext .ts",
|
|
61
|
+
"lint:fix": "eslint src --ext .ts --fix"
|
|
62
|
+
},
|
|
63
|
+
"keywords": [
|
|
64
|
+
"typescript",
|
|
65
|
+
"api",
|
|
66
|
+
"functions",
|
|
67
|
+
"context",
|
|
68
|
+
"validation",
|
|
69
|
+
"zod",
|
|
70
|
+
"type-safe",
|
|
71
|
+
"utility"
|
|
72
|
+
],
|
|
73
|
+
"author": "Votre Nom",
|
|
74
|
+
"license": "MIT",
|
|
75
|
+
"repository": {
|
|
76
|
+
"type": "git",
|
|
77
|
+
"url": "https://github.com/votre-organisation/deesse-api.git",
|
|
78
|
+
"directory": "packages/functions"
|
|
79
|
+
},
|
|
80
|
+
"bugs": {
|
|
81
|
+
"url": "https://github.com/votre-organisation/deesse-api/issues"
|
|
82
|
+
},
|
|
83
|
+
"homepage": "https://github.com/votre-organisation/deesse-api#readme",
|
|
84
|
+
"engines": {
|
|
85
|
+
"node": ">=16.0.0"
|
|
86
|
+
},
|
|
87
|
+
"packageManager": "pnpm@10.11.1",
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"zod": "^4.1.12"
|
|
90
|
+
},
|
|
91
|
+
"devDependencies": {
|
|
92
|
+
"@types/node": "^20.0.0",
|
|
93
|
+
"typescript": "^5.0.0",
|
|
94
|
+
"eslint": "^8.0.0",
|
|
95
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
96
|
+
"@typescript-eslint/parser": "^6.0.0"
|
|
97
|
+
}
|
|
98
|
+
}
|