Comparing version 2.0.0 to 2.0.1
{ | ||
"name": "adon-api", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "API for all Adon-based projects", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
148
README.md
@@ -26,6 +26,6 @@ # ADON-API | ||
```typescript | ||
import { Express } from 'express'; | ||
import { ExpressApp, ExpressAppConfig } from 'adon-api'; | ||
import { ExpressApp, ExpressAppConfig, EnvConfig } from 'adon-api' | ||
let app: ExpressApp; | ||
// eslint-disable-next-line prefer-const | ||
let app: ExpressApp | ||
@@ -36,6 +36,6 @@ async function onHealthCheck(): Promise<boolean> { | ||
// for now simulate all are ok | ||
return true; | ||
return true | ||
} | ||
async function onLoading(e: Express): Promise<void> { | ||
async function onLoading(): Promise<void> { | ||
// perform middleWareHandling here and anything that requires initializations. | ||
@@ -47,44 +47,29 @@ } | ||
// other functionalities before or after it like logging or starting other services. | ||
app.start(); | ||
app.start() | ||
} | ||
// This is an ExpressApp configuration | ||
const cfg: ExpressAppConfig = { | ||
port: config.server.port, | ||
port: parseInt(EnvConfig.get('PORT')) || 3000, | ||
onHealthCheck, | ||
onReady, | ||
onLoading, | ||
}; | ||
app = new ExpressApp(cfg); | ||
} | ||
app = new ExpressApp(cfg) | ||
``` | ||
### Environment configurations | ||
As best practice, the service insist on setting your service level and security risk configurations within the environment. The best method is using the dotEnv module. | ||
As best practice, the service insist on setting your service level and security risk configurations within the environment. The EnvConfig module uses the dotEnv module to load all environment variables in a .env file which populates the config with all environment variables. | ||
Create a file named '.env' in the root of the application. enter the following | ||
```text | ||
API_SERVER_HOST_URL=http://localhost:8100 | ||
NODE_ENV=development | ||
MY_VAR=HELLO | ||
PORT=3000 | ||
SERVER_CORS_ALLOWED_ORIGINS=http://localhost.com;http://bytecommander.com;http://bcomm-local.com | ||
``` | ||
You can add more configurations here as much as you like and they can be accessed in code as | ||
```javascript 1.6 | ||
const message = process.env.MY_VAR; | ||
server.log('debug', 'test', 'The environment says ' + message); | ||
``` | ||
import { EnvConfig } from 'adon-api' | ||
## Logs | ||
adon-api uses intel for logging. You can perform logs by calling server.log() function. The parameters are as follows : | ||
* log_level - specifies one of intel's log level. To ensure consistency, use the following for the reason mentioned | ||
* critical (DEV/QA/PROD) - a server critical issue that was not handled by caught. Usually stuffs that fall into a general catch in a try-catch block. | ||
* error (DEV/QA/PROD) - Managed and non-trivial errors. These are errors like not found, server maintenance issue, network error that is not process breaking | ||
* warning (DEV/QA/PROD) - Issues that can be used to indicate some problems. Ex: memory about to be fully utilized, suspicious access, etc | ||
* info (DEV/QA/PROD) - High level Information that helps in providing service state to the administrator | ||
* debug (DEV/QA) - Information that is not needed for production servers that typically helps developers and QA understand where the flow went | ||
* verbose (DEV/QA) - Information that is not needed for production servers that details the step by step (internal and detailed) flow of functions | ||
* trace (DEV/QA) - Information that is not needed for production servers that includes everything even user inputs and provided outputs. This is not advisable in PROD environment | ||
* sender - this indicator specifies which object or instance triggered the log (ex: 'server', 'my_app') | ||
* message - the message body to render to stdout | ||
* data (optional) - any object or variable you want to tage with the log (ex: the Error object) | ||
```javascript | ||
server.log('info', 'my_app', 'wow! adon-api is simple!'); | ||
const port = EnvConfig.get('PORT') | ||
// you can also set configurations on the fly | ||
EnvConfig.set('APP_VAR', 'Hello world') | ||
``` | ||
@@ -94,91 +79,20 @@ | ||
Adding new routes is simple and organized in this framework. First create a 'routes' folder in the root. | ||
Add new folder inside the routes folder (to organize your routes) or any javascript file. | ||
Add new file inside the routes folder with the 'rt.ts' extension. To create a root route '/' name the file 'index.rt.ts' | ||
You can also create routes on specific paths based on the filename. To make a route on path '/users/properties' name the file 'users_properties.rt.ts' | ||
For this sample, lets create a file named 'get-say_hello.js' and enter the following | ||
```javascript 1.6 | ||
"use strict"; | ||
For this sample, lets create a file named 'index.rt.ts' and 'users_test.rt.ts' then enter the following in both files | ||
```typescript | ||
import { Router, Request, Response } from 'express' | ||
import { ExpressApp } from '../../src/libs/ExpressApp' | ||
module.exports = function Route(server){ | ||
return { | ||
method: 'GET', | ||
path:'/say/hello', | ||
config: { | ||
description: 'An endpoint that makes you happy (you introvert!)', | ||
notes:'you need a friend...', | ||
cors: { | ||
origin: [ '*' ], | ||
additionalHeaders: [ 'cache-control', 'x-requested-with' ] | ||
}, | ||
tags: [ 'api' ] | ||
}, | ||
handler: function (req, rep) { | ||
server.log('info', 'my_server', 'GET /say/hello called!'); | ||
return rep({ | ||
message: 'hello client' | ||
}); | ||
} | ||
} | ||
export default function route (app: ExpressApp, router: Router): void { | ||
router.get('/', async (req: Request, res: Response) => { | ||
res.send("WOW!") | ||
}) | ||
router.get('/test', async (req: Request, res: Response) => { | ||
res.send("YO!") | ||
}) | ||
} | ||
``` | ||
Run the application and use postman to invoke GET http://localhost:8100/say/hello | ||
Now lets create a route that contains parameters (POST/PUT/etc...) | ||
lets create a file named 'post-say_hello.js' and enter the following | ||
```javascript 1.6 | ||
"use strict"; | ||
const Joi = require('joi'); | ||
module.exports = function Route(server){ | ||
return { | ||
method: 'POST', | ||
path:'/say/hello/{name}', | ||
config: { | ||
description: 'makes the greeting more personal', | ||
notes:'hello mr. loner...', | ||
cors: { | ||
origin: [ '*' ], | ||
additionalHeaders: [ 'cache-control', 'x-requested-with' ] | ||
}, | ||
tags: [ 'api' ], | ||
validate: { | ||
payload: { | ||
message : Joi.string().min(5).max(30).required() | ||
.description('any message you want to tell yourself?') | ||
} | ||
} | ||
}, | ||
handler: function (req, rep) { | ||
server.log('info', 'POST /say/hello/{name} called!'); | ||
return rep({ | ||
message: 'hello ' + req.params.name + ', ' + req.payload.message | ||
}); | ||
} | ||
} | ||
} | ||
``` | ||
Again, run the application and use postman to invoke POST http://localhost:8100/say/hello/meathead with the following body: | ||
```javascript 1.6 | ||
{ | ||
message: 'lets eat!' | ||
} | ||
``` | ||
## Bootstrap | ||
Some files might be necessary to be loaded during the server startup. These processes will be organized and accessed via the bootstrap mechanism in place. First create a folder named 'bootstrap' in the root directory. | ||
## Events | ||
### Errors | ||
Certain objects within the server provide error events emitters. to capture route related events, add the following line of code where the server was initialized | ||
```javascript 1.6 | ||
server.routes.on('error', function (req, rep, err) { | ||
//..do some stuffs to validate or modify the error | ||
return rep({ | ||
error: err | ||
}); | ||
}); | ||
``` | ||
Run the application and use postman to invoke GET on 'http://localhost:3000/', 'http://localhost:3000/test' and 'http://localhost:3000/users/properties/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35999
95