Created
April 24, 2020 21:07
-
-
Save kenanhancer/5ddcb383938ed6c4dd01c3e8429f36e3 to your computer and use it in GitHub Desktop.
nut-ioc loading dependencies with interceptors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nutIoc = require('nut-ioc'); | |
const nutIocContainer = nutIoc(); | |
const mainAsync = async () => { | |
nutIocContainer.useDependency({ | |
IsInterceptor: true, | |
ServiceName: "errorInterceptor", | |
Namespace: "interceptors", | |
Service: ({ }) => | |
(environment, next) => { | |
let result; | |
try { | |
result = next(environment); | |
} catch (error) { | |
appLogger.error(`ERROR: ${`${environment.moduleName}.${environment.method.name}`} method`, error); | |
throw error; | |
} | |
return result; | |
} | |
}); | |
nutIocContainer.useDependency({ | |
IsInterceptor: true, | |
ServiceName: "logInterceptor", | |
Namespace: "interceptors", | |
Service: ({ }) => | |
(environment, next) => { | |
const { method, args } = environment; | |
console.log(`ENTRY: ${method.name}(${JSON.stringify(args[0])}) function`) | |
const startDate = new Date(); | |
const result = next(environment); | |
const elapsedMilliseconds = new Date() - startDate; | |
console.log(`SUCCESS: ${method.name} function returns //${result}: Elapsed milliseconds is ${elapsedMilliseconds}`); | |
return result; | |
} | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "authorBasicInfo", | |
Service: ({ firstName: "Kenan", lastName: "Hancer" }) | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "authorWithContacts", | |
Service: ({ authorBasicInfo }) => ({ ...authorBasicInfo, city: "London", mail: "[email protected]" }) | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "greetingHelper", | |
Service: ({ }) => ({ | |
getFullName: ({ firstName, lastName }) => `${firstName} ${lastName}` | |
}), | |
Interceptor: ({ interceptors: { errorInterceptor, logInterceptor } }) => { | |
return [errorInterceptor, logInterceptor]; | |
} | |
}); | |
nutIocContainer.useDependency({ | |
ServiceName: "greetingService", | |
Service: ({ greetingHelper: { getFullName } }) => ({ | |
sayHello: ({ firstName, lastName }) => { | |
const fullName = getFullName({ firstName, lastName }); | |
return `Hello ${fullName}`; | |
}, | |
sayGoodbye: ({ firstName, lastName }) => { | |
const fullName = getFullName({ firstName, lastName }); | |
return `Goodbye, ${fullName}`; | |
} | |
}), | |
Interceptor: ({ interceptors: { errorInterceptor, logInterceptor } }) => { | |
return [errorInterceptor, logInterceptor]; | |
} | |
}); | |
const { authorWithContacts, greetingService } = await nutIocContainer.build(); | |
const helloMsg = greetingService.sayHello(authorWithContacts); | |
console.log(helloMsg); | |
const goodBydMsg = greetingService.sayGoodbye(authorWithContacts); | |
console.log(goodBydMsg); | |
}; | |
mainAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment