unnbound-events 1.0.19 → 1.0.21

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Unnbound Team
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/README.md CHANGED
@@ -132,6 +132,56 @@ API:
132
132
  - `use(matcher, middleware)` where matcher is `string | RegExp | (path) => boolean`
133
133
  - `middleware(req, next) -> Promise<EventResponse>`
134
134
 
135
+ ## Enqueue Events
136
+
137
+ The `enqueue` function allows you to send events to the queue by using the async endpoint.
138
+
139
+ ### Basic Usage
140
+
141
+ ```ts
142
+ import { createEventsClient } from 'unnbound-events-sdk';
143
+
144
+ const client = createEventsClient();
145
+
146
+ client.post('/process-job', async (req) => {
147
+ console.log('Handle job:', req.body);
148
+
149
+ // Split job into multiple tasks
150
+ const tasks = [...];
151
+
152
+ tasks.map(async (task) => {
153
+ await client.enqueue({
154
+ method: 'POST',
155
+ path: '/process-task',
156
+ body: { jobId: '123', data: 'important data' }
157
+ });
158
+ });
159
+
160
+ return { status: 200, body: { processed: true } };
161
+ });
162
+
163
+ client.post('/process-task', async (req) => {
164
+ console.log('Handle task:', req.body);
165
+
166
+ return { status: 200, body: { processed: true } };
167
+ });
168
+
169
+ await client.start();
170
+ ```
171
+
172
+ ### Scheduled Jobs
173
+
174
+ ```ts
175
+ // Trigger scheduled processing
176
+ setInterval(async () => {
177
+ await client.enqueue({
178
+ method: 'POST',
179
+ path: '/cleanup',
180
+ body: { timestamp: Date.now() },
181
+ });
182
+ }, 60000); // Every minute
183
+ ```
184
+
135
185
  ## Message envelope
136
186
 
137
187
  The SQS record `body` should be a JSON string matching this shape:
@@ -288,14 +338,3 @@ For S3 payload support, configure:
288
338
  export UNNBOUND_S3_PAYLOAD_BUCKET="my-payload-bucket"
289
339
  export AWS_REGION="us-west-2"
