api-def 0.7.1-alpha.1 → 0.7.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.
- package/LICENSE +20 -20
- package/README.md +34 -34
- package/cjs/ApiUtils.js +2 -2
- package/cjs/RequestContext.js +1 -1
- package/cjs/backend/FetchRequestBackend.js +12 -6
- package/esm/ApiUtils.js +2 -2
- package/esm/RequestContext.js +1 -1
- package/esm/backend/FetchRequestBackend.js +12 -6
- package/package.json +76 -76
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2019 James Waterhouse
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 James Waterhouse
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
# [api-def](https://github.com/Censkh/api-def/) · [](https://github.com/Censkh/api-def/blob/master/LICENSE) [](https://www.npmjs.com/package/api-def) [](https://github.com/Censkh/api-def/actions)
|
|
2
|
-
|
|
3
|
-
Typed APIs with middleware support
|
|
4
|
-
|
|
5
|
-
API def provides a unified way to type your endpoints allowing for compile time checking of query, body, response and even url parameters
|
|
6
|
-
|
|
7
|
-
``` npm i api-def ```
|
|
8
|
-
|
|
9
|
-
- [Documentation](https://censkh.github.io/api-def/)
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
import {Api} from "api-def";
|
|
13
|
-
|
|
14
|
-
const api = new Api({
|
|
15
|
-
baseUrl: "https://my-api/",
|
|
16
|
-
name: "My API",
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const fetchData = api.endpoint()
|
|
20
|
-
.queryOf<{ includeAwesome: boolean; }>()
|
|
21
|
-
.responseOf<{ data: {awesome: boolean; } }>()
|
|
22
|
-
.build({
|
|
23
|
-
id: "fetch_data",
|
|
24
|
-
method: "get",
|
|
25
|
-
path: "/data"
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
// calls GET https://my-api/data?includeAwesome=true
|
|
29
|
-
const res = await fetchData.submit({
|
|
30
|
-
query: {includeAwesome: true}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
console.log(res.data); // { data: { awesome: true } }
|
|
34
|
-
```
|
|
1
|
+
# [api-def](https://github.com/Censkh/api-def/) · [](https://github.com/Censkh/api-def/blob/master/LICENSE) [](https://www.npmjs.com/package/api-def) [](https://github.com/Censkh/api-def/actions)
|
|
2
|
+
|
|
3
|
+
Typed APIs with middleware support
|
|
4
|
+
|
|
5
|
+
API def provides a unified way to type your endpoints allowing for compile time checking of query, body, response and even url parameters
|
|
6
|
+
|
|
7
|
+
``` npm i api-def ```
|
|
8
|
+
|
|
9
|
+
- [Documentation](https://censkh.github.io/api-def/)
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import {Api} from "api-def";
|
|
13
|
+
|
|
14
|
+
const api = new Api({
|
|
15
|
+
baseUrl: "https://my-api/",
|
|
16
|
+
name: "My API",
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const fetchData = api.endpoint()
|
|
20
|
+
.queryOf<{ includeAwesome: boolean; }>()
|
|
21
|
+
.responseOf<{ data: {awesome: boolean; } }>()
|
|
22
|
+
.build({
|
|
23
|
+
id: "fetch_data",
|
|
24
|
+
method: "get",
|
|
25
|
+
path: "/data"
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// calls GET https://my-api/data?includeAwesome=true
|
|
29
|
+
const res = await fetchData.submit({
|
|
30
|
+
query: {includeAwesome: true}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(res.data); // { data: { awesome: true } }
|
|
34
|
+
```
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -34,7 +34,7 @@ var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
34
34
|
exports.isAcceptableStatus = isAcceptableStatus;
|
|
35
35
|
var TEXT_CONTENT_TYPES = ["text/plain"];
|
|
36
36
|
var JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
37
|
-
var
|
|
37
|
+
var ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
|
|
38
38
|
var inferResponseType = function (contentType) {
|
|
39
39
|
var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
|
|
40
40
|
if (contentTypePart) {
|
|
@@ -44,7 +44,7 @@ var inferResponseType = function (contentType) {
|
|
|
44
44
|
else if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
|
|
45
45
|
return "json";
|
|
46
46
|
}
|
|
47
|
-
else if (
|
|
47
|
+
else if (ARRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
|
|
48
48
|
return "arraybuffer";
|
|
49
49
|
}
|
|
50
50
|
}
|
package/cjs/RequestContext.js
CHANGED
|
@@ -129,7 +129,7 @@ var RequestContext = /** @class */ (function () {
|
|
|
129
129
|
var _a;
|
|
130
130
|
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
131
131
|
var contentTypeKey = Object.keys(this.computedConfig.headers).find(function (key) { return key.toLowerCase() === "content-type"; });
|
|
132
|
-
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString());
|
|
132
|
+
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
133
133
|
if (contentType === "application/x-www-form-urlencoded") {
|
|
134
134
|
var searchParams = new URLSearchParams();
|
|
135
135
|
for (var _i = 0, _b = Object.keys(this.computedConfig.body); _i < _b.length; _i++) {
|
|
@@ -84,7 +84,7 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
84
84
|
};
|
|
85
85
|
FetchRequestBackend.prototype.convertResponse = function (context, response) {
|
|
86
86
|
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
-
var contentType, responseType, _a, data, status, headers, error_1;
|
|
87
|
+
var contentType, responseType, _a, data, status, headers, error_1, processedHeaders;
|
|
88
88
|
return __generator(this, function (_b) {
|
|
89
89
|
switch (_b.label) {
|
|
90
90
|
case 0:
|
|
@@ -118,11 +118,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
118
118
|
throw Object.assign(new Error("[api-def] Invalid '".concat(context.responseType, "' response, got: '").concat(response.__text, "'")), {
|
|
119
119
|
response: response,
|
|
120
120
|
});
|
|
121
|
-
case 8:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
121
|
+
case 8:
|
|
122
|
+
processedHeaders = {};
|
|
123
|
+
headers.forEach(function (value, key) {
|
|
124
|
+
processedHeaders[key] = value;
|
|
125
|
+
});
|
|
126
|
+
return [2 /*return*/, {
|
|
127
|
+
__lowercaseHeaders: processedHeaders,
|
|
128
|
+
data: data,
|
|
129
|
+
status: status,
|
|
130
|
+
headers: processedHeaders,
|
|
131
|
+
}];
|
|
126
132
|
}
|
|
127
133
|
});
|
|
128
134
|
});
|
package/esm/ApiUtils.js
CHANGED
|
@@ -28,7 +28,7 @@ export var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
28
28
|
};
|
|
29
29
|
var TEXT_CONTENT_TYPES = ["text/plain"];
|
|
30
30
|
var JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
31
|
-
var
|
|
31
|
+
var ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
|
|
32
32
|
export var inferResponseType = function (contentType) {
|
|
33
33
|
var contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
|
|
34
34
|
if (contentTypePart) {
|
|
@@ -38,7 +38,7 @@ export var inferResponseType = function (contentType) {
|
|
|
38
38
|
else if (JSON_CONTENT_TYPES.includes(contentTypePart)) {
|
|
39
39
|
return "json";
|
|
40
40
|
}
|
|
41
|
-
else if (
|
|
41
|
+
else if (ARRAY_BUFFER_CONTENT_TYPES.includes(contentTypePart)) {
|
|
42
42
|
return "arraybuffer";
|
|
43
43
|
}
|
|
44
44
|
}
|
package/esm/RequestContext.js
CHANGED
|
@@ -127,7 +127,7 @@ var RequestContext = /** @class */ (function () {
|
|
|
127
127
|
var _a;
|
|
128
128
|
if (this.computedConfig.body && this.computedConfig.headers) {
|
|
129
129
|
var contentTypeKey = Object.keys(this.computedConfig.headers).find(function (key) { return key.toLowerCase() === "content-type"; });
|
|
130
|
-
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString());
|
|
130
|
+
var contentType = contentTypeKey && ((_a = this.computedConfig.headers[contentTypeKey]) === null || _a === void 0 ? void 0 : _a.toString().split(";")[0].trim());
|
|
131
131
|
if (contentType === "application/x-www-form-urlencoded") {
|
|
132
132
|
var searchParams = new URLSearchParams();
|
|
133
133
|
for (var _i = 0, _b = Object.keys(this.computedConfig.body); _i < _b.length; _i++) {
|
|
@@ -82,7 +82,7 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
82
82
|
};
|
|
83
83
|
FetchRequestBackend.prototype.convertResponse = function (context, response) {
|
|
84
84
|
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
-
var contentType, responseType, _a, data, status, headers, error_1;
|
|
85
|
+
var contentType, responseType, _a, data, status, headers, error_1, processedHeaders;
|
|
86
86
|
return __generator(this, function (_b) {
|
|
87
87
|
switch (_b.label) {
|
|
88
88
|
case 0:
|
|
@@ -116,11 +116,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
116
116
|
throw Object.assign(new Error("[api-def] Invalid '".concat(context.responseType, "' response, got: '").concat(response.__text, "'")), {
|
|
117
117
|
response: response,
|
|
118
118
|
});
|
|
119
|
-
case 8:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
119
|
+
case 8:
|
|
120
|
+
processedHeaders = {};
|
|
121
|
+
headers.forEach(function (value, key) {
|
|
122
|
+
processedHeaders[key] = value;
|
|
123
|
+
});
|
|
124
|
+
return [2 /*return*/, {
|
|
125
|
+
__lowercaseHeaders: processedHeaders,
|
|
126
|
+
data: data,
|
|
127
|
+
status: status,
|
|
128
|
+
headers: processedHeaders,
|
|
129
|
+
}];
|
|
124
130
|
}
|
|
125
131
|
});
|
|
126
132
|
});
|
package/package.json
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "api-def",
|
|
3
|
-
"version": "0.7.
|
|
4
|
-
"description": "Typed API definitions with middleware support",
|
|
5
|
-
"main": "cjs/index.js",
|
|
6
|
-
"types": "esm/index.d.ts",
|
|
7
|
-
"module": "esm/index.js",
|
|
8
|
-
"sideEffects": false,
|
|
9
|
-
"scripts": {
|
|
10
|
-
"prepublishOnly": "npm run test && npm run build",
|
|
11
|
-
"test": "npm run test:types && npm run test:lint && npm run test:unit",
|
|
12
|
-
"test:unit": "ava",
|
|
13
|
-
"test:lint": "npm run lint:strict",
|
|
14
|
-
"test:types": "tsc --noEmit -p .",
|
|
15
|
-
"example:start": "cd example && npm run start",
|
|
16
|
-
"lint": "esw --ext .tsx,.ts src",
|
|
17
|
-
"lint:watch": "npm run lint -- --watch",
|
|
18
|
-
"lint:fix": "npm run lint -- --fix",
|
|
19
|
-
"lint:strict": "npm run lint -- --max-warnings 0",
|
|
20
|
-
"cleanup": "rimraf esm && rimraf cjs",
|
|
21
|
-
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
22
|
-
"build:esm": "tsc --module es2015 --target es5 --outDir esm --preserveWatchOutput",
|
|
23
|
-
"build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput",
|
|
24
|
-
"build:watch": "npm-run-all -p \"build:esm -- -w\" \"build:cjs -- -w\" \"lint:watch\"",
|
|
25
|
-
"website:dev": "cd website && npm run start",
|
|
26
|
-
"website:deploy": "cd website && npm run deploy"
|
|
27
|
-
},
|
|
28
|
-
"keywords": [
|
|
29
|
-
"typescript",
|
|
30
|
-
"javascript",
|
|
31
|
-
"node",
|
|
32
|
-
"web",
|
|
33
|
-
"api",
|
|
34
|
-
"typed",
|
|
35
|
-
"cache",
|
|
36
|
-
"fetch",
|
|
37
|
-
"retry",
|
|
38
|
-
"middleware"
|
|
39
|
-
],
|
|
40
|
-
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
41
|
-
"license": "MIT",
|
|
42
|
-
"files": [
|
|
43
|
-
"LICENSE",
|
|
44
|
-
"README.md",
|
|
45
|
-
"esm/",
|
|
46
|
-
"cjs/"
|
|
47
|
-
],
|
|
48
|
-
"repository": "https://github.com/Censkh/api-def",
|
|
49
|
-
"ava": {
|
|
50
|
-
"extensions": [
|
|
51
|
-
"ts"
|
|
52
|
-
],
|
|
53
|
-
"files": [
|
|
54
|
-
"src/tests/**/*.test.ts"
|
|
55
|
-
],
|
|
56
|
-
"nodeArguments": [
|
|
57
|
-
"--require=@esbuild-kit/cjs-loader"
|
|
58
|
-
]
|
|
59
|
-
},
|
|
60
|
-
"devDependencies": {
|
|
61
|
-
"@babel/preset-env": "7.14.8",
|
|
62
|
-
"@babel/preset-typescript": "7.14.5",
|
|
63
|
-
"@esbuild-kit/cjs-loader": "2.4.0",
|
|
64
|
-
"@types/axios": "0.14.0",
|
|
65
|
-
"@types/node": "16.4.6",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "4.28.5",
|
|
67
|
-
"@typescript-eslint/parser": "4.28.5",
|
|
68
|
-
"ava": "5.0.1",
|
|
69
|
-
"axios": "1.1.3",
|
|
70
|
-
"cross-env": "7.0.3",
|
|
71
|
-
"eslint": "7.31.0",
|
|
72
|
-
"eslint-watch": "7.0.0",
|
|
73
|
-
"npm-run-all": "4.1.5",
|
|
74
|
-
"typescript": "4.8.4"
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "api-def",
|
|
3
|
+
"version": "0.7.2",
|
|
4
|
+
"description": "Typed API definitions with middleware support",
|
|
5
|
+
"main": "cjs/index.js",
|
|
6
|
+
"types": "esm/index.d.ts",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepublishOnly": "npm run test && npm run build",
|
|
11
|
+
"test": "npm run test:types && npm run test:lint && npm run test:unit",
|
|
12
|
+
"test:unit": "ava",
|
|
13
|
+
"test:lint": "npm run lint:strict",
|
|
14
|
+
"test:types": "tsc --noEmit -p .",
|
|
15
|
+
"example:start": "cd example && npm run start",
|
|
16
|
+
"lint": "esw --ext .tsx,.ts src",
|
|
17
|
+
"lint:watch": "npm run lint -- --watch",
|
|
18
|
+
"lint:fix": "npm run lint -- --fix",
|
|
19
|
+
"lint:strict": "npm run lint -- --max-warnings 0",
|
|
20
|
+
"cleanup": "rimraf esm && rimraf cjs",
|
|
21
|
+
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
22
|
+
"build:esm": "tsc --module es2015 --target es5 --outDir esm --preserveWatchOutput",
|
|
23
|
+
"build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput",
|
|
24
|
+
"build:watch": "npm-run-all -p \"build:esm -- -w\" \"build:cjs -- -w\" \"lint:watch\"",
|
|
25
|
+
"website:dev": "cd website && npm run start",
|
|
26
|
+
"website:deploy": "cd website && npm run deploy"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"typescript",
|
|
30
|
+
"javascript",
|
|
31
|
+
"node",
|
|
32
|
+
"web",
|
|
33
|
+
"api",
|
|
34
|
+
"typed",
|
|
35
|
+
"cache",
|
|
36
|
+
"fetch",
|
|
37
|
+
"retry",
|
|
38
|
+
"middleware"
|
|
39
|
+
],
|
|
40
|
+
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"files": [
|
|
43
|
+
"LICENSE",
|
|
44
|
+
"README.md",
|
|
45
|
+
"esm/",
|
|
46
|
+
"cjs/"
|
|
47
|
+
],
|
|
48
|
+
"repository": "https://github.com/Censkh/api-def",
|
|
49
|
+
"ava": {
|
|
50
|
+
"extensions": [
|
|
51
|
+
"ts"
|
|
52
|
+
],
|
|
53
|
+
"files": [
|
|
54
|
+
"src/tests/**/*.test.ts"
|
|
55
|
+
],
|
|
56
|
+
"nodeArguments": [
|
|
57
|
+
"--require=@esbuild-kit/cjs-loader"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@babel/preset-env": "7.14.8",
|
|
62
|
+
"@babel/preset-typescript": "7.14.5",
|
|
63
|
+
"@esbuild-kit/cjs-loader": "2.4.0",
|
|
64
|
+
"@types/axios": "0.14.0",
|
|
65
|
+
"@types/node": "16.4.6",
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "4.28.5",
|
|
67
|
+
"@typescript-eslint/parser": "4.28.5",
|
|
68
|
+
"ava": "5.0.1",
|
|
69
|
+
"axios": "1.1.3",
|
|
70
|
+
"cross-env": "7.0.3",
|
|
71
|
+
"eslint": "7.31.0",
|
|
72
|
+
"eslint-watch": "7.0.0",
|
|
73
|
+
"npm-run-all": "4.1.5",
|
|
74
|
+
"typescript": "4.8.4"
|
|
75
|
+
}
|
|
76
|
+
}
|