api-def 0.9.0 → 0.9.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.
@@ -34,7 +34,7 @@ var convertToRequestError = function (config) {
34
34
  },
35
35
  });
36
36
  try {
37
- Object.defineProperty(resultError, "message", { value: "Request failed".concat((response === null || response === void 0 ? void 0 : response.status) ? " with status code ".concat(response.status) : "", " (").concat(code, ")") });
37
+ Object.defineProperty(resultError, "message", { value: "Request failed".concat((response === null || response === void 0 ? void 0 : response.status) ? " with status code ".concat(response.status) : "", " [").concat(code, "]: ").concat(resultError.message) });
38
38
  }
39
39
  catch (e) {
40
40
  // ignore
@@ -54,6 +54,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
54
54
  var Utils = require("../Utils");
55
55
  var Utils_1 = require("../Utils");
56
56
  var ApiUtils_1 = require("../ApiUtils");
57
+ var RequestError_1 = require("../RequestError");
57
58
  var FetchError = /** @class */ (function (_super) {
58
59
  __extends(FetchError, _super);
59
60
  function FetchError() {
@@ -86,54 +87,58 @@ var FetchRequestBackend = /** @class */ (function () {
86
87
  });
87
88
  };
88
89
  FetchRequestBackend.prototype.convertResponse = function (context, response) {
90
+ var _a;
89
91
  return __awaiter(this, void 0, void 0, function () {
90
- var contentType, responseType, _a, data, status, headers, error_1, processedHeaders;
92
+ var status, headers, processedHeaders, convertedResponse, responseType, text, data, error_1;
91
93
  return __generator(this, function (_b) {
92
94
  switch (_b.label) {
93
95
  case 0:
94
- contentType = response.headers.get("Content-Type");
95
- responseType = (0, ApiUtils_1.inferResponseType)(contentType);
96
- if (!!response.__text) return [3 /*break*/, 2];
97
- _a = response;
98
- return [4 /*yield*/, response.clone().text()];
96
+ status = response.status, headers = response.headers;
97
+ processedHeaders = {};
98
+ headers.forEach(function (value, key) {
99
+ processedHeaders[key] = value;
100
+ });
101
+ convertedResponse = {
102
+ __lowercaseHeaders: processedHeaders,
103
+ method: context.method,
104
+ url: response.url,
105
+ data: undefined,
106
+ status: status,
107
+ headers: processedHeaders,
108
+ };
109
+ responseType = (_a = context.responseType) !== null && _a !== void 0 ? _a : (0, ApiUtils_1.inferResponseType)(response.headers.get("Content-Type"));
110
+ _b.label = 1;
99
111
  case 1:
100
- _a.__text = _b.sent();
101
- _b.label = 2;
112
+ _b.trys.push([1, 7, , 8]);
113
+ if (!(responseType === "arraybuffer")) return [3 /*break*/, 3];
114
+ return [4 /*yield*/, response.arrayBuffer()];
102
115
  case 2:
103
- data = response.__text;
104
- status = response.status, headers = response.headers;
105
- _b.label = 3;
116
+ data = _b.sent();
117
+ return [3 /*break*/, 6];
106
118
  case 3:
107
- _b.trys.push([3, 7, , 8]);
108
- if (!(responseType === "arraybuffer")) return [3 /*break*/, 5];
109
- return [4 /*yield*/, response.clone().arrayBuffer()];
119
+ if (!(responseType === "json")) return [3 /*break*/, 5];
120
+ return [4 /*yield*/, response.text()];
110
121
  case 4:
111
- data = _b.sent();
122
+ text = _b.sent();
123
+ data = JSON.parse(text);
112
124
  return [3 /*break*/, 6];
113
125
  case 5:
114
- if (responseType === "json") {
115
- data = JSON.parse(response.__text);
116
- }
126
+ data = response.text();
117
127
  _b.label = 6;
118
128
  case 6: return [3 /*break*/, 8];
119
129
  case 7:
120
130
  error_1 = _b.sent();
121
- throw Object.assign(new Error("[api-def] Invalid '".concat(context.responseType, "' response, got: '").concat(response.__text, "'")), {
122
- response: response,
131
+ throw (0, RequestError_1.convertToRequestError)({
132
+ error: Object.assign(new Error("[api-def] Failed to parse response as '".concat(responseType, "'").concat(text ? ", got: ".concat(text) : "")), {
133
+ cause: error_1,
134
+ }),
135
+ code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
136
+ context: context,
137
+ response: convertedResponse,
123
138
  });
124
139
  case 8:
125
- processedHeaders = {};
126
- headers.forEach(function (value, key) {
127
- processedHeaders[key] = value;
128
- });
129
- return [2 /*return*/, {
130
- __lowercaseHeaders: processedHeaders,
131
- method: context.method,
132
- url: response.url,
133
- data: data,
134
- status: status,
135
- headers: processedHeaders,
136
- }];
140
+ convertedResponse.data = data;
141
+ return [2 /*return*/, convertedResponse];
137
142
  }
138
143
  });
139
144
  });
@@ -30,7 +30,7 @@ export const convertToRequestError = (config) => {
30
30
  },
31
31
  });
32
32
  try {
33
- Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} (${code})` });
33
+ Object.defineProperty(resultError, "message", { value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}` });
34
34
  }
35
35
  catch (e) {
36
36
  // ignore
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import * as Utils from "../Utils";
11
11
  import { getGlobal, getGlobalFetch } from "../Utils";
12
12
  import { inferResponseType } from "../ApiUtils";
13
+ import { convertToRequestError, RequestErrorCode } from "../RequestError";
13
14
  class FetchError extends Error {
14
15
  }
15
16
  export default class FetchRequestBackend {
@@ -34,39 +35,48 @@ export default class FetchRequestBackend {
34
35
  });
35
36
  }
36
37
  convertResponse(context, response) {
38
+ var _a;
37
39
  return __awaiter(this, void 0, void 0, function* () {
38
- const contentType = response.headers.get("Content-Type");
39
- const responseType = inferResponseType(contentType);
40
- if (!response.__text) {
41
- response.__text = yield response.clone().text();
42
- }
43
- let data = response.__text;
44
40
  const { status, headers } = response;
45
- try {
46
- if (responseType === "arraybuffer") {
47
- data = yield response.clone().arrayBuffer();
48
- }
49
- else if (responseType === "json") {
50
- data = JSON.parse(response.__text);
51
- }
52
- }
53
- catch (error) {
54
- throw Object.assign(new Error(`[api-def] Invalid '${context.responseType}' response, got: '${response.__text}'`), {
55
- response,
56
- });
57
- }
58
41
  const processedHeaders = {};
59
42
  headers.forEach((value, key) => {
60
43
  processedHeaders[key] = value;
61
44
  });
62
- return {
45
+ const convertedResponse = {
63
46
  __lowercaseHeaders: processedHeaders,
64
47
  method: context.method,
65
48
  url: response.url,
66
- data: data,
49
+ data: undefined,
67
50
  status: status,
68
51
  headers: processedHeaders,
69
52
  };
53
+ const responseType = (_a = context.responseType) !== null && _a !== void 0 ? _a : inferResponseType(response.headers.get("Content-Type"));
54
+ let text;
55
+ let data;
56
+ try {
57
+ if (responseType === "arraybuffer") {
58
+ data = yield response.arrayBuffer();
59
+ }
60
+ else if (responseType === "json") {
61
+ text = yield response.text();
62
+ data = JSON.parse(text);
63
+ }
64
+ else {
65
+ data = response.text();
66
+ }
67
+ }
68
+ catch (error) {
69
+ throw convertToRequestError({
70
+ error: Object.assign(new Error(`[api-def] Failed to parse response as '${responseType}'${text ? `, got: ${text}` : ""}`), {
71
+ cause: error,
72
+ }),
73
+ code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
74
+ context: context,
75
+ response: convertedResponse,
76
+ });
77
+ }
78
+ convertedResponse.data = data;
79
+ return convertedResponse;
70
80
  });
71
81
  }
72
82
  makeRequest(context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",