@aomex/logger 0.0.17 → 0.0.19

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 CHANGED
@@ -1,131 +1,17 @@
1
1
  # @aomex/logger
2
2
 
3
- ## 0.0.17
3
+ ## 0.0.19
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - Updated dependencies [[`e9996cb`](https://github.com/aomex/aomex/commit/e9996cb33cc0ef70b42147be02fbbe2b4954e022)]:
8
- - @aomex/web@0.0.22
9
- - @aomex/core@0.0.21
7
+ - Updated dependencies [[`6621e4f`](https://github.com/aomex/aomex/commit/6621e4ff0f3509beeb332a0571a7db9c7d6ca99a), [`7b09277`](https://github.com/aomex/aomex/commit/7b09277136910966f500c8132303c7ddee84340c), [`46c5b72`](https://github.com/aomex/aomex/commit/46c5b72785011fa181767f4c8ea0d0f5008b21ae), [`9c78999`](https://github.com/aomex/aomex/commit/9c78999ebcb2962f30344acfbf6de0733d6fdd41), [`f4b012d`](https://github.com/aomex/aomex/commit/f4b012d98cddb2918479ea05df6c266dd914e53a)]:
8
+ - @aomex/web@0.0.24
9
+ - @aomex/core@0.0.23
10
10
 
11
- ## 0.0.16
11
+ ## 0.0.18
12
12
 
13
13
  ### Patch Changes
14
14
 
15
- - Updated dependencies [[`a8dbaf3`](https://github.com/aomex/aomex/commit/a8dbaf38524f5a5d58337bc617175115742f9e1a), [`ddcec91`](https://github.com/aomex/aomex/commit/ddcec91803b00f35f7c55964d1a2d97c40a22c6e)]:
16
- - @aomex/web@0.0.21
17
- - @aomex/core@0.0.20
18
-
19
- ## 0.0.15
20
-
21
- ### Patch Changes
22
-
23
- - Updated dependencies [[`6cbc69b`](https://github.com/aomex/aomex/commit/6cbc69bfce9d03b4880b94f9ec0ee20c9ae937a8)]:
24
- - @aomex/utility@0.0.7
25
- - @aomex/core@0.0.19
26
- - @aomex/web@0.0.20
27
-
28
- ## 0.0.14
29
-
30
- ### Patch Changes
31
-
32
- - Updated dependencies []:
33
- - @aomex/core@0.0.18
34
- - @aomex/web@0.0.19
35
-
36
- ## 0.0.13
37
-
38
- ### Patch Changes
39
-
40
- - Updated dependencies [[`37946d2`](https://github.com/aomex/aomex/commit/37946d2b91a560c851fa21fa327b5b968b794862)]:
41
- - @aomex/utility@0.0.6
42
- - @aomex/core@0.0.17
43
- - @aomex/web@0.0.18
44
-
45
- ## 0.0.12
46
-
47
- ### Patch Changes
48
-
49
- - Updated dependencies [[`216aa79`](https://github.com/aomex/aomex/commit/216aa798bcd2d0856f5fe4028235aa54d4b10922)]:
50
- - @aomex/web@0.0.17
51
- - @aomex/core@0.0.16
52
-
53
- ## 0.0.11
54
-
55
- ### Patch Changes
56
-
57
- - Updated dependencies [[`280359e`](https://github.com/aomex/aomex/commit/280359eb90b71720304cfc212374b6dc4c90236d), [`9881653`](https://github.com/aomex/aomex/commit/988165361439738d27bc29bd0ed5a85c608db5ef), [`15ecd1c`](https://github.com/aomex/aomex/commit/15ecd1c75bc21929f79980b32e63ea46e081cc6a)]:
58
- - @aomex/web@0.0.16
59
- - @aomex/core@0.0.15
60
-
61
- ## 0.0.10
62
-
63
- ### Patch Changes
64
-
65
- - Updated dependencies []:
66
- - @aomex/core@0.0.14
67
- - @aomex/web@0.0.15
68
-
69
- ## 0.0.9
70
-
71
- ### Patch Changes
72
-
73
- - [`ea0fd22`](https://github.com/aomex/aomex/commit/ea0fd22e50a5193f923910f3e433145361a55a9d) Thanks [@geekact](https://github.com/geekact)! - feat(logger): support ip token
74
-
75
- - Updated dependencies [[`715cf66`](https://github.com/aomex/aomex/commit/715cf660e3b275b338dcf15c25ba9523cf6fb204)]:
76
- - @aomex/web@0.0.14
77
- - @aomex/core@0.0.13
78
-
79
- ## 0.0.8
80
-
81
- ### Patch Changes
82
-
83
- - Updated dependencies [[`6e63df8`](https://github.com/aomex/aomex/commit/6e63df84d1b32e713aba8c52451e289a72b57bae), [`67acf4f`](https://github.com/aomex/aomex/commit/67acf4f42cd86bf1f0a7c647a5ff779c7cd0fa10)]:
84
- - @aomex/web@0.0.13
85
- - @aomex/core@0.0.12
86
-
87
- ## 0.0.7
88
-
89
- ### Patch Changes
90
-
91
- - Updated dependencies [[`f14eb5f`](https://github.com/aomex/aomex/commit/f14eb5fe7bf06e1e2f64d45e3546c96d6079a2f1), [`7d923cf`](https://github.com/aomex/aomex/commit/7d923cf971b6a23d50113ab104c67041c6489053), [`3c5ad4e`](https://github.com/aomex/aomex/commit/3c5ad4e2f34be7da54c8b63f6fd4b88fd1e593c4), [`6154ee5`](https://github.com/aomex/aomex/commit/6154ee5ed500984f969d4c335ac3ad800c739937), [`92273f9`](https://github.com/aomex/aomex/commit/92273f913e38543f34f41ec5941ff43b6e1caaa9), [`a0fd49e`](https://github.com/aomex/aomex/commit/a0fd49ef276c05f312257521c75887458857bbb7)]:
92
- - @aomex/web@0.0.12
93
- - @aomex/core@0.0.11
94
-
95
- ## 0.0.6
96
-
97
- ### Patch Changes
98
-
99
- - Updated dependencies [[`d9dc050`](https://github.com/aomex/aomex/commit/d9dc050897367353588c4bb778cbafb7607406ab), [`41ae605`](https://github.com/aomex/aomex/commit/41ae6054b2e87c4120f3fcee12302bb3d96f1df3)]:
100
- - @aomex/web@0.0.11
101
- - @aomex/core@0.0.10
102
-
103
- ## 0.0.5
104
-
105
- ### Patch Changes
106
-
107
- - Updated dependencies [[`fbb7ab2`](https://github.com/aomex/aomex/commit/fbb7ab2e6ec2a18e7ad8f018788523952049b6fd), [`2439286`](https://github.com/aomex/aomex/commit/2439286e69dcc7211ac280b91dbca5de95cc488b)]:
108
- - @aomex/utility@0.0.5
109
- - @aomex/web@0.0.10
110
- - @aomex/core@0.0.9
111
-
112
- ## 0.0.4
113
-
114
- ### Patch Changes
115
-
116
- - [`bf58245`](https://github.com/aomex/aomex/commit/bf58245b980d7b8a9e8aead48d298cb28be27aa9) Thanks [@geekact](https://github.com/geekact)! - feat(logger): add token enum
117
-
118
- - Updated dependencies [[`dcf2f4b`](https://github.com/aomex/aomex/commit/dcf2f4b6a13228d4731e4969e6abff9bef8ef0e2), [`d788ce7`](https://github.com/aomex/aomex/commit/d788ce731af885eae3819683813b2a9502742cea), [`c6fe6cb`](https://github.com/aomex/aomex/commit/c6fe6cb2f21acf74bc06db1509deff4aa35c48b3), [`a2727fb`](https://github.com/aomex/aomex/commit/a2727fb7619341dc3b23736e5dc11cee7d65e0bc), [`4253004`](https://github.com/aomex/aomex/commit/42530043854956a42d16be090b89fbac70e2fa17), [`fc7008e`](https://github.com/aomex/aomex/commit/fc7008e889a55e6a0a268bb77cab5be0f0bd9d30), [`d80df1a`](https://github.com/aomex/aomex/commit/d80df1a07eb92214a9c3c7fa15c629ea1ee88c43)]:
119
- - @aomex/web@0.0.9
120
- - @aomex/core@0.0.8
121
-
122
- ## 0.0.2
123
-
124
- ### Patch Changes
125
-
126
- - [`bbc8dda`](https://github.com/aomex/aomex/commit/bbc8dda9f0876c3402608133085350cc693def6f) Thanks [@geekact](https://github.com/geekact)! - refactor: rename package helper to utility
127
-
128
- - Updated dependencies [[`bbc8dda`](https://github.com/aomex/aomex/commit/bbc8dda9f0876c3402608133085350cc693def6f), [`ceb2d82`](https://github.com/aomex/aomex/commit/ceb2d8269755dca3f79b755224259b8a99bc65d0)]:
129
- - @aomex/utility@0.0.4
130
- - @aomex/core@0.0.6
131
- - @aomex/web@0.0.7
15
+ - Updated dependencies [[`6f7d706`](https://github.com/aomex/aomex/commit/6f7d7066c23711abdd149eb1c9a293ab8c4284a4), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`e7bf93c`](https://github.com/aomex/aomex/commit/e7bf93cee6896c61d0bf3eb0921151dc6c1bc107), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`818e840`](https://github.com/aomex/aomex/commit/818e840d36c7456a863fc071968b246c123c17f5), [`fb6c72d`](https://github.com/aomex/aomex/commit/fb6c72dbb266be4db92a542afe93dfa5d8c7cd41)]:
16
+ - @aomex/core@0.0.22
17
+ - @aomex/web@0.0.23
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ var Counter = class extends Transform {
16
16
 
17
17
  // src/format.ts
18
18
  import prettyTime from "pretty-time";
19
- import { bytes, chalk } from "@aomex/utility";
19
+ import { bytes, chalk } from "@aomex/core";
20
20
  import { statuses } from "@aomex/web";
21
21
 
22
22
  // src/format-token.ts
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/logger.ts","../src/counter.ts","../src/format.ts","../src/format-token.ts"],"sourcesContent":["import { Readable } from 'node:stream';\nimport util from 'node:util';\nimport { middleware } from '@aomex/core';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { Counter } from './counter';\nimport { format } from './format';\nimport { FormatToken } from './format-token';\n\ntype Printer = (message: string, ...args: any[]) => any;\n\nexport interface LoggerOptions {\n /**\n * Defaults ` [request] [ip] [method] [url]`\n */\n requestFormat?: string | false;\n /**\n * Defaults ` [response] [ip] [method] [url] [statusCode] [time] [contentLength]`\n */\n responseFormat?: string | false;\n printer?: Printer;\n customTokens?: { [key: string]: (ctx: WebContext) => string };\n}\n\nconst defaultPrinter: Printer = (message, ...args) => {\n console.log(util.format(message, ...args));\n};\n\nconst defaultRequestFormat = ` ${FormatToken.request} ${FormatToken.ip} ${FormatToken.method} ${FormatToken.url}`;\nconst defaultResponseFormat = ` ${FormatToken.response} ${FormatToken.ip} ${FormatToken.method} ${FormatToken.url} ${FormatToken.statusCode} ${FormatToken.time} ${FormatToken.contentLength}`;\n\nexport const logger = (options: LoggerOptions = {}): WebMiddleware => {\n const {\n requestFormat = defaultRequestFormat,\n responseFormat = defaultResponseFormat,\n printer = defaultPrinter,\n customTokens,\n } = options;\n\n return middleware.web(async (ctx, next) => {\n const start = process.hrtime();\n const { request, response } = ctx;\n const tokens = customTokens\n ? Object.fromEntries(\n Object.entries(customTokens).map(([key, value]) => {\n return [key, value.bind(null, ctx)];\n }),\n )\n : {};\n\n if (requestFormat) {\n printer(\n ...format({\n message: requestFormat!,\n tokens,\n startTime: start,\n request,\n }),\n );\n }\n\n if (!responseFormat) return next();\n\n ctx.app.on('error', onError);\n await next();\n ctx.app.off('error', onError);\n\n const body = response.body;\n let counter: Counter;\n\n if (body instanceof Readable && body.readable) {\n ctx.send(body.pipe((counter = new Counter())));\n }\n\n const onfinish = onDone.bind(null, 'finish');\n const onclose = onDone.bind(null, 'close');\n response.once('finish', onfinish);\n response.once('close', onclose);\n\n function onError() {\n printer(\n ...format({\n message: responseFormat as string,\n tokens,\n startTime: start,\n request,\n response,\n }),\n );\n }\n\n function onDone(event: 'finish' | 'close') {\n response.off('finish', onfinish);\n response.off('close', onclose);\n printer(\n ...format({\n message: responseFormat as string,\n tokens,\n startTime: start,\n request,\n response: {\n statusCode: response.statusCode,\n contentType: response.contentType,\n contentLength: counter ? counter.length : response.contentLength,\n },\n finished: event === 'finish',\n }),\n );\n }\n });\n};\n","import { Transform, type TransformCallback } from 'node:stream';\n\nexport class Counter extends Transform {\n public length: number = 0;\n\n override _transform(\n chunk: any,\n _encoding: BufferEncoding,\n callback: TransformCallback,\n ): void {\n this.length += chunk.length;\n this.push(chunk);\n callback();\n }\n}\n","import prettyTime from 'pretty-time';\nimport { bytes, chalk } from '@aomex/utility';\nimport { statuses } from '@aomex/web';\nimport { FormatToken } from './format-token';\n\nexport const colorCodes = <const>{\n 7: 'magenta',\n 5: 'red',\n 4: 'yellow',\n 3: 'cyan',\n 2: 'green',\n 1: 'green',\n 0: 'yellow',\n};\n\nexport const format = (opts: {\n message: string;\n startTime: [number, number];\n tokens?: { [key: string]: () => string };\n finished?: boolean;\n request: {\n method: string;\n url: string;\n ip: string;\n };\n response?: {\n statusCode?: number;\n contentType?: string;\n contentLength?: number;\n };\n}): [message: string, ...args: any[]] => {\n const {\n message,\n startTime,\n request,\n response = {},\n tokens = {},\n finished,\n } = opts;\n const { method, url } = request;\n const { contentLength, contentType = '-', statusCode = 404 } = response;\n\n const args: string[] = [];\n const formatted = message.replaceAll(/\\[[^\\]]+\\]/g, (token) => {\n token = token.slice(1, -1);\n\n if (tokens[token]) {\n args.push(tokens[token]!());\n return '%s';\n }\n\n switch (`[${token}]`) {\n case FormatToken.request:\n args.push('<--');\n return chalk.gray('%s');\n case FormatToken.response:\n if (statusCode >= 400) {\n args.push('xxx');\n return chalk.red('%s');\n }\n if (finished === false) {\n args.push('-x-');\n return chalk.yellow('%s');\n }\n\n args.push('-->');\n return chalk.gray('%s');\n case FormatToken.method:\n args.push(method);\n return chalk.bold('%s');\n case FormatToken.url:\n args.push(url);\n return chalk.gray('%s');\n case FormatToken.statusCode:\n const c =\n colorCodes[((statusCode / 100) | 0) as keyof typeof colorCodes] ||\n colorCodes[0]!;\n args.push(statusCode.toString());\n return chalk[c]('%s');\n case FormatToken.contentLength:\n args.push(\n statuses.empty[statusCode] || contentLength === 0\n ? '0'\n : contentLength == null\n ? '-'\n : bytes(contentLength).toLowerCase(),\n );\n return chalk.gray('%s');\n case FormatToken.contentType:\n args.push(contentType);\n return chalk.gray('%s');\n case FormatToken.time:\n args.push(prettyTime(process.hrtime(startTime)));\n return chalk.gray('%s');\n case FormatToken.ip:\n args.push(request.ip);\n return chalk.gray('%s');\n default:\n return `[${token}]`;\n }\n });\n\n return [formatted, ...args];\n};\n","export enum FormatToken {\n request = '[request]',\n response = '[response]',\n method = '[method]',\n url = '[url]',\n statusCode = '[statusCode]',\n contentLength = '[contentLength]',\n contentType = '[contentType]',\n time = '[time]',\n ip = '[ip]',\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,SAAS,kBAAkB;;;ACF3B,SAAS,iBAAyC;AAE3C,IAAM,UAAN,cAAsB,UAAU;AAAA,EAC9B,SAAiB;AAAA,EAEf,WACP,OACA,WACA,UACM;AACN,SAAK,UAAU,MAAM;AACrB,SAAK,KAAK,KAAK;AACf,aAAS;AAAA,EACX;AACF;;;ACdA,OAAO,gBA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AAA,MACH;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":["FormatToken"]}
1
+ {"version":3,"sources":["../src/logger.ts","../src/counter.ts","../src/format.ts","../src/format-token.ts"],"sourcesContent":["import { Readable } from 'node:stream';\nimport util from 'node:util';\nimport { middleware } from '@aomex/core';\nimport type { WebContext, WebMiddleware } from '@aomex/web';\nimport { Counter } from './counter';\nimport { format } from './format';\nimport { FormatToken } from './format-token';\n\ntype Printer = (message: string, ...args: any[]) => any;\n\nexport interface LoggerOptions {\n /**\n * Defaults ` [request] [ip] [method] [url]`\n */\n requestFormat?: string | false;\n /**\n * Defaults ` [response] [ip] [method] [url] [statusCode] [time] [contentLength]`\n */\n responseFormat?: string | false;\n printer?: Printer;\n customTokens?: { [key: string]: (ctx: WebContext) => string };\n}\n\nconst defaultPrinter: Printer = (message, ...args) => {\n console.log(util.format(message, ...args));\n};\n\nconst defaultRequestFormat = ` ${FormatToken.request} ${FormatToken.ip} ${FormatToken.method} ${FormatToken.url}`;\nconst defaultResponseFormat = ` ${FormatToken.response} ${FormatToken.ip} ${FormatToken.method} ${FormatToken.url} ${FormatToken.statusCode} ${FormatToken.time} ${FormatToken.contentLength}`;\n\nexport const logger = (options: LoggerOptions = {}): WebMiddleware => {\n const {\n requestFormat = defaultRequestFormat,\n responseFormat = defaultResponseFormat,\n printer = defaultPrinter,\n customTokens,\n } = options;\n\n return middleware.web(async (ctx, next) => {\n const start = process.hrtime();\n const { request, response } = ctx;\n const tokens = customTokens\n ? Object.fromEntries(\n Object.entries(customTokens).map(([key, value]) => {\n return [key, value.bind(null, ctx)];\n }),\n )\n : {};\n\n if (requestFormat) {\n printer(\n ...format({\n message: requestFormat!,\n tokens,\n startTime: start,\n request,\n }),\n );\n }\n\n if (!responseFormat) return next();\n\n ctx.app.on('error', onError);\n await next();\n ctx.app.off('error', onError);\n\n const body = response.body;\n let counter: Counter;\n\n if (body instanceof Readable && body.readable) {\n ctx.send(body.pipe((counter = new Counter())));\n }\n\n const onfinish = onDone.bind(null, 'finish');\n const onclose = onDone.bind(null, 'close');\n response.once('finish', onfinish);\n response.once('close', onclose);\n\n function onError() {\n printer(\n ...format({\n message: responseFormat as string,\n tokens,\n startTime: start,\n request,\n response,\n }),\n );\n }\n\n function onDone(event: 'finish' | 'close') {\n response.off('finish', onfinish);\n response.off('close', onclose);\n printer(\n ...format({\n message: responseFormat as string,\n tokens,\n startTime: start,\n request,\n response: {\n statusCode: response.statusCode,\n contentType: response.contentType,\n contentLength: counter ? counter.length : response.contentLength,\n },\n finished: event === 'finish',\n }),\n );\n }\n });\n};\n","import { Transform, type TransformCallback } from 'node:stream';\n\nexport class Counter extends Transform {\n public length: number = 0;\n\n override _transform(\n chunk: any,\n _encoding: BufferEncoding,\n callback: TransformCallback,\n ): void {\n this.length += chunk.length;\n this.push(chunk);\n callback();\n }\n}\n","import prettyTime from 'pretty-time';\nimport { bytes, chalk } from '@aomex/core';\nimport { statuses } from '@aomex/web';\nimport { FormatToken } from './format-token';\n\nexport const colorCodes = <const>{\n 7: 'magenta',\n 5: 'red',\n 4: 'yellow',\n 3: 'cyan',\n 2: 'green',\n 1: 'green',\n 0: 'yellow',\n};\n\nexport const format = (opts: {\n message: string;\n startTime: [number, number];\n tokens?: { [key: string]: () => string };\n finished?: boolean;\n request: {\n method: string;\n url: string;\n ip: string;\n };\n response?: {\n statusCode?: number;\n contentType?: string;\n contentLength?: number;\n };\n}): [message: string, ...args: any[]] => {\n const {\n message,\n startTime,\n request,\n response = {},\n tokens = {},\n finished,\n } = opts;\n const { method, url } = request;\n const { contentLength, contentType = '-', statusCode = 404 } = response;\n\n const args: string[] = [];\n const formatted = message.replaceAll(/\\[[^\\]]+\\]/g, (token) => {\n token = token.slice(1, -1);\n\n if (tokens[token]) {\n args.push(tokens[token]!());\n return '%s';\n }\n\n switch (`[${token}]`) {\n case FormatToken.request:\n args.push('<--');\n return chalk.gray('%s');\n case FormatToken.response:\n if (statusCode >= 400) {\n args.push('xxx');\n return chalk.red('%s');\n }\n if (finished === false) {\n args.push('-x-');\n return chalk.yellow('%s');\n }\n\n args.push('-->');\n return chalk.gray('%s');\n case FormatToken.method:\n args.push(method);\n return chalk.bold('%s');\n case FormatToken.url:\n args.push(url);\n return chalk.gray('%s');\n case FormatToken.statusCode:\n const c =\n colorCodes[((statusCode / 100) | 0) as keyof typeof colorCodes] ||\n colorCodes[0]!;\n args.push(statusCode.toString());\n return chalk[c]('%s');\n case FormatToken.contentLength:\n args.push(\n statuses.empty[statusCode] || contentLength === 0\n ? '0'\n : contentLength == null\n ? '-'\n : bytes(contentLength).toLowerCase(),\n );\n return chalk.gray('%s');\n case FormatToken.contentType:\n args.push(contentType);\n return chalk.gray('%s');\n case FormatToken.time:\n args.push(prettyTime(process.hrtime(startTime)));\n return chalk.gray('%s');\n case FormatToken.ip:\n args.push(request.ip);\n return chalk.gray('%s');\n default:\n return `[${token}]`;\n }\n });\n\n return [formatted, ...args];\n};\n","export enum FormatToken {\n request = '[request]',\n response = '[response]',\n method = '[method]',\n url = '[url]',\n statusCode = '[statusCode]',\n contentLength = '[contentLength]',\n contentType = '[contentType]',\n time = '[time]',\n ip = '[ip]',\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,SAAS,kBAAkB;;;ACF3B,SAAS,iBAAyC;AAE3C,IAAM,UAAN,cAAsB,UAAU;AAAA,EAC9B,SAAiB;AAAA,EAEf,WACP,OACA,WACA,UACM;AACN,SAAK,UAAU,MAAM;AACrB,SAAK,KAAK,KAAK;AACf,aAAS;AAAA,EACX;AACF;;;ACdA,OAAO,gBAAgB;AACvB,SAAS,OAAO,aAAa;AAC7B,SAAS,gBAAgB;;;ACFlB,IAAK,cAAL,kBAAKA,iBAAL;AACL,EAAAA,aAAA,aAAU;AACV,EAAAA,aAAA,cAAW;AACX,EAAAA,aAAA,YAAS;AACT,EAAAA,aAAA,SAAM;AACN,EAAAA,aAAA,gBAAa;AACb,EAAAA,aAAA,mBAAgB;AAChB,EAAA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"version": "0.0.19",
4
4
  "description": "web request log",
5
5
  "keywords": [
6
6
  "api-request-logger"
@@ -29,16 +29,15 @@
29
29
  "access": "public"
30
30
  },
31
31
  "peerDependencies": {
32
- "@aomex/core": "^0.0.21",
33
- "@aomex/web": "^0.0.22"
32
+ "@aomex/core": "^0.0.23",
33
+ "@aomex/web": "^0.0.24"
34
34
  },
35
35
  "dependencies": {
36
- "@aomex/utility": "0.0.7",
37
36
  "pretty-time": "^1.1.0"
38
37
  },
39
38
  "devDependencies": {
40
- "@aomex/core": "^0.0.21",
41
- "@aomex/web": "^0.0.22",
39
+ "@aomex/core": "^0.0.23",
40
+ "@aomex/web": "^0.0.24",
42
41
  "@types/pretty-time": "^1.1.2",
43
42
  "strip-ansi": "^7.0.1"
44
43
  },