Skip to content

Commit

Permalink
feat: add event logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mav-rik committed Apr 11, 2023
1 parent 6468d1b commit 7ced254
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/event-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@wooksjs/event-core": "0.2.13"
},
"dependencies": {
"minimist": "^1.2.6"
"minimist": "^1.2.6",
"@prostojs/logger": "^0.3.2"
},
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-cli#readme"
}
9 changes: 8 additions & 1 deletion packages/event-cli/src/cli-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TWooksHandler, Wooks, WooksAdapterBase } from 'wooks'
import { createCliContext } from './event-cli'
import { TConsoleBase } from '@prostojs/logger'
import { TEventOptions } from '@wooksjs/event-core'

export const cliShortcuts = {
cli: 'CLI',
Expand All @@ -8,11 +10,16 @@ export const cliShortcuts = {
export interface TWooksCliOptions {
onError?(e: Error): void
onUnknownParams?(pathParams: string[]): void
logger?: TConsoleBase
eventOptions?: TEventOptions
}

export class WooksCli extends WooksAdapterBase {
protected logger: TConsoleBase

constructor(protected opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase) {
super(wooks)
this.logger = opts?.logger || this.getLogger('wooks-cli')
}

cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>) {
Expand All @@ -24,7 +31,7 @@ export class WooksCli extends WooksAdapterBase {
const firstFlagIndex = argv.findIndex(a => a.startsWith('-')) + 1
const pathParams = (firstFlagIndex ? argv.slice(0, firstFlagIndex - 1) : argv)
const path = '/' + pathParams.map(v => encodeURIComponent(v)).join('/')
const { restoreCtx, clearCtx } = createCliContext({ argv })
const { restoreCtx, clearCtx } = createCliContext({ argv }, this.opts?.eventOptions || {})
const handlers = this.wooks.lookup('CLI', path)
if (handlers) {
try {
Expand Down
5 changes: 3 additions & 2 deletions packages/event-cli/src/event-cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createEventContext, useEventContext } from '@wooksjs/event-core'
import { TEventOptions, createEventContext, useEventContext } from '@wooksjs/event-core'
import { TCliContextStore, TCliEventData } from './types'

export function createCliContext(data: TCliEventData) {
export function createCliContext(data: TCliEventData, options: TEventOptions) {
return createEventContext<TCliContextStore>({
event: {
...data,
type: 'CLI',
},
options,
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/event-http/src/errors/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('response', () => {
Object.defineProperty(res, 'writableEnded', { value: false })

beforeEach(() => {
createHttpContext({ req, res })
createHttpContext({ req, res }, {})
})

it('must create error-response in json', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/event-http/src/response/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseHttpResponse } from './core'
import { panic } from 'common/panic'

export class BaseHttpResponseRenderer<T = unknown> implements TWooksResponseRenderer<T> {
render(response: BaseHttpResponse<T>): string | Uint8Array {
Expand All @@ -17,7 +16,7 @@ export class BaseHttpResponseRenderer<T = unknown> implements TWooksResponseRend
if (!response.getContentType()) response.setContentType('application/json')
return JSON.stringify(response.body)
}
throw panic('Unsupported body format "' + typeof response.body + '"')
throw new Error('Unsupported body format "' + typeof response.body + '"')
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/event-http/src/response/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('response', () => {
const res = new ServerResponse(req)

beforeEach(() => {
createHttpContext({ req, res })
createHttpContext({ req, res }, {})
})

it('must create response from json', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/event-http/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function setTestHttpContext(options: TTestHttpContext) {
req.headers = options.headers || {}
req.url = options.url
const res = new ServerResponse(req)
const { store } = createHttpContext({ req, res })
const { store } = createHttpContext({ req, res }, {})
store('routeParams').value = options.params
if (options.cachedContext) {
for (const key of Object.keys(options.cachedContext)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/event-http/src/utils/cache-control.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { panic } from 'common/panic'
import { convertTime, TTimeMultiString } from './time'

export type TCacheControl = {
Expand All @@ -24,7 +23,7 @@ export function renderCacheControl(data: TCacheControl) {
attrs += attrs ? ', ' + val : val
}
} else {
panic('Unknown Cache-Control attribute ' + a)
throw new Error('Unknown Cache-Control attribute ' + a)
}
}
return attrs
Expand Down
3 changes: 1 addition & 2 deletions packages/event-http/src/utils/set-cookie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { panic } from 'common/panic'
import { TCookieAttributes, TSetCookieData } from '../types'
import { convertTime } from './time'

Expand All @@ -10,7 +9,7 @@ export function renderCookie(key: string, data: TSetCookieData) {
const val = func(v)
attrs += val ? '; ' + val : ''
} else {
panic('Unknown Set-Cookie attribute ' + a)
throw new Error('Unknown Set-Cookie attribute ' + a)
}
}
return `${key}=${encodeURIComponent(data.value)}${attrs}`
Expand Down
3 changes: 1 addition & 2 deletions packages/http-body/src/body.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useHeaders, useRequest, EHttpStatusCode, HttpError, WooksURLSearchParams, useHttpContext } from '@wooksjs/event-http'
import { panic } from 'common/panic'
import { compressors, TBodyCompressor, uncompressBody } from './utils/body-compressor'

type TBodyStore = {
Expand Down Expand Up @@ -135,7 +134,7 @@ export function useBody() {

export function registerBodyCompressor(name: string, compressor: TBodyCompressor) {
if (compressors[name]) {
throw panic(`Body compressor "${name}" already registered.`)
throw new Error(`Body compressor "${name}" already registered.`)
}
compressors[name] = compressor
}
5 changes: 2 additions & 3 deletions packages/http-body/src/utils/body-compressor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { panic } from 'common/panic'
// https://nodejs.org/docs/latest-v16.x/api/zlib.html#zlib
export type TBodyCompressor = {
compress: (data: string) => string | Promise<string>
Expand All @@ -16,7 +15,7 @@ export async function compressBody(encodings: string[], body: string): Promise<s
let newBody = body
for (const e of encodings) {
if (!compressors[e]) {
throw panic(`Usupported compression type "${e}".`)
throw new Error(`Usupported compression type "${e}".`)
}
newBody = await compressors[e].compress(body)
}
Expand All @@ -27,7 +26,7 @@ export async function uncompressBody(encodings: string[], body: string): Promise
let newBody = body
for (const e of encodings.reverse()) {
if (!compressors[e]) {
throw panic(`Usupported compression type "${e}".`)
throw new Error(`Usupported compression type "${e}".`)
}
newBody = await compressors[e].uncompress(body)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/http-static/src/serve-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('serve-file', () => {
const res = new ServerResponse(req)

beforeEach(() => {
createHttpContext({ req, res })
createHttpContext({ req, res }, {})
})
it('', () => {
// const r = serveFile('package.json')
Expand Down
5 changes: 2 additions & 3 deletions packages/http-static/src/utils/body-compressor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { panic } from 'common/panic'

export type TBodyCompressor = {
compress: (data: string) => string | Promise<string>
Expand All @@ -16,7 +15,7 @@ export async function compressBody(encodings: string[], body: string): Promise<s
let newBody = body
for (const e of encodings) {
if (!compressors[e]) {
throw panic(`Unsupported compression type "${e}".`)
throw new Error(`Unsupported compression type "${e}".`)
}
newBody = await compressors[e].compress(body)
}
Expand All @@ -27,7 +26,7 @@ export async function uncompressBody(encodings: string[], body: string): Promise
let newBody = body
for (const e of encodings.reverse()) {
if (!compressors[e]) {
throw panic(`Usupported compression type "${e}".`)
throw new Error(`Usupported compression type "${e}".`)
}
newBody = await compressors[e].uncompress(body)
}
Expand Down
4 changes: 0 additions & 4 deletions packages/wooks/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export interface TWooksOptions {

}

export type TWooksHandler<ResType = unknown> = () => Promise<ResType> | ResType

0 comments on commit 7ced254

Please sign in to comment.