core-services-sdk 1.3.87 → 1.3.88

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.3.87",
3
+ "version": "1.3.88",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "types": "types/index.d.ts",
package/src/http/http.js CHANGED
@@ -183,6 +183,26 @@ const getResponsePayload = async (response, responseType) => {
183
183
  }
184
184
  }
185
185
 
186
+ /**
187
+ * Checks if value is a Node.js Readable stream.
188
+ *
189
+ * @param {any} value
190
+ * @returns {boolean}
191
+ */
192
+ const isReadableStream = (value) => {
193
+ return value && typeof value === 'object' && typeof value.pipe === 'function'
194
+ }
195
+
196
+ /**
197
+ * Checks if value is a TypedArray (Uint8Array, etc).
198
+ *
199
+ * @param {any} value
200
+ * @returns {boolean}
201
+ */
202
+ const isTypedArray = (value) => {
203
+ return ArrayBuffer.isView(value)
204
+ }
205
+
186
206
  /**
187
207
  * Normalizes the request body before sending it via fetch.
188
208
  *
@@ -192,6 +212,9 @@ const getResponsePayload = async (response, responseType) => {
192
212
  * - URLSearchParams → sent as-is (fetch serializes automatically)
193
213
  * - FormData → sent as-is (fetch sets multipart boundary automatically)
194
214
  * - ArrayBuffer / Blob → sent as-is
215
+ * - Buffer → sent as-is
216
+ * - TypedArray → sent as-is
217
+ * - Readable stream → sent as-is
195
218
  * - object / array → JSON.stringify applied and Content-Type ensured
196
219
  *
197
220
  * @param {any} body
@@ -203,22 +226,89 @@ const prepareRequestBody = (body, headers = {}) => {
203
226
  return { body: undefined, headers }
204
227
  }
205
228
 
206
- if (
207
- typeof body === 'string' ||
208
- body instanceof URLSearchParams ||
209
- body instanceof FormData ||
210
- body instanceof ArrayBuffer ||
211
- body instanceof Blob
212
- ) {
229
+ if (typeof body === 'string') {
230
+ return { body, headers }
231
+ }
232
+
233
+ if (body instanceof URLSearchParams) {
234
+ return { body, headers }
235
+ }
236
+
237
+ if (body instanceof FormData) {
238
+ return { body, headers }
239
+ }
240
+
241
+ if (Buffer.isBuffer(body)) {
242
+ return { body, headers }
243
+ }
244
+
245
+ if (body instanceof ArrayBuffer) {
246
+ return { body, headers }
247
+ }
248
+
249
+ if (isTypedArray(body)) {
250
+ return { body, headers }
251
+ }
252
+
253
+ if (typeof Blob !== 'undefined' && body instanceof Blob) {
213
254
  return { body, headers }
214
255
  }
215
256
 
257
+ if (isReadableStream(body)) {
258
+ return { body, headers }
259
+ }
260
+
261
+ if (typeof body === 'object') {
262
+ return {
263
+ body: JSON.stringify(body),
264
+ headers: {
265
+ 'Content-Type': 'application/json',
266
+ ...headers,
267
+ },
268
+ }
269
+ }
270
+
271
+ return { body, headers }
272
+ }
273
+
274
+ /**
275
+ * Resolves headers safely based on body type.
276
+ *
277
+ * Adds default headers only when no Content-Type is provided
278
+ * AND the body is a plain JSON object or array.
279
+ *
280
+ * @param {Record<string,string>} preparedHeaders
281
+ * @param {any} body
282
+ * @param {Record<string,string>} defaultHeaders
283
+ * @returns {Record<string,string>}
284
+ */
285
+ const resolveHeaders = (
286
+ preparedHeaders = {},
287
+ body,
288
+ defaultHeaders = JSON_HEADER,
289
+ ) => {
290
+ const hasContentType = Object.keys(preparedHeaders).some(
291
+ (key) => key.toLowerCase() === 'content-type',
292
+ )
293
+
294
+ if (hasContentType) {
295
+ return preparedHeaders
296
+ }
297
+
298
+ const isPlainObject =
299
+ body !== null && typeof body === 'object' && body.constructor === Object
300
+
301
+ const isArray = Array.isArray(body)
302
+
303
+ const isJsonCandidate = isPlainObject || isArray
304
+
305
+ if (!isJsonCandidate) {
306
+ return preparedHeaders
307
+ }
308
+
216
309
  return {
217
- body: JSON.stringify(body),
218
- headers: {
219
- 'Content-Type': 'application/json',
220
- ...headers,
221
- },
310
+ ...defaultHeaders,
311
+ ...preparedHeaders,
222
312
  }
223
313
  }
224
314
 
