api-def 0.6.0-alpha10 → 0.6.0-alpha13

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/cjs/ApiUtils.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { AcceptableStatus, ApiResponse, CancelledRequestError } from "./ApiTypes";
1
+ import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
2
2
  import { ResponseType } from "./ApiConstants";
3
3
  export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
4
4
  export declare const isNetworkError: (error: Error) => boolean;
5
- export declare const parseResponseDataToObject: (response: ApiResponse) => void;
6
5
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[] | undefined) => boolean;
7
6
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
package/cjs/ApiUtils.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.inferResponseType = exports.isAcceptableStatus = exports.parseResponseDataToObject = exports.isNetworkError = exports.isCancelledError = void 0;
4
- var TextDecoding_1 = require("./TextDecoding");
3
+ exports.inferResponseType = exports.isAcceptableStatus = exports.isNetworkError = exports.isCancelledError = void 0;
5
4
  var isCancelledError = function (error) {
6
5
  return "isCancelledRequest" in error;
7
6
  };
@@ -13,23 +12,6 @@ var isNetworkError = function (error) {
13
12
  ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
14
13
  };
15
14
  exports.isNetworkError = isNetworkError;
16
- var parseResponseDataToObject = function (response) {
17
- if (response.data &&
18
- typeof response.data === "object") {
19
- var data = response.data;
20
- if (data.constructor && data.constructor.name === "ArrayBuffer") {
21
- try {
22
- var decodedData = (response.data = TextDecoding_1.textDecode(data));
23
- response.data = JSON.parse(decodedData);
24
- }
25
- catch (e) {
26
- // eslint-disable-next-line
27
- console.warn("[api-def] Couldn't parse array buffer content to JSON response", e);
28
- }
29
- }
30
- }
31
- };
32
- exports.parseResponseDataToObject = parseResponseDataToObject;
33
15
  var DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
34
16
  var isAcceptableStatus = function (status, acceptableStatus) {
35
17
  var acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
@@ -50,11 +32,21 @@ var isAcceptableStatus = function (status, acceptableStatus) {
50
32
  return (false);
51
33
  };
52
34
  exports.isAcceptableStatus = isAcceptableStatus;
35
+ var TEXT_CONTENT_TYPES = ["text/plain"];
53
36
  var JSON_CONTENT_TYPES = ["text/json", "application/json"];
37
+ var ARRRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
54
38
  var inferResponseType = function (contentType) {
55
39
  var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
56
- if (contentTypePart && JSON_CONTENT_TYPES.includes(contentTypePart)) {
57
- return "json";
40
+ if (contentTypePart) {
41
+ if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
42
+ return "text";
43
+ }
44
+ else if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
45
+ return "json";
46
+ }
47
+ else if (ARRRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
48
+ return "arraybuffer";
49
+ }
58
50
  }
59
51
  return "text";
60
52
  };
package/cjs/Requester.js CHANGED
@@ -56,6 +56,7 @@ var ApiConstants_1 = require("./ApiConstants");
56
56
  var retry_1 = require("./util/retry");
57
57
  var MockRequestBackend_1 = require("./backend/MockRequestBackend");
58
58
  var RequestError_1 = require("./RequestError");
59
+ var TextDecoding_1 = require("./TextDecoding");
59
60
  var locks = {};
60
61
  var runningOperations = {};
61
62
  var MOCK_REQUEST_BACKEND = new MockRequestBackend_1.default();
@@ -174,10 +175,6 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
174
175
  error = _b.sent();
175
176
  context.error = error;
176
177
  context.response = error.response;
177
- // transform array buffer responses to objs
178
- if (context.response) {
179
- ApiUtils.parseResponseDataToObject(context.response);
180
- }
181
178
  return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.Error)];
182
179
  case 6:
183
180
  errorEventResult = _b.sent();
@@ -214,16 +211,50 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
214
211
  });
215
212
  }); };
