@3kles/3kles-coreion
v2.0.0
Published
3Kles Generic Ion Service
Downloads
10
Readme
@3kles/3kles-coreion
This package contains interface and class to interact with ION API Gateway based on 3kles-corebe
Models
IMIRequest is an interface for request
- path (string): Path of the request
- method (string): HTTP method like GET, POST, PUT, DELETE...
- body (any): Object passed in request
- contentType (string): Request content type like application/json
- params ([key:string]:any): List of request parameters
- query ([key:string]:any): List of URL query
- cookie (string): Request cookie
- authorization (string): Request authorization (see Authentication)
IIonRoute is an interface for ION Route
- middleware (string): Middleware name for this route
- routingKey (string): Routing key
- routes (ServiceParams): Define route from ServiceParams (path, method, option...)
IIonMessage is an interface for ION Message
- key (string): Message key
- data (any): Message data
- params ([key:string]:any): Message parameters
- query ([key:string]:any): Message query
IError is an interface for error
- status (number): HTTP Error status (401,404,500...)
- message (string): Error message
Authentication
AuthService is a class to manage Authentication for ION API Gateway based on AuthToken
- authenticate: Method with request in parameters and generate ION token
- getIONBEServiceToken: Check if token exist and not expired else generate new ION token
- isTokenExpired: Check is token expired
- loadIONBEServiceToken: Generate token from ionapi file information
- checkAuth: Method to check request authorization
Connector
IONConnector is a class to manage connection with ION based on HttpAPI
- buildRequest: Create ION request from paramters and data
- executeRequest: Execute ION request with authorization
Broker
IONBroker is a service that extends from AbstractGenericService from @3kles/3kles-corebe to do CRUD operations
- listen: Method to subscribe exchange and queue passed in option
App
IONApp is a class to create IONApp based on GenericApp
- startBroker: Start listening for broker list
- addBroker: Add IonBroker in broker list
Install
npm
npm install @3kles/3kles-coreion --save
How to use
How to create an app
const authService = new AuthService(null);
const iONConnector = new IONConnector(authService, {
protocol: 'https',
context: 'IONSERVICES',
middleware: '/process/application'
});
class ParserCustom implements IParserResponse {
parseResponse(data: any) {
const response = Buffer.concat(data).toString();
try {
return JSON.parse(response);
} catch (_) {
return response;
}
}
}
iONConnector.setResponseParser(new ParserCustom());
iONConnector.setErrorParser(new ParserCustom());
const endpoints: IIonRoute[] = [
{
routes: {
ping: {
method: 'GET',
path: '/ping',
option: {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
path: '/ping'
},
},
}
},
{
routingKey: 'pulse.alert',
middleware: 'pulse/alert',
routes: {
create: {
method: 'POST',
path: '/create',
option: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/alert/create'
},
},
cancel: {
method: 'POST',
path: '/cancel',
option: {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/alert/cancel'
},
},
redistribute: {
method: 'POST',
path: '/redistribute',
option: {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/alert/redistribute'
},
}
}
},
{
routingKey: 'pulse.notification',
middleware: 'pulse/notification',
routes: {
create: {
method: 'POST',
path: '/create',
option: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/notification/create'
},
},
cancel: {
method: 'POST',
path: '/cancel',
option: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/notification/cancel'
},
}
}
},
{
routingKey: 'pulse.workflow',
middleware: 'pulse/workflow',
routes: {
cancel: {
method: 'POST',
path: '/cancel',
option: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/workflow/cancel'
},
},
start: {
method: 'POST',
path: '/start',
option: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/workflow/start'
},
},
interface: {
method: 'GET',
path: '/start',
option: {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
path: 'v1/pulse/workflow/interface'
},
},
}
},
];
(async () => {
const broker = await MessageBroker.initInstance('0', { prefetch: 50 });
const app = new IonApp();
endpoints.forEach(endpoint => {
app.addRoute(new SecureRouter(authService,
new GenericController(new GenericService(iONConnector, endpoint.routes))).router,
endpoint.middleware || undefined);
if (endpoint.routingKey) {
app.addIonBroker(new IonBroker(broker, iONConnector, endpoint));
}
});
const port = process.env.PORT ? +process.env.PORT : 80;
app.startApp(port);
app.startBroker();
})();
app.startBroker();
})();
Check the documentation
here.