Kokos SDK Node is the SDK for creating a micro executor based on Node.js with typescript and ESM support. It encapsulates all standard features such as:
Create a new Node.js project using NPM.
# Create project folder
mkdir [PROJECT_FOLDER]
# Positioning inside project folder
cd [PROJECT_FOLDER]
# Init Node.js project
npm init
Install SDK:
npm i @sme.up/kokos-sdk-node
Now open the project with your favorite TypeScript IDE.
Update package.json
, add the following config:
package.json
{
...
"type": "module",
"scripts": {
"clean": "rimraf lib",
"build": "npm run clean && tsc",
"test": "NODE_ENV=test jest",
"dev": "NODE_ENV=development nodemon --watch './**/*.ts' --exec node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
"start": "NODE_ENV=production node ./lib/index.js",
"test:win": "SET NODE_ENV=test jest",
"dev:win": "SET NODE_ENV=development&&nodemon --watch . -e ts --exec \"node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts\"",
"start:win": "SET NODE_ENV=production node ./lib/entrypoint/restapi/index.js"
}
...
}
NB: [command]:win
identifies the command for Windows.
Create tsconfig.json
file:
tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"outDir": "./lib",
"baseUrl": "./src",
"rootDir": "./src",
"moduleResolution": "node",
"alwaysStrict": true,
"strict": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"declaration": true,
"sourceMap": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"lib",
"**/*.test.ts"
]
}
Create src/index.ts
file:
import { startServer } from "@sme.up/kokos-sdk-node";
startServer([MICRO_EXECUTOR_ID]);
MICRO_EXECUTOR_ID
= The id of your micro executor (i.e. me-node-[YOUR_COMPANY]
)
Create a sample Kokos Service in src/services/JS_00_01.ts
:
import {
ExecutionContext,
Fun,
KokosService,
SmeupDataStructureWriter,
} from "@sme.up/kokos-sdk-node";
const JS_00_01: KokosService = {
methods: {
"GET.TRE": getTree,
},
};
async function getTree(
_fun: Fun,
_context: ExecutionContext,
printer: SmeupDataStructureWriter
) {
printer.writeTreeNode({
children: [],
content: {
tipo: "",
parametro: "",
codice: "",
testo: "Hello World",
},
});
}
export default JS_00_01;
Run application:
# Linux
npm run dev
# Windows
npm run dev:win
Try to execute FUN using swagger execute fun api (http://localhost:8011/swagger/#/execution/ExecuteFun
)
Add the following payload:
{
"fun": {
"component": "TRE",
"service": "JS_00_01",
"function": "GET.TRE"
},
"context": {
"user": {
"sessionId": "",
"username": "",
"environment": "",
"device": ""
}
}
}
Kokos Node.js service is a Javascript Object based on KokosService
interface located under the src/services
folder as a previous example. It is a collection of Methods:
services/[SERVICE_NAME].ts
import {
ExecutionContext,
Fun,
KokosService,
SmeupDataStructureWriter,
} from "@sme.up/kokos-sdk-node";
// return a SmeupTree
const JS_00_01: KokosService = {
methods: {
"GET.TRE": getTree,
"GET.EXB": getTable
},
};
async function getTree(
_fun: Fun,
_context: ExecutionContext,
printer: SmeupDataStructureWriter
) {
printer.writeTreeNode({
children: [],
content: {
tipo: "",
parametro: "",
codice: "",
testo: "Hello World",
},
});
}
// return a SmeupTable
async function getTable(
_fun: Fun,
_context: ExecutionContext,
printer: SmeupDataStructureWriter
) {
printer.writeColumns([
{
code: "COL1",
text: "Column 1",
},
{
code: "COL2",
text: "Column 2",
},
]);
printer.writeRow({
fields: {
COL1: {
name: "COL1",
tooltip: false,
smeupObject: {
tipo: "CN",
parametro: "COL",
codice: "PIPPO",
testo: "Pippo",
},
},
COL2: {
name: "COL2",
tooltip: false,
smeupObject: {
tipo: "CN",
parametro: "COL",
codice: "PLUTO",
testo: "Pluto",
},
},
},
});
}
export default JS_00_01;
The service exposes method called GET.TRE
callable by FUN: F(TRE;[SERVICE_NAME];GET.TRE)
and the method GET.EXB
callable by FUN F(EXB;[SERVICE_NAME];GET.EXB)
.
The SDK automatically creates a [USER_HOME]/etc/kokos/[ME_ID]/[ME_ID].yaml file based on MicroExecutorConfiguration
interface. You can extends it and execute a callback function before the server starts.
src/index.ts
import { MicroExecutorConfiguration, startServer, startServerWithCustomConfig } from "@sme.up/kokos-sdk-node";
interface CustomMicroExecutorConfiguration extends MicroExecutorConfiguration {
// your custom properties
}
const defaultConfig: CustomMicroExecutorConfiguration = {
server: {
port: 8011
},
logger: {
format: Format.JSON,
level: Level.INFO
}
}
startServerWithCustomConfig<CustomMicroExecutorConfiguration>("[ME_ID]", defaultConfig, (config: CustomMicroExecutorConfiguration) => {
})
The sdk provides a default configurable Logger via the yaml configuration file.
[USER_HOME]/etc/kokos/[ME_ID]/[ME_ID].yaml
logger:
level: INFO
format: TXT
Available level: INFO
, DEBUG
, ERROR
Available format: TXT
, JSON
You can use Logger instance in the following way:
import { LOGGER } from "@sme.up/kokos-sdk-node";
LOGGER.info([STRING_MESSAGE])
LOGGER.debug([STRING_MESSAGE])
LOGGER.error([ERROR])
To properly build the micro executor you can run:
# Linux & Windows
npm run build
After the build you can test the micro executor in production mode launching:
# Linux
npm run start
# Windows
npm run start:win