216
213
  var parseResponse = function (context, response, error) { return __awaiter(void 0, void 0, void 0, function () {
217
- var parsedResponse;
218
- return __generator(this, function (_a) {
219
- switch (_a.label) {
214
+ var parsedResponse, contentType, inferredResponseType, data, decodedData;
215
+ var _a;
216
+ return __generator(this, function (_b) {
217
+ switch (_b.label) {
220
218
  case 0:
221
219
  if (!response) return [3 /*break*/, 2];
222
- return [4 /*yield*/, context.backend.convertResponse(context, response, error)];
220
+ return [4 /*yield*/, context.backend.convertResponse(context, response)];
223
221
  case 1:
224
- parsedResponse = _a.sent();
225
- if (parsedResponse) {
226
- ApiUtils.parseResponseDataToObject(parsedResponse);
222
+ parsedResponse = _b.sent();
223
+ // lowercase all header names
224
+ response.headers = Object.keys(response.headers).reduce(function (headers, header) {
225
+ headers[header.toLowerCase()] = response.headers[header];
226
+ }, {});
227
+ contentType = response.headers["Content-Type"];
228
+ inferredResponseType = ApiUtils_1.inferResponseType(contentType);
229
+ if (!error) {
230
+ // expand to array buffer once we support that in inferResponseType
231
+ if (inferredResponseType === "text" && context.responseType === "json") {
232
+ throw RequestError_1.convertToRequestError({
233
+ error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
234
+ code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
235
+ response: parsedResponse,
236
+ });
237
+ }
238
+ // transform arrayBuffer to json
239
+ if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
240
+ if (parsedResponse.data &&
241
+ typeof parsedResponse.data === "object") {
242
+ data = response.data;
243
+ if (((_a = data.constructor) === null || _a === void 0 ? void 0 : _a.name) === "ArrayBuffer") {
244
+ try {
245
+ decodedData = (response.data = TextDecoding_1.textDecode(data));
246
+ response.data = JSON.parse(decodedData);
247
+ }
248
+ catch (e) {
249
+ throw RequestError_1.convertToRequestError({
250
+ error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
251
+ code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
252
+ response: parsedResponse,
253
+ });
254
+ }
255
+ }
256
+ }
257
+ }
227
258
  }
228
259
  return [2 /*return*/, parsedResponse];
229
260
  case 2: return [2 /*return*/, response];
@@ -37,8 +37,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.isAxiosError = void 0;
40
- var ApiUtils_1 = require("../ApiUtils");
41
- var RequestError_1 = require("../RequestError");
42
40
  var axios;
43
41
  var isAxiosError = function (error) {
44
42
  return "isAxiosError" in error;
@@ -62,18 +60,7 @@ var AxiosRequestBackend = /** @class */ (function () {
62
60
  };
63
61
  AxiosRequestBackend.prototype.convertResponse = function (context, response) {
64
62
  return __awaiter(this, void 0, void 0, function () {
65
- var contentType, inferredResponseType;
66
63
  return __generator(this, function (_a) {
67
- contentType = response.headers["content-type"];
68
- inferredResponseType = ApiUtils_1.inferResponseType(contentType);
69
- // expand to array buffer once we support that in inferResponseType
70
- if (inferredResponseType === "text" && context.responseType === "json") {
71
- throw RequestError_1.convertToRequestError({
72
- error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
73
- code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
74
- response: response,
75
- });
76
- }
77
64
  return [2 /*return*/, response];
78
65
  });
79
66
  });
@@ -9,7 +9,7 @@ export default class FetchRequestBackend implements RequestBackend<Response> {
9
9
  extractResponseFromError(error: Error): Promise<Response | null | undefined>;
10
10
  convertResponse<T>(context: RequestContext, response: Response & {
11
11
  __text?: string;
12
- }, error?: boolean): Promise<ApiResponse<T>>;
12
+ }): Promise<ApiResponse<T>>;
13
13
  makeRequest(context: RequestContext): RequestOperation<Response>;
14
14
  getErrorInfo(error: Error, response: ApiResponse | undefined | null): RequestBackendErrorInfo | undefined;
15
15
  }
@@ -55,7 +55,6 @@ var Utils = require("../Utils");
55
55
  var Utils_1 = require("../Utils");
56
56
  var ApiConstants_1 = require("../ApiConstants");
57
57
  var ApiUtils_1 = require("../ApiUtils");
58
- var RequestError_1 = require("../RequestError");
59
58
  var FetchError = /** @class */ (function (_super) {
60
59
  __extends(FetchError, _super);
61
60
  function FetchError() {
@@ -83,15 +82,14 @@ var FetchRequestBackend = /** @class */ (function () {
83
82
  });
84
83
  });
85
84
  };
86
- FetchRequestBackend.prototype.convertResponse = function (context, response, error) {
85
+ FetchRequestBackend.prototype.convertResponse = function (context, response) {
87
86
  return __awaiter(this, void 0, void 0, function () {
88
- var data, contentType, inferredResponseType, responseType, _a, status, headers, error_1;
87
+ var contentType, responseType, _a, data, status, headers, error_1;
89
88
  return __generator(this, function (_b) {
90
89
  switch (_b.label) {
91
90
  case 0:
92
91
  contentType = response.headers.get("Content-Type");
93
- inferredResponseType = ApiUtils_1.inferResponseType(contentType);
94
- responseType = error ? inferredResponseType : context.responseType;
92
+ responseType = ApiUtils_1.inferResponseType(contentType);
95
93
  if (!!response.__text) return [3 /*break*/, 2];
96
94
  _a = response;
97
95
  return [4 /*yield*/, response.clone().text()];
@@ -99,43 +97,28 @@ var FetchRequestBackend = /** @class */ (function () {
99
97
  _a.__text = _b.sent();
100
98
  _b.label = 2;
101
99
  case 2:
100
+ data = response.__text;
102
101
  status = response.status, headers = response.headers;
103
- // expand to array buffer once we support that in inferResponseType
104
- if (inferredResponseType === "text" && context.responseType === "json") {
105
- throw RequestError_1.convertToRequestError({
106
- error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
107
- code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
108
- response: {
109
- data: response.__text,
110
- status: status,
111
- headers: headers,
112
- },
113
- });
114
- }
115
102
  _b.label = 3;
116
103
  case 3:
117
- _b.trys.push([3, 8, , 9]);
118
- if (!(responseType === ApiConstants_1.ResponseType.Text)) return [3 /*break*/, 4];
119
- data = response.__text;
120
- return [3 /*break*/, 7];
121
- case 4:
122
- if (!(responseType === ApiConstants_1.ResponseType.ArrayBuffer)) return [3 /*break*/, 6];
104
+ _b.trys.push([3, 7, , 8]);
105
+ if (!(responseType === ApiConstants_1.ResponseType.ArrayBuffer)) return [3 /*break*/, 5];
123
106
  return [4 /*yield*/, response.clone().arrayBuffer()];
124
- case 5:
107
+ case 4:
125
108
  data = _b.sent();
126
- return [3 /*break*/, 7];
127
- case 6:
109
+ return [3 /*break*/, 6];
110
+ case 5:
128
111
  if (responseType === ApiConstants_1.ResponseType.Json) {
129
112
  data = JSON.parse(response.__text);
130
113
  }
131
- _b.label = 7;
132
- case 7: return [3 /*break*/, 9];
133
- case 8:
114
+ _b.label = 6;
115
+ case 6: return [3 /*break*/, 8];
116
+ case 7:
134
117
  error_1 = _b.sent();
135
118
  throw Object.assign(new Error("[api-def] Invalid '" + context.responseType + "' response, got: '" + response.__text + "'"), {
136
119
  response: response,
137
120
  });
138
- case 9: return [2 /*return*/, {
121
+ case 8: return [2 /*return*/, {
139
122
  data: data,
140
123
  status: status,
141
124
  headers: headers,
@@ -10,7 +10,7 @@ export interface RequestBackendErrorInfo {
10
10
  export default interface RequestBackend<R = any> {
11
11
  readonly id: string;
12
12
  makeRequest(context: RequestContext): RequestOperation<R>;
13
- convertResponse<T>(context: RequestContext, response: R, error?: boolean): Promise<ApiResponse<T>>;
13
+ convertResponse<T>(context: RequestContext, response: R): Promise<ApiResponse<T>>;
14
14
  extractResponseFromError(error: Error): Promise<R | null | undefined>;
15
15
  getErrorInfo(error: Error, response: ApiResponse | undefined | null): RequestBackendErrorInfo | undefined;
16
16
  }
package/esm/ApiUtils.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { AcceptableStatus, ApiResponse, CancelledRequestError } from "./ApiTypes";
1
+ import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
2
2
  import { ResponseType } from "./ApiConstants";
3
3
  export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
4
4
  export declare const isNetworkError: (error: Error) => boolean;
5
- export declare const parseResponseDataToObject: (response: ApiResponse) => void;
6
5
  export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[] | undefined) => boolean;
7
6
  export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
package/esm/ApiUtils.js CHANGED
@@ -1,4 +1,3 @@
1
- import { textDecode } from "./TextDecoding";
2
1
  export var isCancelledError = function (error) {
3
2
  return "isCancelledRequest" in error;
4
3
  };
@@ -8,22 +7,6 @@ export var isNetworkError = function (error) {
8
7
  error.message === "Network Error" ||
9
8
  ((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
10
9
  };
11
- export var parseResponseDataToObject = function (response) {
12
- if (response.data &&
13
- typeof response.data === "object") {
14
- var data = response.data;
15
- if (data.constructor && data.constructor.name === "ArrayBuffer") {
16
- try {
17
- var decodedData = (response.data = textDecode(data));
18
- response.data = JSON.parse(decodedData);
19
- }
20
- catch (e) {
21
- // eslint-disable-next-line
22
- console.warn("[api-def] Couldn't parse array buffer content to JSON response", e);
23
- }
24
- }
25
- }
26
- };
27
10
  var DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
28
11
  export var isAcceptableStatus = function (status, acceptableStatus) {
29
12
  var acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
@@ -43,11 +26,21 @@ export var isAcceptableStatus = function (status, acceptableStatus) {
43
26
  }
44
27
  return (false);
45
28
  };
29
+ var TEXT_CONTENT_TYPES = ["text/plain"];
46
30
  var JSON_CONTENT_TYPES = ["text/json", "application/json"];
31
+ var ARRRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
47
32
  export var inferResponseType = function (contentType) {
48
33
  var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
49
- if (contentTypePart && JSON_CONTENT_TYPES.includes(contentTypePart)) {
50
- return "json";
34
+ if (contentTypePart) {
35
+ if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
36
+ return "text";
37
+ }
38
+ else if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
39
+ return "json";
40
+ }
41
+ else if (ARRRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
42
+ return "arraybuffer";
43
+ }
51
44
  }
52
45
  return "text";
53
46
  };
package/esm/Requester.js CHANGED
@@ -46,13 +46,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  }
47
47
  };
48
48
  import * as ApiUtils from "./ApiUtils";
49
- import { isAcceptableStatus, isNetworkError } from "./ApiUtils";
49
+ import { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
50
50
  import RequestContext from "./RequestContext";
51
51
  import * as Api from "./Api";
52
52
  import { EventResultType, RequestEvent } from "./ApiConstants";
53
53
  import retry from "./util/retry";
54
54
  import MockRequestBackend from "./backend/MockRequestBackend";
55
55
  import { convertToRequestError, isRequestError, RequestErrorCode } from "./RequestError";
56
+ import { textDecode } from "./TextDecoding";
56
57
  var locks = {};
57
58
  var runningOperations = {};
58
59
  var MOCK_REQUEST_BACKEND = new MockRequestBackend();
@@ -170,10 +171,6 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
170
171
  error = _b.sent();
171
172
  context.error = error;
172
173
  context.response = error.response;
173
- // transform array buffer responses to objs
174
- if (context.response) {
175
- ApiUtils.parseResponseDataToObject(context.response);
176
- }
177
174
  return [4 /*yield*/, context.triggerEvent(RequestEvent.Error)];
178
175
  case 6:
179
176
  errorEventResult = _b.sent();
@@ -210,16 +207,50 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
210
207
  });
211
208
  }); };
212
209
  var parseResponse = function (context, response, error) { return __awaiter(void 0, void 0, void 0, function () {
213
- var parsedResponse;
214
- return __generator(this, function (_a) {
215
- switch (_a.label) {
210
+ var parsedResponse, contentType, inferredResponseType, data, decodedData;
211
+ var _a;
212
+ return __generator(this, function (_b) {
213
+ switch (_b.label) {
216
214
  case 0:
217
215
  if (!response) return [3 /*break*/, 2];
218
- return [4 /*yield*/, context.backend.convertResponse(context, response, error)];
216
+ return [4 /*yield*/, context.backend.convertResponse(context, response)];
219
217
  case 1:
220
- parsedResponse = _a.sent();
221
- if (parsedResponse) {
222
- ApiUtils.parseResponseDataToObject(parsedResponse);
218
+ parsedResponse = _b.sent();
219
+ // lowercase all header names
220
+ response.headers = Object.keys(response.headers).reduce(function (headers, header) {
221
+ headers[header.toLowerCase()] = response.headers[header];
222
+ }, {});
223
+ contentType = response.headers["Content-Type"];
224
+ inferredResponseType = inferResponseType(contentType);
225
+ if (!error) {
226
+ // expand to array buffer once we support that in inferResponseType
227
+ if (inferredResponseType === "text" && context.responseType === "json") {
228
+ throw convertToRequestError({
229
+ error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
230
+ code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
231
+ response: parsedResponse,
232
+ });
233
+ }
234
+ // transform arrayBuffer to json
235
+ if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
236
+ if (parsedResponse.data &&
237
+ typeof parsedResponse.data === "object") {
238
+ data = response.data;
239
+ if (((_a = data.constructor) === null || _a === void 0 ? void 0 : _a.name) === "ArrayBuffer") {
240
+ try {
241
+ decodedData = (response.data = textDecode(data));
242
+ response.data = JSON.parse(decodedData);
243
+ }
244
+ catch (e) {
245
+ throw convertToRequestError({
246
+ error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
247
+ code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
248
+ response: parsedResponse,
249
+ });
250
+ }
251
+ }
252
+ }
253
+ }
223
254
  }
224
255
  return [2 /*return*/, parsedResponse];
225
256
  case 2: return [2 /*return*/, response];
@@ -34,8 +34,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import { inferResponseType } from "../ApiUtils";
38
- import { convertToRequestError, RequestErrorCode } from "../RequestError";
39
37
  var axios;
40
38
  export var isAxiosError = function (error) {
41
39
  return "isAxiosError" in error;
@@ -58,18 +56,7 @@ var AxiosRequestBackend = /** @class */ (function () {
58
56
  };
59
57
  AxiosRequestBackend.prototype.convertResponse = function (context, response) {
60
58
  return __awaiter(this, void 0, void 0, function () {
61
- var contentType, inferredResponseType;
62
59
  return __generator(this, function (_a) {
63
- contentType = response.headers["content-type"];
64
- inferredResponseType = inferResponseType(contentType);
65
- // expand to array buffer once we support that in inferResponseType
66
- if (inferredResponseType === "text" && context.responseType === "json") {
67
- throw convertToRequestError({
68
- error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
69
- code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
70
- response: response,
71
- });
72
- }
73
60
  return [2 /*return*/, response];
74
61
  });
75
62
  });
@@ -9,7 +9,7 @@ export default class FetchRequestBackend implements RequestBackend<Response> {
9
9
  extractResponseFromError(error: Error): Promise<Response | null | undefined>;
10
10
  convertResponse<T>(context: RequestContext, response: Response & {
11
11
  __text?: string;
12
- }, error?: boolean): Promise<ApiResponse<T>>;
12
+ }): Promise<ApiResponse<T>>;
13
13
  makeRequest(context: RequestContext): RequestOperation<Response>;
14
14
  getErrorInfo(error: Error, response: ApiResponse | undefined | null): RequestBackendErrorInfo | undefined;
15
15
  }
@@ -53,7 +53,6 @@ import * as Utils from "../Utils";
53
53
  import { getGlobalFetch } from "../Utils";
54
54
  import { ResponseType } from "../ApiConstants";
55
55
  import { inferResponseType } from "../ApiUtils";
56
- import { convertToRequestError, RequestErrorCode } from "../RequestError";
57
56
  var FetchError = /** @class */ (function (_super) {
58
57
  __extends(FetchError, _super);
59
58
  function FetchError() {
@@ -81,15 +80,14 @@ var FetchRequestBackend = /** @class */ (function () {
81
80
  });
82
81
  });
83
82
  };
84
- FetchRequestBackend.prototype.convertResponse = function (context, response, error) {
83
+ FetchRequestBackend.prototype.convertResponse = function (context, response) {
85
84
  return __awaiter(this, void 0, void 0, function () {
86
- var data, contentType, inferredResponseType, responseType, _a, status, headers, error_1;
85
+ var contentType, responseType, _a, data, status, headers, error_1;
87
86
  return __generator(this, function (_b) {
88
87
  switch (_b.label) {
89
88
  case 0:
90
89
  contentType = response.headers.get("Content-Type");
91
- inferredResponseType = inferResponseType(contentType);
92
- responseType = error ? inferredResponseType : context.responseType;
90
+ responseType = inferResponseType(contentType);
93
91
  if (!!response.__text) return [3 /*break*/, 2];
94
92
  _a = response;
95
93
  return [4 /*yield*/, response.clone().text()];
@@ -97,43 +95,28 @@ var FetchRequestBackend = /** @class */ (function () {
97
95
  _a.__text = _b.sent();
98
96
  _b.label = 2;
99
97
  case 2:
98
+ data = response.__text;
100
99
  status = response.status, headers = response.headers;
101
- // expand to array buffer once we support that in inferResponseType
102
- if (inferredResponseType === "text" && context.responseType === "json") {
103
- throw convertToRequestError({
104
- error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
105
- code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
106
- response: {
107
- data: response.__text,
108
- status: status,
109
- headers: headers,
110
- },
111
- });
112
- }
113
100
  _b.label = 3;
114
101
  case 3:
115
- _b.trys.push([3, 8, , 9]);
116
- if (!(responseType === ResponseType.Text)) return [3 /*break*/, 4];
117
- data = response.__text;
118
- return [3 /*break*/, 7];
119
- case 4:
120
- if (!(responseType === ResponseType.ArrayBuffer)) return [3 /*break*/, 6];
102
+ _b.trys.push([3, 7, , 8]);
103
+ if (!(responseType === ResponseType.ArrayBuffer)) return [3 /*break*/, 5];
121
104
  return [4 /*yield*/, response.clone().arrayBuffer()];
122
- case 5:
105
+ case 4:
123
106
  data = _b.sent();
124
- return [3 /*break*/, 7];
125
- case 6:
107
+ return [3 /*break*/, 6];
108
+ case 5:
126
109
  if (responseType === ResponseType.Json) {
127
110
  data = JSON.parse(response.__text);
128
111
  }
129
- _b.label = 7;
130
- case 7: return [3 /*break*/, 9];
131
- case 8:
112
+ _b.label = 6;
113
+ case 6: return [3 /*break*/, 8];
114
+ case 7:
132
115
  error_1 = _b.sent();
133
116
  throw Object.assign(new Error("[api-def] Invalid '" + context.responseType + "' response, got: '" + response.__text + "'"), {
134
117
  response: response,
135
118
  });
136
- case 9: return [2 /*return*/, {
119
+ case 8: return [2 /*return*/, {
137
120
  data: data,
138
121
  status: status,
139
122
  headers: headers,
@@ -10,7 +10,7 @@ export interface RequestBackendErrorInfo {
10
10
  export default interface RequestBackend<R = any> {
11
11
  readonly id: string;
12
12
  makeRequest(context: RequestContext): RequestOperation<R>;
13
- convertResponse<T>(context: RequestContext, response: R, error?: boolean): Promise<ApiResponse<T>>;
13
+ convertResponse<T>(context: RequestContext, response: R): Promise<ApiResponse<T>>;
14
14
  extractResponseFromError(error: Error): Promise<R | null | undefined>;
15
15
  getErrorInfo(error: Error, response: ApiResponse | undefined | null): RequestBackendErrorInfo | undefined;
16
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-def",
3
- "version": "0.6.0-alpha10",
3
+ "version": "0.6.0-alpha13",
4
4
  "description": "Typed API definitions with middleware support",
5
5
  "main": "cjs/index.js",
6
6
  "types": "esm/index.d.ts",