core-services-sdk 1.3.59 → 1.3.61
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 +1 -1
- package/src/core/index.js +1 -0
- package/src/core/parse-json-if-string.js +19 -0
- package/src/rabbit-mq/rabbit-mq.js +12 -0
- package/tests/core/parse-json-if-string.unit.test.js +51 -0
- package/tests/postgresql/paginate.integration.test.js +1 -0
- package/types/core/index.d.ts +1 -0
- package/types/core/parse-json-if-string.d.ts +1 -0
package/package.json
CHANGED
package/src/core/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from './otp-generators.js'
|
|
|
4
4
|
export * from './sanitize-objects.js'
|
|
5
5
|
export * from './normalize-min-max.js'
|
|
6
6
|
export * from './normalize-to-array.js'
|
|
7
|
+
export * from './parse-json-if-string.js'
|
|
7
8
|
export * from './combine-unique-arrays.js'
|
|
8
9
|
export * from './normalize-phone-number.js'
|
|
9
10
|
export * from './normalize-array-operators.js'
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a value that may be a JSON string or already a parsed object.
|
|
3
|
+
*
|
|
4
|
+
* @param {any} value
|
|
5
|
+
* The value to parse.
|
|
6
|
+
*
|
|
7
|
+
* @returns {any}
|
|
8
|
+
* Parsed JSON if value is a string, otherwise the original value.
|
|
9
|
+
*
|
|
10
|
+
* @throws {Error}
|
|
11
|
+
* Throws if the string is not valid JSON.
|
|
12
|
+
*/
|
|
13
|
+
export const parseJsonIfString = (value) => {
|
|
14
|
+
if (typeof value === 'string') {
|
|
15
|
+
return JSON.parse(value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return value
|
|
19
|
+
}
|
|
@@ -191,6 +191,18 @@ export const subscribeToQueue = async ({
|
|
|
191
191
|
}) => {
|
|
192
192
|
const logger = log.child({ op: 'subscribeToQueue', queue })
|
|
193
193
|
|
|
194
|
+
if (!queue || !queue.trim()) {
|
|
195
|
+
const message = 'Cannot subscribe to RabbitMQ with an empty queue name'
|
|
196
|
+
logger.error({ error: message })
|
|
197
|
+
throw new Error(message)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (typeof onReceive !== 'function') {
|
|
201
|
+
const message = `Cannot subscribe to queue "${queue}" because onReceive is not a function`
|
|
202
|
+
logger.error({ error: message })
|
|
203
|
+
throw new Error(message)
|
|
204
|
+
}
|
|
205
|
+
|
|
194
206
|
try {
|
|
195
207
|
await channel.assertQueue(queue, { durable: true })
|
|
196
208
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { parseJsonIfString } from '../../src/core/parse-json-if-string.js'
|
|
4
|
+
|
|
5
|
+
describe('parseJsonIfString', () => {
|
|
6
|
+
it('parses a valid JSON string', () => {
|
|
7
|
+
const value = '{"a":1,"b":"test"}'
|
|
8
|
+
|
|
9
|
+
const result = parseJsonIfString(value)
|
|
10
|
+
|
|
11
|
+
expect(result).toEqual({ a: 1, b: 'test' })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns an object as-is', () => {
|
|
15
|
+
const value = { a: 1, b: 'test' }
|
|
16
|
+
|
|
17
|
+
const result = parseJsonIfString(value)
|
|
18
|
+
|
|
19
|
+
expect(result).toBe(value)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns an array as-is', () => {
|
|
23
|
+
const value = [1, 2, 3]
|
|
24
|
+
|
|
25
|
+
const result = parseJsonIfString(value)
|
|
26
|
+
|
|
27
|
+
expect(result).toBe(value)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('returns null as-is', () => {
|
|
31
|
+
const value = null
|
|
32
|
+
|
|
33
|
+
const result = parseJsonIfString(value)
|
|
34
|
+
|
|
35
|
+
expect(result).toBeNull()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns a number as-is', () => {
|
|
39
|
+
const value = 42
|
|
40
|
+
|
|
41
|
+
const result = parseJsonIfString(value)
|
|
42
|
+
|
|
43
|
+
expect(result).toBe(42)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('throws when JSON string is invalid', () => {
|
|
47
|
+
const value = '{"a":1,'
|
|
48
|
+
|
|
49
|
+
expect(() => parseJsonIfString(value)).toThrow(SyntaxError)
|
|
50
|
+
})
|
|
51
|
+
})
|
package/types/core/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './otp-generators.js'
|
|
|
4
4
|
export * from './sanitize-objects.js'
|
|
5
5
|
export * from './normalize-min-max.js'
|
|
6
6
|
export * from './normalize-to-array.js'
|
|
7
|
+
export * from './parse-json-if-string.js'
|
|
7
8
|
export * from './combine-unique-arrays.js'
|
|
8
9
|
export * from './normalize-phone-number.js'
|
|
9
10
|
export * from './normalize-array-operators.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function parseJsonIfString(value: any): any
|