core-services-sdk 1.3.7 → 1.3.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.
Files changed (36) hide show
  1. package/package.json +3 -2
  2. package/src/fastify/error-codes.js +11 -0
  3. package/src/http/HttpError.js +84 -10
  4. package/src/http/http.js +41 -31
  5. package/src/http/index.js +4 -0
  6. package/src/http/responseType.js +10 -0
  7. package/src/ids/index.js +2 -0
  8. package/src/index.js +7 -21
  9. package/src/mailer/transport.factory.js +19 -6
  10. package/src/mongodb/initialize-mongodb.js +9 -7
  11. package/src/rabbit-mq/index.js +1 -186
  12. package/src/rabbit-mq/rabbit-mq.js +189 -0
  13. package/src/templates/index.js +1 -0
  14. package/tests/fastify/error-handler.unit.test.js +39 -0
  15. package/tests/{with-error-handling.test.js → fastify/error-handlers/with-error-handling.test.js} +4 -3
  16. package/tests/http/HttpError.unit.test.js +112 -0
  17. package/tests/http/http-method.unit.test.js +29 -0
  18. package/tests/http/http.unit.test.js +167 -0
  19. package/tests/http/responseType.unit.test.js +45 -0
  20. package/tests/ids/prefixes.unit.test.js +1 -0
  21. package/tests/mailer/mailer.integration.test.js +95 -0
  22. package/tests/{mailer.unit.test.js → mailer/mailer.unit.test.js} +7 -11
  23. package/tests/mailer/transport.factory.unit.test.js +204 -0
  24. package/tests/mongodb/connect.unit.test.js +60 -0
  25. package/tests/mongodb/initialize-mongodb.unit.test.js +98 -0
  26. package/tests/mongodb/validate-mongo-uri.unit.test.js +52 -0
  27. package/tests/{rabbit-mq.test.js → rabbit-mq/rabbit-mq.test.js} +3 -2
  28. package/tests/{template-loader.integration.test.js → templates/template-loader.integration.test.js} +1 -1
  29. package/tests/{template-loader.unit.test.js → templates/template-loader.unit.test.js} +1 -1
  30. package/vitest.config.js +3 -0
  31. package/index.js +0 -3
  32. package/tests/HttpError.test.js +0 -80
  33. package/tests/core-util.js +0 -24
  34. package/tests/mailer.integration.test.js +0 -46
  35. package/tests/mongodb.test.js +0 -70
  36. package/tests/transport.factory.unit.test.js +0 -128
