Move ILogger to @rondo/common
This commit is contained in:
parent
aeb208162f
commit
15d54639ab
9
packages/common/src/ILogger.ts
Normal file
9
packages/common/src/ILogger.ts
Normal file
@ -0,0 +1,9 @@
|
||||
type ILogFunction = (message: string, ...meta: any[]) => void
|
||||
|
||||
export interface ILogger {
|
||||
error: ILogFunction
|
||||
warn: ILogFunction
|
||||
info: ILogFunction
|
||||
debug: ILogFunction
|
||||
verbose: ILogFunction
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
export * from './IAPIDef'
|
||||
export * from './ICredentials'
|
||||
export * from './ILogger'
|
||||
export * from './INewUser'
|
||||
export * from './IRequestParams'
|
||||
export * from './IRole'
|
||||
|
||||
@ -3,6 +3,7 @@ import express from 'express'
|
||||
import request from 'supertest'
|
||||
import {createClient} from './supertest'
|
||||
import {jsonrpc} from './express'
|
||||
import {noopLogger} from './test-utils'
|
||||
|
||||
describe('jsonrpc', () => {
|
||||
|
||||
@ -55,15 +56,17 @@ describe('jsonrpc', () => {
|
||||
function createApp() {
|
||||
const app = express()
|
||||
app.use(bodyParser.json())
|
||||
app.use('/myService', jsonrpc(req => ({userId: 1000}))
|
||||
app.use('/myService',
|
||||
jsonrpc(req => ({userId: 1000}), noopLogger)
|
||||
.addService(new Service(5), [
|
||||
'add',
|
||||
'delay',
|
||||
'syncError',
|
||||
'asyncError',
|
||||
'httpError',
|
||||
'addWithContext',
|
||||
]))
|
||||
'add',
|
||||
'delay',
|
||||
'syncError',
|
||||
'asyncError',
|
||||
'httpError',
|
||||
'addWithContext',
|
||||
]),
|
||||
)
|
||||
return app
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ import express, {ErrorRequestHandler} from 'express'
|
||||
import {FunctionPropertyNames} from './types'
|
||||
import {IDEMPOTENT_METHOD_REGEX} from './idempotent'
|
||||
import {IErrorResponse} from './jsonrpc'
|
||||
import {ILogger} from '@rondo/common'
|
||||
import {ISuccessResponse} from './jsonrpc'
|
||||
import {NextFunction, Request, Response, Router} from 'express'
|
||||
import {createError, isJSONRPCError, IJSONRPCError, IError} from './error'
|
||||
@ -11,8 +12,36 @@ export type TGetContext<Context> = (req: Request) => Context
|
||||
|
||||
export function jsonrpc<Context>(
|
||||
getContext: TGetContext<Context>,
|
||||
logger: ILogger,
|
||||
idempotentMethodRegex = IDEMPOTENT_METHOD_REGEX,
|
||||
) {
|
||||
|
||||
const handleError: ErrorRequestHandler = (err, req, res, next) => {
|
||||
logger.error('JSON-RPC Error: %s', err.stack)
|
||||
// TODO log error
|
||||
|
||||
if (isJSONRPCError(err)) {
|
||||
res.status(err.statusCode)
|
||||
res.json(err.response)
|
||||
return
|
||||
}
|
||||
|
||||
const id = getRequestId(req)
|
||||
const statusCode: number = err.statusCode || 500
|
||||
const errorResponse: IErrorResponse<unknown> = {
|
||||
jsonrpc: '2.0',
|
||||
id,
|
||||
result: null,
|
||||
error: {
|
||||
code: ERROR_SERVER.code,
|
||||
message: statusCode >= 500 ? ERROR_SERVER.message : err.message,
|
||||
data: 'errors' in err ? err.errors : null,
|
||||
},
|
||||
}
|
||||
res.status(statusCode)
|
||||
res.json(errorResponse)
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to
|
||||
@ -65,30 +94,6 @@ export function jsonrpc<Context>(
|
||||
},
|
||||
}
|
||||
|
||||
const handleError: ErrorRequestHandler = (err, req, res, next) => {
|
||||
// TODO log error
|
||||
|
||||
if (isJSONRPCError(err)) {
|
||||
res.status(err.statusCode)
|
||||
res.json(err.response)
|
||||
return
|
||||
}
|
||||
|
||||
const id = getRequestId(req)
|
||||
const statusCode: number = err.statusCode || 500
|
||||
const errorResponse: IErrorResponse<unknown> = {
|
||||
jsonrpc: '2.0',
|
||||
id,
|
||||
result: null,
|
||||
error: {
|
||||
code: ERROR_SERVER.code,
|
||||
message: statusCode >= 500 ? ERROR_SERVER.message : err.message,
|
||||
data: 'errors' in err ? err.errors : null,
|
||||
},
|
||||
}
|
||||
res.status(statusCode)
|
||||
res.json(errorResponse)
|
||||
}
|
||||
}
|
||||
|
||||
function getRequestId(req: express.Request) {
|
||||
|
||||
@ -13,6 +13,7 @@ import {createRemoteClient} from './remote'
|
||||
import {createStore} from '@rondo/client'
|
||||
import {jsonrpc} from './express'
|
||||
import {keys} from 'ts-transformer-keys'
|
||||
import {noopLogger} from './test-utils'
|
||||
|
||||
describe('createActions', () => {
|
||||
|
||||
@ -54,10 +55,11 @@ describe('createActions', () => {
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.json())
|
||||
app.use('/service', jsonrpc(() => ({userId: 1000})).addService(
|
||||
new Service(),
|
||||
keys<IService>(),
|
||||
))
|
||||
app.use('/service', jsonrpc(() => ({userId: 1000}), noopLogger)
|
||||
.addService(
|
||||
new Service(),
|
||||
keys<IService>(),
|
||||
))
|
||||
|
||||
let baseUrl: string
|
||||
let server: Server
|
||||
|
||||
@ -9,6 +9,7 @@ import {Server} from 'http'
|
||||
import {createRemoteClient} from './remote'
|
||||
import {jsonrpc} from './express'
|
||||
import {keys} from 'ts-transformer-keys'
|
||||
import {noopLogger} from './test-utils'
|
||||
|
||||
describe('remote', () => {
|
||||
|
||||
@ -28,7 +29,8 @@ describe('remote', () => {
|
||||
function createApp() {
|
||||
const a = express()
|
||||
a.use(bodyParser.json())
|
||||
a.use('/myService', jsonrpc(() => ({})).addService(service, IServiceKeys))
|
||||
a.use('/myService', jsonrpc(() => ({}), noopLogger)
|
||||
.addService(service, IServiceKeys))
|
||||
return a
|
||||
}
|
||||
|
||||
|
||||
11
packages/jsonrpc/src/test-utils.ts
Normal file
11
packages/jsonrpc/src/test-utils.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import {ILogger} from '@rondo/common'
|
||||
|
||||
const noop = () => undefined
|
||||
|
||||
export const noopLogger: ILogger = {
|
||||
error: noop,
|
||||
warn: noop,
|
||||
info: noop,
|
||||
debug: noop,
|
||||
verbose: noop,
|
||||
}
|
||||
@ -1,9 +1 @@
|
||||
type ILogFunction = (message: string, ...meta: any[]) => void
|
||||
|
||||
export interface ILogger {
|
||||
error: ILogFunction
|
||||
warn: ILogFunction
|
||||
info: ILogFunction
|
||||
debug: ILogFunction
|
||||
verbose: ILogFunction
|
||||
}
|
||||
export {ILogger} from '@rondo/common'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user