ts-forge 1.0.3 → 2.0.0-beta.0
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/CHANGELOG.md +4 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +4 -0
- package/dist/decorators/resolver.d.ts +20 -0
- package/dist/decorators/resolver.js +25 -0
- package/dist/decorators/resolverFn.d.ts +9 -0
- package/dist/decorators/resolverFn.js +93 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/types/forge.d.ts +15 -0
- package/dist/types/forge.js +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/resolver.d.ts +29 -0
- package/dist/types/resolver.js +1 -0
- package/dist/types/utils.d.ts +6 -0
- package/dist/types/utils.js +1 -0
- package/dist/utils/getDefinitionsForClass.d.ts +10 -0
- package/dist/utils/getDefinitionsForClass.js +40 -0
- package/dist/utils/isResolverFnConfig.d.ts +2 -0
- package/dist/utils/isResolverFnConfig.js +6 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ResolverConfig } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator for defining forge resolvers.
|
|
4
|
+
* @param ResolverConfig - Configuration object for the resolver.
|
|
5
|
+
* @param ResolverConfig.middlewares - Array of middleware functions to be applied to the resolver functions.
|
|
6
|
+
* @param ResolverConfig.errorHandler - Custom error handler function for the resolver functions.
|
|
7
|
+
* @returns A modified class with resolver configuration.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* \@Resolver({
|
|
12
|
+
* middlewares: [myMiddleware],
|
|
13
|
+
* errorHandler: myErrorHandler,
|
|
14
|
+
* })
|
|
15
|
+
* class MyResolver{}
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function Resolver(config?: ResolverConfig): <T extends {
|
|
19
|
+
new (...args: any[]): {};
|
|
20
|
+
}>(constructor: T) => void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import _ from "../constants";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator for defining forge resolvers.
|
|
4
|
+
* @param ResolverConfig - Configuration object for the resolver.
|
|
5
|
+
* @param ResolverConfig.middlewares - Array of middleware functions to be applied to the resolver functions.
|
|
6
|
+
* @param ResolverConfig.errorHandler - Custom error handler function for the resolver functions.
|
|
7
|
+
* @returns A modified class with resolver configuration.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* \@Resolver({
|
|
12
|
+
* middlewares: [myMiddleware],
|
|
13
|
+
* errorHandler: myErrorHandler,
|
|
14
|
+
* })
|
|
15
|
+
* class MyResolver{}
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function Resolver(config = { middlewares: [], errorHandler: undefined }) {
|
|
19
|
+
return function (constructor) {
|
|
20
|
+
constructor.prototype[_.RESOLVER_CONFIG] = {
|
|
21
|
+
middlewares: Array.from(config.middlewares || []),
|
|
22
|
+
errorHandler: config.errorHandler
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ResolverFnConfig } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Define a resolver function
|
|
4
|
+
* @param resolverFnConfig - Resolver function config
|
|
5
|
+
* @param resolverFnConfig.middlewares - Array of middleware functions to be applied to this resolver function
|
|
6
|
+
* @param resolverFnConfig.errorHandler - Custom error handler function for this resolver function
|
|
7
|
+
* @returns - Response
|
|
8
|
+
*/
|
|
9
|
+
export declare function ResolverFn(resolverFnConfig: ResolverFnConfig | string): (target: any, propertyKey: string, descriptor: any) => void;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import isResolverFnConfig from "../utils/isResolverFnConfig";
|
|
11
|
+
import _ from "../constants";
|
|
12
|
+
/**
|
|
13
|
+
* Define a resolver function
|
|
14
|
+
* @param resolverFnConfig - Resolver function config
|
|
15
|
+
* @param resolverFnConfig.middlewares - Array of middleware functions to be applied to this resolver function
|
|
16
|
+
* @param resolverFnConfig.errorHandler - Custom error handler function for this resolver function
|
|
17
|
+
* @returns - Response
|
|
18
|
+
*/
|
|
19
|
+
export function ResolverFn(resolverFnConfig) {
|
|
20
|
+
return function (target, propertyKey, descriptor) {
|
|
21
|
+
// Handle the case where resolverFnConfig is a string
|
|
22
|
+
const config = isResolverFnConfig(resolverFnConfig)
|
|
23
|
+
? resolverFnConfig
|
|
24
|
+
: { key: resolverFnConfig, middlewares: [], errorHandler: undefined };
|
|
25
|
+
// If the middlewares property is not an array, initialize it as an empty array
|
|
26
|
+
if (!Array.isArray(config.middlewares)) {
|
|
27
|
+
config.middlewares = [];
|
|
28
|
+
}
|
|
29
|
+
// Check if the target has the resolverNames property
|
|
30
|
+
if (!target[_.RESOLVER_FUNCTIONS]) {
|
|
31
|
+
target[_.RESOLVER_FUNCTIONS] = [];
|
|
32
|
+
}
|
|
33
|
+
// Add the resolver's data to the target
|
|
34
|
+
target[_.RESOLVER_FUNCTIONS].push({
|
|
35
|
+
config,
|
|
36
|
+
methodName: propertyKey
|
|
37
|
+
});
|
|
38
|
+
const method = descriptor.value;
|
|
39
|
+
descriptor.value = function (req) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const targetConfig = this[_.RESOLVER_CONFIG] || {};
|
|
42
|
+
try {
|
|
43
|
+
// Merge middlewares from the resolver function and the resolver class
|
|
44
|
+
// Method middlewares are always executed first
|
|
45
|
+
const middlewares = Array.from((config === null || config === void 0 ? void 0 : config.middlewares) || []);
|
|
46
|
+
// If there are middlewares defined in the resolver class, add them to the middlewares array
|
|
47
|
+
if (Array.isArray(targetConfig.middlewares)) {
|
|
48
|
+
middlewares.push(...targetConfig.middlewares);
|
|
49
|
+
}
|
|
50
|
+
// If there are middlewares defined in the getDefinitionsForClass config, add them to the middlewares array
|
|
51
|
+
if (Array.isArray(targetConfig.globalMiddlewares)) {
|
|
52
|
+
middlewares.push(...targetConfig.globalMiddlewares);
|
|
53
|
+
}
|
|
54
|
+
// Run provided middlewares
|
|
55
|
+
for (const middleware of middlewares) {
|
|
56
|
+
const response = yield middleware(req);
|
|
57
|
+
// If response is provided, send it to the frontend
|
|
58
|
+
// This means that the resolver or next middleware will not be called
|
|
59
|
+
if (response) {
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Call the original method
|
|
64
|
+
return yield method.call(this, req);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
// Resolver function error handler has priority over the resolver error handler
|
|
68
|
+
const errorHandlerFn = config.errorHandler || (targetConfig === null || targetConfig === void 0 ? void 0 : targetConfig.errorHandler) || (targetConfig === null || targetConfig === void 0 ? void 0 : targetConfig.globalErrorHandler);
|
|
69
|
+
// If an error handler is provided, call it with the request data and error object
|
|
70
|
+
// This allows the error handler to handle the error and return a response
|
|
71
|
+
if (errorHandlerFn) {
|
|
72
|
+
try {
|
|
73
|
+
req.context.resolver = {
|
|
74
|
+
key: config.key,
|
|
75
|
+
className: target.constructor.name,
|
|
76
|
+
methodName: propertyKey
|
|
77
|
+
};
|
|
78
|
+
return yield errorHandlerFn(error, req);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
// If the error handler throws an error, log it and return the original error
|
|
82
|
+
console.error(err);
|
|
83
|
+
return error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// If no error handler is provided, log the error, then return it
|
|
87
|
+
console.error(error);
|
|
88
|
+
return error;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ResolverFunction, Request } from "@forge/resolver";
|
|
2
|
+
interface InvokePayload {
|
|
3
|
+
call: {
|
|
4
|
+
functionKey: string;
|
|
5
|
+
payload?: {
|
|
6
|
+
[key in number | string]: any;
|
|
7
|
+
};
|
|
8
|
+
jobId?: string;
|
|
9
|
+
};
|
|
10
|
+
context: {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export type DefinitionsHandler = (payload: InvokePayload, backendRuntimePayload?: Request["payload"]) => Promise<ReturnType<ResolverFunction>>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Request } from "@forge/resolver";
|
|
2
|
+
export interface ResolverConfig {
|
|
3
|
+
middlewares?: MiddlewareFn[];
|
|
4
|
+
errorHandler?: ErrorHandlerFn;
|
|
5
|
+
}
|
|
6
|
+
export interface ResolverClassConfig extends ResolverConfig {
|
|
7
|
+
globalMiddlewares?: MiddlewareFn[];
|
|
8
|
+
globalErrorHandler?: ErrorHandlerFn;
|
|
9
|
+
}
|
|
10
|
+
export type MiddlewareFn = (req: Request) => Promise<any> | any;
|
|
11
|
+
export type ErrorHandlerFn = (error: any, req: RequestError) => any;
|
|
12
|
+
export type RequestError = Request & {
|
|
13
|
+
context: Request["context"] & {
|
|
14
|
+
resolver: {
|
|
15
|
+
key: string;
|
|
16
|
+
className: string;
|
|
17
|
+
methodName: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export interface ResolverFnConfig {
|
|
22
|
+
key: string;
|
|
23
|
+
middlewares?: MiddlewareFn[];
|
|
24
|
+
errorHandler?: ErrorHandlerFn;
|
|
25
|
+
}
|
|
26
|
+
export interface TargetResolverFnConfig {
|
|
27
|
+
config: ResolverFnConfig;
|
|
28
|
+
methodName: string;
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DefinitionsHandler, GetDefinitionsForClassParams } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Get definitions for the provided resolvers
|
|
4
|
+
* @param config - Config
|
|
5
|
+
* @param config.resolvers - Array of resolvers
|
|
6
|
+
* @param config.middlewares - Array of middlewares to be applied to the provided resolvers
|
|
7
|
+
* @param config.errorHandler - Custom error handler to be applied to the provided resolvers
|
|
8
|
+
* @returns - Resolver definitions
|
|
9
|
+
*/
|
|
10
|
+
export declare function getDefinitionsForClass({ resolvers, middlewares, errorHandler }: GetDefinitionsForClassParams): DefinitionsHandler;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import ForgeResolver from "@forge/resolver";
|
|
2
|
+
import _ from "../constants";
|
|
3
|
+
/**
|
|
4
|
+
* Get definitions for the provided resolvers
|
|
5
|
+
* @param config - Config
|
|
6
|
+
* @param config.resolvers - Array of resolvers
|
|
7
|
+
* @param config.middlewares - Array of middlewares to be applied to the provided resolvers
|
|
8
|
+
* @param config.errorHandler - Custom error handler to be applied to the provided resolvers
|
|
9
|
+
* @returns - Resolver definitions
|
|
10
|
+
*/
|
|
11
|
+
export function getDefinitionsForClass({ resolvers, middlewares = [], errorHandler }) {
|
|
12
|
+
const forgeResolver = new ForgeResolver();
|
|
13
|
+
if (!Array.isArray(middlewares)) {
|
|
14
|
+
throw new Error("Middlewares must be an array");
|
|
15
|
+
}
|
|
16
|
+
if (middlewares.length > 0 && !middlewares.every((m) => typeof m === "function")) {
|
|
17
|
+
throw new Error("All middlewares must be functions");
|
|
18
|
+
}
|
|
19
|
+
if (errorHandler && typeof errorHandler !== "function") {
|
|
20
|
+
throw new Error("Error handler must be a function");
|
|
21
|
+
}
|
|
22
|
+
for (const instance of resolvers) {
|
|
23
|
+
if (instance[_.RESOLVER_CONFIG]) {
|
|
24
|
+
// Set global middlewares and error handler for the resolver class
|
|
25
|
+
const instanceConfig = {
|
|
26
|
+
middlewares: Array.from(instance[_.RESOLVER_CONFIG].middlewares || []),
|
|
27
|
+
errorHandler: instance[_.RESOLVER_CONFIG].errorHandler
|
|
28
|
+
};
|
|
29
|
+
instanceConfig.globalMiddlewares = Array.from(middlewares);
|
|
30
|
+
// Set the global error handler
|
|
31
|
+
instanceConfig.globalErrorHandler = errorHandler;
|
|
32
|
+
// Set the updated config on the instance
|
|
33
|
+
instance[_.RESOLVER_CONFIG] = instanceConfig;
|
|
34
|
+
}
|
|
35
|
+
for (const resolver of instance[_.RESOLVER_FUNCTIONS]) {
|
|
36
|
+
forgeResolver.define(resolver.config.key, instance[resolver.methodName].bind(instance));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return forgeResolver.getDefinitions();
|
|
40
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export default function isResolverFnConfig(config) {
|
|
2
|
+
return (typeof config === "object" &&
|
|
3
|
+
typeof (config === null || config === void 0 ? void 0 : config.key) === "string" &&
|
|
4
|
+
(typeof config.middlewares === "undefined" || Array.isArray(config.middlewares)) &&
|
|
5
|
+
(typeof config.errorHandler === "undefined" || typeof config.errorHandler === "function"));
|
|
6
|
+
}
|