@anchan828/nest-redlock 0.0.12 → 0.1.2

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 CHANGED
@@ -68,7 +68,9 @@ import { Redlock } from "@anchan828/nest-redlock";
68
68
  @Injectable()
69
69
  export class ExampleService {
70
70
  // The arguments define the class object to which the decorator is being added and the method arguments in order.
71
- @Redlock((target: ExampleService, projectId: number, comment: string) => `projects/${projectId}/comments`)
71
+ @Redlock<ExampleService["addComment"]>(
72
+ (target: ExampleService, projectId: number, comment: string) => `projects/${projectId}/comments`,
73
+ )
72
74
  public async addComment(projectId: number, comment: string): Promise<void> {}
73
75
  }
74
76
  ```
@@ -78,8 +80,9 @@ Of course, you can lock multiple keys.
78
80
  ```ts
79
81
  @Injectable()
80
82
  export class ExampleService {
81
- @Redlock((target: ExampleService, projectId: number, args: Array<{ commentId: number; comment: string }>) =>
82
- args.map((arg) => `projects/${projectId}/comments/${arg.commentId}`),
83
+ @Redlock<ExampleService["updateComments"]>(
84
+ (target: ExampleService, projectId: number, args: Array<{ commentId: number; comment: string }>) =>
85
+ args.map((arg) => `projects/${projectId}/comments/${arg.commentId}`),
83
86
  )
84
87
  public async updateComments(projectId: number, args: Array<{ commentId: number; comment: string }>): Promise<void> {}
85
88
  }
@@ -108,7 +111,7 @@ export class ExampleService {
108
111
  }
109
112
  ```
110
113
 
111
- ## Using fake RedlockService
114
+ ## Using fake RedlockService
112
115
 
113
116
  If you do not want to use Redis in your Unit tests, define the fake class as RedlockService.
114
117
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Redlock } from "./redlock.decorator";
2
2
  export { FakeRedlockService } from "./redlock.fake-service";
3
- export { GenerateResourceFunc, RedlockModuleOptions } from "./redlock.interface";
3
+ export { RedlockKeyFunction, RedlockModuleOptions } from "./redlock.interface";
4
4
  export { RedlockModule } from "./redlock.module";
5
5
  export { RedlockService } from "./redlock.service";
@@ -1,3 +1,3 @@
1
1
  import { Settings } from "redlock";
2
- import { GenerateResourceFunc } from "./redlock.interface";
3
- export declare function Redlock(resource: string | string[] | GenerateResourceFunc, duration?: number, settings?: Partial<Settings>): MethodDecorator;
2
+ import { RedlockKeyFunction } from "./redlock.interface";
3
+ export declare function Redlock<T extends (...args: any) => any = (...args: any) => any>(key: string | string[] | RedlockKeyFunction<T>, duration?: number, settings?: Partial<Settings>): MethodDecorator;
@@ -4,7 +4,7 @@ exports.Redlock = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const redlock_constants_1 = require("./redlock.constants");
6
6
  const redlock_service_1 = require("./redlock.service");
7
- function Redlock(resource, duration, settings = {}) {
7
+ function Redlock(key, duration, settings = {}) {
8
8
  const injectRedlockService = (0, common_1.Inject)(redlock_service_1.RedlockService);
9
9
  return (target, propertyKey, descriptor) => {
10
10
  const serviceSymbol = "@redlockService";
@@ -14,8 +14,8 @@ function Redlock(resource, duration, settings = {}) {
14
14
  // eslint-disable-next-line @typescript-eslint/no-this-alias
15
15
  const descriptorThis = this;
16
16
  const redlockService = descriptorThis[serviceSymbol];
17
- const resources = getResources(resource, descriptorThis, args);
18
- return await redlockService.using(resources, duration || redlockService.options?.duration || redlock_constants_1.DEFAULT_DURATION, settings, async (signal) => {
17
+ const keys = getKeys(key, descriptorThis, args);
18
+ return await redlockService.using(keys, duration || redlockService.options?.duration || redlock_constants_1.DEFAULT_DURATION, settings, async (signal) => {
19
19
  const result = await originalMethod.apply(descriptorThis, args);
20
20
  if (signal.aborted) {
21
21
  throw signal.error;
@@ -27,15 +27,15 @@ function Redlock(resource, duration, settings = {}) {
27
27
  };
28
28
  }
29
29
  exports.Redlock = Redlock;
30
- function getResources(resource, descriptorThis, args) {
31
- if (typeof resource === "string") {
32
- return [resource];
30
+ function getKeys(key, descriptorThis, args) {
31
+ if (typeof key === "string") {
32
+ return [key];
33
33
  }
34
- else if (Array.isArray(resource)) {
35
- return resource;
34
+ else if (Array.isArray(key)) {
35
+ return key;
36
36
  }
37
- else if (typeof resource === "function") {
38
- return [resource(descriptorThis, ...args)].flat();
37
+ else if (typeof key === "function") {
38
+ return [key(descriptorThis, ...args)].flat();
39
39
  }
40
40
  return [];
41
41
  }
@@ -3,9 +3,9 @@ import { EventEmitter } from "events";
3
3
  import { ExecutionResult, Lock, RedlockAbortSignal, Settings } from "redlock";
4
4
  export declare class FakeRedlockService extends EventEmitter {
5
5
  quit(): Promise<void>;
6
- acquire(resources: string[], duration: number, settings?: Partial<Settings> | undefined): Promise<Lock>;
6
+ acquire(keys: string[], duration: number, settings?: Partial<Settings> | undefined): Promise<Lock>;
7
7
  release(lock: Lock, settings?: Partial<Settings> | undefined): Promise<ExecutionResult>;
8
8
  extend(existing: Lock, duration: number, settings?: Partial<Settings> | undefined): Promise<Lock>;
9
- using<T>(resources: string[], duration: number, settings: Partial<Settings>, routine?: ((signal: RedlockAbortSignal) => Promise<T>) | undefined): Promise<T>;
10
- using<T>(resources: string[], duration: number, routine: (signal: RedlockAbortSignal) => Promise<T>): Promise<T>;
9
+ using<T>(keys: string[], duration: number, settings: Partial<Settings>, routine?: ((signal: RedlockAbortSignal) => Promise<T>) | undefined): Promise<T>;
10
+ using<T>(keys: string[], duration: number, routine: (signal: RedlockAbortSignal) => Promise<T>): Promise<T>;
11
11
  }
@@ -6,7 +6,7 @@ const events_1 = require("events");
6
6
  class FakeRedlockService extends events_1.EventEmitter {
7
7
  // eslint-disable-next-line @typescript-eslint/no-empty-function
8
8
  async quit() { }
9
- async acquire(resources, duration, settings) {
9
+ async acquire(keys, duration, settings) {
10
10
  return createLockFake();
11
11
  }
12
12
  async release(lock, settings) {
@@ -15,7 +15,7 @@ class FakeRedlockService extends events_1.EventEmitter {
15
15
  async extend(existing, duration, settings) {
16
16
  return createLockFake();
17
17
  }
18
- async using(resources, duration, settingsOrRoutine, routine) {
18
+ async using(keys, duration, settingsOrRoutine, routine) {
19
19
  const routineFunc = typeof settingsOrRoutine === "function" ? settingsOrRoutine : routine;
20
20
  return await routineFunc?.({ aborted: false });
21
21
  }
@@ -15,4 +15,4 @@ export declare type RedlockModuleOptions = {
15
15
  */
16
16
  duration?: number;
17
17
  };
18
- export declare type GenerateResourceFunc = (target: any, ...args: any[]) => string | string[];
18
+ export declare type RedlockKeyFunction<T extends (...args: any) => any = (...args: any) => any> = (target: any, ...args: Parameters<T>) => string[] | string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchan828/nest-redlock",
3
- "version": "0.0.12",
3
+ "version": "0.1.2",
4
4
  "description": "This is a [Nest](https://github.com/nestjs/nest) implementation of the redlock algorithm for distributed redis locks.",
5
5
  "homepage": "https://github.com/anchan828/nest-redlock#readme",
6
6
  "bugs": {
@@ -33,24 +33,24 @@
33
33
  "@nestjs/platform-express": "^9.0.11",
34
34
  "@nestjs/testing": "^9.0.11",
35
35
  "@types/jest": "^29.0.0",
36
- "@types/node": "^18.7.14",
36
+ "@types/node": "^18.7.16",
37
37
  "@types/supertest": "^2.0.12",
38
- "@typescript-eslint/eslint-plugin": "^5.36.1",
39
- "@typescript-eslint/parser": "^5.36.1",
38
+ "@typescript-eslint/eslint-plugin": "^5.36.2",
39
+ "@typescript-eslint/parser": "^5.36.2",
40
40
  "eslint": "^8.23.0",
41
41
  "eslint-config-prettier": "^8.5.0",
42
42
  "eslint-plugin-prettier": "^4.2.1",
43
43
  "husky": "^8.0.1",
44
44
  "ioredis": "^5.2.3",
45
- "jest": "^29.0.2",
45
+ "jest": "^29.0.3",
46
46
  "lint-staged": "^13.0.3",
47
47
  "prettier": "^2.7.1",
48
48
  "reflect-metadata": "^0.1.13",
49
49
  "rxjs": "^7.5.6",
50
50
  "supertest": "^6.2.4",
51
- "ts-jest": "^29.0.0-next.1",
51
+ "ts-jest": "^29.0.0",
52
52
  "ts-node": "^10.9.1",
53
- "typescript": "^4.8.2"
53
+ "typescript": "^4.8.3"
54
54
  },
55
55
  "volta": {
56
56
  "node": "18.4.0"