@proteinjs/service 1.0.32 → 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 CHANGED
@@ -3,6 +3,17 @@
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
+
6
17
  ## [1.0.32](https://github.com/proteinjs/service/compare/@proteinjs/service@1.0.31...@proteinjs/service@1.0.32) (2024-08-30)
7
18
 
8
19
 
@@ -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,YAatB,CAAC"}
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;AAE5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,cACzB,SAAS,sDAmCtB,CAAC"}
@@ -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
- for (var _i = 0, _a = serviceInterface.methods; _i < _a.length; _i++) {
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
- service[method.name] = serviceClient.send.bind(serviceClient);
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
  };
@@ -1 +1 @@
1
- {"version":3,"file":"Service.js","sourceRoot":"","sources":["../../src/Service.ts"],"names":[],"mappings":";;;AACA,oDAAmE;AACnE,iDAAgD;AA0BhD;;;;;;GAMG;AACI,IAAM,cAAc,GAAG,UAC5B,6BAAqC,EACrC,SAAqB;IAErB,OAAO;QACL,IAAM,OAAO,GAAQ,EAAE,CAAC;QACxB,IAAM,gBAAgB,GAAG,6BAAgB,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,6BAA6B,CAAC,CAAC;QACzF,KAAqB,UAAwB,EAAxB,KAAA,gBAAgB,CAAC,OAAO,EAAxB,cAAwB,EAAxB,IAAwB,EAAE;YAA1C,IAAM,MAAM,SAAA;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,OAAO,CAAC,MAAM,CAAC,IAA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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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proteinjs/service",
3
- "version": "1.0.32",
3
+ "version": "1.1.0",
4
4
  "description": "Service api",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -43,5 +43,5 @@
43
43
  },
44
44
  "main": "./dist/generated/index.js",
45
45
  "types": "./dist/generated/index.d.ts",
46
- "gitHead": "634f90f0f01748585e2f6c8be36c4d832d1fe604"
46
+ "gitHead": "22d0bff91a32a3e34b35d6985a0ce5863270757d"
47
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
- service[method.name] = serviceClient.send.bind(serviceClient);
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;