@@ -1,24 +0,0 @@
1
- import { exec } from 'child_process'
2
- /**
3
- * Starts a MongoDB Docker container on the specified port.
4
- * @param {string} command - Command to run, like: 'docker run -d --name mongo-test -p 2730:27017 mongo'.
5
- * @returns {Promise<void>}
6
- */
7
- export const runInTerminal = async (command) => {
8
- return new Promise((resolve, reject) => {
9
- exec(command, (error, stdout, stderr) => {
10
- if (error) {
11
- console.error('Error starting command:', error.message)
12
- return reject(error)
13
- }
14
- if (stderr) {
15
- console.warn('stderr:', stderr)
16
- }
17
- console.log('Command started:', stdout.trim())
18
- resolve()
19
- })
20
- })
21
- }
22
-
23
- export const sleep = async (milliseconds) =>
24
- new Promise((res) => setTimeout(res, milliseconds))
@@ -1,46 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import nodemailer from 'nodemailer'
3
- import { Mailer } from '../src/mailer/mailer.service.js'
4
-
5
- describe('Mailer (integration)', () => {
6
- it('should send email using ethereal SMTP', async () => {
7
- // Create ethereal test account
8
- const testAccount = await nodemailer.createTestAccount()
9
-
10
- // Create Nodemailer transporter for ethereal
11
- const transporter = nodemailer.createTransport({
12
- host: testAccount.smtp.host,
13
- port: testAccount.smtp.port,
14
- secure: testAccount.smtp.secure,
15
- auth: {
16
- user: testAccount.user,
17
- pass: testAccount.pass,
18
- },
19
- })
20
-
21
- // Create Mailer instance
22
- const mailer = new Mailer(transporter)
23
-
24
- // Send test email
25
- const result = await mailer.send({
26
- to: 'recipient@example.com',
27
- text: 'Hello from test - plain text',
28
- from: '"My App" <no-reply@example.com>',
29
- subject: 'core-service-sdk:Integration Test Email',
30
- html: '<h1>Hello from core-service-sdk test</h1><p>This is a core-service-sdk</p>',
31
- cc: '',
32
- bcc: '',
33
- replyTo: '',
34
- attachments: '',
35
- })
36
-
37
- // Assert response
38
- expect(result.messageId).toBeDefined()
39
-
40
- // Print preview URL (clickable in terminal)
41
- const previewUrl = nodemailer.getTestMessageUrl(result)
42
- console.log(`📨 Preview this email: ${previewUrl}`)
43
-
44
- expect(previewUrl).toMatch(/^https:\/\/ethereal\.email/)
45
- })
46
- })
@@ -1,70 +0,0 @@
1
- import { describe, it, expect, beforeAll, afterAll } from 'vitest'
2
- import { runInTerminal, sleep } from './core-util.js'
3
- import { initializeMongoDb } from '../src/mongodb/index.js'
4
-
5
- const port = 2730
6
- const dbName = 'testdb'
7
- const host = 'localhost'
8
- const mongoUri = `mongodb://${host}:${port}/?replicaSet=rs0`
9
- const dockerStopCommand = `docker stop ${dbName} && docker rm ${dbName}`
10
- const dockerCreateCommant = `docker run -d --name ${dbName} -p ${port}:27017 mongo --replSet rs0`
11
- const dockerReplicaSetCommand = `docker exec -i ${dbName} mongosh --eval "rs.initiate()"`
12
-
13
- describe('MongoDB Init & Transaction SDK', () => {
14
- let collections
15
-
16
- beforeAll(async () => {
17
- try {
18
- await runInTerminal(dockerStopCommand)
19
- } catch (error) {
20
- console.log('No existing container to stop.')
21
- }
22
-
23
- await runInTerminal(dockerCreateCommant)
24
- await sleep(5000)
25
- await runInTerminal(dockerReplicaSetCommand)
26
-
27
- collections = await initializeMongoDb({
28
- config: {
29
- uri: mongoUri,
30
- options: { dbName },
31
- },
32
- collectionNames: {
33
- users: 'users',
34
- logs: 'logs',
35
- },
36
- })
37
-
38
- await collections.users.deleteMany({})
39
- await collections.logs.deleteMany({})
40
- }, 60000)
41
-
42
- afterAll(async () => {
43
- if (collections?.client) {
44
- await collections.client.db(dbName).dropDatabase()
45
- await collections.client.close()
46
- }
47
- }, 20000)
48
-
49
- it.skip('should insert into multiple collections within a transaction', async () => {
50
- if (!collections) throw new Error('collections not initialized')
51
-
52
- await collections.withTransaction(async ({ session }) => {
53
- const userInsert = collections.users.insertOne(
54
- { name: 'Alice' },
55
- { session },
56
- )
57
- const logInsert = collections.logs.insertOne(
58
- { action: 'UserCreated', user: 'Alice' },
59
- { session },
60
- )
61
- await Promise.all([userInsert, logInsert])
62
- })
63
-
64
- const insertedUser = await collections.users.findOne({ name: 'Alice' })
65
- const insertedLog = await collections.logs.findOne({ user: 'Alice' })
66
-
67
- expect(insertedUser).not.toBeNull()
68
- expect(insertedLog).not.toBeNull()
69
- }, 20000)
70
- }, 60000)
@@ -1,128 +0,0 @@
1
- import aws from 'aws-sdk'
2
- import nodemailer from 'nodemailer'
3
- import { describe, it, expect, vi } from 'vitest'
4
- import sgTransport from 'nodemailer-sendgrid-transport'
5
-
6
- vi.mock('nodemailer', () => {
7
- const createTransport = vi.fn((options) => ({
8
- type: 'mock-transport',
9
- options,
10
- }))
11
- return {
12
- __esModule: true,
13
- default: { createTransport },
14
- createTransport,
15
- }
16
- })
17
-
18
- vi.mock('aws-sdk', () => {
19
- return {
20
- __esModule: true,
21
- default: {
22
- SES: vi.fn(() => 'mocked-ses'),
23
- },
24
- }
25
- })
26
-
27
- vi.mock('nodemailer-sendgrid-transport', () => {
28
- return {
29
- __esModule: true,
30
- default: vi.fn((opts) => ({
31
- sendgrid: true,
32
- options: opts,
33
- })),
34
- }
35
- })
36
-
37
- import { TransportFactory } from '../src/mailer/transport.factory.js'
38
- describe('TransportFactory', () => {
39
- it('should create smtp transport', () => {
40
- const config = {
41
- type: 'smtp',
42
- host: 'smtp.example.com',
43
- port: 587,
44
- secure: false,
45
- auth: { user: 'user', pass: 'pass' },
46
- }
47
-
48
- const transport = TransportFactory.create(config)
49
- expect(nodemailer.createTransport).toHaveBeenCalledWith({
50
- host: config.host,
51
- port: config.port,
52
- secure: config.secure,
53
- auth: config.auth,
54
- })
55
- expect(transport.type).toBe('mock-transport')
56
- })
57
-
58
- it('should create gmail transport', () => {
59
- const config = {
60
- type: 'gmail',
61
- auth: { user: 'user@gmail.com', pass: 'pass' },
62
- }
63
-
64
- const transport = TransportFactory.create(config)
65
- expect(nodemailer.createTransport).toHaveBeenCalledWith({
66
- service: 'gmail',
67
- auth: config.auth,
68
- })
69
- expect(transport.type).toBe('mock-transport')
70
- })
71
-
72
- it('should create sendgrid transport', () => {
73
- const config = {
74
- type: 'sendgrid',
75
- apiKey: 'SG.xxxx',
76
- }
77
-
78
- const transport = TransportFactory.create(config)
79
-
80
- expect(nodemailer.createTransport).toHaveBeenCalled()
81
-
82
- const args = nodemailer.createTransport.mock.calls[0][0]
83
-
84
- expect(args).toEqual({
85
- sendgrid: true,
86
- options: {
87
- auth: { api_key: config.apiKey },
88
- },
89
- })
90
-
91
- expect(transport).toEqual({
92
- type: 'mock-transport',
93
- options: {
94
- sendgrid: true,
95
- options: {
96
- auth: { api_key: config.apiKey },
97
- },
98
- },
99
- })
100
- })
101
-
102
- it('should create ses transport', () => {
103
- const config = {
104
- type: 'ses',
105
- accessKeyId: 'AKIA...',
106
- secretAccessKey: 'secret',
107
- region: 'us-west-2',
108
- }
109
-
110
- TransportFactory.create(config)
111
-
112
- expect(nodemailer.createTransport).toHaveBeenCalled()
113
- const args = nodemailer.createTransport.mock.calls[0][0]
114
-
115
- expect(args).toEqual({
116
- SES: {
117
- ses: 'mocked-ses',
118
- aws: expect.anything(),
119
- },
120
- })
121
- })
122
-
123
- it('should throw error for unsupported type', () => {
124
- expect(() => {
125
- TransportFactory.create({ type: 'unsupported' })
126
- }).toThrow('Unsupported transport type: unsupported')
127
- })
128
- })