@unito/integration-sdk 1.0.16 → 1.0.19

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.
@@ -1,6 +1,6 @@
1
1
  import { Router } from 'express';
2
2
  import { InvalidHandler } from './errors.js';
3
- import { UnauthorizedError, BadRequestError } from './httpErrors.js';
3
+ import { HttpError, UnauthorizedError, BadRequestError } from './httpErrors.js';
4
4
  import busboy from 'busboy';
5
5
  function assertValidPath(path) {
6
6
  if (!path.startsWith('/')) {
@@ -136,21 +136,34 @@ export class Handler {
136
136
  */
137
137
  const bb = busboy({ headers: req.headers });
138
138
  bb.on('file', async (_name, file, info) => {
139
- const createdBlob = await handler({
140
- credentials: res.locals.credentials,
141
- secrets: res.locals.secrets,
142
- body: {
143
- file: file,
144
- mimeType: info.mimeType,
145
- encoding: info.encoding,
146
- filename: info.filename,
147
- },
148
- logger: res.locals.logger,
149
- signal: res.locals.signal,
150
- params: req.params,
151
- query: req.query,
152
- });
153
- res.status(201).send(createdBlob);
139
+ try {
140
+ const createdBlob = await handler({
141
+ credentials: res.locals.credentials,
142
+ secrets: res.locals.secrets,
143
+ body: {
144
+ file: file,
145
+ mimeType: info.mimeType,
146
+ encoding: info.encoding,
147
+ filename: info.filename,
148
+ },
149
+ logger: res.locals.logger,
150
+ signal: res.locals.signal,
151
+ params: req.params,
152
+ query: req.query,
153
+ });
154
+ res.status(201).send(createdBlob);
155
+ }
156
+ catch (error) {
157
+ if (error instanceof HttpError) {
158
+ res.status(error.status).send(error);
159
+ }
160
+ else {
161
+ res.status(500).send({ message: `Error creating the blob: ${error}` });
162
+ }
163
+ }
164
+ });
165
+ bb.on('error', error => {
166
+ res.status(500).send({ message: `Error parsing the form data: ${error}` });
154
167
  });
155
168
  req.pipe(bb);
156
169
  });
@@ -46,7 +46,7 @@ export declare class UnprocessableEntityError extends HttpError {
46
46
  constructor(message?: string);
47
47
  }
48
48
  /**
49
- * Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
49
+ * Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
50
50
  * error on the provider's side.
51
51
  */
52
52
  export declare class RateLimitExceededError extends HttpError {
@@ -62,7 +62,7 @@ export class UnprocessableEntityError extends HttpError {
62
62
  }
63
63
  }
64
64
  /**
65
- * Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
65
+ * Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
66
66
  * error on the provider's side.
67
67
  */
68
68
  export class RateLimitExceededError extends HttpError {
@@ -295,7 +295,7 @@ class UnprocessableEntityError extends HttpError {
295
295
  }
296
296
  }
297
297
  /**
298
- * Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
298
+ * Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
299
299
  * error on the provider's side.
300
300
  */
301
301
  class RateLimitExceededError extends HttpError {
@@ -683,21 +683,34 @@ class Handler {
683
683
  */
684
684
  const bb = busboy({ headers: req.headers });
685
685
  bb.on('file', async (_name, file, info) => {
686
- const createdBlob = await handler({
687
- credentials: res.locals.credentials,
688
- secrets: res.locals.secrets,
689
- body: {
690
- file: file,
691
- mimeType: info.mimeType,
692
- encoding: info.encoding,
693
- filename: info.filename,
694
- },
695
- logger: res.locals.logger,
696
- signal: res.locals.signal,
697
- params: req.params,
698
- query: req.query,
699
- });
700
- res.status(201).send(createdBlob);
686
+ try {
687
+ const createdBlob = await handler({
688
+ credentials: res.locals.credentials,
689
+ secrets: res.locals.secrets,
690
+ body: {
691
+ file: file,
692
+ mimeType: info.mimeType,
693
+ encoding: info.encoding,
694
+ filename: info.filename,
695
+ },
696
+ logger: res.locals.logger,
697
+ signal: res.locals.signal,
698
+ params: req.params,
699
+ query: req.query,
700
+ });
701
+ res.status(201).send(createdBlob);
702
+ }
703
+ catch (error) {
704
+ if (error instanceof HttpError) {
705
+ res.status(error.status).send(error);
706
+ }
707
+ else {
708
+ res.status(500).send({ message: `Error creating the blob: ${error}` });
709
+ }
710
+ }
711
+ });
712
+ bb.on('error', error => {
713
+ res.status(500).send({ message: `Error parsing the form data: ${error}` });
701
714
  });
702
715
  req.pipe(bb);
703
716
  });
@@ -1142,7 +1155,10 @@ class Provider {
1142
1155
  response.on('end', () => {
1143
1156
  try {
1144
1157
  const body = JSON.parse(responseBody);
1145
- resolve({ status: 201, headers: response.headers, body });
1158
+ if (body.error) {
1159
+ reject(this.handleError(400, body.error.message));
1160
+ }
1161
+ resolve({ status: 201, headers: response.headers, body: body });
1146
1162
  }
1147
1163
  catch (error) {
1148
1164
  reject(this.handleError(500, `Failed to parse response body: "${error}"`));
@@ -1283,7 +1299,9 @@ class Provider {
1283
1299
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
1284
1300
  // Default to application/json if no Content-Type header is provided
1285
1301
  if (responseContentType && !responseContentType.includes('application/json')) {
1286
- throw this.handleError(500, `Unsupported content-type. Expected 'application/json', got '${responseContentType}'`);
1302
+ const textResult = await response.text();
1303
+ throw this.handleError(500, `Unsupported content-type, expected 'application/json' but got '${responseContentType}'.
1304
+ Original response (${response.status}): ${textResult}`);
1287
1305
  }
1288
1306
  try {
1289
1307
  body = response.body ? await response.json() : undefined;
@@ -136,7 +136,10 @@ export class Provider {
136
136
  response.on('end', () => {
137
137
  try {
138
138
  const body = JSON.parse(responseBody);
139
- resolve({ status: 201, headers: response.headers, body });
139
+ if (body.error) {
140
+ reject(this.handleError(400, body.error.message));
141
+ }
142
+ resolve({ status: 201, headers: response.headers, body: body });
140
143
  }
141
144
  catch (error) {
142
145
  reject(this.handleError(500, `Failed to parse response body: "${error}"`));
@@ -277,7 +280,9 @@ export class Provider {
277
280
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
278
281
  // Default to application/json if no Content-Type header is provided
279
282
  if (responseContentType && !responseContentType.includes('application/json')) {
280
- throw this.handleError(500, `Unsupported content-type. Expected 'application/json', got '${responseContentType}'`);
283
+ const textResult = await response.text();
284
+ throw this.handleError(500, `Unsupported content-type, expected 'application/json' but got '${responseContentType}'.
285
+ Original response (${response.status}): ${textResult}`);
281
286
  }
282
287
  try {
283
288
  body = response.body ? await response.json() : undefined;
@@ -401,7 +401,7 @@ describe('Provider', () => {
401
401
  error = e;
402
402
  }
403
403
  assert.ok(error instanceof HttpErrors.HttpError);
404
- assert.equal(error.message, `Unsupported content-type. Expected 'application/json', got 'application/text'`);
404
+ assert.equal(error.status, 500);
405
405
  });
406
406
  it('throws on status 400', async (context) => {
407
407
  const response = new Response('response body', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unito/integration-sdk",
3
- "version": "1.0.16",
3
+ "version": "1.0.19",
4
4
  "description": "Integration SDK",
5
5
  "type": "module",
6
6
  "types": "dist/src/index.d.ts",
package/src/handler.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Router } from 'express';
2
2
  import * as API from '@unito/integration-api';
3
3
  import { InvalidHandler } from './errors.js';
4
- import { UnauthorizedError, BadRequestError } from './httpErrors.js';
4
+ import { HttpError, UnauthorizedError, BadRequestError } from './httpErrors.js';
5
5
  import {
6
6
  GetBlobContext,
7
7
  GetItemContext,
@@ -351,21 +351,33 @@ export class Handler {
351
351
  */
352
352
  const bb = busboy({ headers: req.headers });
353
353
  bb.on('file', async (_name, file, info: FileInfo) => {
354
- const createdBlob = await handler({
355
- credentials: res.locals.credentials,
356
- secrets: res.locals.secrets,
357
- body: {
358
- file: file,
359
- mimeType: info.mimeType,
360
- encoding: info.encoding,
361
- filename: info.filename,
362
- },
363
- logger: res.locals.logger,
364
- signal: res.locals.signal,
365
- params: req.params,
366
- query: req.query,
367
- });
368
- res.status(201).send(createdBlob);
354
+ try {
355
+ const createdBlob = await handler({
356
+ credentials: res.locals.credentials,
357
+ secrets: res.locals.secrets,
358
+ body: {
359
+ file: file,
360
+ mimeType: info.mimeType,
361
+ encoding: info.encoding,
362
+ filename: info.filename,
363
+ },
364
+ logger: res.locals.logger,
365
+ signal: res.locals.signal,
366
+ params: req.params,
367
+ query: req.query,
368
+ });
369
+ res.status(201).send(createdBlob);
370
+ } catch (error) {
371
+ if (error instanceof HttpError) {
372
+ res.status((error as HttpError).status).send(error);
373
+ } else {
374
+ res.status(500).send({ message: `Error creating the blob: ${error}` });
375
+ }
376
+ }
377
+ });
378
+
379
+ bb.on('error', error => {
380
+ res.status(500).send({ message: `Error parsing the form data: ${error}` });
369
381
  });
370
382
 
371
383
  req.pipe(bb);
package/src/httpErrors.ts CHANGED
@@ -70,7 +70,7 @@ export class UnprocessableEntityError extends HttpError {
70
70
  }
71
71
 
72
72
  /**
73
- * Used to generate a 429 Unprocessable Entity. Usually used when an operation triggers or would trigger a rate limit
73
+ * Used to generate a 429 Rate Limit Exceeded. Usually used when an operation triggers or would trigger a rate limit
74
74
  * error on the provider's side.
75
75
  */
76
76
  export class RateLimitExceededError extends HttpError {
@@ -209,8 +209,11 @@ export class Provider {
209
209
 
210
210
  response.on('end', () => {
211
211
  try {
212
- const body = JSON.parse(responseBody) as T;
213
- resolve({ status: 201, headers: response.headers as unknown as Headers, body });
212
+ const body = JSON.parse(responseBody);
213
+ if (body.error) {
214
+ reject(this.handleError(400, body.error.message));
215
+ }
216
+ resolve({ status: 201, headers: response.headers as unknown as Headers, body: body as T });
214
217
  } catch (error) {
215
218
  reject(this.handleError(500, `Failed to parse response body: "${error}"`));
216
219
  }
@@ -372,9 +375,11 @@ export class Provider {
372
375
  // (Provider's response Content-Type might be more specific, e.g. application/json;charset=utf-8)
373
376
  // Default to application/json if no Content-Type header is provided
374
377
  if (responseContentType && !responseContentType.includes('application/json')) {
378
+ const textResult = await response.text();
375
379
  throw this.handleError(
376
380
  500,
377
- `Unsupported content-type. Expected 'application/json', got '${responseContentType}'`,
381
+ `Unsupported content-type, expected 'application/json' but got '${responseContentType}'.
382
+ Original response (${response.status}): ${textResult}`,
378
383
  );
379
384
  }
380
385
 
@@ -479,7 +479,7 @@ describe('Provider', () => {
479
479
  }
480
480
 
481
481
  assert.ok(error instanceof HttpErrors.HttpError);
482
- assert.equal(error.message, `Unsupported content-type. Expected 'application/json', got 'application/text'`);
482
+ assert.equal(error.status, 500);
483
483
  });
484
484
 
485
485
  it('throws on status 400', async context => {