290
340
  ```
291
-
292
- ### AWS Credentials
293
-
294
- When running in ECS with a Task Role, no additional credentials are needed. The AWS SDK will automatically use the ECS Task Role credentials.
295
-
296
- For local development or other environments, set:
297
-
298
- ```bash
299
- export AWS_ACCESS_KEY_ID="your-access-key"
300
- export AWS_SECRET_ACCESS_KEY="your-secret-key"
301
- ```
@@ -1,4 +1,12 @@
1
1
  export { createEventsClient } from './lib/client';
2
- export type { EventsClient, EventRequest, EventResponse, RouteHandler, RouteMatcher, RegisteredRoute, SqsBatchEvent, StartOptions, } from './lib/types';
2
+ export type {
3
+ EventsClient,
4
+ EventRequest,
5
+ EventResponse,
6
+ RouteHandler,
7
+ RouteMatcher,
8
+ RegisteredRoute,
9
+ SqsBatchEvent,
10
+ } from './lib/types';
3
11
  export { createExpressMiddleware } from './lib/adapters/express';
4
12
  export { createSqsConsumer } from './lib/adapters/sqs';
@@ -1,9 +1,24 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.createSqsConsumer = exports.createExpressMiddleware = exports.createEventsClient = void 0;
4
- var client_1 = require("./lib/client");
5
- Object.defineProperty(exports, "createEventsClient", { enumerable: true, get: function () { return client_1.createEventsClient; } });
6
- var express_1 = require("./lib/adapters/express");
7
- Object.defineProperty(exports, "createExpressMiddleware", { enumerable: true, get: function () { return express_1.createExpressMiddleware; } });
8
- var sqs_1 = require("./lib/adapters/sqs");
9
- Object.defineProperty(exports, "createSqsConsumer", { enumerable: true, get: function () { return sqs_1.createSqsConsumer; } });
4
+ var client_1 = require('./lib/client');
5
+ Object.defineProperty(exports, 'createEventsClient', {
6
+ enumerable: true,
7
+ get: function () {
8
+ return client_1.createEventsClient;
9
+ },
10
+ });
11
+ var express_1 = require('./lib/adapters/express');
12
+ Object.defineProperty(exports, 'createExpressMiddleware', {
13
+ enumerable: true,
14
+ get: function () {
15
+ return express_1.createExpressMiddleware;
16
+ },
17
+ });
18
+ var sqs_1 = require('./lib/adapters/sqs');
19
+ Object.defineProperty(exports, 'createSqsConsumer', {
20
+ enumerable: true,
21
+ get: function () {
22
+ return sqs_1.createSqsConsumer;
23
+ },
24
+ });
@@ -1,16 +1,18 @@
1
1
  type Request = {
2
- method: string;
3
- path: string;
4
- headers: Record<string, string | string[] | undefined>;
5
- query: Record<string, unknown>;
6
- body: unknown;
2
+ method: string;
3
+ path: string;
4
+ headers: Record<string, string | string[] | undefined>;
5
+ query: Record<string, unknown>;
6
+ body: unknown;
7
7
  };
8
8
  type Response = {
9
- status: (code: number) => Response;
10
- setHeader: (name: string, value: string) => void;
11
- send: (body: unknown) => void;
12
- end: () => void;
9
+ status: (code: number) => Response;
10
+ setHeader: (name: string, value: string) => void;
11
+ send: (body: unknown) => void;
12
+ end: () => void;
13
13
  };
14
14
  import type { EventsClient } from '../types';
15
- export declare function createExpressMiddleware(client: EventsClient): (req: Request, res: Response) => Promise<void>;
15
+ export declare function createExpressMiddleware(
16
+ client: EventsClient
17
+ ): (req: Request, res: Response) => Promise<void>;
16
18
  export {};
@@ -1,38 +1,34 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.createExpressMiddleware = createExpressMiddleware;
4
4
  function toEventRequest(req) {
5
- const method = req.method.toUpperCase();
6
- const headers = {};
7
- for (const [key, value] of Object.entries(req.headers)) {
8
- if (typeof value === 'string')
9
- headers[key] = value;
10
- else if (Array.isArray(value))
11
- headers[key] = value.join(',');
12
- }
13
- return {
14
- method,
15
- path: req.path,
16
- headers,
17
- query: req.query,
18
- body: req.body,
19
- metadata: { source: 'http' },
20
- };
5
+ const method = req.method.toUpperCase();
6
+ const headers = {};
7
+ for (const [key, value] of Object.entries(req.headers)) {
8
+ if (typeof value === 'string') headers[key] = value;
9
+ else if (Array.isArray(value)) headers[key] = value.join(',');
10
+ }
11
+ return {
12
+ method,
13
+ path: req.path,
14
+ headers,
15
+ query: req.query,
16
+ body: req.body,
17
+ metadata: { source: 'http' },
18
+ };
21
19
  }
22
20
  function applyResponse(res, response) {
23
- res.status(response.status);
24
- if (response.headers) {
25
- for (const [k, v] of Object.entries(response.headers))
26
- res.setHeader(k, v);
27
- }
28
- if (response.body !== undefined)
29
- return res.send(response.body);
30
- return res.end();
21
+ res.status(response.status);
22
+ if (response.headers) {
23
+ for (const [k, v] of Object.entries(response.headers)) res.setHeader(k, v);
24
+ }
25
+ if (response.body !== undefined) return res.send(response.body);
26
+ return res.end();
31
27
  }
32
28
  function createExpressMiddleware(client) {
33
- return async function eventsMiddleware(req, res) {
34
- const eventReq = toEventRequest(req);
35
- const eventRes = await client.handle(eventReq);
36
- applyResponse(res, eventRes);
37
- };
29
+ return async function eventsMiddleware(req, res) {
30
+ const eventReq = toEventRequest(req);
31
+ const eventRes = await client.handle(eventReq);
32
+ applyResponse(res, eventRes);
33
+ };
38
34
  }
@@ -1,36 +1,36 @@
1
1
  import type { EventsClient, HttpMethod } from '../types';
2
2
  export interface QueueEnvelope {
3
- id: string;
4
- timestamp: number;
5
- version: string;
6
- workflow: string;
7
- priority: number;
8
- payload: {
9
- type: string;
10
- size: number;
11
- compressed: boolean;
12
- };
13
- request: {
14
- method: HttpMethod;
15
- path: string;
16
- headers: Record<string, string>;
17
- query: Record<string, unknown>;
18
- body: unknown;
19
- };
20
- metadata?: Record<string, unknown>;
21
- addons?: Record<string, unknown>;
3
+ id: string;
4
+ timestamp: number;
5
+ version: string;
6
+ workflow: string;
7
+ priority: number;
8
+ payload: {
9
+ type: string;
10
+ size: number;
11
+ compressed: boolean;
12
+ };
13
+ request: {
14
+ method: HttpMethod;
15
+ path: string;
16
+ headers: Record<string, string>;
17
+ query: Record<string, unknown>;
18
+ body: unknown;
19
+ };
20
+ metadata?: Record<string, unknown>;
21
+ addons?: Record<string, unknown>;
22
22
  }
23
23
  export type SqsRecord = {
24
- messageId: string;
25
- body: string;
24
+ messageId: string;
25
+ body: string;
26
26
  };
27
27
  export interface SqsBatchEvent {
28
- Records: SqsRecord[];
28
+ Records: SqsRecord[];
29
29
  }
30
30
  export declare function createSqsConsumer(client: EventsClient): (event: SqsBatchEvent) => Promise<{
31
- successes: number;
32
- failures: Array<{
33
- messageId: string;
34
- reason: string;
35
- }>;
31
+ successes: number;
32
+ failures: Array<{
33
+ messageId: string;
34
+ reason: string;
35
+ }>;
36
36
  }>;
@@ -1,42 +1,42 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.createSqsConsumer = createSqsConsumer;
4
4
  function createSqsConsumer(client) {
5
- return async function consume(event) {
6
- let successes = 0;
7
- const failures = [];
8
- await Promise.all(event.Records.map(async (record) => {
9
- try {
10
- const envelope = JSON.parse(record.body);
11
- const req = {
12
- method: envelope.request.method,
13
- path: envelope.request.path,
14
- headers: envelope.request.headers,
15
- query: envelope.request.query,
16
- body: envelope.request.body,
17
- metadata: {
18
- source: 'sqs',
19
- id: envelope.id,
20
- workflow: envelope.workflow,
21
- timestamp: envelope.timestamp,
22
- version: envelope.version,
23
- ...envelope.metadata,
24
- },
25
- };
26
- const res = await client.handle(req);
27
- if (res.status >= 200 && res.status < 300)
28
- successes += 1;
29
- else
30
- failures.push({
31
- messageId: record.messageId,
32
- reason: `Handler returned status ${res.status}`,
33
- });
34
- }
35
- catch (error) {
36
- const err = error instanceof Error ? error : new Error(String(error));
37
- failures.push({ messageId: record.messageId, reason: err.message ?? 'Unknown error' });
38
- }
39
- }));
40
- return { successes, failures };
41
- };
5
+ return async function consume(event) {
6
+ let successes = 0;
7
+ const failures = [];
8
+ await Promise.all(
9
+ event.Records.map(async (record) => {
10
+ try {
11
+ const envelope = JSON.parse(record.body);
12
+ const req = {
13
+ method: envelope.request.method,
14
+ path: envelope.request.path,
15
+ headers: envelope.request.headers,
16
+ query: envelope.request.query,
17
+ body: envelope.request.body,
18
+ metadata: {
19
+ source: 'sqs',
20
+ id: envelope.id,
21
+ workflow: envelope.workflow,
22
+ timestamp: envelope.timestamp,
23
+ version: envelope.version,
24
+ ...envelope.metadata,
25
+ },
26
+ };
27
+ const res = await client.handle(req);
28
+ if (res.status >= 200 && res.status < 300) successes += 1;
29
+ else
30
+ failures.push({
31
+ messageId: record.messageId,
32
+ reason: `Handler returned status ${res.status}`,
33
+ });
34
+ } catch (error) {
35
+ const err = error instanceof Error ? error : new Error(String(error));
36
+ failures.push({ messageId: record.messageId, reason: err.message ?? 'Unknown error' });
37
+ }
38
+ })
39
+ );
40
+ return { successes, failures };
41
+ };
42
42
  }