core-services-sdk 1.1.0 → 1.2.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.
@@ -15,6 +15,20 @@
15
15
  "smartStep": true,
16
16
  "skipFiles": ["<node_internals>/**"],
17
17
  "console": "integratedTerminal"
18
+ },
19
+ {
20
+ "type": "node",
21
+ "request": "launch",
22
+ "name": "Debug Vitest Current File",
23
+ "autoAttachChildProcesses": true,
24
+ "program": "${workspaceFolder}/node_modules/vitest/vitest.mjs",
25
+ "args": [
26
+ "run",
27
+ "${relativeFile}" // ← no `--test`
28
+ ],
29
+ "smartStep": true,
30
+ "skipFiles": ["<node_internals>/**"],
31
+ "console": "integratedTerminal"
18
32
  }
19
33
  ]
20
34
  }
@@ -12,5 +12,5 @@
12
12
  },
13
13
  "javascript.preferences.importModuleSpecifierEnding": "js",
14
14
  "js/ts.implicitProjectConfig.checkJs": true,
15
- "javascript.validate.enable": false
15
+ "javascript.validate.enable": true
16
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -21,8 +21,10 @@
21
21
  "dependencies": {
22
22
  "amqplib": "^0.10.8",
23
23
  "http-status": "^2.1.0",
24
+ "mongodb": "^6.17.0",
24
25
  "node-fetch": "^3.3.2",
25
- "uuid": "^11.1.0"
26
+ "uuid": "^11.1.0",
27
+ "xml2js": "^0.6.2"
26
28
  },
