Skip to content

Commit

Permalink
feat: common action overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Jan 26, 2023
1 parent 03e7777 commit 516cebb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
45 changes: 45 additions & 0 deletions packages/plugin-router-amqp/__tests__/suites/router-amqp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,51 @@ describe('AMQP suite: custom redefined routing', function testSuite() {
})
})

describe('AMQP suite: allRoutes config', function testSuite() {
const service = new Microfleet({
name: 'tester',
plugins: [
'logger', // essensial plugin
'validator', // essensial plugin
'amqp', // init amqp
'router', // enable router
'router-amqp' // attach amqp transport to router
],
router: {
routes: {
directory: resolve(__dirname, '../artifacts/actions'),
allRoutes: {
bindingKey: ['10', 'amqp-custom.echo'],
omitPrefix: true,
},
},
},
routerAmqp: {
prefix: 'amqp-custom',
}
})

beforeAll(() => service.connect())
afterAll(() => service.close())

it('able to observe an action', async () => {
const amqpRoutes = service.router.routes.get('amqp')
assert(typeof amqpRoutes.get('echo')?.handler === 'function')
})

it('able to dispatch action and return response', async () => {
const { amqp } = service

const headers = { 'routing-key': 'amqp-custom.echo' }

const response = await amqp.publishAndWait('amqp-custom.echo', { foo: 'bar' }, { headers })
deepStrictEqual(response, { foo: 'bar' })

const response2 = await amqp.publishAndWait('10', { foo: 'bar' }, { headers })
deepStrictEqual(response2, { foo: 'bar' })
})
})

describe('AMQP suite: retry + amqp router prefix', function testSuite() {
const service = new Microfleet({
name: 'tester',
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-router/schemas/router.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"type": "string"
}
},
"allRoutes": {
"type": "object",
"additionalProperties": true
},
"enabled": {
"type": "object",
"additionalProperties": {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-router/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const priority = 10
* Shallow copies object, pass-through everything else
*/
const shallowObjectClone = (prop: any) => isObject(prop)
? Object.assign(Object.create(null), prop)
? { ...prop }
: prop

/**
Expand Down Expand Up @@ -97,6 +97,7 @@ export function attach(
prefix,
directory,
enabled,
allRoutes,
enabledGenericActions,
responseValidation: validateResponse
}
Expand All @@ -116,6 +117,7 @@ export function attach(
directory,
enabled,
enabledGenericActions,
allRoutes,
},
log: this.log,
requestCountTracker: new Tracker(this)
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class Router {
const { routes, config } = this
let name: string = route


if (config !== undefined) {
if (config.disabled !== undefined && Object.keys(config.disabled).length > 0) {
const disabledRoute = config.disabled[route]
Expand All @@ -132,6 +131,11 @@ export class Router {
}
}

// allows generic overwrites
if (config.allRoutes) {
Object.assign(handler, config.allRoutes)
}

if (config.enabled !== undefined && Object.keys(config.enabled).length > 0) {
const updatedConfig = config.enabled[route]
if (updatedConfig === undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-router/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RouterPluginRoutesConfig {
directory?: string
enabled?: Record<string, string | { name?: string, config?: Partial<ServiceAction> }>
disabled?: Record<string, string>
allRoutes?: Partial<ServiceAction>
prefix?: string
responseValidation?: ValidateResponseConfig
enabledGenericActions?: string[]
Expand Down

0 comments on commit 516cebb

Please sign in to comment.