logixlysia 2.0.1 โ†’ 2.0.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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.0.2](https://github.com/PunGrumpy/logixlysia/compare/v2.0.1...v2.0.2) (2024-01-07)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **logger:** change time from `iso` to `local` ([33ff28a](https://github.com/PunGrumpy/logixlysia/commit/33ff28a4a41db1dda7ca224e4807bd2451b95985))
9
+
3
10
  ## [2.0.1](https://github.com/PunGrumpy/logixlysia/compare/v2.0.0...v2.0.1) (2024-01-03)
4
11
 
5
12
 
@@ -9,11 +9,17 @@ const isPnpm = existsSync(join(process.cwd(), 'pnpm-lock.yaml'))
9
9
  const packageManager = isBun ? 'bun' : isYarn ? 'yarn' : isPnpm ? 'pnpm' : 'npm'
10
10
 
11
11
  const options = {
12
+ // TypeScript & JavaScript files
12
13
  '**/*.(ts|tsx)': () => `${packageManager} tsc --noEmit`,
13
14
  '**/*.(ts|tsx|js)': filenames => [
14
15
  `${packageManager} eslint --fix ${filenames.join(' ')}`,
15
16
  `${packageManager} prettier --write ${filenames.join(' ')}`
16
17
  ],
18
+ '**/*.(css|less|scss)': filenames =>
19
+ `${packageManager} test --timeout 5000 --coverage --update-snapshots ${filenames.join(
20
+ ' '
21
+ )}`,
22
+ // Markdown & JSON files
17
23
  '**/*.(md|json)': filenames =>
18
24
  `${packageManager} prettier --write ${filenames.join(' ')}`
19
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "๐ŸฆŠ Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -15,7 +15,10 @@
15
15
  },
16
16
  "license": "MIT",
17
17
  "scripts": {
18
- "test": "echo \"Error: no test specified\" && exit 1",
18
+ "pretest": "bun run lint && bun run prettier && bun run lint:fix",
19
+ "test": "bun test --timeout 5000 --coverage --update-snapshots",
20
+ "prepublish": "bun run test",
21
+ "publish": "bun run prepublish && npm publish",
19
22
  "dev": "bun run --watch example/basic.ts",
20
23
  "prepare": "husky install",
21
24
  "lint": "eslint . --ext .ts",
@@ -66,17 +69,17 @@
66
69
  "middleware"
67
70
  ],
68
71
  "dependencies": {
69
- "@typescript-eslint/eslint-plugin": "^6.13.2",
70
- "@typescript-eslint/parser": "^6.13.2",
72
+ "@typescript-eslint/eslint-plugin": "^6.17.0",
73
+ "@typescript-eslint/parser": "^6.17.0",
71
74
  "chalk": "^5.3.0",
72
- "elysia": "^0.8.3",
73
- "eslint": "^8.55.0"
75
+ "elysia": "^0.8.8",
76
+ "eslint": "^8.56.0"
74
77
  },
75
78
  "devDependencies": {
76
- "bun-types": "^1.0.15",
79
+ "bun-types": "^1.0.21",
77
80
  "husky": "^8.0.3",
78
81
  "lint-staged": "^15.2.0",
79
- "prettier": "^3.1.0"
82
+ "prettier": "^3.1.1"
80
83
  },
81
84
  "peerDependencies": {
82
85
  "typescript": "^5.0.0"
package/src/logger.ts CHANGED
@@ -25,11 +25,7 @@ function log(
25
25
  store: StoreData
26
26
  ): void {
27
27
  const logStr: string[] = []
28
- const nowStr = chalk.bgYellow(
29
- chalk.black(
30
- new Date().toISOString().replace('T', ' ').replace('Z', '').slice(0, -4)
31
- )
32
- )
28
+ const nowStr = chalk.bgYellow(chalk.black(new Date().toLocaleString()))
33
29
  const levelStr = logString(level)
34
30
  const durationStr = durationString(store.beforeTime)
35
31
  const methodStr = methodString(request.method)
@@ -29,8 +29,6 @@ function startString(config: Server): void {
29
29
  const boxWidth = Math.max(title.length, messageWidth) + 4
30
30
  const border = 'โ”€'.repeat(boxWidth)
31
31
 
32
- process.stdout.write('\x1Bc')
33
-
34
32
  console.log(`
35
33
  โ”Œ${border}โ”
36
34
  โ”‚${createBoxText('', boxWidth)} โ”‚
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { ColorMap } from '~/types/ColorMap'
3
+
4
+ describe('Color Mapping Interface', () => {
5
+ it('Defines an object with string keys mapping to functions', () => {
6
+ const colorMap: ColorMap = {
7
+ red: (str: string) => `Red: ${str}`,
8
+ green: (str: string) => `Green: ${str}`,
9
+ blue: (str: string) => `Blue: ${str}`
10
+ }
11
+
12
+ expect(colorMap).toEqual(
13
+ expect.objectContaining({
14
+ red: expect.any(Function),
15
+ green: expect.any(Function),
16
+ blue: expect.any(Function)
17
+ })
18
+ )
19
+
20
+ Object.keys(colorMap).forEach(key => {
21
+ expect(typeof colorMap[key]).toBe('function')
22
+ })
23
+ })
24
+ })
@@ -0,0 +1,92 @@
1
+ import { beforeEach, describe, expect, it, jest } from 'bun:test'
2
+ import { LogData } from '~/types/Logger'
3
+ import { RequestInfo } from '~/types/RequestInfo'
4
+ import { StoreData } from '~/types/StoreData'
5
+
6
+ interface Logger {
7
+ info(request: RequestInfo, data: LogData, store: StoreData): void
8
+ warning(request: RequestInfo, data: LogData, store: StoreData): void
9
+ error(request: RequestInfo, data: LogData, store: StoreData): void
10
+ }
11
+
12
+ describe('Logger interface', () => {
13
+ let logger: Logger
14
+
15
+ beforeEach(() => {
16
+ logger = {
17
+ info: jest.fn(),
18
+ warning: jest.fn(),
19
+ error: jest.fn()
20
+ }
21
+ })
22
+
23
+ it('Defines the Logger interface correctly', () => {
24
+ expect(logger).toEqual(
25
+ expect.objectContaining({
26
+ info: expect.any(Function),
27
+ warning: expect.any(Function),
28
+ error: expect.any(Function)
29
+ })
30
+ )
31
+ })
32
+
33
+ it('Calls the info log function with the correct arguments', () => {
34
+ const request: RequestInfo = {
35
+ url: '/info',
36
+ method: 'GET',
37
+ headers: {
38
+ get: function () {
39
+ throw new Error('Function not implemented.')
40
+ }
41
+ }
42
+ }
43
+ const data: LogData = { status: 200, message: 'Info log message' }
44
+ const store: StoreData = {
45
+ beforeTime: 0n
46
+ }
47
+
48
+ logger.info(request, data, store)
49
+
50
+ expect(logger.info).toHaveBeenCalledWith(request, data, store)
51
+ })
52
+
53
+ it('Calls the warning log function with the correct arguments', () => {
54
+ const request: RequestInfo = {
55
+ url: '/warning',
56
+ method: 'POST',
57
+ headers: {
58
+ get: function () {
59
+ throw new Error('Function not implemented.')
60
+ }
61
+ }
62
+ }
63
+ const data: LogData = { status: 404, message: 'Warning log message' }
64
+ const store: StoreData = {
65
+ beforeTime: 0n
66
+ }
67
+
68
+ logger.warning(request, data, store)
69
+
70
+ expect(logger.warning).toHaveBeenCalledWith(request, data, store)
71
+ })
72
+
73
+ it('Calls the error log function with the correct arguments', () => {
74
+ const request: RequestInfo = {
75
+ url: '/error',
76
+ method: 'DELETE',
77
+ headers: {
78
+ get: function () {
79
+ throw new Error('Function not implemented.')
80
+ }
81
+ }
82
+ }
83
+ const data: LogData = { status: 500, message: 'Error log message' }
84
+ const store: StoreData = {
85
+ beforeTime: 0n
86
+ }
87
+
88
+ logger.error(request, data, store)
89
+
90
+ expect(logger.error).toHaveBeenCalledWith(request, data, store)
91
+ })
92
+ })
@@ -0,0 +1,22 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { RequestInfo } from '~/types/RequestInfo'
3
+
4
+ describe('Request Infomation interface', () => {
5
+ it('Defines the RequestInfo interface correctly', () => {
6
+ const headers = { get: () => 'value' }
7
+ const method = 'GET'
8
+ const url = 'https://example.com/api'
9
+
10
+ const request: RequestInfo = { headers, method, url }
11
+
12
+ expect(request).toEqual(
13
+ expect.objectContaining({
14
+ headers: expect.objectContaining({
15
+ get: expect.any(Function)
16
+ }),
17
+ method: expect.any(String),
18
+ url: expect.any(String)
19
+ })
20
+ )
21
+ })
22
+ })
@@ -0,0 +1,26 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { Server } from '~/types/Server'
3
+
4
+ describe('Server interface', () => {
5
+ it('Defines the Server interface correctly', () => {
6
+ const server: Server = {
7
+ hostname: 'example.com',
8
+ port: 8080,
9
+ protocol: 'https'
10
+ }
11
+
12
+ expect(server).toEqual(
13
+ expect.objectContaining({
14
+ hostname: expect.any(String),
15
+ port: expect.any(Number),
16
+ protocol: expect.any(String)
17
+ })
18
+ )
19
+ })
20
+
21
+ it('Allows optional properties in the Server interface', () => {
22
+ const serverWithoutOptionalProps: Server = {}
23
+
24
+ expect(serverWithoutOptionalProps).toEqual(expect.objectContaining({}))
25
+ })
26
+ })
@@ -0,0 +1,16 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { StoreData } from '~/types/StoreData'
3
+
4
+ describe('Store Data interface', () => {
5
+ it('Defines the StoreData interface correctly', () => {
6
+ const beforeTime: bigint = BigInt(1633393533526) // Example bigint value
7
+
8
+ const storeData: StoreData = { beforeTime }
9
+
10
+ expect(storeData).toEqual(
11
+ expect.objectContaining({
12
+ beforeTime: expect.any(BigInt)
13
+ })
14
+ )
15
+ })
16
+ })
@@ -0,0 +1,20 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import durationString from '~/utils/duration'
3
+
4
+ describe('Duration String', () => {
5
+ it('Returns a duration string with different units', () => {
6
+ const testCases = [
7
+ [1e9, 's'],
8
+ [1e6, 'ms'],
9
+ [1e3, 'ยตs'],
10
+ [1, 'ns']
11
+ ]
12
+
13
+ for (const [nanoseconds, unit] of testCases) {
14
+ const beforeTime = process.hrtime.bigint() - BigInt(String(nanoseconds))
15
+
16
+ const result = durationString(beforeTime)
17
+ expect(result).toMatch(String(unit))
18
+ }
19
+ })
20
+ })
@@ -0,0 +1,25 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import chalk from 'chalk'
3
+ import logString from '~/utils/log'
4
+
5
+ describe('Log String', () => {
6
+ it('Returns a colored string for INFO log level', () => {
7
+ const result = logString('INFO')
8
+ expect(result).toBe(chalk.bgGreen.black('INFO '))
9
+ })
10
+
11
+ it('Returns a colored string for WARNING log level', () => {
12
+ const result = logString('WARNING')
13
+ expect(result).toBe(chalk.bgYellow.black('WARNING'))
14
+ })
15
+
16
+ it('Returns a colored string for ERROR log level', () => {
17
+ const result = logString('ERROR')
18
+ expect(result).toBe(chalk.bgRed.black('ERROR '))
19
+ })
20
+
21
+ it('Returns the input string if log level is not recognized', () => {
22
+ const result = logString('DEBUG') // Assuming 'DEBUG' is not in the colorMap
23
+ expect(result).toBe('DEBUG') // No coloring, returns the original string
24
+ })
25
+ })
@@ -0,0 +1,20 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import chalk from 'chalk'
3
+ import methodString from '~/utils/method'
4
+
5
+ describe('Method String', () => {
6
+ it('Returns a colored string for GET method', () => {
7
+ const result = methodString('GET')
8
+ expect(result).toBe(chalk.white('GET '))
9
+ })
10
+
11
+ it('Returns a colored string for POST method', () => {
12
+ const result = methodString('POST')
13
+ expect(result).toBe(chalk.yellow('POST '))
14
+ })
15
+
16
+ it('Returns the input string if method is not recognized', () => {
17
+ const result = methodString('INVALID_METHOD')
18
+ expect(result).toBe('INVALID_METHOD') // No coloring, returns the original string
19
+ })
20
+ })
@@ -0,0 +1,10 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import pathString from '~/utils/path'
3
+
4
+ describe('Path String', () => {
5
+ it('Returns the path string from a URL', () => {
6
+ const testPath: any = { url: 'https://www.example.com/path/to/resource' }
7
+ const result = pathString(testPath)
8
+ expect(result).toMatch('/path/to/resource')
9
+ })
10
+ })
@@ -0,0 +1,35 @@
1
+ import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test'
2
+ import { Server } from '~/types/Server'
3
+ import startString from '~/utils/start'
4
+
5
+ describe('Start String', () => {
6
+ let originalConsoleLog: any
7
+ let mockConsoleLog: jest.Mock
8
+
9
+ beforeEach(() => {
10
+ originalConsoleLog = console.log
11
+ mockConsoleLog = jest.fn()
12
+ console.log = mockConsoleLog
13
+ })
14
+
15
+ afterEach(() => {
16
+ console.log = originalConsoleLog
17
+ })
18
+
19
+ it('Logs the expected server start message', () => {
20
+ const config: Server = {
21
+ hostname: 'localhost',
22
+ port: 3000,
23
+ protocol: 'http'
24
+ }
25
+
26
+ startString(config)
27
+
28
+ const expectedMessage = `๐ŸฆŠ Elysia is running at http://localhost:3000`
29
+
30
+ // Extract the arguments passed to console.log during the function call
31
+ const [[logMessage]] = mockConsoleLog.mock.calls
32
+
33
+ expect(logMessage).toMatch(expectedMessage)
34
+ })
35
+ })
@@ -0,0 +1,30 @@
1
+ import chalk from 'chalk'
2
+ import { describe, expect, it } from 'bun:test'
3
+ import statusString from '~/utils/status'
4
+
5
+ describe('Status String', () => {
6
+ it('Returns the status string for 200 status code', () => {
7
+ const result = statusString(200)
8
+ expect(result).toBe(chalk.green('200'))
9
+ })
10
+
11
+ it('Returns the status string for 301 status code', () => {
12
+ const result = statusString(301)
13
+ expect(result).toBe(chalk.cyan('301'))
14
+ })
15
+
16
+ it('Returns the status string for 404 status code', () => {
17
+ const result = statusString(404)
18
+ expect(result).toBe(chalk.yellow('404'))
19
+ })
20
+
21
+ it('Returns the status string for 500 status code', () => {
22
+ const result = statusString(500)
23
+ expect(result).toBe(chalk.red('500'))
24
+ })
25
+
26
+ it('Returns the status string for 100 status code', () => {
27
+ const result = statusString(100)
28
+ expect(result).toBe('100')
29
+ })
30
+ })