@marianmeres/http-utils 1.0.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.
- package/.editorconfig +12 -0
- package/.prettierignore +1 -0
- package/.prettierrc.yaml +5 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +45 -0
- package/rollup.config.js +15 -0
- package/src/api.ts +189 -0
- package/src/error.ts +162 -0
- package/src/index.ts +3 -0
- package/src/status.ts +107 -0
- package/tests/api.test.js +110 -0
- package/tests/utils.test.js +64 -0
- package/tsconfig.json +113 -0
package/.editorconfig
ADDED
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/node_modules/
|
package/.prettierrc.yaml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Marian Meres
|
|
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
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# http-utils
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@marianmeres/http-utils",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Misc DRY http fetch related helpers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"require": "./dist/index.cjs",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"clean": "rimraf dist/*",
|
|
14
|
+
"prettier": "prettier --write \"{src,tests}/**/*.{js,ts,json}\"",
|
|
15
|
+
"release": "release",
|
|
16
|
+
"release:patch": "release -v patch",
|
|
17
|
+
"test": "test-runner",
|
|
18
|
+
"build": "npm run clean && rollup -c"
|
|
19
|
+
},
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/marianmeres/http-utils.git"
|
|
24
|
+
},
|
|
25
|
+
"author": "Marian Meres <marian@meres.sk>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/marianmeres/http-utils/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/marianmeres/http-utils#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@marianmeres/release": "^1.1.2",
|
|
33
|
+
"@marianmeres/test-runner": "^2.0.16",
|
|
34
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
35
|
+
"@types/node": "^22.3.0",
|
|
36
|
+
"prettier": "^3.3.3",
|
|
37
|
+
"rimraf": "^6.0.1",
|
|
38
|
+
"rollup": "^4.20.0",
|
|
39
|
+
"tslib": "^2.6.3",
|
|
40
|
+
"typescript": "^5.5.4"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"dset": "^3.1.3"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import typescript from '@rollup/plugin-typescript';
|
|
3
|
+
|
|
4
|
+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
|
|
5
|
+
|
|
6
|
+
export default [
|
|
7
|
+
{
|
|
8
|
+
input: 'src/index.ts',
|
|
9
|
+
plugins: [ typescript() ],
|
|
10
|
+
output: [
|
|
11
|
+
{ file: pkg.main, format: 'cjs' },
|
|
12
|
+
{ file: pkg.module, format: 'es' }
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
];
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { dset } from 'dset/merge';
|
|
2
|
+
import { createHttpError } from './error.js';
|
|
3
|
+
|
|
4
|
+
// this is all very opinionated and may not be useful for every use case...
|
|
5
|
+
// there is no magic added over plain fetch calls
|
|
6
|
+
|
|
7
|
+
interface BaseParams {
|
|
8
|
+
method: 'GET' | 'POST' | 'PATCH' | 'DELETE' | 'PUT';
|
|
9
|
+
path: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FetchParams {
|
|
13
|
+
data?: any;
|
|
14
|
+
token?: string | null;
|
|
15
|
+
headers?: any;
|
|
16
|
+
signal?: any;
|
|
17
|
+
credentials?: null | 'omit' | 'same-origin' | 'include';
|
|
18
|
+
raw?: null | boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type BaseFetchParams = BaseParams & FetchParams; // Exclude<Drinks, Soda> |
|
|
22
|
+
|
|
23
|
+
const _fetchRaw = async ({
|
|
24
|
+
method,
|
|
25
|
+
path,
|
|
26
|
+
data = null,
|
|
27
|
+
token = null,
|
|
28
|
+
headers = null,
|
|
29
|
+
signal = null,
|
|
30
|
+
credentials,
|
|
31
|
+
}: BaseFetchParams) => {
|
|
32
|
+
headers = Object.entries(headers || {}).map(([k, v]) => ({ [k.toLowerCase()]: v }));
|
|
33
|
+
const opts: any = { method, credentials, headers, signal };
|
|
34
|
+
|
|
35
|
+
if (data) {
|
|
36
|
+
const isObj = typeof data === 'object';
|
|
37
|
+
|
|
38
|
+
// multipart/form-data -- no explicit Content-Type
|
|
39
|
+
if (data instanceof FormData) {
|
|
40
|
+
opts.body = data;
|
|
41
|
+
}
|
|
42
|
+
// cover 99% use cases (may not fit all)
|
|
43
|
+
else {
|
|
44
|
+
// if not stated, assuming json
|
|
45
|
+
if (isObj || !headers['content-type']) {
|
|
46
|
+
opts.headers['content-type'] = 'application/json';
|
|
47
|
+
}
|
|
48
|
+
opts.body = JSON.stringify(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// opinionated convention
|
|
53
|
+
if (token) {
|
|
54
|
+
opts.headers['authorization'] = `Bearer ${token}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return await fetch(path, opts);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const _fetch = async (
|
|
61
|
+
params: BaseFetchParams,
|
|
62
|
+
respHeaders = null,
|
|
63
|
+
_dumpParams = false
|
|
64
|
+
) => {
|
|
65
|
+
if (_dumpParams) return params;
|
|
66
|
+
|
|
67
|
+
const r = await _fetchRaw(params);
|
|
68
|
+
if (params.raw) return r;
|
|
69
|
+
|
|
70
|
+
// quick-n-dirty reference to headers (so it's still accessible over this api wrap)
|
|
71
|
+
if (respHeaders) {
|
|
72
|
+
Object.assign(
|
|
73
|
+
respHeaders,
|
|
74
|
+
[...r.headers.entries()].reduce((m, [k, v]) => ({ ...m, [k]: v }), {}),
|
|
75
|
+
// adding status/text under special keys
|
|
76
|
+
{
|
|
77
|
+
__http_status_code__: r.status,
|
|
78
|
+
__http_status_text__: r.statusText,
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let body: string = await r.text();
|
|
84
|
+
// prettier-ignore
|
|
85
|
+
try { body = JSON.parse(body); } catch (e) {}
|
|
86
|
+
|
|
87
|
+
if (!r.ok) {
|
|
88
|
+
throw createHttpError(r.status, null, body);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return body;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const createHttpApi = (
|
|
95
|
+
defaults?: Partial<BaseFetchParams> | (() => Promise<Partial<BaseFetchParams>>)
|
|
96
|
+
) => {
|
|
97
|
+
const _merge = (a: any, b: any): any => {
|
|
98
|
+
const wrap = { result: a };
|
|
99
|
+
dset(wrap, 'result', b);
|
|
100
|
+
return wrap.result;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const _getDefs = async () => {
|
|
104
|
+
return new Promise<Partial<BaseFetchParams>>(async (resolve) => {
|
|
105
|
+
if (typeof defaults === 'function') {
|
|
106
|
+
resolve(await defaults());
|
|
107
|
+
} else {
|
|
108
|
+
resolve(defaults || {});
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
// GET
|
|
115
|
+
async get(
|
|
116
|
+
path: string,
|
|
117
|
+
params?: FetchParams,
|
|
118
|
+
respHeaders = null,
|
|
119
|
+
_dumpParams = false
|
|
120
|
+
) {
|
|
121
|
+
return _fetch(
|
|
122
|
+
_merge(await _getDefs(), { ...params, method: 'GET', path }),
|
|
123
|
+
respHeaders,
|
|
124
|
+
_dumpParams
|
|
125
|
+
);
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
// POST
|
|
129
|
+
async post(
|
|
130
|
+
path: string,
|
|
131
|
+
data = null,
|
|
132
|
+
params?: FetchParams,
|
|
133
|
+
respHeaders = null,
|
|
134
|
+
_dumpParams = false
|
|
135
|
+
) {
|
|
136
|
+
return _fetch(
|
|
137
|
+
_merge(await _getDefs(), { ...(params || {}), data, method: 'POST', path }),
|
|
138
|
+
respHeaders,
|
|
139
|
+
_dumpParams
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
// PUT
|
|
144
|
+
async put(
|
|
145
|
+
path: string,
|
|
146
|
+
data = null,
|
|
147
|
+
params?: FetchParams,
|
|
148
|
+
respHeaders = null,
|
|
149
|
+
_dumpParams = false
|
|
150
|
+
) {
|
|
151
|
+
return _fetch(
|
|
152
|
+
_merge(await _getDefs(), { ...(params || {}), data, method: 'PUT', path }),
|
|
153
|
+
respHeaders,
|
|
154
|
+
_dumpParams
|
|
155
|
+
);
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
// PATCH
|
|
159
|
+
async patch(
|
|
160
|
+
path: string,
|
|
161
|
+
data = null,
|
|
162
|
+
params?: FetchParams,
|
|
163
|
+
respHeaders = null,
|
|
164
|
+
_dumpParams = false
|
|
165
|
+
) {
|
|
166
|
+
return _fetch(
|
|
167
|
+
_merge(await _getDefs(), { ...(params || {}), data, method: 'PATCH', path }),
|
|
168
|
+
respHeaders,
|
|
169
|
+
_dumpParams
|
|
170
|
+
);
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
// DELETE
|
|
174
|
+
// https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
|
|
175
|
+
async del(
|
|
176
|
+
path: string,
|
|
177
|
+
data = null,
|
|
178
|
+
params?: FetchParams,
|
|
179
|
+
respHeaders = null,
|
|
180
|
+
_dumpParams = false
|
|
181
|
+
) {
|
|
182
|
+
return _fetch(
|
|
183
|
+
_merge(await _getDefs(), { ...(params || {}), data, method: 'DELETE', path }),
|
|
184
|
+
respHeaders,
|
|
185
|
+
_dumpParams
|
|
186
|
+
);
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
};
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { HTTP_STATUS } from './status.js';
|
|
2
|
+
|
|
3
|
+
// opinionated base for all
|
|
4
|
+
class HttpError extends Error {
|
|
5
|
+
public name = 'HttpError';
|
|
6
|
+
public status = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE;
|
|
7
|
+
public statusText = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.TEXT;
|
|
8
|
+
public body: string | null = null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// some more specific instances of the well known ones...
|
|
12
|
+
|
|
13
|
+
// client
|
|
14
|
+
|
|
15
|
+
class BadRequest extends HttpError {
|
|
16
|
+
public name = 'HttpBadRequestError';
|
|
17
|
+
public status = HTTP_STATUS.ERROR_CLIENT.BAD_REQUEST.CODE;
|
|
18
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.BAD_REQUEST.TEXT;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class Unauthorized extends HttpError {
|
|
22
|
+
public name = 'HttpUnauthorizedError';
|
|
23
|
+
public status = HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.CODE;
|
|
24
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.TEXT;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class Forbidden extends HttpError {
|
|
28
|
+
public name = 'HttpForbiddenError';
|
|
29
|
+
public status = HTTP_STATUS.ERROR_CLIENT.FORBIDDEN.CODE;
|
|
30
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.FORBIDDEN.TEXT;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class NotFound extends HttpError {
|
|
34
|
+
public name = 'HttpNotFoundError';
|
|
35
|
+
public status = HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE;
|
|
36
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.TEXT;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class MethodNotAllowed extends HttpError {
|
|
40
|
+
public name = 'HttpMethodNotAllowedError';
|
|
41
|
+
public status = HTTP_STATUS.ERROR_CLIENT.METHOD_NOT_ALLOWED.CODE;
|
|
42
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.METHOD_NOT_ALLOWED.TEXT;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class RequestTimeout extends HttpError {
|
|
46
|
+
public name = 'HttpRequestTimeoutError';
|
|
47
|
+
public status = HTTP_STATUS.ERROR_CLIENT.REQUEST_TIMEOUT.CODE;
|
|
48
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.REQUEST_TIMEOUT.TEXT;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class Conflict extends HttpError {
|
|
52
|
+
public name = 'HttpConflictError';
|
|
53
|
+
public status = HTTP_STATUS.ERROR_CLIENT.CONFLICT.CODE;
|
|
54
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.CONFLICT.TEXT;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class Gone extends HttpError {
|
|
58
|
+
public name = 'HttpGoneError';
|
|
59
|
+
public status = HTTP_STATUS.ERROR_CLIENT.GONE.CODE;
|
|
60
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.GONE.TEXT;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class ImATeapot extends HttpError {
|
|
64
|
+
public name = 'HttpImATeapotError';
|
|
65
|
+
public status = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.CODE;
|
|
66
|
+
public statusText = HTTP_STATUS.ERROR_CLIENT.IM_A_TEAPOT.TEXT;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// server
|
|
70
|
+
|
|
71
|
+
class InternalServerError extends HttpError {
|
|
72
|
+
public name = 'HttpInternalServerError';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class NotImplemented extends HttpError {
|
|
76
|
+
public name = 'HttpServiceUnavailableError';
|
|
77
|
+
public status = HTTP_STATUS.ERROR_SERVER.NOT_IMPLEMENTED.CODE;
|
|
78
|
+
public statusText = HTTP_STATUS.ERROR_SERVER.NOT_IMPLEMENTED.TEXT;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class BadGateway extends HttpError {
|
|
82
|
+
public name = 'HttpBadGatewayError';
|
|
83
|
+
public status = HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.CODE;
|
|
84
|
+
public statusText = HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.TEXT;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class ServiceUnavailable extends HttpError {
|
|
88
|
+
public name = 'HttpServiceUnavailableError';
|
|
89
|
+
public status = HTTP_STATUS.ERROR_SERVER.SERVICE_UNAVAILABLE.CODE;
|
|
90
|
+
public statusText = HTTP_STATUS.ERROR_SERVER.SERVICE_UNAVAILABLE.TEXT;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
//
|
|
94
|
+
export const HTTP_ERROR = {
|
|
95
|
+
// base
|
|
96
|
+
HttpError,
|
|
97
|
+
// client
|
|
98
|
+
BadRequest,
|
|
99
|
+
Unauthorized,
|
|
100
|
+
Forbidden,
|
|
101
|
+
NotFound,
|
|
102
|
+
MethodNotAllowed,
|
|
103
|
+
RequestTimeout,
|
|
104
|
+
Conflict,
|
|
105
|
+
Gone,
|
|
106
|
+
ImATeapot,
|
|
107
|
+
// server
|
|
108
|
+
InternalServerError,
|
|
109
|
+
NotImplemented,
|
|
110
|
+
BadGateway,
|
|
111
|
+
ServiceUnavailable,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const _wellKnownCtorMap = {
|
|
115
|
+
'400': BadRequest,
|
|
116
|
+
'401': Unauthorized,
|
|
117
|
+
'403': Forbidden,
|
|
118
|
+
'404': NotFound,
|
|
119
|
+
'405': MethodNotAllowed,
|
|
120
|
+
'408': RequestTimeout,
|
|
121
|
+
'409': Conflict,
|
|
122
|
+
'410': Gone,
|
|
123
|
+
'418': ImATeapot,
|
|
124
|
+
//
|
|
125
|
+
'500': InternalServerError,
|
|
126
|
+
'501': NotImplemented,
|
|
127
|
+
'502': BadGateway,
|
|
128
|
+
'503': ServiceUnavailable,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
132
|
+
export const createHttpError = (
|
|
133
|
+
code: number | string,
|
|
134
|
+
message?: string | null,
|
|
135
|
+
body?: any,
|
|
136
|
+
cause?: any
|
|
137
|
+
) => {
|
|
138
|
+
const fallback = HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR;
|
|
139
|
+
|
|
140
|
+
code = Number(code);
|
|
141
|
+
if (isNaN(code) || !(code >= 400 && code < 600)) code = fallback.CODE;
|
|
142
|
+
|
|
143
|
+
const found = HTTP_STATUS.findByCode(code);
|
|
144
|
+
const statusText = found?.TEXT ?? fallback.TEXT;
|
|
145
|
+
|
|
146
|
+
// opinionated convention
|
|
147
|
+
if (typeof body === 'string') {
|
|
148
|
+
// prettier-ignore
|
|
149
|
+
try { body = JSON.parse(body); } catch (e) {}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// try to find the well known one, otherwise fallback to generic
|
|
153
|
+
const ctor =
|
|
154
|
+
_wellKnownCtorMap[`${code}` as keyof typeof _wellKnownCtorMap] ?? HttpError;
|
|
155
|
+
|
|
156
|
+
let e = new ctor(message || statusText, { cause });
|
|
157
|
+
e.status = found?.CODE ?? fallback.CODE;
|
|
158
|
+
e.statusText = statusText;
|
|
159
|
+
e.body = body;
|
|
160
|
+
|
|
161
|
+
return e;
|
|
162
|
+
};
|
package/src/index.ts
ADDED
package/src/status.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export class HTTP_STATUS {
|
|
2
|
+
// 1xx
|
|
3
|
+
// prettier-ignore
|
|
4
|
+
static readonly INFO = {
|
|
5
|
+
CONTINUE: { CODE: 100, TEXT: 'Continue' },
|
|
6
|
+
SWITCHING_PROTOCOLS: { CODE: 101, TEXT: 'Switching Protocols' },
|
|
7
|
+
PROCESSING: { CODE: 102, TEXT: 'Processing' },
|
|
8
|
+
EARLY_HINTS: { CODE: 103, TEXT: 'Early Hints' },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// 2xx
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
static readonly SUCCESS = {
|
|
14
|
+
OK: { CODE: 200, TEXT: 'OK' },
|
|
15
|
+
NON_AUTHORITATIVE_INFO: { CODE: 203, TEXT: 'Non-Authoritative Information' },
|
|
16
|
+
ACCEPTED: { CODE: 202, TEXT: 'Accepted' },
|
|
17
|
+
NO_CONTENT: { CODE: 204, TEXT: 'No Content' },
|
|
18
|
+
RESET_CONTENT: { CODE: 205, TEXT: 'Reset Content' },
|
|
19
|
+
PARTIAL_CONTENT: { CODE: 206, TEXT: 'Partial Content' },
|
|
20
|
+
MULTI_STATUS: { CODE: 207, TEXT: 'Multi-Status' },
|
|
21
|
+
ALREADY_REPORTED: { CODE: 208, TEXT: 'Already Reported' },
|
|
22
|
+
IM_USED: { CODE: 226, TEXT: 'IM Used' }, // ?
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// 3xx
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
static readonly REDIRECT = {
|
|
28
|
+
MUTLIPLE_CHOICES: { CODE: 300, TEXT: 'Multiple Choices' },
|
|
29
|
+
MOVED_PERMANENTLY: { CODE: 301, TEXT: 'Moved Permanently' },
|
|
30
|
+
FOUND: { CODE: 302, TEXT: 'Found' },
|
|
31
|
+
SEE_OTHER: { CODE: 303, TEXT: 'See Other' },
|
|
32
|
+
NOT_MODIFIED: { CODE: 304, TEXT: 'Not Modified' },
|
|
33
|
+
TEMPORARY_REDIRECT: { CODE: 307, TEXT: 'Temporary Redirect' },
|
|
34
|
+
PERMANENT_REDIRECT: { CODE: 308, TEXT: 'Permanent Redirect' },
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// 4xx
|
|
38
|
+
// prettier-ignore
|
|
39
|
+
static readonly ERROR_CLIENT = {
|
|
40
|
+
BAD_REQUEST: { CODE: 400, TEXT: 'Bad Request' },
|
|
41
|
+
UNAUTHORIZED: { CODE: 401, TEXT: 'Unauthorized' },
|
|
42
|
+
PAYMENT_REQUIRED_EXPERIMENTAL: { CODE: 402, TEXT: 'Payment Required Experimental' },
|
|
43
|
+
FORBIDDEN: { CODE: 403, TEXT: 'Forbidden' },
|
|
44
|
+
NOT_FOUND: { CODE: 404, TEXT: 'Not Found' },
|
|
45
|
+
METHOD_NOT_ALLOWED: { CODE: 405, TEXT: 'Method Not Allowed' },
|
|
46
|
+
NOT_ACCEPTABLE: { CODE: 406, TEXT: 'Not Acceptable' },
|
|
47
|
+
PROXY_AUTHENTICATION_REQUIRED: { CODE: 407, TEXT: 'Proxy Authentication Required' },
|
|
48
|
+
REQUEST_TIMEOUT: { CODE: 408, TEXT: 'Request Timeout' },
|
|
49
|
+
CONFLICT: { CODE: 409, TEXT: 'Conflict' },
|
|
50
|
+
GONE: { CODE: 410, TEXT: 'Gone' },
|
|
51
|
+
LENGTH_REQUIRED: { CODE: 411, TEXT: 'Length Required' },
|
|
52
|
+
PRECONDITION_FAILED: { CODE: 412, TEXT: 'Precondition Failed' },
|
|
53
|
+
PAYLOAD_TOO_LARGE: { CODE: 413, TEXT: 'Payload Too Large' },
|
|
54
|
+
URI_TOO_LONG: { CODE: 414, TEXT: 'URI Too Long' },
|
|
55
|
+
UNSUPPORTED_MEDIA_TYPE: { CODE: 415, TEXT: 'Unsupported Media Type' },
|
|
56
|
+
RANGE_NOT_SATISFIABLE: { CODE: 416, TEXT: 'Range Not Satisfiable' },
|
|
57
|
+
EXPECTATION_FAILED: { CODE: 417, TEXT: 'Expectation Failed' },
|
|
58
|
+
IM_A_TEAPOT: { CODE: 418, TEXT: "I'm a teapot" },
|
|
59
|
+
MISDIRECTED_REQUEST: { CODE: 421, TEXT: 'Misdirected Request' },
|
|
60
|
+
UNPROCESSABLE_CONTENT: { CODE: 422, TEXT: 'Unprocessable Content' },
|
|
61
|
+
LOCKED: { CODE: 423, TEXT: 'Locked' },
|
|
62
|
+
FAILED_DEPENDENCY: { CODE: 424, TEXT: 'Failed Dependency' },
|
|
63
|
+
TOO_EARLY_EXPERIMENTAL: { CODE: 425, TEXT: 'Too Early Experimental' },
|
|
64
|
+
UPGRADE_REQUIRED: { CODE: 426, TEXT: 'Upgrade Required' },
|
|
65
|
+
PRECONDITION_REQUIRED: { CODE: 428, TEXT: 'Precondition Required' },
|
|
66
|
+
TOO_MANY_REQUESTS: { CODE: 429, TEXT: 'Too Many Requests' },
|
|
67
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: { CODE: 431, TEXT: 'Request Header Fields Too Large' },
|
|
68
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: { CODE: 451, TEXT: 'Unavailable For Legal Reasons' },
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// 5xx
|
|
72
|
+
// prettier-ignore
|
|
73
|
+
static readonly ERROR_SERVER = {
|
|
74
|
+
INTERNAL_SERVER_ERROR: { CODE: 500, TEXT: 'Internal Server Error' },
|
|
75
|
+
NOT_IMPLEMENTED: { CODE: 501, TEXT: 'Not Implemented' },
|
|
76
|
+
BAD_GATEWAY: { CODE: 502, TEXT: 'Bad Gateway' },
|
|
77
|
+
SERVICE_UNAVAILABLE: { CODE: 503, TEXT: 'Service Unavailable' },
|
|
78
|
+
GATEWAY_TIMEOUT: { CODE: 504, TEXT: 'Gateway Timeout' },
|
|
79
|
+
HTTP_VERSION_NOT_SUPPORTED: { CODE: 505, TEXT: 'HTTP Version Not Supported' },
|
|
80
|
+
VARIANT_ALSO_NEGOTIATES: { CODE: 506, TEXT: 'Variant Also Negotiates' },
|
|
81
|
+
INSUFFICIENT_STORAGE: { CODE: 507, TEXT: 'Insufficient Storage' },
|
|
82
|
+
LOOP_DETECTED: { CODE: 508, TEXT: 'Loop Detected' },
|
|
83
|
+
NOT_EXTENDED: { CODE: 510, TEXT: 'Not Extended' },
|
|
84
|
+
NETWORK_AUTH_REQUIRED: { CODE: 511, TEXT: 'Network Authentication Required' },
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//
|
|
88
|
+
static findByCode(
|
|
89
|
+
code: number | string
|
|
90
|
+
): { CODE: number; TEXT: string; _TYPE: string; _KEY: string } | null {
|
|
91
|
+
const keys: (keyof typeof HTTP_STATUS)[] = [
|
|
92
|
+
'INFO',
|
|
93
|
+
'SUCCESS',
|
|
94
|
+
'REDIRECT',
|
|
95
|
+
'ERROR_CLIENT',
|
|
96
|
+
'ERROR_SERVER',
|
|
97
|
+
];
|
|
98
|
+
for (const _TYPE of keys) {
|
|
99
|
+
for (const [_KEY, data] of Object.entries(HTTP_STATUS[_TYPE]) as any) {
|
|
100
|
+
if (data.CODE == code) {
|
|
101
|
+
return { ...data, _TYPE, _KEY };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { TestRunner } from '@marianmeres/test-runner';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { HTTP_ERROR, createHttpApi } from '../dist/index.cjs';
|
|
7
|
+
|
|
8
|
+
let server;
|
|
9
|
+
const hostname = '127.0.0.1';
|
|
10
|
+
const port = 3456;
|
|
11
|
+
const url = `http://${hostname}:${port}`;
|
|
12
|
+
|
|
13
|
+
// https://nodejs.org/en/learn/modules/anatomy-of-an-http-transaction
|
|
14
|
+
const collectBody = async (request) =>
|
|
15
|
+
new Promise((resolve) => {
|
|
16
|
+
let body = [];
|
|
17
|
+
request
|
|
18
|
+
.on('data', (chunk) => body.push(chunk))
|
|
19
|
+
.on('end', () => resolve(Buffer.concat(body).toString()));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const suite = new TestRunner(path.basename(fileURLToPath(import.meta.url)), {
|
|
23
|
+
before: async () => {
|
|
24
|
+
server = createServer(async (req, res) => {
|
|
25
|
+
res.setHeader('Content-Type', 'application/json');
|
|
26
|
+
if (req.url === '/echo') {
|
|
27
|
+
res.statusCode = 200;
|
|
28
|
+
if (req.method === 'POST') {
|
|
29
|
+
res.end(await collectBody(req));
|
|
30
|
+
} else {
|
|
31
|
+
res.end('{"foo":"bar"}');
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
res.statusCode = 404;
|
|
35
|
+
res.end('{"some":{"deep":"message"}}');
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return new Promise((resolve) => server.listen(port, hostname, resolve));
|
|
39
|
+
},
|
|
40
|
+
after: async () => new Promise((resolve) => server.close(resolve)),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
suite.test('createHttpApi GET', async () => {
|
|
44
|
+
let api = createHttpApi();
|
|
45
|
+
let respHeaders = {};
|
|
46
|
+
|
|
47
|
+
let r = await api.get(`${url}/echo`, {}, respHeaders);
|
|
48
|
+
assert(r.foo === 'bar');
|
|
49
|
+
assert(respHeaders.__http_status_code__ === 200);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
suite.test('createHttpApi RAW', async () => {
|
|
53
|
+
let api = createHttpApi();
|
|
54
|
+
let headers = {};
|
|
55
|
+
|
|
56
|
+
// raw
|
|
57
|
+
let r = await api.get(`${url}/echo`, { raw: true });
|
|
58
|
+
assert(r instanceof Response);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
suite.test('createHttpApi error', async () => {
|
|
62
|
+
let api = createHttpApi();
|
|
63
|
+
|
|
64
|
+
let err;
|
|
65
|
+
try {
|
|
66
|
+
let r = await api.get(`${url}/asdf`);
|
|
67
|
+
assert(false); // must not be reached
|
|
68
|
+
} catch (e) {
|
|
69
|
+
err = e;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
assert(err instanceof HTTP_ERROR.NotFound);
|
|
73
|
+
assert(err.body.some.deep === 'message');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
suite.test('createHttpApi POST', async () => {
|
|
77
|
+
let api = createHttpApi();
|
|
78
|
+
let respHeaders = {};
|
|
79
|
+
|
|
80
|
+
let r = await api.post(`${url}/echo`, { hey: 'ho' }, {}, respHeaders);
|
|
81
|
+
assert(r.hey === 'ho');
|
|
82
|
+
assert(respHeaders.__http_status_code__ === 200);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
suite.test('createHttpApi merge default params', async () => {
|
|
86
|
+
let api = createHttpApi({
|
|
87
|
+
headers: { authorization: 'Bearer foo' },
|
|
88
|
+
method: 'must be ignored',
|
|
89
|
+
path: 'must be ignored',
|
|
90
|
+
credentials: 'include',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const params = await api.post(
|
|
94
|
+
'/hoho',
|
|
95
|
+
{ foo: 'bar' },
|
|
96
|
+
{ headers: { hey: 'ho' } },
|
|
97
|
+
null,
|
|
98
|
+
true
|
|
99
|
+
);
|
|
100
|
+
// console.log(params);
|
|
101
|
+
|
|
102
|
+
assert(params.headers.authorization === 'Bearer foo');
|
|
103
|
+
assert(params.headers.hey === 'ho');
|
|
104
|
+
assert(params.method === 'POST');
|
|
105
|
+
assert(params.path === '/hoho');
|
|
106
|
+
assert(params.credentials === 'include');
|
|
107
|
+
assert(params.data.foo === 'bar');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export default suite;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { TestRunner } from '@marianmeres/test-runner';
|
|
2
|
+
import { strict as assert } from 'node:assert';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { HTTP_ERROR, HTTP_STATUS, createHttpError } from '../dist/index.cjs';
|
|
6
|
+
|
|
7
|
+
const suite = new TestRunner(path.basename(fileURLToPath(import.meta.url)));
|
|
8
|
+
|
|
9
|
+
suite.test('HTTP_STATUS.findByCode', () => {
|
|
10
|
+
const s = HTTP_STATUS.findByCode(200);
|
|
11
|
+
assert(s.CODE === 200);
|
|
12
|
+
assert(s.TEXT === 'OK');
|
|
13
|
+
assert(s._TYPE === 'SUCCESS');
|
|
14
|
+
assert(s._KEY === 'OK');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
suite.test('HTTP_ERROR', () => {
|
|
18
|
+
let e = new HTTP_ERROR.HttpError();
|
|
19
|
+
assert(e.toString() === 'HttpError');
|
|
20
|
+
assert(e.status === HTTP_STATUS.ERROR_SERVER.INTERNAL_SERVER_ERROR.CODE);
|
|
21
|
+
|
|
22
|
+
// random client pick
|
|
23
|
+
e = new HTTP_ERROR.Unauthorized('Foo');
|
|
24
|
+
assert(e.toString() === 'HttpUnauthorizedError: Foo');
|
|
25
|
+
assert(e.status === HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.CODE);
|
|
26
|
+
assert(e.statusText === HTTP_STATUS.ERROR_CLIENT.UNAUTHORIZED.TEXT);
|
|
27
|
+
|
|
28
|
+
// random server pick
|
|
29
|
+
e = new HTTP_ERROR.BadGateway('Foo');
|
|
30
|
+
assert(e.toString() === 'HttpBadGatewayError: Foo');
|
|
31
|
+
assert(e.status === HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.CODE);
|
|
32
|
+
assert(e.statusText === HTTP_STATUS.ERROR_SERVER.BAD_GATEWAY.TEXT);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
suite.test('createHttpErrorByCode', () => {
|
|
36
|
+
// well known
|
|
37
|
+
let e = createHttpError(404, null, '{"foo":"bar"}', { baz: 'bat' });
|
|
38
|
+
|
|
39
|
+
assert(e instanceof HTTP_ERROR.NotFound);
|
|
40
|
+
assert(e.toString() === 'HttpNotFoundError: Not Found');
|
|
41
|
+
assert(e.body.foo === 'bar');
|
|
42
|
+
assert(e.cause.baz === 'bat');
|
|
43
|
+
|
|
44
|
+
// NOT well known
|
|
45
|
+
e = createHttpError(423, null, '{invalid json}', 123);
|
|
46
|
+
assert(e instanceof HTTP_ERROR.HttpError);
|
|
47
|
+
assert(e.toString() === 'HttpError: Locked');
|
|
48
|
+
assert(e.body === '{invalid json}');
|
|
49
|
+
assert(e.cause === 123);
|
|
50
|
+
|
|
51
|
+
// unknown code must fall back to 500
|
|
52
|
+
e = createHttpError(123, null, '123');
|
|
53
|
+
assert(e instanceof HTTP_ERROR.InternalServerError);
|
|
54
|
+
assert(e.toString() === 'HttpInternalServerError: Internal Server Error');
|
|
55
|
+
assert(e.body === 123); // '123' is a valid json string
|
|
56
|
+
assert(e.cause === undefined);
|
|
57
|
+
|
|
58
|
+
// custom message
|
|
59
|
+
e = createHttpError(123, 'Hey', '123');
|
|
60
|
+
assert(e.toString() === 'HttpInternalServerError: Hey');
|
|
61
|
+
assert(e.body === 123);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
export default suite;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["src"],
|
|
3
|
+
"exclude": ["node_modules"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
6
|
+
|
|
7
|
+
/* Projects */
|
|
8
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
9
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
10
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
11
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
12
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
13
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
14
|
+
|
|
15
|
+
/* Language and Environment */
|
|
16
|
+
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
17
|
+
"lib": [
|
|
18
|
+
"ESNext"
|
|
19
|
+
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
|
|
20
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
21
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
22
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
23
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
24
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
25
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
26
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
27
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
28
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
29
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
30
|
+
|
|
31
|
+
/* Modules */
|
|
32
|
+
"module": "ESNext" /* Specify what module code is generated. */,
|
|
33
|
+
"rootDir": "src" /* Specify the root folder within your source files. */,
|
|
34
|
+
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
|
35
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
36
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
37
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
38
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
39
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
40
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
41
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
42
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
43
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
44
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
45
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
46
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
47
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
48
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
49
|
+
|
|
50
|
+
/* JavaScript Support */
|
|
51
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
52
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
53
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
54
|
+
|
|
55
|
+
/* Emit */
|
|
56
|
+
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
|
57
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
58
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
59
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
60
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
61
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
62
|
+
"outDir": "dist" /* Specify an output folder for all emitted files. */,
|
|
63
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
64
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
65
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
66
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
67
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
68
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
69
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
70
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
71
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
72
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
73
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
74
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
75
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
76
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
77
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
78
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
79
|
+
|
|
80
|
+
/* Interop Constraints */
|
|
81
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
82
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
83
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
84
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
85
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
86
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
87
|
+
|
|
88
|
+
/* Type Checking */
|
|
89
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
90
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
91
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
92
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
93
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
94
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
95
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
96
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
97
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
98
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
99
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
100
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
101
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
102
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
103
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
104
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
105
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
106
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
107
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
108
|
+
|
|
109
|
+
/* Completeness */
|
|
110
|
+
// "skipDefaultLibCheck": true /* Skip type checking .d.ts files that are included with TypeScript. */,
|
|
111
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
112
|
+
}
|
|
113
|
+
}
|