@randstad-uca/aws-sns-publisher 1.0.5 → 1.0.8
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/package.json +12 -2
- package/readme.md +147 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@randstad-uca/aws-sns-publisher",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "AWS SNS Publisher",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"clean": "rimraf build",
|
|
12
12
|
"build": "npm run clean && npx tsc",
|
|
13
|
-
"
|
|
13
|
+
"test": "echo \"No tests implemented yet\" && exit 0",
|
|
14
|
+
"prepublishOnly": "npm run test && npm run build",
|
|
15
|
+
"pub": "npm version patch && npm publish && git push && git push --tags"
|
|
14
16
|
},
|
|
15
17
|
"keywords": [
|
|
16
18
|
"aws",
|
|
@@ -18,6 +20,14 @@
|
|
|
18
20
|
"publisher"
|
|
19
21
|
],
|
|
20
22
|
"author": "Facundo Brusa",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/randstad-argentina/aws-sns-publisher.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/randstad-argentina/aws-sns-publisher/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/randstad-argentina/aws-sns-publisher#readme",
|
|
21
31
|
"license": "ISC",
|
|
22
32
|
"publishConfig": {
|
|
23
33
|
"access": "public"
|
package/readme.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @randstad-uca/aws-sns-publisher
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Es una librería ligera desarrollada en Node.js para publicar mensajes a Amazon SNS de forma simple, validada y con logging configurable. Ideal para sistemas desacoplados y orientados a eventos en entornos AWS.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📦 Instalación
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @randstad-uca/aws-sns-publisher
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 🚀 Uso Básico
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { SimpleSNSPublisher } from '@randstad-uca/aws-sns-publisher';
|
|
21
|
+
|
|
22
|
+
const SNSClient = new SimpleSNSPublisher({
|
|
23
|
+
awsConfig: { region: 'us-east-1' }, // required obj
|
|
24
|
+
logEnabled: true, // optional (default value: true)
|
|
25
|
+
logLevel: 'info', // optional (default value: info)
|
|
26
|
+
throwError: true, // optional (default value: true)
|
|
27
|
+
logHandler: {
|
|
28
|
+
// You can implement your own logging handler here (optional)
|
|
29
|
+
info: (args) =>
|
|
30
|
+
// success case
|
|
31
|
+
console.log('Custom success logging handler logic', args),
|
|
32
|
+
error: (args) =>
|
|
33
|
+
// error case
|
|
34
|
+
console.error('Custom error logging handler logic', args),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const payload = {
|
|
39
|
+
eventType: 'sms',
|
|
40
|
+
payload: {
|
|
41
|
+
phone: '+543564123123',
|
|
42
|
+
message: 'Hello there!',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
SNSClient.publish({
|
|
47
|
+
message: JSON.stringify(payload),
|
|
48
|
+
topicARN: 'arn:aws:sns:us-east-1:575108923687:t-notifications-dev',
|
|
49
|
+
})
|
|
50
|
+
.then((result) => console.log('Success:', result))
|
|
51
|
+
.catch((err) => console.error('Error:', err));
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 📘 API
|
|
57
|
+
|
|
58
|
+
#### Clase: `SimpleSNSPublisher`
|
|
59
|
+
|
|
60
|
+
##### Constructor
|
|
61
|
+
```js
|
|
62
|
+
new SimpleSNSPublisher(options: IPublisherOptions)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`IPublisherOptions:`
|
|
66
|
+
|
|
67
|
+
- `awsConfig: SNSClientConfig` – **Requerido**. Como minimo se debe especificar la región de AWS para instanciar el cliente SNS.
|
|
68
|
+
|
|
69
|
+
- `logEnabled?: boolean` – Habilita logs. Default: `true`.
|
|
70
|
+
|
|
71
|
+
- `logLevel?: 'info' | 'silent'` – Nivel de log. Default: `'info'`.
|
|
72
|
+
|
|
73
|
+
- `throwError?: boolean` – Si lanzar errores o no. Default: `true`.
|
|
74
|
+
|
|
75
|
+
- `logHandler?: { info: Function; error: Function }` – Handler personalizado para logs.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## ✨ Metodos
|
|
80
|
+
|
|
81
|
+
#### Método: `publish(options: IPublishOptions): Promise<PublishResponse>`
|
|
82
|
+
|
|
83
|
+
Publica un mensaje en un topic SNS. Antes de enviar el mensaje, se valida su estructura usando la libreria `Joi`.
|
|
84
|
+
|
|
85
|
+
`IPublishOptions`:
|
|
86
|
+
|
|
87
|
+
- `message: string` – Debe ser un string que contenga un objeto, y este **debe** tener las siguientes propiedades:
|
|
88
|
+
|
|
89
|
+
- `eventType: string` – Tipo del evento (ej: `"sms"`, `"add-personal-data"` o cualquiera que se encuentre dentro del objeto ***validEventTypes*** ubicado en el archivo `constants.ts`).
|
|
90
|
+
|
|
91
|
+
- `payload: object` – Objeto con los datos del evento.
|
|
92
|
+
|
|
93
|
+
- `topicARN: string` – ARN del SNS Topic.
|
|
94
|
+
|
|
95
|
+
- `apiName?: string` – Nombre de la API emisora, las unicas validas son las que se encuentran dentro del objeto ***validApiNames*** ubicado en el archivo `constants.ts`. Esta se utiliza como bandera para la creación del MessageDeduplicationId y MessageGroupId necesarios en topics FIFO.
|
|
96
|
+
|
|
97
|
+
- `extraOptions?: object` – Parámetros adicionales para `PublishCommand`.
|
|
98
|
+
|
|
99
|
+
**Validaciones automáticas:**
|
|
100
|
+
|
|
101
|
+
- `eventType` es obligatorio y debe estar en la lista de eventos válidos.
|
|
102
|
+
|
|
103
|
+
- Se valida el `payload` según el tipo de evento con esquemas Joi definidos internamente.
|
|
104
|
+
|
|
105
|
+
- Si se configura `throwError: true`, lanza error. Si no, devuelve `Promise.reject()` con el error.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
#### Método: `validateSchema(eventType: string, payload: object): boolean`
|
|
109
|
+
|
|
110
|
+
Valida el `payload` contra el esquema correspondiente según eventType.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
### 📎 Ejemplo de payload válido
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"eventType": "email",
|
|
119
|
+
"payload": {
|
|
120
|
+
"to": "user@example.com",
|
|
121
|
+
"subject": "Hello!",
|
|
122
|
+
"body": "This is an example email."
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
🧩 Características
|
|
130
|
+
|
|
131
|
+
- ✅ Compatible con múltiples regiones AWS.
|
|
132
|
+
|
|
133
|
+
- ✅ Validación automática con Joi.
|
|
134
|
+
|
|
135
|
+
- ✅ Logging personalizable (info y error).
|
|
136
|
+
|
|
137
|
+
- ✅ Ideal para arquitectura orientada a eventos.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
📌 Consideraciones
|
|
142
|
+
|
|
143
|
+
- Se espera que los topics estén previamente creados en SNS.
|
|
144
|
+
|
|
145
|
+
- Esta librería no crea los topics automáticamente.
|
|
146
|
+
|
|
147
|
+
- En caso de usar topics FIFO, es importante pasar un `apiName` **valido** para deduplicación.
|