Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Created April 24, 2020 21:13
Show Gist options
  • Save kenanhancer/ca6f10c7ce095259faf309b46240fe69 to your computer and use it in GitHub Desktop.
Save kenanhancer/ca6f10c7ce095259faf309b46240fe69 to your computer and use it in GitHub Desktop.
nut-ioc loading and grouping YAML and JSON dependencies
module.exports = {
Namespace: "",
ServiceName: "", //fileName if empty,null or undefine
Service: ({ }) => {
}
};
{
"swagger": "2.0",
"info": {
"description": "English Greeting API",
"version": "1.0.0",
"title": "English Greeting API"
},
"basePath": "/greeting-api/v1",
"schemes": [
"http"
],
"paths": {
"/sayHello": {
"get": {
"summary": "Say English Hello Message",
"operationId": "greetingService.sayHello",
"produces": [
"application/json"
],
"parameters": [
{
"name": "firstName",
"in": "header",
"type": "string",
"maxLength": 100,
"required": true,
"description": "Person First Name."
},
{
"name": "lastName",
"in": "header",
"type": "string",
"maxLength": 100,
"required": true,
"description": "Person Last Name."
}
],
"responses": {
"200": {
"description": "success"
}
}
}
},
"/sayGoodbye": {
"get": {
"summary": "Say English Goodbye Message",
"operationId": "greetingService.sayGoodbye",
"produces": [
"application/json"
],
"parameters": [
{
"name": "firstName",
"in": "header",
"type": "string",
"maxLength": 100,
"required": true,
"description": "Person First Name."
},
{
"name": "lastName",
"in": "header",
"type": "string",
"maxLength": 100,
"required": true,
"description": "Person Last Name."
}
],
"responses": {
"200": {
"description": "success"
}
}
}
}
}
}
swagger: '2.0'
info:
description: Greeting API
version: 1.0.0
title: Greeting API v1
host: localhost:8080
basePath: /greeting-api/v1
schemes:
- http
paths:
/sayHello/{firstName}:
get:
summary: Say Hello Message
operationId: greetingService.sayHello
produces:
- application/json
parameters:
- name: language
in: header
type: string
enum:
- EN
- TR
required: true
description: Greeting Language.
- name: firstName
in: path
type: string
maxLength: 100
required: true
description: Person First Name.
- name: lastName
in: query
type: string
maxLength: 100
required: true
description: Person Last Name.
responses:
'200':
description: success
/sayGoodbye:
get:
summary: Say Goodbye Message
operationId: greetingService.sayGoodbye
produces:
- application/json
parameters:
- name: language
in: header
type: string
enum:
- EN
- TR
required: true
description: Greeting Language.
- name: firstName
in: query
type: string
maxLength: 100
required: true
description: Person First Name.
- name: lastName
in: query
type: string
maxLength: 100
required: true
description: Person Last Name.
responses:
'200':
description: success
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: "kenanhancer@gmail.com" })
});
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];
}
});
nutIocContainer.use({ dependencyPath: './swagger-definitions' });
const { authorWithContacts, greetingService, swaggerDefinitions } = await nutIocContainer.build();
const helloMsg = greetingService.sayHello(authorWithContacts);
console.log(helloMsg);
const goodBydMsg = greetingService.sayGoodbye(authorWithContacts);
console.log(goodBydMsg);
console.log();
console.log('swaggerDefinitions:', swaggerDefinitions);
};
mainAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment