@unito/integration-sdk 1.4.1 → 1.4.2

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.
@@ -1184,6 +1184,7 @@ class Provider {
1184
1184
  defaultHeaders: {
1185
1185
  Accept: 'application/octet-stream',
1186
1186
  },
1187
+ rawBody: true,
1187
1188
  });
1188
1189
  }
1189
1190
  /**
@@ -1375,7 +1376,11 @@ class Provider {
1375
1376
  }
1376
1377
  const responseContentType = response.headers.get('content-type');
1377
1378
  let body;
1378
- if (headers.Accept === 'application/json') {
1379
+ if (options.rawBody || headers.Accept === 'application/octet-stream') {
1380
+ // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it
1381
+ body = response.body;
1382
+ }
1383
+ else if (headers.Accept === 'application/json') {
1379
1384
  // Validate that the response content type is at least similar to what we expect
1380
1385
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
1381
1386
  // Default to application/json if no Content-Type header is provided
@@ -1391,10 +1396,6 @@ class Provider {
1391
1396
  throw this.handleError(500, `Invalid JSON response`, options);
1392
1397
  }
1393
1398
  }
1394
- else if (headers.Accept == 'application/octet-stream') {
1395
- // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
1396
- body = response.body;
1397
- }
1398
1399
  else if (headers.Accept?.includes('text/html')) {
1399
1400
  // Accept text based content types
1400
1401
  body = (await response.text());
@@ -29,6 +29,7 @@ export type RateLimiter = <T>(options: {
29
29
  * @property logger - The logger to use during the call.
30
30
  * @property queryParams - The query parameters to add when calling the provider.
31
31
  * @property additionnalheaders - The headers to add when calling the provider.
32
+ * @property rawBody - Whether to return the raw response body.
32
33
  */
33
34
  export type RequestOptions = {
34
35
  credentials: Credentials;
@@ -40,6 +41,7 @@ export type RequestOptions = {
40
41
  additionnalheaders?: {
41
42
  [key: string]: string;
42
43
  };
44
+ rawBody?: boolean;
43
45
  };
44
46
  /**
45
47
  * Response object returned by the Provider's method.
@@ -85,6 +85,7 @@ export class Provider {
85
85
  defaultHeaders: {
86
86
  Accept: 'application/octet-stream',
87
87
  },
88
+ rawBody: true,
88
89
  });
89
90
  }
90
91
  /**
@@ -276,7 +277,11 @@ export class Provider {
276
277
  }
277
278
  const responseContentType = response.headers.get('content-type');
278
279
  let body;
279
- if (headers.Accept === 'application/json') {
280
+ if (options.rawBody || headers.Accept === 'application/octet-stream') {
281
+ // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it
282
+ body = response.body;
283
+ }
284
+ else if (headers.Accept === 'application/json') {
280
285
  // Validate that the response content type is at least similar to what we expect
281
286
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
282
287
  // Default to application/json if no Content-Type header is provided
@@ -292,10 +297,6 @@ export class Provider {
292
297
  throw this.handleError(500, `Invalid JSON response`, options);
293
298
  }
294
299
  }
295
- else if (headers.Accept == 'application/octet-stream') {
296
- // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
297
- body = response.body;
298
- }
299
300
  else if (headers.Accept?.includes('text/html')) {
300
301
  // Accept text based content types
301
302
  body = (await response.text());
@@ -79,6 +79,25 @@ describe('Provider', () => {
79
79
  ]);
80
80
  assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
81
81
  });
82
+ it('should return the raw response body if specified', async (context) => {
83
+ const response = new Response(`IMAGINE A HUGE PAYLOAD`, {
84
+ status: 200,
85
+ headers: { 'Content-Type': 'image/png' },
86
+ });
87
+ context.mock.method(global, 'fetch', () => Promise.resolve(response));
88
+ const providerResponse = await provider.streamingGet('/endpoint/123', {
89
+ credentials: { apiKey: 'apikey#1111' },
90
+ logger: logger,
91
+ signal: new AbortController().signal,
92
+ additionnalheaders: {
93
+ Accept: 'application/json',
94
+ },
95
+ rawBody: true,
96
+ });
97
+ assert.ok(providerResponse);
98
+ // What matters: still returns a stream
99
+ assert.ok(providerResponse.body instanceof ReadableStream);
100
+ });
82
101
  it('gets an endpoint which is an absolute url', async (context) => {
83
102
  const response = new Response('{"data": "value"}', {
84
103
  status: 200,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unito/integration-sdk",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Integration SDK",
5
5
  "type": "module",
6
6
  "types": "dist/src/index.d.ts",
@@ -34,6 +34,7 @@ export type RateLimiter = <T>(
34
34
  * @property logger - The logger to use during the call.
35
35
  * @property queryParams - The query parameters to add when calling the provider.
36
36
  * @property additionnalheaders - The headers to add when calling the provider.
37
+ * @property rawBody - Whether to return the raw response body.
37
38
  */
38
39
  export type RequestOptions = {
39
40
  credentials: Credentials;
@@ -41,6 +42,7 @@ export type RequestOptions = {
41
42
  signal?: AbortSignal;
42
43
  queryParams?: { [key: string]: string };
43
44
  additionnalheaders?: { [key: string]: string };
45
+ rawBody?: boolean;
44
46
  };
45
47
 
46
48
  /**
@@ -166,6 +168,7 @@ export class Provider {
166
168
  defaultHeaders: {
167
169
  Accept: 'application/octet-stream',
168
170
  },
171
+ rawBody: true,
169
172
  });
170
173
  }
171
174
 
@@ -391,7 +394,10 @@ export class Provider {
391
394
  const responseContentType = response.headers.get('content-type');
392
395
  let body: T;
393
396
 
394
- if (headers.Accept === 'application/json') {
397
+ if (options.rawBody || headers.Accept === 'application/octet-stream') {
398
+ // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it
399
+ body = response.body as T;
400
+ } else if (headers.Accept === 'application/json') {
395
401
  // Validate that the response content type is at least similar to what we expect
396
402
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
397
403
  // Default to application/json if no Content-Type header is provided
@@ -410,9 +416,6 @@ export class Provider {
410
416
  } catch (err) {
411
417
  throw this.handleError(500, `Invalid JSON response`, options);
412
418
  }
413
- } else if (headers.Accept == 'application/octet-stream') {
414
- // When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
415
- body = response.body as T;
416
419
  } else if (headers.Accept?.includes('text/html')) {
417
420
  // Accept text based content types
418
421
  body = (await response.text()) as T;
@@ -94,6 +94,29 @@ describe('Provider', () => {
94
94
  assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
95
95
  });
96
96
 
97
+ it('should return the raw response body if specified', async context => {
98
+ const response = new Response(`IMAGINE A HUGE PAYLOAD`, {
99
+ status: 200,
100
+ headers: { 'Content-Type': 'image/png' },
101
+ });
102
+
103
+ context.mock.method(global, 'fetch', () => Promise.resolve(response));
104
+
105
+ const providerResponse = await provider.streamingGet('/endpoint/123', {
106
+ credentials: { apiKey: 'apikey#1111' },
107
+ logger: logger,
108
+ signal: new AbortController().signal,
109
+ additionnalheaders: {
110
+ Accept: 'application/json',
111
+ },
112
+ rawBody: true,
113
+ });
114
+
115
+ assert.ok(providerResponse);
116
+ // What matters: still returns a stream
117
+ assert.ok(providerResponse.body instanceof ReadableStream);
118
+ });
119
+
97
120
  it('gets an endpoint which is an absolute url', async context => {
98
121
  const response = new Response('{"data": "value"}', {
99
122
  status: 200,