Skip to content

Commit

Permalink
+version and truly on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
willguitaradmfar committed Oct 14, 2018
1 parent 1c8e7e0 commit 56c7272
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cdi-node",
"version": "0.0.11",
"version": "0.0.12",
"description": "",
"main": "src/index.js",
"index": "src/index.js",
Expand Down
26 changes: 15 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,24 @@ module.exports = class CDI {

if (arg && arg.constructor !== {}.constructor) throw new Error(`only 1 argument of object type is allowed`)

const resolveInterceptor = {}
for (let _var in arg) {
if (this.interceptorsVariable && this.interceptorsVariable[_var]) {
resolveInterceptor[_var] = await this.interceptorsVariable[_var](prop, arg)
} else {
resolveInterceptor[_var] = arg[_var]
const resolveInterceptor = new Proxy({
...arg,
...this.interceptorsVariable
}, {
get: (_obj, _prop) => {
if (!_obj[_prop]) return

if (typeof _obj[_prop] === 'function') return this.interceptorsVariable[_prop](prop, arg)

return _obj[_prop]
}
}
})
// for (let _var in this.interceptorsVariable) {
// resolveInterceptor[_var] = await this.interceptorsVariable[_var](prop, arg)
// }

try {
const response = await obj[prop].call(ctx, {
...arg,
...resolveInterceptor
})
const response = await obj[prop].call(ctx, resolveInterceptor)

if (this.interceptorDone) {
return this.interceptorDone(response, prop, arg)
Expand Down
2 changes: 1 addition & 1 deletion test/catchs/inject-catchs-var-with-intercept.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Catchs inject variable with intercept', function () {
before(function () {
this.target = new Controller()

this.target.fn = async ({ _var1 }) => {
this.target.fn = async () => {
throw new Error(`capture error catch interceptor`)
}
})
Expand Down
4 changes: 2 additions & 2 deletions test/class/inject-class-var-with-intercept.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Controller {
const cdi = new CDI()

cdi.addInterceptorVariable('_var1', async (fnName, args) => {
return args._var1 ? args._var1 + '_interceptor' : '_interceptor'
return args && args._var1 ? args._var1 + '_interceptor' : '_interceptor'
})

cdi.addInterceptorVariable('_notexitst', async (fnName, args) => {
Expand Down Expand Up @@ -35,6 +35,6 @@ describe('Class inject variable with intercept', function () {
})

it('should pass _var1 undefined and response with interceptor', async function () {
expect.isUndefined(await this.target.fn())
expect.equal(await this.target.fn(), '_interceptor')
})
})
4 changes: 2 additions & 2 deletions test/simples/inject-simple-var-with-intercept.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Simples inject variable with intercept', function () {
this.cdiClass = new CDI()

this.cdiClass.addInterceptorVariable('_var1', async (fnName, args) => {
return args._var1 ? args._var1 + '_interceptor' : '_interceptor'
return args && args._var1 ? args._var1 + '_interceptor' : '_interceptor'
})

this.target = this.cdiClass.configure({})
Expand All @@ -23,6 +23,6 @@ describe('Simples inject variable with intercept', function () {
})

it('should pass _var1 undefined and response with interceptor', async function () {
expect.isUndefined(await this.target.fn())
expect.equal(await this.target.fn(), '_interceptor')
})
})
5 changes: 3 additions & 2 deletions test/with-error/inject-with-error-var-with-intercept.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ describe('throw error normal and Interceptor', function () {
})

it('should pass _var1 undefined and response with interceptor', async function () {
this.cdiClass.addInterceptorVariable('_var1', async (fnName, args) => {
this.cdiClass.addInterceptorVariable('_var1', (fnName, args) => {
throw new Error(`error from interceptor`)
})
try {
await this.target.fn({ _var1: 'test222' })
await this.target.fn()
throw new Error()
} catch (err) {
expect.equal(err.message, 'error from interceptor')
}
Expand Down

0 comments on commit 56c7272

Please sign in to comment.