Skip to content

Commit

Permalink
feat: Daf-express overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and mirceanis committed Sep 7, 2020
1 parent 00c02d6 commit 31a0970
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/daf-express/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IAgent } from 'daf-core'
import { Request, Response, NextFunction, Router, json } from 'express'
import { supportedMethods } from 'daf-rest'
import { supportedMethods, IAgentRESTMethod } from 'daf-rest'
import Debug from 'debug'

interface RequestWithAgent extends Request {
Expand All @@ -10,6 +10,7 @@ interface RequestWithAgent extends Request {
export const AgentRouter = (options: {
getAgentForRequest: (req: Request) => Promise<IAgent>
exposedMethods: string[]
overrides?: Record<string, IAgentRESTMethod>
}): Router => {
const router = Router()
router.use(json())
Expand All @@ -18,8 +19,10 @@ export const AgentRouter = (options: {
next()
})

const allMethods: Record<string, IAgentRESTMethod> = { ...supportedMethods, ...options.overrides }

for (const exposedMethod of options.exposedMethods) {
const method = supportedMethods[exposedMethod]
const method = allMethods[exposedMethod]
if (!method) throw Error('Method not supported: ' + exposedMethod)
Debug('daf:express:initializing')(exposedMethod)

Expand Down

0 comments on commit 31a0970

Please sign in to comment.