@miqro/core 1.6.1 → 1.7.0
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/README.md +101 -41
- package/dist/api-router.d.ts +1 -1
- package/dist/common/api-router-utils.d.ts +1 -1
- package/dist/common/api-router-utils.js +6 -21
- package/dist/common/api-router-utils.js.map +1 -1
- package/dist/common/api-router.d.ts +3 -16
- package/dist/common/context.d.ts +1 -1
- package/dist/common/logger/constants.d.ts +13 -0
- package/dist/common/logger/constants.js +26 -0
- package/dist/common/logger/constants.js.map +1 -0
- package/dist/common/logger/factory.d.ts +2 -4
- package/dist/common/logger/factory.js +7 -3
- package/dist/common/logger/factory.js.map +1 -1
- package/dist/common/logger/index.d.ts +5 -5
- package/dist/common/logger/index.js +5 -4
- package/dist/common/logger/index.js.map +1 -1
- package/dist/common/logger/{transport.d.ts → interfaces.d.ts} +6 -3
- package/dist/common/logger/interfaces.js +3 -0
- package/dist/common/logger/interfaces.js.map +1 -0
- package/dist/common/logger/logger.d.ts +15 -7
- package/dist/common/logger/logger.js +35 -27
- package/dist/common/logger/logger.js.map +1 -1
- package/dist/common/logger/transport/console.d.ts +5 -0
- package/dist/common/logger/transport/console.js +39 -0
- package/dist/common/logger/transport/console.js.map +1 -0
- package/dist/common/logger/transport/file.d.ts +2 -0
- package/dist/common/logger/transport/file.js +29 -0
- package/dist/common/logger/transport/file.js.map +1 -0
- package/dist/common/router-helpers.d.ts +1 -1
- package/dist/common/router-helpers.js +3 -3
- package/dist/common/router-helpers.js.map +1 -1
- package/dist/router.d.ts +1 -1
- package/package.json +2 -2
- package/src/api-router.ts +1 -1
- package/src/common/api-router-utils.ts +18 -34
- package/src/common/api-router.ts +3 -16
- package/src/common/context.ts +1 -1
- package/src/common/logger/constants.ts +27 -0
- package/src/common/logger/factory.ts +11 -9
- package/src/common/logger/index.ts +5 -5
- package/src/common/logger/interfaces.ts +19 -0
- package/src/common/logger/logger.ts +52 -30
- package/src/common/logger/transport/console.ts +34 -0
- package/src/common/logger/transport/file.ts +26 -0
- package/src/common/router-helpers.ts +2 -2
- package/src/router.ts +1 -1
- package/dist/common/logger/common.d.ts +0 -4
- package/dist/common/logger/common.js +0 -7
- package/dist/common/logger/common.js.map +0 -1
- package/dist/common/logger/formatter.d.ts +0 -7
- package/dist/common/logger/formatter.js +0 -9
- package/dist/common/logger/formatter.js.map +0 -1
- package/dist/common/logger/transport.js +0 -59
- package/dist/common/logger/transport.js.map +0 -1
- package/src/common/logger/common.ts +0 -6
- package/src/common/logger/formatter.ts +0 -9
- package/src/common/logger/transport.ts +0 -71
package/README.md
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
# @miqro/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
promise based router
|
|
4
|
+
|
|
5
|
+
- [router](#router)
|
|
6
|
+
- [request parsing](#request-parsing)
|
|
7
|
+
- [custom types](#custom-types)
|
|
8
|
+
- [static files](#static-files)
|
|
9
|
+
- [proxy router](#proxy-router)
|
|
10
|
+
- [error handling](#error-handling)
|
|
11
|
+
- [apirouter](#apirouter)
|
|
12
|
+
- [auto documentation](#auto-documentation)
|
|
13
|
+
|
|
14
|
+
## router
|
|
4
15
|
|
|
5
16
|
```typescript
|
|
6
17
|
import {createServer} from "http";
|
|
@@ -32,14 +43,9 @@ router.use(async (ctx) => {
|
|
|
32
43
|
});
|
|
33
44
|
|
|
34
45
|
const server = createServer(router.listener).listen(8080);
|
|
35
|
-
// or to handle 404 and default errors
|
|
36
|
-
// import { App } from "@miqro/core";
|
|
37
|
-
// const server = createServer(new App().use(router, "/api").listener).listen(8080);
|
|
38
|
-
// or
|
|
39
|
-
// const server = app.listen(8080);
|
|
40
46
|
```
|
|
41
47
|
|
|
42
|
-
|
|
48
|
+
### request parsing
|
|
43
49
|
|
|
44
50
|
for more examples of parsing see [@miqro/parser](https://www.npmjs.com/package/@miqro/parser)
|
|
45
51
|
|
|
@@ -49,8 +55,8 @@ router.get("/add/:a/:b", {
|
|
|
49
55
|
body: false, // do not allow body
|
|
50
56
|
params: {
|
|
51
57
|
options: {
|
|
52
|
-
a: "number", // parse param :a to a number if posible if not throw
|
|
53
|
-
b: "number", // parse param :b to a number if posible if not throw
|
|
58
|
+
a: "number", // parse param :a to a number if posible if not throw
|
|
59
|
+
b: "number", // parse param :b to a number if posible if not throw
|
|
54
60
|
}
|
|
55
61
|
//,mode: "no_extra" // defaults to no_extra
|
|
56
62
|
},
|
|
@@ -66,9 +72,6 @@ router.get("/add/:a/:b", {
|
|
|
66
72
|
}
|
|
67
73
|
}
|
|
68
74
|
},
|
|
69
|
-
response: {
|
|
70
|
-
body: "number|string"
|
|
71
|
-
},
|
|
72
75
|
handler: async (ctx) => {
|
|
73
76
|
// ctx.params.a and ctx.params.b are numbers and not strings
|
|
74
77
|
const result = ctx.params.a + ctx.params.b;
|
|
@@ -92,54 +95,43 @@ router.get("/add/:a/:b", {
|
|
|
92
95
|
});
|
|
93
96
|
```
|
|
94
97
|
|
|
95
|
-
|
|
98
|
+
#### custom types
|
|
96
99
|
|
|
97
100
|
```typescript
|
|
98
101
|
import {Parser} from "@miqro/parser";
|
|
99
102
|
|
|
100
103
|
const parser = new Parser();
|
|
101
|
-
parser.registerType("
|
|
102
|
-
|
|
103
|
-
b: "number", // parse param :b to a number if posible if not throw 400
|
|
104
|
+
parser.registerType("CustomType", {
|
|
105
|
+
//...
|
|
104
106
|
});
|
|
105
|
-
parser.registerType("AddQuery", {
|
|
106
|
-
decimals: "boolean?|number?", // two optional posible parsing types
|
|
107
|
-
format: {
|
|
108
|
-
type: "enum?", // optional enum
|
|
109
|
-
enumValues: ["number", "text"],
|
|
110
|
-
defaultValue: "number"
|
|
111
|
-
}
|
|
112
|
-
}, "add_extra");
|
|
113
|
-
|
|
114
107
|
router.get("/add/:a/:b", {
|
|
115
108
|
parser,
|
|
116
|
-
|
|
117
|
-
body: false, // do not allow body
|
|
118
|
-
params: "AddParams",
|
|
119
|
-
query: "AddQuery"
|
|
120
|
-
},
|
|
121
|
-
response: {
|
|
122
|
-
body: "number|string"
|
|
123
|
-
},
|
|
124
|
-
handler: async (ctx) => {
|
|
125
|
-
// ...
|
|
126
|
-
}
|
|
109
|
+
//...
|
|
127
110
|
});
|
|
128
111
|
```
|
|
129
112
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
## serving static files
|
|
113
|
+
### static files
|
|
133
114
|
|
|
134
115
|
```typescript
|
|
135
116
|
import {Static} from "@miqro/core";
|
|
136
117
|
|
|
137
118
|
router.use(Static({
|
|
138
|
-
directory: resolve(__dirname, "
|
|
119
|
+
directory: resolve(__dirname, "static")
|
|
139
120
|
}), "/content/public");
|
|
140
121
|
```
|
|
141
122
|
|
|
142
|
-
|
|
123
|
+
### proxy router
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import {Proxy} from "@miqro/core";
|
|
127
|
+
|
|
128
|
+
router.use(Proxy({
|
|
129
|
+
url: "http://otherhost/api/somepath/";
|
|
130
|
+
//rejectUnauthorized: false;
|
|
131
|
+
}), "/proxy/");
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### error handling
|
|
143
135
|
|
|
144
136
|
```typescript
|
|
145
137
|
router.catch(myFallBackerrorHandler1) // this will catch all throws
|
|
@@ -150,3 +142,71 @@ router.use(async (ctx) => {
|
|
|
150
142
|
});
|
|
151
143
|
```
|
|
152
144
|
|
|
145
|
+
## apirouter
|
|
146
|
+
|
|
147
|
+
for loading full directories with the folders acting as path's
|
|
148
|
+
|
|
149
|
+
consider the following directory structure
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
api/
|
|
153
|
+
v1/
|
|
154
|
+
user/
|
|
155
|
+
:userId/
|
|
156
|
+
get.ts
|
|
157
|
+
delete.ts
|
|
158
|
+
patch.ts
|
|
159
|
+
post.ts
|
|
160
|
+
post/
|
|
161
|
+
:postId/
|
|
162
|
+
get.ts
|
|
163
|
+
post.ts
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
this will producte the following routes.
|
|
167
|
+
|
|
168
|
+
- [GET] /api/v1/user/:userId/
|
|
169
|
+
- [DELETE] /api/v1/user/:userId/
|
|
170
|
+
- [PATCH] /api/v1/user/:userId/
|
|
171
|
+
- [POST] /api/v1/user/
|
|
172
|
+
|
|
173
|
+
- [GET] /api/v1/post/
|
|
174
|
+
- [POST] /api/v1/post/:postId/
|
|
175
|
+
|
|
176
|
+
each file has to implement the ```APIRoute``` interface.
|
|
177
|
+
|
|
178
|
+
example
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import {APIRoute} from "@miqro/core";
|
|
182
|
+
|
|
183
|
+
export default {
|
|
184
|
+
handler: async (ctx) => {
|
|
185
|
+
|
|
186
|
+
}
|
|
187
|
+
} as APIRoute;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
to load the routes
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
import {APIRouter} from "@miqro/core";
|
|
194
|
+
|
|
195
|
+
const api = APIRouter({ dirname: resolve(__dirname, "api") });
|
|
196
|
+
|
|
197
|
+
app.use(api);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### auto documentation
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
...
|
|
204
|
+
console.dir(router.getJSONDoc());
|
|
205
|
+
...
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
or generate a markdown
|
|
209
|
+
|
|
210
|
+
```npx miqro doc:md build/api /api API.md```
|
|
211
|
+
|
|
212
|
+
where ```build/api``` is the APIRouter directory
|
package/dist/api-router.d.ts
CHANGED
|
@@ -6,22 +6,12 @@ const path_1 = require("path");
|
|
|
6
6
|
const tokenize_match_1 = require("./tokenize-match");
|
|
7
7
|
const config_1 = require("./config");
|
|
8
8
|
const router_1 = require("../router");
|
|
9
|
+
const router_utils_1 = require("./router-utils");
|
|
9
10
|
const parser_1 = require("@miqro/parser");
|
|
10
11
|
exports.APIRouteValidator = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
path: "string[]!?",
|
|
15
|
-
parser: "ParserInterface?",
|
|
16
|
-
headers: "ParseOptions[]!?",
|
|
17
|
-
query: "boolean?|ParseOptions[]!?|string?",
|
|
18
|
-
params: "boolean?|ParseOptions[]!?|string?",
|
|
19
|
-
body: "boolean?|ParseOptions[]!?|string?",
|
|
20
|
-
response: "ParseOptions[]!?|string?",
|
|
21
|
-
handler: "function[]!",
|
|
22
|
-
middleware: "function[]!?",
|
|
23
|
-
session: "function?|SessionHandlerOptions?",
|
|
24
|
-
policy: "GroupPolicy?"
|
|
12
|
+
...router_utils_1.handlerWithOptionsParseOptions,
|
|
13
|
+
method: "string?|string[]?",
|
|
14
|
+
path: "string?|string[]?"
|
|
25
15
|
};
|
|
26
16
|
function traverseAPIRouteDir(featureName, dirname, basePath = "/", features = {}, logger) {
|
|
27
17
|
if (logger) {
|
|
@@ -93,14 +83,9 @@ function traverseAPIRouteDir(featureName, dirname, basePath = "/", features = {}
|
|
|
93
83
|
middleware: route.middleware,
|
|
94
84
|
session: route.session,
|
|
95
85
|
policy: route.policy,
|
|
96
|
-
request:
|
|
97
|
-
headers: route.headers,
|
|
98
|
-
query: route.query,
|
|
99
|
-
body: route.body,
|
|
100
|
-
params: route.params,
|
|
101
|
-
},
|
|
86
|
+
request: route.request,
|
|
102
87
|
response: {
|
|
103
|
-
body: route.response ? route.response : true
|
|
88
|
+
body: route.response && route.response.body ? route.response.body : true
|
|
104
89
|
}
|
|
105
90
|
},
|
|
106
91
|
path,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-router-utils.js","sourceRoot":"","sources":["../../src/common/api-router-utils.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"api-router-utils.js","sourceRoot":"","sources":["../../src/common/api-router-utils.ts"],"names":[],"mappings":";;;AAAA,2BAA4C;AAC5C,+BAAyD;AAGzD,qDAAiD;AACjD,qCAA4C;AAC5C,sCAAmC;AAEnC,i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AAC;4BACvC,MAAM,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,EAAE,CAAC,CAAC,iBAAiB,EAAY,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;yBACxF;qBACF;iBACF;qBAAM;oBACL,IAAI,MAAM,EAAE;wBACV,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,WAAW,CAAC,CAAC;qBACpD;oBACD,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;iBAC5B;aACF;SACF;KACF;IACD,IAAI,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;QAChC,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;KAC1D;IACD,IAAI,MAAM,EAAE;QACV,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACvB,MAAM,CAAC,IAAI,CAAC,wBAAwB,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;YAC1D,MAAM,CAAC,IAAI,CAAC,oDAAoD,CAAC,CAAC;SACnE;aAAM;YACL,MAAM,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACtC;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAA;AAzEY,QAAA,aAAa,iBAyEzB"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Handler, Method } from "./context";
|
|
1
|
+
import { Method } from "./context";
|
|
2
|
+
import { HandlerWithOptions } from "./router-utils";
|
|
4
3
|
/**
|
|
5
4
|
* # APIRoute
|
|
6
5
|
*
|
|
@@ -13,19 +12,7 @@ import { Handler, Method } from "./context";
|
|
|
13
12
|
*export default route;
|
|
14
13
|
* ```
|
|
15
14
|
*/
|
|
16
|
-
export interface APIRoute {
|
|
17
|
-
name?: string;
|
|
18
|
-
description?: string;
|
|
15
|
+
export interface APIRoute extends HandlerWithOptions {
|
|
19
16
|
method?: Method | Method[];
|
|
20
17
|
path?: string | string[];
|
|
21
|
-
parser?: ParserInterface;
|
|
22
|
-
headers?: ParseOptions | ParseOptions[];
|
|
23
|
-
query?: ParseOptions | boolean | ParseOptions[] | string;
|
|
24
|
-
params?: ParseOptions | boolean | ParseOptions[] | string;
|
|
25
|
-
body?: ParseOptions | boolean | ParseOptions[] | string;
|
|
26
|
-
response?: ParseOptions | ParseOptions[] | string;
|
|
27
|
-
handler: Array<Handler> | Handler;
|
|
28
|
-
middleware?: Handler[];
|
|
29
|
-
session?: SessionHandlerOptions | Handler;
|
|
30
|
-
policy?: GroupPolicy;
|
|
31
18
|
}
|
package/dist/common/context.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
-
import { Logger } from "./logger";
|
|
4
|
+
import { Logger } from "./logger/index.js";
|
|
5
5
|
import { Session } from "./session";
|
|
6
6
|
import { IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http";
|
|
7
7
|
import { URL } from "url";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LoggerFormatter, LoggerTransport } from "./interfaces.js";
|
|
2
|
+
export declare const DEFAULT_LOGGER_TRANSPORTS: () => LoggerTransport[];
|
|
3
|
+
export declare const DEFAULT_LOGGER_FORMATTER: LoggerFormatter;
|
|
4
|
+
export declare const LOG_LEVEL_MAP: {
|
|
5
|
+
error: number;
|
|
6
|
+
warn: number;
|
|
7
|
+
info: number;
|
|
8
|
+
debug: number;
|
|
9
|
+
trace: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const LoggerEvents: {
|
|
12
|
+
error: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LoggerEvents = exports.LOG_LEVEL_MAP = exports.DEFAULT_LOGGER_FORMATTER = exports.DEFAULT_LOGGER_TRANSPORTS = void 0;
|
|
4
|
+
const file_js_1 = require("./transport/file.js");
|
|
5
|
+
const console_js_1 = require("./transport/console.js");
|
|
6
|
+
const DEFAULT_LOGGER_TRANSPORTS = () => [
|
|
7
|
+
(0, console_js_1.Console)(),
|
|
8
|
+
(0, file_js_1.File)()
|
|
9
|
+
];
|
|
10
|
+
exports.DEFAULT_LOGGER_TRANSPORTS = DEFAULT_LOGGER_TRANSPORTS;
|
|
11
|
+
const DEFAULT_LOGGER_FORMATTER = ({ identifier, level, message }) => `${new Date().toISOString()} ${process.pid} ` +
|
|
12
|
+
`${identifier ? `[${identifier}] ` : ""}` +
|
|
13
|
+
`${level !== "info" ? (level === "error" || level === "warn" ? `[${level.toUpperCase()}] ` : `[${level}] `) : ""}` +
|
|
14
|
+
`${message}`;
|
|
15
|
+
exports.DEFAULT_LOGGER_FORMATTER = DEFAULT_LOGGER_FORMATTER;
|
|
16
|
+
exports.LOG_LEVEL_MAP = {
|
|
17
|
+
"error": 1,
|
|
18
|
+
"warn": 2,
|
|
19
|
+
"info": 3,
|
|
20
|
+
"debug": 4,
|
|
21
|
+
"trace": 5,
|
|
22
|
+
};
|
|
23
|
+
exports.LoggerEvents = {
|
|
24
|
+
error: "error"
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/common/logger/constants.ts"],"names":[],"mappings":";;;AACA,iDAAyC;AACzC,uDAA+C;AAExC,MAAM,yBAAyB,GAA4B,GAAG,EAAE,CAAC;IACtE,IAAA,oBAAO,GAAE;IACT,IAAA,cAAI,GAAE;CACP,CAAC;AAHW,QAAA,yBAAyB,6BAGpC;AAEK,MAAM,wBAAwB,GACnC,CAAC,EAAC,UAAU,EAAE,KAAK,EAAE,OAAO,EAAC,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,IAAI,OAAO,CAAC,GAAG,GAAG;IAC7E,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,UAAU,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE;IACzC,GAAG,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;IAClH,GAAG,OAAO,EAAE,CAAC;AAJJ,QAAA,wBAAwB,4BAIpB;AAEJ,QAAA,aAAa,GACxB;IACE,OAAO,EAAE,CAAC;IACV,MAAM,EAAE,CAAC;IACT,MAAM,EAAE,CAAC;IACT,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;CACX,CAAC;AAES,QAAA,YAAY,GAAG;IAC1B,KAAK,EAAE,OAAO;CACf,CAAC"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { LoggerTransport } from "./transport";
|
|
4
|
-
import { LogLevel } from "./common";
|
|
1
|
+
import { LoggerFormatter, LoggerTransport, LogLevel } from "./interfaces";
|
|
2
|
+
import { Logger } from "./logger.js";
|
|
5
3
|
export declare const getLogger: (identifier: string, options?: {
|
|
6
4
|
formatter?: LoggerFormatter;
|
|
7
5
|
transports?: LoggerTransport[];
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getLoggerFactory = exports.setLoggerFactory = exports.getLogger = void 0;
|
|
4
|
-
const logger_1 = require("./logger");
|
|
5
4
|
const config_1 = require("../config");
|
|
5
|
+
const logger_js_1 = require("./logger.js");
|
|
6
|
+
const constants_js_1 = require("./constants.js");
|
|
6
7
|
const loggerContainer = new Map();
|
|
7
8
|
const DEFAULT_ENV_NAME = "LOG_LEVEL";
|
|
8
9
|
const getLogger = (identifier, options, useCache = true) => {
|
|
@@ -25,12 +26,15 @@ const getLogger = (identifier, options, useCache = true) => {
|
|
|
25
26
|
};
|
|
26
27
|
exports.getLogger = getLogger;
|
|
27
28
|
const defaultLoggerFactory = ({ identifier, options, level }) => {
|
|
28
|
-
return new
|
|
29
|
+
return new logger_js_1.Logger(identifier, level, {
|
|
30
|
+
formatter: options && options.formatter ? options.formatter : constants_js_1.DEFAULT_LOGGER_FORMATTER,
|
|
31
|
+
transports: options && options.transports ? options.transports : (0, constants_js_1.DEFAULT_LOGGER_TRANSPORTS)()
|
|
32
|
+
});
|
|
29
33
|
};
|
|
30
34
|
let customLoggerFactory = undefined;
|
|
31
35
|
const setLoggerFactory = (factory) => {
|
|
32
36
|
if (customLoggerFactory !== undefined) {
|
|
33
|
-
throw new Error("cannot change logger factory
|
|
37
|
+
throw new Error("cannot change logger factory after it has been used.");
|
|
34
38
|
}
|
|
35
39
|
customLoggerFactory = factory;
|
|
36
40
|
return customLoggerFactory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/common/logger/factory.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/common/logger/factory.ts"],"names":[],"mappings":";;;AACA,sCAAwC;AACxC,2CAAmC;AACnC,iDAAmF;AAEnF,MAAM,eAAe,GAAwB,IAAI,GAAG,EAAkB,CAAC;AACvE,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAE9B,MAAM,SAAS,GAAG,CAAC,UAAkB,EAAE,OAAyE,EAAE,QAAQ,GAAG,IAAI,EAAU,EAAE;IAClJ,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;QACvE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;KACvC;IACD,I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AI,KAAK,CAAC,sDAAsD,CAAC,CAAC;KACzE;IACD,mBAAmB,GAAG,OAAO,CAAC;IAC9B,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AANW,QAAA,gBAAgB,oBAM3B;AAEK,MAAM,gBAAgB,GAAG,GAAkB,EAAE;IAClD,IAAI,mBAAmB,EAAE;QACvB,OAAO,mBAAmB,CAAC;KAC5B;SAAM;QACL,mBAAmB,GAAG,oBAAoB,CAAC;QAC3C,OAAO,oBAAoB,CAAC;KAC7B;AACH,CAAC,CAAC;AAPW,QAAA,gBAAgB,oBAO3B"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./transport";
|
|
1
|
+
export * from "./constants.js";
|
|
2
|
+
export * from "./logger.js";
|
|
3
|
+
export * from "./factory.js";
|
|
4
|
+
export * from "./interfaces.js";
|
|
5
|
+
export * from "./transport/console.js";
|
|
@@ -14,8 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
19
|
-
__exportStar(require("./
|
|
20
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./constants.js"), exports);
|
|
18
|
+
__exportStar(require("./logger.js"), exports);
|
|
19
|
+
__exportStar(require("./factory.js"), exports);
|
|
20
|
+
__exportStar(require("./interfaces.js"), exports);
|
|
21
|
+
__exportStar(require("./transport/console.js"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/logger/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/logger/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,8CAA4B;AAC5B,+CAA6B;AAC7B,kDAAgC;AAChC,yDAAuC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export declare type LogLevel = "error" | "warn" | "info" | "debug" | "trace";
|
|
2
2
|
export interface WriteArgs {
|
|
3
3
|
level: LogLevel;
|
|
4
4
|
message?: any;
|
|
@@ -11,5 +11,8 @@ export interface LoggerTransport {
|
|
|
11
11
|
level?: LogLevel;
|
|
12
12
|
write(args: LoggerTransportWriteArgs): Promise<void> | void;
|
|
13
13
|
}
|
|
14
|
-
export declare
|
|
15
|
-
|
|
14
|
+
export declare type LoggerFormatter = (args: {
|
|
15
|
+
identifier: string;
|
|
16
|
+
level: LogLevel;
|
|
17
|
+
message: string;
|
|
18
|
+
}) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/common/logger/interfaces.ts"],"names":[],"mappings":""}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { LoggerFormatter, LoggerTransport, LogLevel, WriteArgs } from "./interfaces.js";
|
|
2
|
+
export interface LoggerErrorEventInit {
|
|
3
|
+
error: any;
|
|
4
|
+
}
|
|
5
|
+
declare var Event: any;
|
|
6
|
+
declare var EventTarget: any;
|
|
7
|
+
export declare class LoggerErrorEvent extends Event {
|
|
8
|
+
error: any;
|
|
9
|
+
constructor(event: string, init: LoggerErrorEventInit);
|
|
10
|
+
}
|
|
6
11
|
export interface Logger {
|
|
7
12
|
log(message?: any, ...optionalParams: any[]): void;
|
|
8
13
|
info(message?: any, ...optionalParams: any[]): void;
|
|
@@ -10,10 +15,12 @@ export interface Logger {
|
|
|
10
15
|
debug(message?: any, ...optionalParams: any[]): void;
|
|
11
16
|
warn(message?: any, ...optionalParams: any[]): void;
|
|
12
17
|
error(message?: any, ...optionalParams: any[]): void;
|
|
18
|
+
addTransport(transport: LoggerTransport): void;
|
|
19
|
+
setLevel(level: LogLevel): void;
|
|
13
20
|
}
|
|
14
|
-
export declare class Logger extends
|
|
21
|
+
export declare class Logger extends EventTarget implements Logger {
|
|
15
22
|
protected readonly identifier: string;
|
|
16
|
-
protected
|
|
23
|
+
protected level: LogLevel;
|
|
17
24
|
protected readonly options: {
|
|
18
25
|
transports: LoggerTransport[];
|
|
19
26
|
formatter: LoggerFormatter;
|
|
@@ -24,3 +31,4 @@ export declare class Logger extends EventEmitter implements Logger {
|
|
|
24
31
|
});
|
|
25
32
|
protected write(args: WriteArgs): void;
|
|
26
33
|
}
|
|
34
|
+
export {};
|
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Logger = void 0;
|
|
3
|
+
exports.Logger = exports.LoggerErrorEvent = void 0;
|
|
4
4
|
const util_1 = require("util");
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"error": 1,
|
|
15
|
-
"warn": 2,
|
|
16
|
-
"info": 3,
|
|
17
|
-
"debug": 4,
|
|
18
|
-
"trace": 5,
|
|
19
|
-
};
|
|
20
|
-
class Logger extends events_1.EventEmitter {
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
|
+
class LoggerErrorEvent extends Event {
|
|
7
|
+
constructor(event, init) {
|
|
8
|
+
super(event, init);
|
|
9
|
+
this.error = init.error;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.LoggerErrorEvent = LoggerErrorEvent;
|
|
13
|
+
class Logger extends EventTarget {
|
|
21
14
|
constructor(identifier, level = "info", options) {
|
|
22
15
|
super();
|
|
23
16
|
this.identifier = identifier;
|
|
24
17
|
this.level = level;
|
|
25
|
-
if (!LOG_LEVEL_MAP[level]) {
|
|
18
|
+
if (!constants_js_1.LOG_LEVEL_MAP[level]) {
|
|
26
19
|
throw new Error(`Unknown level [${level}]`);
|
|
27
20
|
}
|
|
28
|
-
this.
|
|
29
|
-
console.error(
|
|
21
|
+
this.addEventListener(constants_js_1.LoggerEvents.error, (ev) => {
|
|
22
|
+
console.error(ev.error);
|
|
30
23
|
});
|
|
31
24
|
this.options = {
|
|
32
|
-
transports: options && options.transports !== undefined ? options.transports :
|
|
33
|
-
formatter: options && options.formatter !== undefined ? options.formatter :
|
|
25
|
+
transports: options && options.transports !== undefined ? options.transports : (0, constants_js_1.DEFAULT_LOGGER_TRANSPORTS)(),
|
|
26
|
+
formatter: options && options.formatter !== undefined ? options.formatter : constants_js_1.DEFAULT_LOGGER_FORMATTER,
|
|
34
27
|
};
|
|
35
28
|
}
|
|
29
|
+
setLevel(level) {
|
|
30
|
+
if (!constants_js_1.LOG_LEVEL_MAP[level]) {
|
|
31
|
+
throw new Error(`Unknown level [${level}]`);
|
|
32
|
+
}
|
|
33
|
+
this.level = level;
|
|
34
|
+
}
|
|
35
|
+
addTransport(transport) {
|
|
36
|
+
this.options.transports.push(transport);
|
|
37
|
+
}
|
|
36
38
|
write(args) {
|
|
37
39
|
try {
|
|
38
40
|
let eventArgs = undefined;
|
|
39
41
|
const tR = [];
|
|
40
42
|
for (const transport of this.options.transports) {
|
|
41
|
-
if (LOG_LEVEL_MAP[transport.level ? transport.level : this.level] >= LOG_LEVEL_MAP[args.level]) {
|
|
43
|
+
if (constants_js_1.LOG_LEVEL_MAP[transport.level ? transport.level : this.level] >= constants_js_1.LOG_LEVEL_MAP[args.level]) {
|
|
42
44
|
try {
|
|
43
45
|
eventArgs = eventArgs ? eventArgs : {
|
|
44
46
|
out: this.options.formatter({
|
|
@@ -51,16 +53,22 @@ class Logger extends events_1.EventEmitter {
|
|
|
51
53
|
tR.push(transport.write(eventArgs));
|
|
52
54
|
}
|
|
53
55
|
catch (e2) {
|
|
54
|
-
this.
|
|
56
|
+
this.dispatchEvent(new LoggerErrorEvent(constants_js_1.LoggerEvents.error, {
|
|
57
|
+
error: e2
|
|
58
|
+
}));
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
|
-
Promise.
|
|
59
|
-
this.
|
|
62
|
+
Promise.allSettled(tR).catch((e) => {
|
|
63
|
+
this.dispatchEvent(new LoggerErrorEvent(constants_js_1.LoggerEvents.error, {
|
|
64
|
+
error: e
|
|
65
|
+
}));
|
|
60
66
|
});
|
|
61
67
|
}
|
|
62
68
|
catch (e) {
|
|
63
|
-
this.
|
|
69
|
+
this.dispatchEvent(new LoggerErrorEvent(constants_js_1.LoggerEvents.error, {
|
|
70
|
+
error: e
|
|
71
|
+
}));
|
|
64
72
|
}
|
|
65
73
|
}
|
|
66
74
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|