@@ -238,7 +328,10 @@ export const get = async ({
238
328
  const response = await fetch(url, {
239
329
  ...extraParams,
240
330
  method: HTTP_METHODS.GET,
241
- headers: { ...JSON_HEADER, ...headers },
331
+ headers: {
332
+ Accept: 'application/json',
333
+ ...headers,
334
+ },
242
335
  })
243
336
  await checkStatus(response)
244
337
  return getResponsePayload(response, expectedType)
@@ -265,7 +358,7 @@ export const post = async ({
265
358
  const response = await fetch(url, {
266
359
  ...extraParams,
267
360
  method: HTTP_METHODS.POST,
268
- headers: { ...JSON_HEADER, ...preparedHeaders },
361
+ headers: resolveHeaders(preparedHeaders, body),
269
362
  body: preparedBody,
270
363
  })
271
364
  await checkStatus(response)
@@ -293,7 +386,7 @@ export const put = async ({
293
386
  const response = await fetch(url, {
294
387
  ...extraParams,
295
388
  method: HTTP_METHODS.PUT,
296
- headers: { ...JSON_HEADER, ...preparedHeaders },
389
+ headers: resolveHeaders(preparedHeaders, body),
297
390
  body: preparedBody,
298
391
  })
299
392
  await checkStatus(response)
@@ -321,7 +414,7 @@ export const patch = async ({
321
414
  const response = await fetch(url, {
322
415
  ...extraParams,
323
416
  method: HTTP_METHODS.PATCH,
324
- headers: { ...JSON_HEADER, ...preparedHeaders },
417
+ headers: resolveHeaders(preparedHeaders, body),
325
418
  body: preparedBody,
326
419
  })
327
420
  await checkStatus(response)
@@ -349,7 +442,7 @@ export const deleteApi = async ({
349
442
  const response = await fetch(url, {
350
443
  ...extraParams,
351
444
  method: HTTP_METHODS.DELETE,
352
- headers: { ...JSON_HEADER, ...preparedHeaders },
445
+ headers: resolveHeaders(preparedHeaders, body),
353
446
  ...(preparedBody ? { body: preparedBody } : {}),
354
447
  })
355
448
  await checkStatus(response)
@@ -367,7 +460,7 @@ export const head = async ({ url, headers = {}, extraParams = {} }) => {
367
460
  const response = await fetch(url, {
368
461
  ...extraParams,
369
462
  method: HTTP_METHODS.HEAD,
370
- headers: { ...JSON_HEADER, ...headers },
463
+ headers,
371
464
  })
372
465
 
373
466
  await checkStatus(response)
@@ -6,7 +6,7 @@ import { http } from '../../src/http/http.js'
6
6
  let server
7
7
  let baseUrl
8
8
 
9
- function collectBody(req) {
9
+ function collectRaw(req) {
10
10
  return new Promise((resolve) => {
11
11
  const chunks = []
12
12
 
@@ -15,14 +15,15 @@ function collectBody(req) {
15
15
  })
16
16
 
17
17
  req.on('end', () => {
18
- resolve(Buffer.concat(chunks).toString())
18
+ const buffer = Buffer.concat(chunks)
19
+ resolve(buffer)
19
20
  })
20
21
  })
21
22
  }
22
23
 
23
24
  beforeAll(async () => {
24
25
  server = httpServer.createServer(async (req, res) => {
25
- const body = await collectBody(req)
26
+ const raw = await collectRaw(req)
26
27
 
27
28
  res.setHeader('content-type', 'application/json')
28
29
 
@@ -30,7 +31,8 @@ beforeAll(async () => {
30
31
  JSON.stringify({
31
32
  method: req.method,
32
33
  headers: req.headers,
33
- body,
34
+ body: raw.toString(),
35
+ bodyLength: raw.length,
34
36
  }),
35
37
  )
36
38
  })
@@ -59,6 +61,15 @@ describe('http util integration', () => {
59
61
  expect(result.body).toBe(JSON.stringify({ hello: 'world' }))
60
62
  })
61
63
 
64
+ it('should send array JSON correctly', async () => {
65
+ const result = await http.post({
66
+ url: `${baseUrl}/array`,
67
+ body: [1, 2, 3],
68
+ })
69
+
70
+ expect(result.body).toBe(JSON.stringify([1, 2, 3]))
71
+ })
72
+
62
73
  it('should send string body without stringify', async () => {
63
74
  const result = await http.post({
64
75
  url: `${baseUrl}/string`,
@@ -93,6 +104,43 @@ describe('http util integration', () => {
93
104
  expect(result.body).toBe(params.toString())
94
105
  })
95
106
 
107
+ it('should support Buffer (binary)', async () => {
108
+ const buffer = Buffer.from('PDF-DATA-TEST')
109
+
110
+ const result = await http.post({
111
+ url: `${baseUrl}/buffer`,
112
+ body: buffer,
113
+ headers: {
114
+ 'Content-Type': 'application/pdf',
115
+ },
116
+ })
117
+
118
+ expect(result.bodyLength).toBe(buffer.length)
119
+ expect(result.body).toBe(buffer.toString())
120
+ })
121
+
122
+ it('should support Uint8Array (TypedArray)', async () => {
123
+ const uint8 = new Uint8Array([1, 2, 3, 4])
124
+
125
+ const result = await http.post({
126
+ url: `${baseUrl}/typed`,
127
+ body: uint8,
128
+ })
129
+
130
+ expect(result.bodyLength).toBe(uint8.length)
131
+ })
132
+
133
+ it('should support ArrayBuffer', async () => {
134
+ const buffer = new Uint8Array([5, 6, 7]).buffer
135
+
136
+ const result = await http.post({
137
+ url: `${baseUrl}/arraybuffer`,
138
+ body: buffer,
139
+ })
140
+
141
+ expect(result.bodyLength).toBe(3)
142
+ })
143
+
96
144
  it('should support PUT with JSON', async () => {
97
145
  const result = await http.put({
98
146
  url: `${baseUrl}/put`,
@@ -123,6 +171,14 @@ describe('http util integration', () => {
123
171
  expect(result.body).toBe(JSON.stringify({ remove: true }))
124
172
  })
125
173
 
174
+ it('should not send body when undefined', async () => {
175
+ const result = await http.post({
176
+ url: `${baseUrl}/empty`,
177
+ })
178
+
179
+ expect(result.bodyLength).toBe(0)
180
+ })
181
+
126
182
  it('should support GET request', async () => {
127
183
  const result = await http.get({
128
184
  url: `${baseUrl}/get`,
@@ -138,4 +194,117 @@ describe('http util integration', () => {
138
194
 
139
195
  expect(response.status).toBe(200)
140
196
  })
197
+
198
+ it('should set JSON content-type by default', async () => {
199
+ const result = await http.post({
200
+ url: `${baseUrl}/json-header`,
201
+ body: { a: 1 },
202
+ })
203
+
204
+ expect(result.headers['content-type']).toContain('application/json')
205
+ })
206
+
207
+ it('should allow overriding content-type', async () => {
208
+ const result = await http.post({
209
+ url: `${baseUrl}/override-header`,
210
+ body: { a: 1 },
211
+ headers: {
212
+ 'Content-Type': 'application/custom+json',
213
+ },
214
+ })
215
+
216
+ expect(result.headers['content-type']).toContain('application/custom+json')
217
+ })
218
+
219
+ it('should preserve binary content-type', async () => {
220
+ const buffer = Buffer.from('file')
221
+
222
+ const result = await http.post({
223
+ url: `${baseUrl}/pdf`,
224
+ body: buffer,
225
+ headers: {
226
+ 'Content-Type': 'application/pdf',
227
+ },
228
+ })
229
+
230
+ expect(result.headers['content-type']).toContain('application/pdf')
231
+ })
232
+
233
+ it('should not force JSON content-type for URLSearchParams', async () => {
234
+ const params = new URLSearchParams({ a: '1' })
235
+
236
+ const result = await http.post({
237
+ url: `${baseUrl}/urlencoded`,
238
+ body: params,
239
+ })
240
+
241
+ expect(result.headers['content-type']).toContain(
242
+ 'application/x-www-form-urlencoded',
243
+ )
244
+ })
245
+
246
+ it('should not override multipart content-type for FormData', async () => {
247
+ const form = new FormData()
248
+ form.append('field', 'value')
249
+
250
+ const result = await http.post({
251
+ url: `${baseUrl}/multipart`,
252
+ body: form,
253
+ })
254
+
255
+ expect(result.headers['content-type']).toContain('multipart/form-data')
256
+ })
257
+
258
+ it('should respect content-type for string body', async () => {
259
+ const result = await http.post({
260
+ url: `${baseUrl}/text`,
261
+ body: 'hello',
262
+ headers: {
263
+ 'Content-Type': 'text/plain',
264
+ },
265
+ })
266
+
267
+ expect(result.headers['content-type']).toContain('text/plain')
268
+ })
269
+
270
+ it('GET should NOT send content-type header', async () => {
271
+ const result = await http.get({
272
+ url: `${baseUrl}/get-no-content-type`,
273
+ })
274
+
275
+ expect(result.headers['content-type']).toBeUndefined()
276
+ })
277
+
278
+ it('should NOT add JSON content-type for Buffer without headers', async () => {
279
+ const buffer = Buffer.from('binary')
280
+
281
+ const result = await http.post({
282
+ url: `${baseUrl}/buffer-no-header`,
283
+ body: buffer,
284
+ })
285
+
286
+ expect(result.headers['content-type']).toBeUndefined()
287
+ })
288
+
289
+ it('should NOT add JSON content-type for TypedArray', async () => {
290
+ const uint8 = new Uint8Array([1, 2, 3])
291
+
292
+ const result = await http.post({
293
+ url: `${baseUrl}/typed-no-header`,
294
+ body: uint8,
295
+ })
296
+
297
+ expect(result.headers['content-type']).toBeUndefined()
298
+ })
299
+
300
+ it('should NOT add JSON content-type for ArrayBuffer', async () => {
301
+ const buffer = new Uint8Array([1, 2]).buffer
302
+
303
+ const result = await http.post({
304
+ url: `${baseUrl}/arraybuffer-no-header`,
305
+ body: buffer,
306
+ })
307
+
308
+ expect(result.headers['content-type']).toBeUndefined()
309
+ })
141
310
  })
@@ -166,14 +166,13 @@ describe('http client (native fetch)', () => {
166
166
  })
167
167
 
168
168
  describe('PUT with non-JSON body', () => {
169
- it('should not stringify body when expectedType is not json', async () => {
169
+ it('should not stringify string body', async () => {
170
170
  const rawBody = 'plain text body'
171
171
  mockFetch.mockResolvedValueOnce(createMockResponse({ body: 'OK' }))
172
172
 
173
173
  await http.put({
174
174
  url: 'http://test.com',
175
175
  body: rawBody,
176
- expectedType: ResponseType.text,
177
176
  })
178
177
 
179
178
  expect(mockFetch).toHaveBeenCalledWith(
@@ -395,4 +394,104 @@ describe('edge cases', () => {
395
394
  }),
396
395
  )
397
396
  })
397
+
398
+ it('should NOT add JSON content-type for Buffer', async () => {
399
+ const buffer = Buffer.from('binary')
400
+
401
+ mockFetch.mockResolvedValueOnce(
402
+ createMockResponse({ body: JSON.stringify({ ok: true }) }),
403
+ )
404
+
405
+ await http.post({
406
+ url: 'http://test.com',
407
+ body: buffer,
408
+ })
409
+
410
+ expect(mockFetch).toHaveBeenCalledWith(
411
+ 'http://test.com',
412
+ expect.objectContaining({
413
+ headers: expect.not.objectContaining({
414
+ 'Content-Type': 'application/json',
415
+ }),
416
+ }),
417
+ )
418
+ })
419
+
420
+ it('should NOT add JSON content-type for TypedArray', async () => {
421
+ const uint8 = new Uint8Array([1, 2, 3])
422
+
423
+ mockFetch.mockResolvedValueOnce(
424
+ createMockResponse({ body: JSON.stringify({ ok: true }) }),
425
+ )
426
+
427
+ await http.post({
428
+ url: 'http://test.com',
429
+ body: uint8,
430
+ })
431
+
432
+ expect(mockFetch).toHaveBeenCalledWith(
433
+ 'http://test.com',
434
+ expect.objectContaining({
435
+ headers: expect.not.objectContaining({
436
+ 'Content-Type': 'application/json',
437
+ }),
438
+ }),
439
+ )
440
+ })
441
+
442
+ it('should NOT add JSON content-type for ArrayBuffer', async () => {
443
+ const buffer = new Uint8Array([1, 2]).buffer
444
+
445
+ mockFetch.mockResolvedValueOnce(
446
+ createMockResponse({ body: JSON.stringify({ ok: true }) }),
447
+ )
448
+
449
+ await http.post({
450
+ url: 'http://test.com',
451
+ body: buffer,
452
+ })
453
+
454
+ expect(mockFetch).toHaveBeenCalledWith(
455
+ 'http://test.com',
456
+ expect.objectContaining({
457
+ headers: expect.not.objectContaining({
458
+ 'Content-Type': 'application/json',
459
+ }),
460
+ }),
461
+ )
462
+ })
463
+
464
+ it('should include default Accept header in GET', async () => {
465
+ mockFetch.mockResolvedValueOnce(
466
+ createMockResponse({ body: JSON.stringify({ ok: true }) }),
467
+ )
468
+
469
+ await http.get({
470
+ url: 'http://test.com',
471
+ })
472
+
473
+ expect(mockFetch).toHaveBeenCalledWith(
474
+ 'http://test.com',
475
+ expect.objectContaining({
476
+ headers: expect.objectContaining({
477
+ Accept: 'application/json',
478
+ }),
479
+ }),
480
+ )
481
+ })
482
+
483
+ it('HEAD should NOT include content-type header', async () => {
484
+ mockFetch.mockResolvedValueOnce(createMockResponse())
485
+
486
+ await http.head({
487
+ url: 'http://test.com',
488
+ })
489
+
490
+ expect(mockFetch).toHaveBeenCalledWith(
491
+ 'http://test.com',
492
+ expect.objectContaining({
493
+ headers: {},
494
+ }),
495
+ )
496
+ })
398
497
  })