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 './IAPIDef'
|
||||||
export * from './ICredentials'
|
export * from './ICredentials'
|
||||||
|
export * from './ILogger'
|
||||||
export * from './INewUser'
|
export * from './INewUser'
|
||||||
export * from './IRequestParams'
|
export * from './IRequestParams'
|
||||||
export * from './IRole'
|
export * from './IRole'
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import express from 'express'
|
|||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
import {createClient} from './supertest'
|
import {createClient} from './supertest'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
|
import {noopLogger} from './test-utils'
|
||||||
|
|
||||||
describe('jsonrpc', () => {
|
describe('jsonrpc', () => {
|
||||||
|
|
||||||
@ -55,15 +56,17 @@ describe('jsonrpc', () => {
|
|||||||
function createApp() {
|
function createApp() {
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
app.use('/myService', jsonrpc(req => ({userId: 1000}))
|
app.use('/myService',
|
||||||
|
jsonrpc(req => ({userId: 1000}), noopLogger)
|
||||||
.addService(new Service(5), [
|
.addService(new Service(5), [
|
||||||
'add',
|
'add',
|
||||||
'delay',
|
'delay',
|
||||||
'syncError',
|
'syncError',
|
||||||
'asyncError',
|
'asyncError',
|
||||||
'httpError',
|
'httpError',
|
||||||
'addWithContext',
|
'addWithContext',
|
||||||
]))
|
]),
|
||||||
|
)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import express, {ErrorRequestHandler} from 'express'
|
|||||||
import {FunctionPropertyNames} from './types'
|
import {FunctionPropertyNames} from './types'
|
||||||
import {IDEMPOTENT_METHOD_REGEX} from './idempotent'
|
import {IDEMPOTENT_METHOD_REGEX} from './idempotent'
|
||||||
import {IErrorResponse} from './jsonrpc'
|
import {IErrorResponse} from './jsonrpc'
|
||||||
|
import {ILogger} from '@rondo/common'
|
||||||
import {ISuccessResponse} from './jsonrpc'
|
import {ISuccessResponse} from './jsonrpc'
|
||||||
import {NextFunction, Request, Response, Router} from 'express'
|
import {NextFunction, Request, Response, Router} from 'express'
|
||||||
import {createError, isJSONRPCError, IJSONRPCError, IError} from './error'
|
import {createError, isJSONRPCError, IJSONRPCError, IError} from './error'
|
||||||
@ -11,8 +12,36 @@ export type TGetContext<Context> = (req: Request) => Context
|
|||||||
|
|
||||||
export function jsonrpc<Context>(
|
export function jsonrpc<Context>(
|
||||||
getContext: TGetContext<Context>,
|
getContext: TGetContext<Context>,
|
||||||
|
logger: ILogger,
|
||||||
idempotentMethodRegex = IDEMPOTENT_METHOD_REGEX,
|
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 {
|
return {
|
||||||
/**
|
/**
|
||||||
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to
|
* 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) {
|
function getRequestId(req: express.Request) {
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import {createRemoteClient} from './remote'
|
|||||||
import {createStore} from '@rondo/client'
|
import {createStore} from '@rondo/client'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
import {keys} from 'ts-transformer-keys'
|
import {keys} from 'ts-transformer-keys'
|
||||||
|
import {noopLogger} from './test-utils'
|
||||||
|
|
||||||
describe('createActions', () => {
|
describe('createActions', () => {
|
||||||
|
|
||||||
@ -54,10 +55,11 @@ describe('createActions', () => {
|
|||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
app.use('/service', jsonrpc(() => ({userId: 1000})).addService(
|
app.use('/service', jsonrpc(() => ({userId: 1000}), noopLogger)
|
||||||
new Service(),
|
.addService(
|
||||||
keys<IService>(),
|
new Service(),
|
||||||
))
|
keys<IService>(),
|
||||||
|
))
|
||||||
|
|
||||||
let baseUrl: string
|
let baseUrl: string
|
||||||
let server: Server
|
let server: Server
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import {Server} from 'http'
|
|||||||
import {createRemoteClient} from './remote'
|
import {createRemoteClient} from './remote'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
import {keys} from 'ts-transformer-keys'
|
import {keys} from 'ts-transformer-keys'
|
||||||
|
import {noopLogger} from './test-utils'
|
||||||
|
|
||||||
describe('remote', () => {
|
describe('remote', () => {
|
||||||
|
|
||||||
@ -28,7 +29,8 @@ describe('remote', () => {
|
|||||||
function createApp() {
|
function createApp() {
|
||||||
const a = express()
|
const a = express()
|
||||||
a.use(bodyParser.json())
|
a.use(bodyParser.json())
|
||||||
a.use('/myService', jsonrpc(() => ({})).addService(service, IServiceKeys))
|
a.use('/myService', jsonrpc(() => ({}), noopLogger)
|
||||||
|
.addService(service, IServiceKeys))
|
||||||
return a
|
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 {ILogger} from '@rondo/common'
|
||||||
|
|
||||||
export interface ILogger {
|
|
||||||
error: ILogFunction
|
|
||||||
warn: ILogFunction
|
|
||||||
info: ILogFunction
|
|
||||||
debug: ILogFunction
|
|
||||||
verbose: ILogFunction
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user