@anchan828/nest-cloud-run-queue-worker 3.0.0 → 3.1.1

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.
@@ -69,9 +69,17 @@ export interface QueueWorkerProcessDecoratorArgs {
69
69
  }
70
70
  export type QueueWorkerRawMessage<T = any> = {
71
71
  readonly data?: string | null | Message<T>;
72
+ readonly messageId?: string;
72
73
  readonly headers?: Record<string, string>;
73
74
  } & Record<string, any>;
74
75
  export type QueueWorkerDecodedMessage<T = any> = {
76
+ /**
77
+ * The message ID is a unique identifier.
78
+ * If controller recieve from Pub/Sub, it will be body.message.messageId.
79
+ * If controller recieve from Cloud Tasks, it will be body.message.headers["X-CloudTasks-TaskName"].
80
+ * If nothing is obtained, it will be an empty string.
81
+ */
82
+ readonly id: string;
75
83
  readonly data: Message<T>;
76
84
  readonly headers?: Record<string, string>;
77
85
  readonly raw: QueueWorkerRawMessage;
@@ -87,6 +95,10 @@ export interface QueueWorkerControllerMetadata extends RequestMappingMetadata {
87
95
  * @memberof QueueWorkerControllerMetadata
88
96
  */
89
97
  statusCode?: number;
98
+ /**
99
+ * Whether to throw the error as is when Processor throws an error. Default is true.
100
+ */
101
+ throwError?: boolean;
90
102
  }
91
103
  export interface QueueWorkerControllerInterface {
92
104
  execute(body: QueueWorkerReceivedMessage, headers: Record<string, string>): Promise<void>;
package/dist/util.js CHANGED
@@ -73,9 +73,13 @@ function decodeMessage(message) {
73
73
  return {
74
74
  data,
75
75
  headers: "headers" in message ? message.headers : undefined,
76
+ id: getMessageId(message),
76
77
  raw: message,
77
78
  };
78
79
  }
80
+ function getMessageId(raw) {
81
+ return raw.messageId ?? raw.headers?.["x-cloudtasks-taskname"] ?? "";
82
+ }
79
83
  function decodeData(data) {
80
84
  if (!data) {
81
85
  throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
@@ -18,12 +18,18 @@ const worker_service_1 = require("./worker.service");
18
18
  function getWorkerController(metadata) {
19
19
  const path = metadata?.path;
20
20
  const method = metadata?.method || common_1.RequestMethod.POST;
21
+ const throwError = metadata?.throwError ?? true;
21
22
  let WorkerController = class WorkerController {
22
23
  constructor(service) {
23
24
  this.service = service;
24
25
  }
25
26
  async execute(body, headers) {
26
- await this.service.execute({ ...body.message, headers });
27
+ const results = await this.service.execute({ ...body.message, headers });
28
+ for (const result of results) {
29
+ if (!result.success && throwError) {
30
+ throw result.error;
31
+ }
32
+ }
27
33
  }
28
34
  };
29
35
  __decorate([
@@ -10,11 +10,15 @@ export declare class QueueWorkerService {
10
10
  /**
11
11
  * Execute all workers that match the worker name. If you want to execute a each worker, use `getWorkers` method.
12
12
  */
13
- execute<T = any>(meessage: QueueWorkerRawMessage<T> | QueueWorkerDecodedMessage<T> | Message<T>): Promise<QueueWorkerProcessResult<T>[]>;
13
+ execute<T = any>(meessage: QueueWorkerRawMessage<T>): Promise<QueueWorkerProcessResult<T>[]>;
14
+ execute<T = any>(meessage: Message<T>): Promise<QueueWorkerProcessResult<T>[]>;
15
+ execute<T = any>(meessage: QueueWorkerDecodedMessage<T>): Promise<QueueWorkerProcessResult<T>[]>;
14
16
  /**
15
17
  * Get all workers that match the worker name. Use this method to execute manually when you want to execute only on specific conditions using metadata such as class name or processor name.
16
18
  * If you want to execute all workers simply, use `execute` method.
17
19
  */
18
- getWorkers<T = any>(meessage: QueueWorkerRawMessage<T> | QueueWorkerDecodedMessage<T> | Message<T>): Worker<T>[];
20
+ getWorkers<T = any>(meessage: QueueWorkerRawMessage<T>): Worker<T>[];
21
+ getWorkers<T = any>(meessage: Message<T>): Worker<T>[];
22
+ getWorkers<T = any>(meessage: QueueWorkerDecodedMessage<T>): Worker<T>[];
19
23
  private isDecodedMessage;
20
24
  }
@@ -37,9 +37,6 @@ let QueueWorkerService = class QueueWorkerService {
37
37
  this.explorerService = explorerService;
38
38
  _QueueWorkerService__allWorkers.set(this, void 0);
39
39
  }
40
- /**
41
- * Execute all workers that match the worker name. If you want to execute a each worker, use `getWorkers` method.
42
- */
43
40
  async execute(meessage) {
44
41
  const decodedMessage = this.isDecodedMessage(meessage) ? meessage : (0, util_1.decodeMessage)(meessage);
45
42
  if (this.options.throwModuleError && !decodedMessage.data.name) {
@@ -55,10 +52,6 @@ let QueueWorkerService = class QueueWorkerService {
55
52
  }
56
53
  return results;
57
54
  }
58
- /**
59
- * Get all workers that match the worker name. Use this method to execute manually when you want to execute only on specific conditions using metadata such as class name or processor name.
60
- * If you want to execute all workers simply, use `execute` method.
61
- */
62
55
  getWorkers(meessage) {
63
56
  const decodedMessage = this.isDecodedMessage(meessage) ? meessage : (0, util_1.decodeMessage)(meessage);
64
57
  if (!decodedMessage.data.name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchan828/nest-cloud-run-queue-worker",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "description": "> TODO: description",
5
5
  "homepage": "https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/worker#readme",
6
6
  "bugs": {
@@ -33,7 +33,7 @@
33
33
  "watch": "tsc -w"
34
34
  },
35
35
  "dependencies": {
36
- "@anchan828/nest-cloud-run-queue-common": "^3.0.0"
36
+ "@anchan828/nest-cloud-run-queue-common": "^3.1.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@nestjs/common": "10.3.10",
@@ -45,6 +45,6 @@
45
45
  "publishConfig": {
46
46
  "access": "public"
47
47
  },
48
- "packageManager": "npm@10.8.1",
49
- "gitHead": "696d3bba1c948cbf84d1386ae553602ac5a53cfd"
48
+ "packageManager": "npm@10.8.2",
49
+ "gitHead": "aa316926176b40816565704e5372ceb0d3938543"
50
50
  }