27
29
  "devDependencies": {
28
30
  "@vitest/coverage-v8": "^3.2.4",
package/src/http/http.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import fetch from 'node-fetch'
2
2
  import httpStatus from 'http-status'
3
+ import { parseStringPromise } from 'xml2js'
3
4
 
4
5
  import { HttpError } from './HttpError.js'
5
6
  import { HTTP_METHODS } from './http-method.js'
7
+ import { ResponseType } from './responseType.js'
6
8
 
7
9
  const JSON_HEADER = {
8
10
  'Content-Type': 'application/json',
@@ -28,6 +30,11 @@ const checkStatus = async (res) => {
28
30
  return res
29
31
  }
30
32
 
33
+ const getTextResponse = async (response) => {
34
+ const text = await response.text()
35
+ return text
36
+ }
37
+
31
38
  const tryConvertJsonResponse = (responseText) => {
32
39
  try {
33
40
  const obj = JSON.parse(responseText)
@@ -39,12 +46,53 @@ const tryConvertJsonResponse = (responseText) => {
39
46
  }
40
47
 
41
48
  const tryGetJsonResponse = async (response) => {
42
- const text = await response.text()
43
- const obj = tryConvertJsonResponse(text)
44
- return obj
49
+ let jsonText
50
+ try {
51
+ jsonText = getTextResponse(response)
52
+ const obj = tryConvertJsonResponse(jsonText)
53
+ return obj
54
+ } catch (error) {
55
+ if (!jsonText) {
56
+ throw error
57
+ }
58
+ return jsonText
59
+ }
45
60
  }
46
61
 
47
- export const get = async ({ url, headers = {}, credentials = 'include' }) => {
62
+ const tryGetXmlResponse = async (response) => {
63
+ let xmlText
64
+ try {
65
+ xmlText = await getTextResponse(response)
66
+ const xml = await parseStringPromise(xmlText)
67
+ return xml
68
+ } catch (error) {
69
+ if (!xmlText) {
70
+ throw error
71
+ }
72
+ return xmlText
73
+ }
74
+ }
75
+
76
+ const getResponsePayload = async (response, responseType) => {
77
+ switch (responseType) {
78
+ case ResponseType.json:
79
+ return tryGetJsonResponse(response)
80
+
81
+ case ResponseType.xml:
82
+ return tryGetXmlResponse(response)
83
+
84
+ default:
85
+ case ResponseType.text:
86
+ return getTextResponse(response)
87
+ }
88
+ }
89
+
90
+ export const get = async ({
91
+ url,
92
+ headers = {},
93
+ credentials = 'include',
94
+ expectedType = ResponseType.json,
95
+ }) => {
48
96
  const response = await fetch(url, {
49
97
  method: HTTP_METHODS.GET,
50
98
  headers: {
@@ -55,11 +103,17 @@ export const get = async ({ url, headers = {}, credentials = 'include' }) => {
55
103
  })
56
104
 
57
105
  await checkStatus(response)
58
- const data = await tryGetJsonResponse(response)
106
+ const data = await getResponsePayload(response, expectedType)
59
107
  return data
60
108
  }
61
109
 
62
- export const post = async ({ url, body, headers, credentials = 'include' }) => {
110
+ export const post = async ({
111
+ url,
112
+ body,
113
+ headers,
114
+ credentials = 'include',
115
+ expectedType = ResponseType.json,
116
+ }) => {
63
117
  const response = await fetch(url, {
64
118
  method: HTTP_METHODS.POST,
65
119
  headers: {
@@ -70,7 +124,7 @@ export const post = async ({ url, body, headers, credentials = 'include' }) => {
70
124
  ...(credentials ? { credentials } : {}),
71
125
  })
72
126
  await checkStatus(response)
73
- const data = await tryGetJsonResponse(response)
127
+ const data = await getResponsePayload(response, expectedType)
74
128
  return data
75
129
  }
76
130
 
@@ -79,6 +133,7 @@ export const put = async ({
79
133
  body,
80
134
  headers = {},
81
135
  credentials = 'include',
136
+ expectedType = ResponseType.json,
82
137
  }) => {
83
138
  const response = await fetch(url, {
84
139
  method: HTTP_METHODS.PUT,
@@ -91,7 +146,7 @@ export const put = async ({
91
146
  })
92
147
 
93
148
  await checkStatus(response)
94
- const data = await tryGetJsonResponse(response)
149
+ const data = await getResponsePayload(response, expectedType)
95
150
  return data
96
151
  }
97
152
 
@@ -100,6 +155,7 @@ export const patch = async ({
100
155
  body,
101
156
  headers,
102
157
  credentials = 'include',
158
+ expectedType = ResponseType.json,
103
159
  }) => {
104
160
  const response = await fetch(url, {
105
161
  method: HTTP_METHODS.PATCH,
@@ -112,7 +168,7 @@ export const patch = async ({
112
168
  })
113
169
 
114
170
  await checkStatus(response)
115
- const data = await tryGetJsonResponse(response)
171
+ const data = await getResponsePayload(response, expectedType)
116
172
  return data
117
173
  }
118
174
 
@@ -121,6 +177,7 @@ export const deleteApi = async ({
121
177
  body,
122
178
  headers,
123
179
  credentials = 'include',
180
+ expectedType = ResponseType.json,
124
181
  }) => {
125
182
  const response = await fetch(url, {
126
183
  method: HTTP_METHODS.DELETE,
@@ -133,14 +190,14 @@ export const deleteApi = async ({
133
190
  })
134
191
 
135
192
  await checkStatus(response)
136
- const data = await tryGetJsonResponse(response)
193
+ const data = await getResponsePayload(response, expectedType)
137
194
  return data
138
195
  }
139
196
 
140
197
  export const http = {
141
198
  get,
199
+ put,
142
200
  post,
143
201
  patch,
144
- put,
145
202
  deleteApi,
146
203
  }
@@ -0,0 +1,6 @@
1
+ export const ResponseType = Object.freeze({
2
+ xml: 'xml',
3
+ json: 'json',
4
+ text: 'text',
5
+ file: 'file',
6
+ })
package/src/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ export * from './mongodb/index.js'
2
+ export * from './mongodb/connect.js'
1
3
  export * from './rabbit-mq/index.js'
2
4
  export * as http from './http/http.js'
5
+ export * from './http/responseType.js'
3
6
  export { HttpError } from './http/HttpError.js'
@@ -0,0 +1,23 @@
1
+ import { MongoClient, ServerApiVersion } from 'mongodb'
2
+
3
+ /**
4
+ * Connects to MongoDB.
5
+ *
6
+ * @param {Object} options
7
+ * @param {string} options.uri - MongoDB connection URI.
8
+ * @param {object} [options.serverApi] - Optional serverApi configuration.
9
+ * @returns {Promise<import('mongodb').MongoClient>}
10
+ */
11
+
12
+ export const mongoConnect = async ({ uri, serverApi }) => {
13
+ const client = await MongoClient.connect(uri, {
14
+ serverApi: {
15
+ version: ServerApiVersion.v1,
16
+ strict: true,
17
+ deprecationErrors: true,
18
+ ...serverApi,
19
+ },
20
+ })
21
+
22
+ return client
23
+ }
@@ -0,0 +1,71 @@
1
+ // @ts-nocheck
2
+ import { mongoConnect } from './connect.js'
3
+
4
+ /**
5
+ * Initializes MongoDB collections and provides a transaction wrapper and read-only client accessor.
6
+ *
7
+ * @param {Object} options
8
+ * @param {{ uri: string, options: { dbName: string } }} options.config - MongoDB connection config
9
+ * @param {Record<string, string>} options.collectionNames - Map of collection keys to MongoDB collection names
10
+ *
11
+ * @returns {Promise<
12
+ * Record<string, import('mongodb').Collection> & {
13
+ * withTransaction: (action: ({ session: import('mongodb').ClientSession }) => Promise<void>) => Promise<void>,
14
+ * readonly client: import('mongodb').MongoClient
15
+ * }
16
+ * >}
17
+ *
18
+ * @example
19
+ * const { users, logs, withTransaction, client } = await initializeMongoDb({
20
+ * config: {
21
+ * uri: 'mongodb://localhost:27017',
22
+ * options: { dbName: 'mydb' },
23
+ * },
24
+ * collectionNames: {
25
+ * users: 'users',
26
+ * logs: 'system_logs',
27
+ * },
28
+ * });
29
+ *
30
+ * await withTransaction(async ({ session }) => {
31
+ * await users.insertOne({ name: 'Alice' }, { session });
32
+ * await logs.insertOne({ event: 'UserCreated', user: 'Alice' }, { session });
33
+ * });
34
+ *
35
+ * await client.close(); // Close connection manually
36
+ */
37
+ export const initializeMongoDb = async ({ config, collectionNames }) => {
38
+ const client = await mongoConnect(config)
39
+ const db = client.db(config.options.dbName)
40
+
41
+ const collectionRefs = Object.entries(collectionNames).reduce(
42
+ (collections, [key, collectionName]) => ({
43
+ ...collections,
44
+ [key]: db.collection(collectionName),
45
+ }),
46
+ {},
47
+ )
48
+
49
+ const withTransaction = async (action) => {
50
+ const session = client.startSession()
51
+ try {
52
+ session.startTransaction()
53
+ await action({ session })
54
+ await session.commitTransaction()
55
+ } catch (error) {
56
+ await session.abortTransaction()
57
+ throw error
58
+ } finally {
59
+ await session.endSession()
60
+ }
61
+ }
62
+
63
+ return {
64
+ ...collectionRefs,
65
+ withTransaction,
66
+ /** @type {import('mongodb').MongoClient} */
67
+ get client() {
68
+ return client
69
+ },
70
+ }
71
+ }
@@ -10,7 +10,7 @@ import { v4 as uuidv4 } from 'uuid'
10
10
  /**
11
11
  * Connects to RabbitMQ server.
12
12
  * @param {{ host: string }} options
13
- * @returns {Promise<amqp.Connection>}
13
+ * @returns {Promise<import('amqplib').Connection>}
14
14
  */
15
15
  export const connectQueueService = async ({ host }) => {
16
16
  try {
@@ -57,10 +57,10 @@ const parseMessage = (msgInfo) => {
57
57
  * @returns {Promise<void>}
58
58
  */
59
59
  export const subscribeToQueue = async ({
60
- channel,
60
+ log,
61
61
  queue,
62
+ channel,
62
63
  onReceive,
63
- log,
64
64
  nackOnError = false,
65
65
  }) => {
66
66
  try {
@@ -0,0 +1,24 @@
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))
@@ -0,0 +1,70 @@
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)
@@ -0,0 +1,6 @@
1
+ export default {
2
+ test: {
3
+ testTimeout: 30000,
4
+ hookTimeout: 30000,
5
+ },
6
+ }
@@ -1,22 +0,0 @@
1
- import { initializeQueue, rabbitUriFromEnv } from './rabbit.js'
2
-
3
- const log = {
4
- info: console.log,
5
- error: console.error,
6
- }
7
-
8
- const start = async () => {
9
- const host = rabbitUriFromEnv(process.env)
10
- const rabbit = await initializeQueue({ host, log })
11
-
12
- await rabbit.subscribe({
13
- queue: 'testQueue',
14
- onReceive: async (data) => {
15
- console.log('Received:', data)
16
- },
17
- })
18
-
19
- await rabbit.publish('testQueue', { hello: 'world' })
20
- }
21
-
22
- start()