ts-forge 2.0.0 → 2.1.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/dist/decorators/resolver.d.ts +0 -16
- package/dist/decorators/resolverFn.d.ts +0 -7
- package/dist/index.min.js +1 -0
- package/dist/utils/getDefinitionsForClass.d.ts +0 -8
- package/package.json +10 -4
- package/CHANGELOG.md +0 -39
- package/dist/constants.js +0 -4
- package/dist/decorators/resolver.js +0 -25
- package/dist/decorators/resolverFn.js +0 -93
- package/dist/index.js +0 -3
- package/dist/types/forge.js +0 -1
- package/dist/types/index.js +0 -3
- package/dist/types/resolver.js +0 -1
- package/dist/types/utils.js +0 -1
- package/dist/utils/getDefinitionsForClass.js +0 -40
- package/dist/utils/isResolverFnConfig.js +0 -6
|
@@ -1,20 +1,4 @@
|
|
|
1
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
2
|
export declare function Resolver(config?: ResolverConfig): <T extends {
|
|
19
3
|
new (...args: any[]): {};
|
|
20
4
|
}>(constructor: T) => void;
|
|
@@ -1,9 +1,2 @@
|
|
|
1
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
2
|
export declare function ResolverFn(resolverFnConfig: ResolverFnConfig | string): (target: any, propertyKey: string, descriptor: any) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import r from"../constants";export function Resolver(e={middlewares:[],errorHandler:void 0}){return function(o){o.prototype[r.RESOLVER_CONFIG]={middlewares:Array.from(e.middlewares||[]),errorHandler:e.errorHandler}}}var e=this&&this.__awaiter||function(r,e,o,n){return new(o||(o=Promise))(function(t,i){function a(r){try{s(n.next(r))}catch(r){i(r)}}function l(r){try{s(n.throw(r))}catch(r){i(r)}}function s(r){var e;r.done?t(r.value):(e=r.value,e instanceof o?e:new o(function(r){r(e)})).then(a,l)}s((n=n.apply(r,e||[])).next())})};import o from"../utils/isResolverFnConfig";import r from"../constants";export function ResolverFn(n){return function(t,i,a){const l=o(n)?n:{key:n,middlewares:[],errorHandler:void 0};Array.isArray(l.middlewares)||(l.middlewares=[]),t[r.RESOLVER_FUNCTIONS]||(t[r.RESOLVER_FUNCTIONS]=[]),t[r.RESOLVER_FUNCTIONS].push({config:l,methodName:i});const s=a.value;a.value=function(o){return e(this,void 0,void 0,function*(){const e=this[r.RESOLVER_CONFIG]||{};try{const r=Array.from((null==l?void 0:l.middlewares)||[]);Array.isArray(e.middlewares)&&r.push(...e.middlewares),Array.isArray(e.globalMiddlewares)&&r.push(...e.globalMiddlewares);for(const e of r){const r=yield e(o);if(r)return r}return yield s.call(this,o)}catch(r){const n=l.errorHandler||(null==e?void 0:e.errorHandler)||(null==e?void 0:e.globalErrorHandler);if(n)try{return o.context.resolver={key:l.key,className:t.constructor.name,methodName:i},yield n(r,o)}catch(e){return console.error(e),r}return console.error(r),r}})}}}export{};export*from"./forge";export*from"./resolver";export*from"./utils";export{};export{};import n from"@forge/resolver";import r from"../constants";export function getDefinitionsForClass({resolvers:e,middlewares:o=[],errorHandler:t}){const i=new n;if(!Array.isArray(o))throw new Error("Middlewares must be an array");if(o.length>0&&!o.every(r=>"function"==typeof r))throw new Error("All middlewares must be functions");if(t&&"function"!=typeof t)throw new Error("Error handler must be a function");for(const n of e){if(n[r.RESOLVER_CONFIG]){const e={middlewares:Array.from(n[r.RESOLVER_CONFIG].middlewares||[]),errorHandler:n[r.RESOLVER_CONFIG].errorHandler};e.globalMiddlewares=Array.from(o),e.globalErrorHandler=t,n[r.RESOLVER_CONFIG]=e}for(const e of n[r.RESOLVER_FUNCTIONS])i.define(e.config.key,n[e.methodName].bind(n))}return i.getDefinitions()}export default function o(r){return"object"==typeof r&&"string"==typeof(null==r?void 0:r.key)&&(void 0===r.middlewares||Array.isArray(r.middlewares))&&(void 0===r.errorHandler||"function"==typeof r.errorHandler)}
|
|
@@ -1,10 +1,2 @@
|
|
|
1
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
2
|
export declare function getDefinitionsForClass({ resolvers, middlewares, errorHandler }: GetDefinitionsForClassParams): DefinitionsHandler;
|
package/package.json
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-forge",
|
|
3
|
-
"version": "2.0.0",
|
|
4
|
-
"main": "dist/index.js",
|
|
3
|
+
"version": "2.1.0-beta.0",
|
|
4
|
+
"main": "dist/index.min.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist/**/*.d.ts",
|
|
8
|
+
"dist/index.min.js"
|
|
9
|
+
],
|
|
5
10
|
"scripts": {
|
|
6
11
|
"test": "vitest --run --coverage",
|
|
7
12
|
"test:ui": "vitest --ui --coverage",
|
|
8
13
|
"dev": "tsc -p ./tsconfig.json --watch",
|
|
9
|
-
"build": "
|
|
14
|
+
"build": "./scripts/build.sh",
|
|
10
15
|
"prepublish": "npm run build"
|
|
11
16
|
},
|
|
12
17
|
"repository": {
|
|
@@ -31,7 +36,8 @@
|
|
|
31
36
|
"@vitest/coverage-v8": "4.1.2",
|
|
32
37
|
"@vitest/ui": "4.1.2",
|
|
33
38
|
"jsdom": "^29.0.1",
|
|
39
|
+
"terser": "^5.46.1",
|
|
34
40
|
"typescript": "6.0.2",
|
|
35
41
|
"vitest": "^4.1.2"
|
|
36
42
|
}
|
|
37
|
-
}
|
|
43
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
## v2.0.0-beta.0
|
|
2
|
-
|
|
3
|
-
- Resolved warnings from TypeScript 6.0
|
|
4
|
-
|
|
5
|
-
## v1.0.3
|
|
6
|
-
|
|
7
|
-
- README.md: updated documentation
|
|
8
|
-
- Keywords were added to package.json
|
|
9
|
-
- Fixed vulnerabilities in package.json
|
|
10
|
-
|
|
11
|
-
## v1.0.2
|
|
12
|
-
|
|
13
|
-
- Files in the .github and scripts folders were added to .npmignore to avoid publishing them to npm registry
|
|
14
|
-
|
|
15
|
-
## v1.0.1
|
|
16
|
-
|
|
17
|
-
- This version contains changes from the following beta versions:
|
|
18
|
-
- `v1.0.1-beta.0`
|
|
19
|
-
- `v1.0.1-beta.1`
|
|
20
|
-
- Github actions:
|
|
21
|
-
- workflow to publish stable and beta versions were created
|
|
22
|
-
- Added a workflow to run tests on development branch
|
|
23
|
-
|
|
24
|
-
## v1.0.1-beta.1
|
|
25
|
-
|
|
26
|
-
- Improved type definitions for getDefinitionsForClass function
|
|
27
|
-
|
|
28
|
-
## v1.0.1-beta.0
|
|
29
|
-
|
|
30
|
-
- README.md: documentation finished
|
|
31
|
-
- Updated dev dependencies
|
|
32
|
-
- coverage folder was added to .npmignore
|
|
33
|
-
- Improved type definitions for getDefinitionsForClass function
|
|
34
|
-
|
|
35
|
-
## v1.0.0
|
|
36
|
-
|
|
37
|
-
- `@Resolver()` and `@ResolverFn()` decorators were created
|
|
38
|
-
- Created `getDefinitionsForClass()` function which handles resolver functions definition
|
|
39
|
-
- Unit testing for decorators and utils
|
package/dist/constants.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
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.js
DELETED
package/dist/types/forge.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/index.js
DELETED
package/dist/types/resolver.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/utils.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
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
|
-
}
|