@proteinjs/service 1.0.31 → 1.1.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 +22 -0
- package/LICENSE +21 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/Service.d.ts +11 -1
- package/dist/src/Service.d.ts.map +1 -1
- package/dist/src/Service.js +88 -4
- package/dist/src/Service.js.map +1 -1
- package/dist/src/ServiceExecutor.d.ts +3 -0
- package/dist/src/ServiceExecutor.d.ts.map +1 -1
- package/dist/src/ServiceExecutor.js +27 -2
- package/dist/src/ServiceExecutor.js.map +1 -1
- package/dist/src/ServiceRouter.d.ts.map +1 -1
- package/dist/src/ServiceRouter.js +10 -3
- package/dist/src/ServiceRouter.js.map +1 -1
- package/generated/index.ts +4 -7
- package/package.json +3 -2
- package/src/Service.ts +39 -2
- package/src/ServiceExecutor.ts +8 -1
- package/src/ServiceRouter.ts +13 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.1.0](https://github.com/proteinjs/service/compare/@proteinjs/service@1.0.32...@proteinjs/service@1.1.0) (2024-09-09)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add retry functionality to serviceFactory with configurable retries per method. Implement RetryConfig type and logic to handle retries for specified service methods. ([cc9627f](https://github.com/proteinjs/service/commit/cc9627fe12aa40920764e0fa2debc1547881b887))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [1.0.32](https://github.com/proteinjs/service/compare/@proteinjs/service@1.0.31...@proteinjs/service@1.0.32) (2024-08-30)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* avoid logging twice for service errors ([5f5e593](https://github.com/proteinjs/service/commit/5f5e59377fbd81d90d4607bd6e56aa2865c7e38d))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
6
28
|
## [1.0.28](https://github.com/proteinjs/service/compare/@proteinjs/service@1.0.27...@proteinjs/service@1.0.28) (2024-08-16)
|
|
7
29
|
|
|
8
30
|
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Brent Bahry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/generated/index.js
CHANGED
|
@@ -22,7 +22,7 @@ require("@proteinjs/server-api");
|
|
|
22
22
|
require("@proteinjs/user-auth");
|
|
23
23
|
require("@proteinjs/util");
|
|
24
24
|
/** Generate Source Graph */
|
|
25
|
-
var sourceGraph = "{\"options\":{\"directed\":true,\"multigraph\":false,\"compound\":false},\"nodes\":[{\"v\":\"@proteinjs/service/Service\",\"value\":{\"packageName\":\"@proteinjs/service\",\"name\":\"Service\",\"filePath\":\"/
|
|
25
|
+
var sourceGraph = "{\"options\":{\"directed\":true,\"multigraph\":false,\"compound\":false},\"nodes\":[{\"v\":\"@proteinjs/service/Service\",\"value\":{\"packageName\":\"@proteinjs/service\",\"name\":\"Service\",\"filePath\":\"/home/runner/work/service/service/packages/service/src/Service.ts\",\"qualifiedName\":\"@proteinjs/service/Service\",\"properties\":[{\"name\":\"serviceMetadata\",\"type\":{\"packageName\":\"@proteinjs/service\",\"name\":\"{\\n auth?: {\\n /** If true, the user does not need to be logged in or have any roles to call this service. If blank, defaults to false. */\\n public?: boolean;\\n /** If true, the user does not need to have any roles to call this service, but must be logged in. If blank, defaults to false. */\\n allUsers?: boolean;\\n /** The user must be logged in and have these roles to call this service. If blank, defaults to requiring the 'admin' role. */\\n roles?: string[];\\n /**\\n * Custom auth function. If provided, all other auth properties are ignored.\\n * @param methodName the name of the service method to be executed\\n * @param args the args[] that will be passed into the method\\n * @return true if the user can access the service method\\n */\\n canAccess?: (methodName: string, args: any[]) => boolean;\\n };\\n /** Don't await the service's execution, return a response to the client immediately */\\n doNotAwait?: boolean;\\n }\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/service/{\\n auth?: {\\n /** If true, the user does not need to be logged in or have any roles to call this service. If blank, defaults to false. */\\n public?: boolean;\\n /** If true, the user does not need to have any roles to call this service, but must be logged in. If blank, defaults to false. */\\n allUsers?: boolean;\\n /** The user must be logged in and have these roles to call this service. If blank, defaults to requiring the 'admin' role. */\\n roles?: string[];\\n /**\\n * Custom auth function. If provided, all other auth properties are ignored.\\n * @param methodName the name of the service method to be executed\\n * @param args the args[] that will be passed into the method\\n * @return true if the user can access the service method\\n */\\n canAccess?: (methodName: string, args: any[]) => boolean;\\n };\\n /** Don't await the service's execution, return a response to the client immediately */\\n doNotAwait?: boolean;\\n }\",\"typeParameters\":null,\"directParents\":null},\"isOptional\":true,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"public\"}],\"methods\":[],\"typeParameters\":[],\"directParents\":[{\"packageName\":\"@proteinjs/reflection\",\"name\":\"Loadable\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/reflection/Loadable\",\"properties\":[],\"methods\":[],\"typeParameters\":[],\"directParents\":[]}],\"sourceType\":3}},{\"v\":\"@proteinjs/reflection/Loadable\"},{\"v\":\"/Error\"},{\"v\":\"@proteinjs/service/ServiceRouter\",\"value\":{\"packageName\":\"@proteinjs/service\",\"name\":\"ServiceRouter\",\"filePath\":\"/home/runner/work/service/service/packages/service/src/ServiceRouter.ts\",\"qualifiedName\":\"@proteinjs/service/ServiceRouter\",\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"public\",\"properties\":[{\"name\":\"logger\",\"type\":null,\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"private\"},{\"name\":\"serviceExecutorMap\",\"type\":{\"packageName\":\"@proteinjs/service\",\"name\":\"{ [path: string]: ServiceExecutor } | undefined\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/service/{ [path: string]: ServiceExecutor } | undefined\",\"typeParameters\":null,\"directParents\":null},\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"private\"},{\"name\":\"path\",\"type\":null,\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"public\"},{\"name\":\"method\",\"type\":{\"packageName\":\"\",\"name\":\"'post'\",\"filePath\":null,\"qualifiedName\":\"/'post'\",\"typeParameters\":null,\"directParents\":null},\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"public\"}],\"methods\":[{\"name\":\"getServiceExecutorMap\",\"returnType\":null,\"isAsync\":false,\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"private\",\"parameters\":[]},{\"name\":\"onRequest\",\"returnType\":{\"packageName\":\"\",\"name\":\"Promise<any>\",\"filePath\":null,\"qualifiedName\":\"/Promise<any>\",\"typeParameters\":null,\"directParents\":null},\"isAsync\":true,\"isOptional\":false,\"isAbstract\":false,\"isStatic\":false,\"visibility\":\"public\",\"parameters\":[{\"name\":\"request\",\"type\":{\"packageName\":\"\",\"name\":\"any\",\"filePath\":null,\"qualifiedName\":\"/any\",\"typeParameters\":null,\"directParents\":null}},{\"name\":\"response\",\"type\":{\"packageName\":\"\",\"name\":\"any\",\"filePath\":null,\"qualifiedName\":\"/any\",\"typeParameters\":null,\"directParents\":null}}]}],\"typeParameters\":[],\"directParentInterfaces\":[{\"packageName\":\"@proteinjs/server-api\",\"name\":\"Route\",\"filePath\":null,\"qualifiedName\":\"@proteinjs/server-api/Route\",\"properties\":[],\"methods\":[],\"typeParameters\":[],\"directParents\":[]}],\"directParentClasses\":[],\"sourceType\":2}},{\"v\":\"@proteinjs/server-api/Route\"}],\"edges\":[{\"v\":\"@proteinjs/service/Service\",\"w\":\"@proteinjs/reflection/Loadable\",\"value\":\"extends interface\"},{\"v\":\"@proteinjs/service/ServiceRouter\",\"w\":\"@proteinjs/server-api/Route\",\"value\":\"implements interface\"}]}";
|
|
26
26
|
/** Generate Source Links */
|
|
27
27
|
var ServiceRouter_1 = require("../src/ServiceRouter");
|
|
28
28
|
var sourceLinks = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../generated/index.ts"],"names":[],"mappings":";AAAA,oCAAoC;;;;;;;;;;;;;;;;AAEpC,6BAA2B;AAC3B,iCAA+B;AAC/B,iCAA+B;AAC/B,iCAA+B;AAC/B,gCAA8B;AAC9B,2BAAyB;AAGzB,4BAA4B;AAE5B,IAAM,WAAW,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../generated/index.ts"],"names":[],"mappings":";AAAA,oCAAoC;;;;;;;;;;;;;;;;AAEpC,6BAA2B;AAC3B,iCAA+B;AAC/B,iCAA+B;AAC/B,iCAA+B;AAC/B,gCAA8B;AAC9B,2BAAyB;AAGzB,4BAA4B;AAE5B,IAAM,WAAW,GAAG,2jLAA2jL,CAAC;AAGhlL,4BAA4B;AAE5B,sDAAqD;AAErD,IAAM,WAAW,GAAG;IACnB,kCAAkC,EAAE,6BAAa;CACjD,CAAC;AAGF,kCAAkC;AAElC,oDAAyD;AACzD,6BAAgB,CAAC,KAAK,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAGjD,2CAAyB"}
|
package/dist/src/Service.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { SerializableFunction, NotFunction } from '@proteinjs/serializer';
|
|
2
2
|
import { Loadable } from '@proteinjs/reflection';
|
|
3
3
|
import { Debouncer } from '@proteinjs/util';
|
|
4
|
+
type RemoveIndex<T> = {
|
|
5
|
+
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
|
|
6
|
+
};
|
|
7
|
+
type KeysWithoutIndexSignature<T> = keyof RemoveIndex<T>;
|
|
8
|
+
type Diff<T, U> = T extends U ? never : T;
|
|
9
|
+
type KeysWithoutService<T extends Service> = Diff<KeysWithoutIndexSignature<T>, KeysWithoutIndexSignature<Service>>;
|
|
10
|
+
type RetryConfig<T extends Service> = {
|
|
11
|
+
[K in KeysWithoutService<T>]?: number;
|
|
12
|
+
};
|
|
4
13
|
export interface Service extends Loadable {
|
|
5
14
|
serviceMetadata?: {
|
|
6
15
|
auth?: {
|
|
@@ -30,5 +39,6 @@ export interface Service extends Loadable {
|
|
|
30
39
|
* @param debouncer optionally pass in an instance of debouncer if you want to limit service client calls
|
|
31
40
|
* @returns a function that creates a Service
|
|
32
41
|
*/
|
|
33
|
-
export declare const serviceFactory: <T extends Service>(serviceInterfaceQualifiedName: string, debouncer?: Debouncer) => () => T;
|
|
42
|
+
export declare const serviceFactory: <T extends Service>(serviceInterfaceQualifiedName: string, debouncer?: Debouncer, retryConfig?: RetryConfig<T> | undefined) => () => T;
|
|
43
|
+
export {};
|
|
34
44
|
//# sourceMappingURL=Service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Service.d.ts","sourceRoot":"","sources":["../../src/Service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,CAAC;AAEnE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,WAAW,OAAQ,SAAQ,QAAQ;IACvC,eAAe,CAAC,EAAE;QAChB,IAAI,CAAC,EAAE;YACL,2HAA2H;YAC3H,MAAM,CAAC,EAAE,OAAO,CAAC;YACjB,kIAAkI;YAClI,QAAQ,CAAC,EAAE,OAAO,CAAC;YACnB,8HAA8H;YAC9H,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;YACjB;;;;;eAKG;YACH,SAAS,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC;SAC1D,CAAC;QACF,uFAAuF;QACvF,UAAU,CAAC,EAAE,OAAO,CAAC;KACtB,CAAC;IACF,CAAC,IAAI,EAAE,MAAM,GAAG,oBAAoB,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;CACzD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,qDACM,MAAM,cACzB,SAAS,
|
|
1
|
+
{"version":3,"file":"Service.d.ts","sourceRoot":"","sources":["../../src/Service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,QAAQ,EAAoB,MAAM,uBAAuB,CAAC;AAEnE,OAAO,EA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;GAMG;AACH,eAAO,MAAM,cAAc,qDACM,MAAM,cACzB,SAAS,sDAmCtB,CAAC"}
|
package/dist/src/Service.js
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.serviceFactory = void 0;
|
|
4
40
|
var reflection_1 = require("@proteinjs/reflection");
|
|
@@ -10,15 +46,63 @@ var ServiceClient_1 = require("./ServiceClient");
|
|
|
10
46
|
* @param debouncer optionally pass in an instance of debouncer if you want to limit service client calls
|
|
11
47
|
* @returns a function that creates a Service
|
|
12
48
|
*/
|
|
13
|
-
var serviceFactory = function (serviceInterfaceQualifiedName, debouncer) {
|
|
49
|
+
var serviceFactory = function (serviceInterfaceQualifiedName, debouncer, retryConfig) {
|
|
14
50
|
return function () {
|
|
15
51
|
var service = {};
|
|
16
52
|
var serviceInterface = reflection_1.SourceRepository.get().interface(serviceInterfaceQualifiedName);
|
|
17
|
-
|
|
18
|
-
var method = _a[_i];
|
|
53
|
+
var _loop_1 = function (method) {
|
|
19
54
|
var servicePath = "/service/".concat(serviceInterface.qualifiedName, "/").concat(method.name);
|
|
20
55
|
var serviceClient = new ServiceClient_1.ServiceClient(servicePath, method, debouncer);
|
|
21
|
-
|
|
56
|
+
if (retryConfig && method.name in retryConfig) {
|
|
57
|
+
var methodName = method.name;
|
|
58
|
+
var maxRetries_1 = retryConfig[methodName];
|
|
59
|
+
service[method.name] = function () {
|
|
60
|
+
var args = [];
|
|
61
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
62
|
+
args[_i] = arguments[_i];
|
|
63
|
+
}
|
|
64
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
65
|
+
var lastError, attempt, error_1;
|
|
66
|
+
return __generator(this, function (_a) {
|
|
67
|
+
switch (_a.label) {
|
|
68
|
+
case 0:
|
|
69
|
+
attempt = 0;
|
|
70
|
+
_a.label = 1;
|
|
71
|
+
case 1:
|
|
72
|
+
if (!(attempt < maxRetries_1)) return [3 /*break*/, 7];
|
|
73
|
+
_a.label = 2;
|
|
74
|
+
case 2:
|
|
75
|
+
_a.trys.push([2, 3, , 6]);
|
|
76
|
+
return [2 /*return*/, serviceClient.send.bind(serviceClient).apply(void 0, args)];
|
|
77
|
+
case 3:
|
|
78
|
+
error_1 = _a.sent();
|
|
79
|
+
lastError = error_1;
|
|
80
|
+
if (!(attempt < maxRetries_1 - 1)) return [3 /*break*/, 5];
|
|
81
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
|
|
82
|
+
case 4:
|
|
83
|
+
_a.sent(); // short wait before retrying
|
|
84
|
+
_a.label = 5;
|
|
85
|
+
case 5: return [3 /*break*/, 6];
|
|
86
|
+
case 6:
|
|
87
|
+
attempt++;
|
|
88
|
+
return [3 /*break*/, 1];
|
|
89
|
+
case 7:
|
|
90
|
+
if (lastError) {
|
|
91
|
+
throw lastError;
|
|
92
|
+
}
|
|
93
|
+
return [2 /*return*/];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
service[method.name] = serviceClient.send.bind(serviceClient);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
for (var _i = 0, _a = serviceInterface.methods; _i < _a.length; _i++) {
|
|
104
|
+
var method = _a[_i];
|
|
105
|
+
_loop_1(method);
|
|
22
106
|
}
|
|
23
107
|
return service;
|
|
24
108
|
};
|
package/dist/src/Service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Service.js","sourceRoot":"","sources":["../../src/Service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Service.js","sourceRoot":"","sources":["../../src/Service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,oDAAmE;AACnE,iDAAgD;AAyChD;;;;;;GAMG;AACI,IAAM,cAAc,GAAG,UAC5B,6BAAqC,EACrC,SAAqB,EACrB,WAA4B;IAE5B,OAAO;QACL,IAAM,OAAO,GAAQ,EAAE,CAAC;QACxB,IAAM,gBAAgB,GAAG,6BAAgB,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,6BAA6B,CAAC,CAAC;gCAC9E,MAAM;YACf,IAAM,WAAW,GAAG,mBAAY,gBAAgB,CAAC,aAAa,cAAI,MAAM,CAAC,IAAI,CAAE,CAAC;YAChF,IAAM,aAAa,GAAG,IAAI,6BAAa,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;YACxE,IAAI,WAAW,IAAI,MAAM,CAAC,IAAI,IAAI,WAAW,EAAE;gBAC7C,IAAM,UAAU,GAAG,MAAM,CAAC,IAA6B,CAAC;gBACxD,IAAM,YAAU,GAAG,WAAW,CAAC,UAAU,CAAE,CAAC;gBAC5C,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG;oBAAO,cAAc;yBAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;wBAAd,yBAAc;;;;;;;oCAEjC,OAAO,GAAG,CAAC;;;yCAAE,CAAA,OAAO,GAAG,YAAU,CAAA;;;;oCAEtC,sBAAO,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,eAAI,IAAI,GAAE;;;oCAEvD,SAAS,GAAG,OAAK,CAAC;yCACd,CAAA,OAAO,GAAG,YAAU,GAAG,CAAC,CAAA,EAAxB,wBAAwB;oCAC1B,qBAAM,IAAI,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,EAAzB,CAAyB,CAAC,EAAA;;oCAAzD,SAAyD,CAAC,CAAC,6BAA6B;;;;oCANlD,OAAO,EAAE,CAAA;;;oCAUrD,IAAI,SAAS,EAAE;wCACb,MAAM,SAAS,CAAC;qCACjB;;;;;iBACF,CAAC;aACH;iBAAM;gBACL,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;aAC/D;;QAxBH,KAAqB,UAAwB,EAAxB,KAAA,gBAAgB,CAAC,OAAO,EAAxB,cAAwB,EAAxB,IAAwB;YAAxC,IAAM,MAAM,SAAA;oBAAN,MAAM;SAyBhB;QAED,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC,CAAC;AArCW,QAAA,cAAc,kBAqCzB"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Interface, Method } from '@proteinjs/reflection';
|
|
2
2
|
import { Service } from './Service';
|
|
3
|
+
export declare class ServiceError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
3
6
|
export declare class ServiceExecutor {
|
|
4
7
|
service: Service;
|
|
5
8
|
private _interface;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceExecutor.d.ts","sourceRoot":"","sources":["../../src/ServiceExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC,qBAAa,eAAe;IAKjB,OAAO,EAAE,OAAO;IACvB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,MAAM;IANhB,OAAO,CAAC,MAAM,CAAS;IAChB,gBAAgB,EAAE,GAAG,CAAC;IAC7B,OAAO,CAAC,iBAAiB,CAAS;gBAEzB,OAAO,EAAE,OAAO,EACf,UAAU,EAAE,SAAS,EACrB,MAAM,EAAE,MAAM;IAMlB,OAAO,CAAC,WAAW,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IA4C7C,OAAO,CAAC,sBAAsB;CAG/B"}
|
|
1
|
+
{"version":3,"file":"ServiceExecutor.d.ts","sourceRoot":"","sources":["../../src/ServiceExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,eAAe;IAKjB,OAAO,EAAE,OAAO;IACvB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,MAAM;IANhB,OAAO,CAAC,MAAM,CAAS;IAChB,gBAAgB,EAAE,GAAG,CAAC;IAC7B,OAAO,CAAC,iBAAiB,CAAS;gBAEzB,OAAO,EAAE,OAAO,EACf,UAAU,EAAE,SAAS,EACrB,MAAM,EAAE,MAAM;IAMlB,OAAO,CAAC,WAAW,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IA4C7C,OAAO,CAAC,sBAAsB;CAG/B"}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
2
17
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
18
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
19
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -36,12 +51,22 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
51
|
}
|
|
37
52
|
};
|
|
38
53
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ServiceExecutor = void 0;
|
|
54
|
+
exports.ServiceExecutor = exports.ServiceError = void 0;
|
|
40
55
|
var logger_1 = require("@proteinjs/logger");
|
|
41
56
|
var serializer_1 = require("@proteinjs/serializer");
|
|
42
57
|
var ServiceAuth_1 = require("./ServiceAuth");
|
|
43
58
|
var isVoidReturnType_1 = require("./isVoidReturnType");
|
|
44
59
|
var server_api_1 = require("@proteinjs/server-api");
|
|
60
|
+
var ServiceError = /** @class */ (function (_super) {
|
|
61
|
+
__extends(ServiceError, _super);
|
|
62
|
+
function ServiceError(message) {
|
|
63
|
+
var _this = _super.call(this, message) || this;
|
|
64
|
+
_this.name = 'ServiceError';
|
|
65
|
+
return _this;
|
|
66
|
+
}
|
|
67
|
+
return ServiceError;
|
|
68
|
+
}(Error));
|
|
69
|
+
exports.ServiceError = ServiceError;
|
|
45
70
|
var ServiceExecutor = /** @class */ (function () {
|
|
46
71
|
function ServiceExecutor(service, _interface, method) {
|
|
47
72
|
this.service = service;
|
|
@@ -84,7 +109,7 @@ var ServiceExecutor = /** @class */ (function () {
|
|
|
84
109
|
error: error_1,
|
|
85
110
|
obj: { functionName: this.serviceMethodName, args: deserializedArgs },
|
|
86
111
|
});
|
|
87
|
-
throw
|
|
112
|
+
throw new ServiceError('Service failed');
|
|
88
113
|
case 6:
|
|
89
114
|
if ((0, isVoidReturnType_1.isVoidReturnType)(this.method)) {
|
|
90
115
|
if (this.shouldLogArgsAndReturn()) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceExecutor.js","sourceRoot":"","sources":["../../src/ServiceExecutor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ServiceExecutor.js","sourceRoot":"","sources":["../../src/ServiceExecutor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,4CAA2C;AAC3C,oDAAmD;AACnD,6CAA4C;AAC5C,uDAAsD;AACtD,oDAAgD;AAEhD;IAAkC,gCAAK;IACrC,sBAAY,OAAe;QAA3B,YACE,kBAAM,OAAO,CAAC,SAEf;QADC,KAAI,CAAC,IAAI,GAAG,cAAc,CAAC;;IAC7B,CAAC;IACH,mBAAC;AAAD,CAAC,AALD,CAAkC,KAAK,GAKtC;AALY,oCAAY;AAOzB;IAIE,yBACS,OAAgB,EACf,UAAqB,EACrB,MAAc;QAFf,YAAO,GAAP,OAAO,CAAS;QACf,eAAU,GAAV,UAAU,CAAW;QACrB,WAAM,GAAN,MAAM,CAAQ;QAEtB,IAAI,CAAC,iBAAiB,GAAG,UAAG,UAAU,CAAC,IAAI,cAAI,MAAM,CAAC,IAAI,CAAE,CAAC;QAC7D,IAAI,CAAC,MAAM,GAAG,IAAI,eAAM,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAC7D,CAAC;IAEK,iCAAO,GAAb,UAAc,WAAgB;;;;;;;wBACtB,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI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;oCACf,OAAO,EAAE,kBAAkB;oCAC3B,GAAG,EAAE,EAAE,YAAY,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE;iCAC9D,CAAC,CAAC;6BACJ;4BACD,sBAAO,SAAS,EAAC;yBAClB;wBAEK,gBAAgB,GAAG,uBAAU,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;wBACvD,IAAI,IAAI,CAAC,sBAAsB,EAAE,EAAE;4BACjC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,EAAE,YAAY,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC;yBAC5G;wBACD,sBAAO,gBAAgB,EAAC;;;;KACzB;IAEO,gDAAsB,GAA9B;QACE,OAAO,CAAC,oBAAO,CAAC,KAAK,EAAE,IAAI,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC;IAC/D,CAAC;IACH,sBAAC;AAAD,CAAC,AA5DD,IA4DC;AA5DY,0CAAe"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceRouter.d.ts","sourceRoot":"","sources":["../../src/ServiceRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"ServiceRouter.d.ts","sourceRoot":"","sources":["../../src/ServiceRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAa9C,qBAAa,aAAc,YAAW,KAAK;IACzC,OAAO,CAAC,MAAM,CAA+C;IAC7D,OAAO,CAAC,kBAAkB,CAAkD;IAC5E,IAAI,SAAe;IACnB,MAAM,EAAE,MAAM,CAAU;IAExB,OAAO,CAAC,qBAAqB;IAqBvB,SAAS,CAAC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;CAqB3D"}
|
|
@@ -41,6 +41,9 @@ var reflection_1 = require("@proteinjs/reflection");
|
|
|
41
41
|
var ServiceExecutor_1 = require("./ServiceExecutor");
|
|
42
42
|
var util_1 = require("@proteinjs/util");
|
|
43
43
|
var logger_1 = require("@proteinjs/logger");
|
|
44
|
+
function isServiceError(error) {
|
|
45
|
+
return (typeof error === 'object' && error !== null && 'name' in error && error.name === 'ServiceError');
|
|
46
|
+
}
|
|
44
47
|
var ServiceRouter = /** @class */ (function () {
|
|
45
48
|
function ServiceRouter() {
|
|
46
49
|
this.logger = new logger_1.Logger({ name: this.constructor.name });
|
|
@@ -69,7 +72,7 @@ var ServiceRouter = /** @class */ (function () {
|
|
|
69
72
|
};
|
|
70
73
|
ServiceRouter.prototype.onRequest = function (request, response) {
|
|
71
74
|
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
-
var serviceExecutor, error, serializedReturn, error_1;
|
|
75
|
+
var serviceExecutor, error, serializedReturn, error_1, errorMessage;
|
|
73
76
|
return __generator(this, function (_a) {
|
|
74
77
|
switch (_a.label) {
|
|
75
78
|
case 0:
|
|
@@ -90,8 +93,12 @@ var ServiceRouter = /** @class */ (function () {
|
|
|
90
93
|
return [3 /*break*/, 4];
|
|
91
94
|
case 3:
|
|
92
95
|
error_1 = _a.sent();
|
|
93
|
-
|
|
94
|
-
|
|
96
|
+
errorMessage = error_1.message;
|
|
97
|
+
if (!isServiceError(error_1)) {
|
|
98
|
+
this.logger.error({ error: error_1 });
|
|
99
|
+
errorMessage = 'Internal server error';
|
|
100
|
+
}
|
|
101
|
+
response.send({ error: errorMessage });
|
|
95
102
|
return [3 /*break*/, 4];
|
|
96
103
|
case 4: return [2 /*return*/];
|
|
97
104
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceRouter.js","sourceRoot":"","sources":["../../src/ServiceRouter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;KAE1C;IACH,oBAAC;AAAD,CAAC,AAhDD,IAgDC;AAhDY,sCAAa"}
|
package/generated/index.ts
CHANGED
|
@@ -7,25 +7,22 @@ import '@proteinjs/server-api';
|
|
|
7
7
|
import '@proteinjs/user-auth';
|
|
8
8
|
import '@proteinjs/util';
|
|
9
9
|
|
|
10
|
-
|
|
11
10
|
/** Generate Source Graph */
|
|
12
11
|
|
|
13
|
-
const sourceGraph =
|
|
14
|
-
|
|
12
|
+
const sourceGraph =
|
|
13
|
+
'{"options":{"directed":true,"multigraph":false,"compound":false},"nodes":[{"v":"@proteinjs/service/Service","value":{"packageName":"@proteinjs/service","name":"Service","filePath":"/home/runner/work/service/service/packages/service/src/Service.ts","qualifiedName":"@proteinjs/service/Service","properties":[{"name":"serviceMetadata","type":{"packageName":"@proteinjs/service","name":"{\\n auth?: {\\n /** If true, the user does not need to be logged in or have any roles to call this service. If blank, defaults to false. */\\n public?: boolean;\\n /** If true, the user does not need to have any roles to call this service, but must be logged in. If blank, defaults to false. */\\n allUsers?: boolean;\\n /** The user must be logged in and have these roles to call this service. If blank, defaults to requiring the \'admin\' role. */\\n roles?: string[];\\n /**\\n * Custom auth function. If provided, all other auth properties are ignored.\\n * @param methodName the name of the service method to be executed\\n * @param args the args[] that will be passed into the method\\n * @return true if the user can access the service method\\n */\\n canAccess?: (methodName: string, args: any[]) => boolean;\\n };\\n /** Don\'t await the service\'s execution, return a response to the client immediately */\\n doNotAwait?: boolean;\\n }","filePath":null,"qualifiedName":"@proteinjs/service/{\\n auth?: {\\n /** If true, the user does not need to be logged in or have any roles to call this service. If blank, defaults to false. */\\n public?: boolean;\\n /** If true, the user does not need to have any roles to call this service, but must be logged in. If blank, defaults to false. */\\n allUsers?: boolean;\\n /** The user must be logged in and have these roles to call this service. If blank, defaults to requiring the \'admin\' role. */\\n roles?: string[];\\n /**\\n * Custom auth function. If provided, all other auth properties are ignored.\\n * @param methodName the name of the service method to be executed\\n * @param args the args[] that will be passed into the method\\n * @return true if the user can access the service method\\n */\\n canAccess?: (methodName: string, args: any[]) => boolean;\\n };\\n /** Don\'t await the service\'s execution, return a response to the client immediately */\\n doNotAwait?: boolean;\\n }","typeParameters":null,"directParents":null},"isOptional":true,"isAbstract":false,"isStatic":false,"visibility":"public"}],"methods":[],"typeParameters":[],"directParents":[{"packageName":"@proteinjs/reflection","name":"Loadable","filePath":null,"qualifiedName":"@proteinjs/reflection/Loadable","properties":[],"methods":[],"typeParameters":[],"directParents":[]}],"sourceType":3}},{"v":"@proteinjs/reflection/Loadable"},{"v":"/Error"},{"v":"@proteinjs/service/ServiceRouter","value":{"packageName":"@proteinjs/service","name":"ServiceRouter","filePath":"/home/runner/work/service/service/packages/service/src/ServiceRouter.ts","qualifiedName":"@proteinjs/service/ServiceRouter","isAbstract":false,"isStatic":false,"visibility":"public","properties":[{"name":"logger","type":null,"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"private"},{"name":"serviceExecutorMap","type":{"packageName":"@proteinjs/service","name":"{ [path: string]: ServiceExecutor } | undefined","filePath":null,"qualifiedName":"@proteinjs/service/{ [path: string]: ServiceExecutor } | undefined","typeParameters":null,"directParents":null},"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"private"},{"name":"path","type":null,"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"public"},{"name":"method","type":{"packageName":"","name":"\'post\'","filePath":null,"qualifiedName":"/\'post\'","typeParameters":null,"directParents":null},"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"public"}],"methods":[{"name":"getServiceExecutorMap","returnType":null,"isAsync":false,"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"private","parameters":[]},{"name":"onRequest","returnType":{"packageName":"","name":"Promise<any>","filePath":null,"qualifiedName":"/Promise<any>","typeParameters":null,"directParents":null},"isAsync":true,"isOptional":false,"isAbstract":false,"isStatic":false,"visibility":"public","parameters":[{"name":"request","type":{"packageName":"","name":"any","filePath":null,"qualifiedName":"/any","typeParameters":null,"directParents":null}},{"name":"response","type":{"packageName":"","name":"any","filePath":null,"qualifiedName":"/any","typeParameters":null,"directParents":null}}]}],"typeParameters":[],"directParentInterfaces":[{"packageName":"@proteinjs/server-api","name":"Route","filePath":null,"qualifiedName":"@proteinjs/server-api/Route","properties":[],"methods":[],"typeParameters":[],"directParents":[]}],"directParentClasses":[],"sourceType":2}},{"v":"@proteinjs/server-api/Route"}],"edges":[{"v":"@proteinjs/service/Service","w":"@proteinjs/reflection/Loadable","value":"extends interface"},{"v":"@proteinjs/service/ServiceRouter","w":"@proteinjs/server-api/Route","value":"implements interface"}]}';
|
|
15
14
|
|
|
16
15
|
/** Generate Source Links */
|
|
17
16
|
|
|
18
17
|
import { ServiceRouter } from '../src/ServiceRouter';
|
|
19
18
|
|
|
20
19
|
const sourceLinks = {
|
|
21
|
-
|
|
20
|
+
'@proteinjs/service/ServiceRouter': ServiceRouter,
|
|
22
21
|
};
|
|
23
22
|
|
|
24
|
-
|
|
25
23
|
/** Load Source Graph and Links */
|
|
26
24
|
|
|
27
25
|
import { SourceRepository } from '@proteinjs/reflection';
|
|
28
26
|
SourceRepository.merge(sourceGraph, sourceLinks);
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
export * from '../index';
|
|
28
|
+
export * from '../index';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proteinjs/service",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Service api",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -42,5 +42,6 @@
|
|
|
42
42
|
"typescript": "5.2.2"
|
|
43
43
|
},
|
|
44
44
|
"main": "./dist/generated/index.js",
|
|
45
|
-
"types": "./dist/generated/index.d.ts"
|
|
45
|
+
"types": "./dist/generated/index.d.ts",
|
|
46
|
+
"gitHead": "22d0bff91a32a3e34b35d6985a0ce5863270757d"
|
|
46
47
|
}
|
package/src/Service.ts
CHANGED
|
@@ -3,6 +3,21 @@ import { Loadable, SourceRepository } from '@proteinjs/reflection';
|
|
|
3
3
|
import { ServiceClient } from './ServiceClient';
|
|
4
4
|
import { Debouncer } from '@proteinjs/util';
|
|
5
5
|
|
|
6
|
+
type RemoveIndex<T> = {
|
|
7
|
+
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
|
|
8
|
+
};
|
|
9
|
+
type KeysWithoutIndexSignature<T> = keyof RemoveIndex<T>;
|
|
10
|
+
|
|
11
|
+
// Get keys of T that are not in U
|
|
12
|
+
type Diff<T, U> = T extends U ? never : T;
|
|
13
|
+
|
|
14
|
+
// Get only the keys specific to T, excluding those from Service
|
|
15
|
+
type KeysWithoutService<T extends Service> = Diff<KeysWithoutIndexSignature<T>, KeysWithoutIndexSignature<Service>>;
|
|
16
|
+
|
|
17
|
+
type RetryConfig<T extends Service> = {
|
|
18
|
+
[K in KeysWithoutService<T>]?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
6
21
|
export interface Service extends Loadable {
|
|
7
22
|
serviceMetadata?: {
|
|
8
23
|
auth?: {
|
|
@@ -35,7 +50,8 @@ export interface Service extends Loadable {
|
|
|
35
50
|
*/
|
|
36
51
|
export const serviceFactory = <T extends Service>(
|
|
37
52
|
serviceInterfaceQualifiedName: string,
|
|
38
|
-
debouncer?: Debouncer
|
|
53
|
+
debouncer?: Debouncer,
|
|
54
|
+
retryConfig?: RetryConfig<T>
|
|
39
55
|
): (() => T) => {
|
|
40
56
|
return () => {
|
|
41
57
|
const service: any = {};
|
|
@@ -43,7 +59,28 @@ export const serviceFactory = <T extends Service>(
|
|
|
43
59
|
for (const method of serviceInterface.methods) {
|
|
44
60
|
const servicePath = `/service/${serviceInterface.qualifiedName}/${method.name}`;
|
|
45
61
|
const serviceClient = new ServiceClient(servicePath, method, debouncer);
|
|
46
|
-
|
|
62
|
+
if (retryConfig && method.name in retryConfig) {
|
|
63
|
+
const methodName = method.name as KeysWithoutService<T>;
|
|
64
|
+
const maxRetries = retryConfig[methodName]!;
|
|
65
|
+
service[method.name] = async (...args: any[]) => {
|
|
66
|
+
let lastError;
|
|
67
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
68
|
+
try {
|
|
69
|
+
return serviceClient.send.bind(serviceClient)(...args);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
lastError = error;
|
|
72
|
+
if (attempt < maxRetries - 1) {
|
|
73
|
+
await new Promise((resolve) => setTimeout(resolve, 1000)); // short wait before retrying
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (lastError) {
|
|
78
|
+
throw lastError;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
} else {
|
|
82
|
+
service[method.name] = serviceClient.send.bind(serviceClient);
|
|
83
|
+
}
|
|
47
84
|
}
|
|
48
85
|
|
|
49
86
|
return service;
|
package/src/ServiceExecutor.ts
CHANGED
|
@@ -6,6 +6,13 @@ import { ServiceAuth } from './ServiceAuth';
|
|
|
6
6
|
import { isVoidReturnType } from './isVoidReturnType';
|
|
7
7
|
import { EnvInfo } from '@proteinjs/server-api';
|
|
8
8
|
|
|
9
|
+
export class ServiceError extends Error {
|
|
10
|
+
constructor(message: string) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'ServiceError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
9
16
|
export class ServiceExecutor {
|
|
10
17
|
private logger: Logger;
|
|
11
18
|
public deserializedArgs: any;
|
|
@@ -43,7 +50,7 @@ export class ServiceExecutor {
|
|
|
43
50
|
error,
|
|
44
51
|
obj: { functionName: this.serviceMethodName, args: deserializedArgs },
|
|
45
52
|
});
|
|
46
|
-
throw
|
|
53
|
+
throw new ServiceError('Service failed');
|
|
47
54
|
}
|
|
48
55
|
|
|
49
56
|
if (isVoidReturnType(this.method)) {
|
package/src/ServiceRouter.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { Route } from '@proteinjs/server-api';
|
|
2
2
|
import { Service } from './Service';
|
|
3
3
|
import { Interface, SourceRepository } from '@proteinjs/reflection';
|
|
4
|
-
import { ServiceExecutor } from './ServiceExecutor';
|
|
4
|
+
import { ServiceError, ServiceExecutor } from './ServiceExecutor';
|
|
5
5
|
import { isInstanceOf } from '@proteinjs/util';
|
|
6
6
|
import { Logger } from '@proteinjs/logger';
|
|
7
7
|
|
|
8
|
+
function isServiceError(error: unknown): error is ServiceError {
|
|
9
|
+
return (
|
|
10
|
+
typeof error === 'object' && error !== null && 'name' in error && (error as ServiceError).name === 'ServiceError'
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
export class ServiceRouter implements Route {
|
|
9
15
|
private logger = new Logger({ name: this.constructor.name });
|
|
10
16
|
private serviceExecutorMap: { [path: string]: ServiceExecutor } | undefined;
|
|
@@ -45,8 +51,12 @@ export class ServiceRouter implements Route {
|
|
|
45
51
|
const serializedReturn = await serviceExecutor.execute(request.body);
|
|
46
52
|
response.send({ serializedReturn });
|
|
47
53
|
} catch (error: any) {
|
|
48
|
-
|
|
49
|
-
|
|
54
|
+
let errorMessage = error.message;
|
|
55
|
+
if (!isServiceError(error)) {
|
|
56
|
+
this.logger.error({ error });
|
|
57
|
+
errorMessage = 'Internal server error';
|
|
58
|
+
}
|
|
59
|
+
response.send({ error: errorMessage });
|
|
50
60
|
}
|
|
51
61
|
}
|
|
52
62
|
}
|