procedere-mq-sdk 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -20,7 +20,10 @@ npm install procedere-mq-sdk
20
20
  const { LiteMQClient } = require("procedere-mq-sdk");
21
21
 
22
22
  async function main() {
23
- const client = new LiteMQClient("http://127.0.0.1:65090");
23
+ const client = new LiteMQClient("http://127.0.0.1:65090", {
24
+ username: "api-user",
25
+ password: "senha-forte-api",
26
+ });
24
27
  const producer = client.producer("emails");
25
28
 
26
29
  const job = await producer.publish(
@@ -40,7 +43,10 @@ main().catch(console.error);
40
43
  const { LiteMQClient, QueueEmptyError } = require("procedere-mq-sdk");
41
44
 
42
45
  async function main() {
43
- const client = new LiteMQClient("http://127.0.0.1:65090");
46
+ const client = new LiteMQClient("http://127.0.0.1:65090", {
47
+ username: "api-user",
48
+ password: "senha-forte-api",
49
+ });
44
50
  const consumer = client.consumer("emails");
45
51
 
46
52
  try {
@@ -64,6 +70,9 @@ main().catch(console.error);
64
70
  - `baseUrl`: endereco HTTP do ProcedereMQ
65
71
  - `options.timeout`: timeout em milissegundos
66
72
  - `options.fetchImpl`: implementacao customizada de `fetch`
73
+ - `options.apiKey`: bearer customizado, se voce precisar compatibilidade externa
74
+ - `options.username`: usuario para autenticar com Basic Auth
75
+ - `options.password`: senha para autenticar com Basic Auth
67
76
 
68
77
  ### `client.producer(queue)`
69
78
 
package/index.d.ts CHANGED
@@ -7,6 +7,9 @@ export class NotFoundError extends LiteMQError {}
7
7
  export interface LiteMQClientOptions {
8
8
  timeout?: number;
9
9
  fetchImpl?: typeof fetch;
10
+ apiKey?: string;
11
+ username?: string;
12
+ password?: string;
10
13
  }
11
14
 
12
15
  export interface PublishOptions {
package/index.js CHANGED
@@ -28,10 +28,11 @@ class Producer {
28
28
  }
29
29
 
30
30
  async publish(payload = null, options = {}) {
31
- const { jobId, maxRetry, runAt } = options;
31
+ const { jobId, maxRetry, visibilityTimeout, runAt } = options;
32
32
  return this.client.enqueue(this.queue, payload, {
33
33
  jobId,
34
34
  maxRetry,
35
+ visibilityTimeout,
35
36
  runAt,
36
37
  });
37
38
  }
@@ -61,6 +62,9 @@ class LiteMQClient {
61
62
  this.baseUrl = normalizeBaseUrl(baseUrl);
62
63
  this.timeout = options.timeout ?? 10000;
63
64
  this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
65
+ this.apiKey = isNonEmptyString(options.apiKey) ? options.apiKey.trim() : "";
66
+ this.username = isNonEmptyString(options.username) ? options.username.trim() : "";
67
+ this.password = typeof options.password === "string" ? options.password : "";
64
68
 
65
69
  if (typeof this.fetchImpl !== "function") {
66
70
  throw new LiteMQError("fetch API is not available in this runtime");
@@ -88,6 +92,9 @@ class LiteMQClient {
88
92
  if (options.maxRetry !== undefined) {
89
93
  body.max_retry = options.maxRetry;
90
94
  }
95
+ if (options.visibilityTimeout !== undefined) {
96
+ body.visibility_timeout = options.visibilityTimeout;
97
+ }
91
98
  if (options.runAt !== undefined && options.runAt !== null) {
92
99
  body.run_at = formatDateTime(options.runAt);
93
100
  }
@@ -115,6 +122,13 @@ class LiteMQClient {
115
122
  });
116
123
  }
117
124
 
125
+ async redrive(queue) {
126
+ const normalizedQueue = normalizeRequiredString(queue, "queue");
127
+ const params = new URLSearchParams({ queue: normalizedQueue });
128
+ const data = await this._request("POST", `/api/redrive?${params.toString()}`, 200);
129
+ return data.count || 0;
130
+ }
131
+
118
132
  async _request(method, path, expectedStatus, options = {}) {
119
133
  const controller = new AbortController();
120
134
  const timeoutId = setTimeout(() => controller.abort(), this.timeout);
@@ -123,6 +137,11 @@ class LiteMQClient {
123
137
  signal: controller.signal,
124
138
  headers: {},
125
139
  };
140
+ if (this.apiKey) {
141
+ init.headers.Authorization = `Bearer ${this.apiKey}`;
142
+ } else if (this.username && this.password) {
143
+ init.headers.Authorization = `Basic ${encodeBase64(`${this.username}:${this.password}`)}`;
144
+ }
126
145
 
127
146
  if (options.body !== undefined) {
128
147
  init.headers["Content-Type"] = "application/json";
@@ -162,6 +181,7 @@ class LiteMQClient {
162
181
  }
163
182
  return { data };
164
183
  }
184
+
165
185
  }
166
186
 
167
187
  async function extractError(response) {
@@ -219,6 +239,13 @@ function isPlainObject(value) {
219
239
  return value !== null && typeof value === "object" && !Array.isArray(value);
220
240
  }
221
241
 
242
+ function encodeBase64(value) {
243
+ if (typeof Buffer !== "undefined") {
244
+ return Buffer.from(value, "utf8").toString("base64");
245
+ }
246
+ throw new LiteMQError("base64 encoder is not available in this runtime");
247
+ }
248
+
222
249
  module.exports = {
223
250
  Consumer,
224
251
  LiteMQClient,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "procedere-mq-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Node.js SDK para ProcedereMQ",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",