api-def 0.6.0-alpha3 → 0.6.0-alpha7
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/CHANGELOG.md +7 -2
- package/cjs/ApiUtils.js +1 -0
- package/cjs/Requester.js +1 -5
- package/cjs/UtilTypes.d.ts +4 -4
- package/cjs/backend/AxiosRequestBackend.js +4 -3
- package/cjs/backend/FetchRequestBackend.js +4 -3
- package/esm/ApiUtils.js +1 -0
- package/esm/Requester.js +1 -5
- package/esm/UtilTypes.d.ts +4 -4
- package/esm/backend/AxiosRequestBackend.js +4 -3
- package/esm/backend/FetchRequestBackend.js +4 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Breaking Changes
|
|
4
4
|
|
|
5
|
-
- axios will enforce ResponseType
|
|
5
|
+
- axios will enforce `ResponseType`
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- add `ResponseOf`, `QueryOf`, `BodyOf`, `ParamsOf` to quickly get types from endpoints
|
|
10
|
+
|
|
11
|
+
## Fixes
|
|
8
12
|
|
|
9
13
|
- absolute paths as `baseUrl` is now supported in fetch backend
|
|
14
|
+
- don't re-use same requests based upon URL to allow for concurrent POST requests
|
|
10
15
|
|
|
11
16
|
# 0.5.0
|
|
12
17
|
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -51,6 +51,7 @@ var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
51
51
|
exports.isAcceptableStatus = isAcceptableStatus;
|
|
52
52
|
var JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
53
53
|
var inferResponseType = function (contentType) {
|
|
54
|
+
console.log(contentType);
|
|
54
55
|
var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
|
|
55
56
|
if (contentTypePart && JSON_CONTENT_TYPES.includes(contentTypePart)) {
|
|
56
57
|
return "json";
|
package/cjs/Requester.js
CHANGED
|
@@ -60,7 +60,7 @@ var locks = {};
|
|
|
60
60
|
var runningOperations = {};
|
|
61
61
|
var MOCK_REQUEST_BACKEND = new MockRequestBackend_1.default();
|
|
62
62
|
var submit = function (host, config, mocking) { return __awaiter(void 0, void 0, void 0, function () {
|
|
63
|
-
var computedConfig, backend, context, key,
|
|
63
|
+
var computedConfig, backend, context, key, lock, lockedContext, response, successEventResult, error_1;
|
|
64
64
|
return __generator(this, function (_a) {
|
|
65
65
|
switch (_a.label) {
|
|
66
66
|
case 0:
|
|
@@ -71,10 +71,6 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
71
71
|
}
|
|
72
72
|
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
73
73
|
key = context.key;
|
|
74
|
-
sameRequest = runningOperations[key];
|
|
75
|
-
if (sameRequest) {
|
|
76
|
-
return [2 /*return*/, sameRequest];
|
|
77
|
-
}
|
|
78
74
|
lock = (context.computedConfig || {}).lock;
|
|
79
75
|
if (lock) {
|
|
80
76
|
lockedContext = locks[lock];
|
package/cjs/UtilTypes.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Endpoint from "./Endpoint";
|
|
2
|
-
export declare type ResponseOf<E extends Endpoint
|
|
3
|
-
export declare type ParamsOf<E extends Endpoint
|
|
4
|
-
export declare type QueryOf<E extends Endpoint
|
|
5
|
-
export declare type BodyOf<E extends Endpoint
|
|
2
|
+
export declare type ResponseOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<infer R, any, any, any> ? R : never;
|
|
3
|
+
export declare type ParamsOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, infer P, any, any> ? P : never;
|
|
4
|
+
export declare type QueryOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, infer Q, any> ? Q : never;
|
|
5
|
+
export declare type BodyOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, any, infer B> ? B : never;
|
|
@@ -62,13 +62,14 @@ var AxiosRequestBackend = /** @class */ (function () {
|
|
|
62
62
|
};
|
|
63
63
|
AxiosRequestBackend.prototype.convertResponse = function (context, response) {
|
|
64
64
|
return __awaiter(this, void 0, void 0, function () {
|
|
65
|
-
var inferredResponseType;
|
|
65
|
+
var contentType, inferredResponseType;
|
|
66
66
|
return __generator(this, function (_a) {
|
|
67
|
-
|
|
67
|
+
contentType = response.headers["content-type"];
|
|
68
|
+
inferredResponseType = ApiUtils_1.inferResponseType(contentType);
|
|
68
69
|
// expand to array buffer once we support that in inferResponseType
|
|
69
70
|
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
70
71
|
throw RequestError_1.convertToRequestError({
|
|
71
|
-
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "'"),
|
|
72
|
+
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
|
|
72
73
|
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
73
74
|
});
|
|
74
75
|
}
|
|
@@ -85,16 +85,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
85
85
|
};
|
|
86
86
|
FetchRequestBackend.prototype.convertResponse = function (context, response, error) {
|
|
87
87
|
return __awaiter(this, void 0, void 0, function () {
|
|
88
|
-
var data, inferredResponseType, responseType, _a, error_1, status;
|
|
88
|
+
var data, contentType, inferredResponseType, responseType, _a, error_1, status;
|
|
89
89
|
return __generator(this, function (_b) {
|
|
90
90
|
switch (_b.label) {
|
|
91
91
|
case 0:
|
|
92
|
-
|
|
92
|
+
contentType = response.headers.get("Content-Type");
|
|
93
|
+
inferredResponseType = ApiUtils_1.inferResponseType(contentType);
|
|
93
94
|
responseType = error ? inferredResponseType : context.responseType;
|
|
94
95
|
// expand to array buffer once we support that in inferResponseType
|
|
95
96
|
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
96
97
|
throw RequestError_1.convertToRequestError({
|
|
97
|
-
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "'"),
|
|
98
|
+
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
|
|
98
99
|
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
99
100
|
});
|
|
100
101
|
}
|
package/esm/ApiUtils.js
CHANGED
|
@@ -44,6 +44,7 @@ export var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
44
44
|
};
|
|
45
45
|
var JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
46
46
|
export var inferResponseType = function (contentType) {
|
|
47
|
+
console.log(contentType);
|
|
47
48
|
var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
|
|
48
49
|
if (contentTypePart && JSON_CONTENT_TYPES.includes(contentTypePart)) {
|
|
49
50
|
return "json";
|
package/esm/Requester.js
CHANGED
|
@@ -57,7 +57,7 @@ var locks = {};
|
|
|
57
57
|
var runningOperations = {};
|
|
58
58
|
var MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
59
59
|
export var submit = function (host, config, mocking) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
-
var computedConfig, backend, context, key,
|
|
60
|
+
var computedConfig, backend, context, key, lock, lockedContext, response, successEventResult, error_1;
|
|
61
61
|
return __generator(this, function (_a) {
|
|
62
62
|
switch (_a.label) {
|
|
63
63
|
case 0:
|
|
@@ -68,10 +68,6 @@ export var submit = function (host, config, mocking) { return __awaiter(void 0,
|
|
|
68
68
|
}
|
|
69
69
|
context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
70
70
|
key = context.key;
|
|
71
|
-
sameRequest = runningOperations[key];
|
|
72
|
-
if (sameRequest) {
|
|
73
|
-
return [2 /*return*/, sameRequest];
|
|
74
|
-
}
|
|
75
71
|
lock = (context.computedConfig || {}).lock;
|
|
76
72
|
if (lock) {
|
|
77
73
|
lockedContext = locks[lock];
|
package/esm/UtilTypes.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Endpoint from "./Endpoint";
|
|
2
|
-
export declare type ResponseOf<E extends Endpoint
|
|
3
|
-
export declare type ParamsOf<E extends Endpoint
|
|
4
|
-
export declare type QueryOf<E extends Endpoint
|
|
5
|
-
export declare type BodyOf<E extends Endpoint
|
|
2
|
+
export declare type ResponseOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<infer R, any, any, any> ? R : never;
|
|
3
|
+
export declare type ParamsOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, infer P, any, any> ? P : never;
|
|
4
|
+
export declare type QueryOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, infer Q, any> ? Q : never;
|
|
5
|
+
export declare type BodyOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, any, infer B> ? B : never;
|
|
@@ -58,13 +58,14 @@ var AxiosRequestBackend = /** @class */ (function () {
|
|
|
58
58
|
};
|
|
59
59
|
AxiosRequestBackend.prototype.convertResponse = function (context, response) {
|
|
60
60
|
return __awaiter(this, void 0, void 0, function () {
|
|
61
|
-
var inferredResponseType;
|
|
61
|
+
var contentType, inferredResponseType;
|
|
62
62
|
return __generator(this, function (_a) {
|
|
63
|
-
|
|
63
|
+
contentType = response.headers["content-type"];
|
|
64
|
+
inferredResponseType = inferResponseType(contentType);
|
|
64
65
|
// expand to array buffer once we support that in inferResponseType
|
|
65
66
|
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
66
67
|
throw convertToRequestError({
|
|
67
|
-
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "'"),
|
|
68
|
+
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
|
|
68
69
|
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
69
70
|
});
|
|
70
71
|
}
|
|
@@ -83,16 +83,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
83
83
|
};
|
|
84
84
|
FetchRequestBackend.prototype.convertResponse = function (context, response, error) {
|
|
85
85
|
return __awaiter(this, void 0, void 0, function () {
|
|
86
|
-
var data, inferredResponseType, responseType, _a, error_1, status;
|
|
86
|
+
var data, contentType, inferredResponseType, responseType, _a, error_1, status;
|
|
87
87
|
return __generator(this, function (_b) {
|
|
88
88
|
switch (_b.label) {
|
|
89
89
|
case 0:
|
|
90
|
-
|
|
90
|
+
contentType = response.headers.get("Content-Type");
|
|
91
|
+
inferredResponseType = inferResponseType(contentType);
|
|
91
92
|
responseType = error ? inferredResponseType : context.responseType;
|
|
92
93
|
// expand to array buffer once we support that in inferResponseType
|
|
93
94
|
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
94
95
|
throw convertToRequestError({
|
|
95
|
-
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "'"),
|
|
96
|
+
error: new Error("[api-def] Expected '" + context.responseType + "' response, got '" + inferredResponseType + "' (from 'Content-Type' of '" + contentType + "')"),
|
|
96
97
|
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
97
98
|
});
|
|
98
99
|
}
|