A powerful RESTful API generator written in Typescript using Express
Made with β€ by Netherium
- Quick Start
- Features
- Cli Options
- Develop
- Basic Routes
- Resource Permissions
- Coding Tips
- Structure
- Uploads
- Tests
- Debug
- Authors
- Copyright and license
-
β‘ Npm install globally
$ npm i -g @netherium/api-gen
-
π Launch
$ neth-api-gen
-
π» Fill in choices
- π Your api is ready!
$ cd myproject $ npm install
- Typescript Intellisense Awesomeness
- Robust routing and middleware based on same principles of Express
- MongoDB integration
- Elasticsearch integration (soonβ’)
- Protected routes, ACL based with middleware, using
jwt-token - File Upload routes and thumbnail generator
- Test and Coverage
- REST API Documentation via swagger-ui-express
- Various helper files and tools, i.e. CORS integration, swagger.yaml, .env environment setup
- Several scripts for easy development and testing
TODO
-
π If you generated project, navigate and install dependencies
$ cd myproject $ npm install -
π² Setup your environment files:
.env,.env.test,.env.production, according to.env.sampleADDRESS=localhost PORT=4000 MONGODB_URL=mongodb://localhost:27017/YOUDBNAMEHERE ELASTIC_URL=localhost:9200 SECRET=YOURSECRETHERE ...
-
π¨ Develop
$ npm run dev
-
π‘ Initialize: Navigate to http://localhost:4000/api/auth/init
- 2 Roles will be created, 1 Admin, 1 Public
- 1 User will be created, based on your
.envcredentials - Resource permissions will be created for basic routes, with default access to admin Role
-
π Build!
$ npm run build
-
πYour build is ready to deploy !π
The basic routes provided are listed below.
Each one of them is being reflected by its own route, controller, model
- π
api/auth/register[POST] - π
api/auth/login[POST] - π
api/auth/profile[GET, PUT, DELETE] - π
api/auth/init[GET]
- π
api/roles[GET, GET/:id, POST, PUT/:id, DELETE/:id]
- π
api/resource-permissions[GET, GET/:id, POST, PUT/:id, DELETE/:id]
- π
api/users[GET, GET/:id, POST, PUT/:id, DELETE/:id]
- π
api/uploads[GET, GET/:id, POST, PUT/:id, DELETE/:id]
- π
api/docs[GET]
- π
/[GET]
- π
api/endpoints[GET]
- π
api/articles[GET, GET/:id, POST, PUT/:id, DELETE/:id]
- Add a new route with Access-Control List (ACL) by invoking middleware function
Auth.getACL()I.e. filearticle.route.ts
export class ArticleRoute {
constructor() {
this.router.get('/', Auth.getAcl(), controller.list);
...
this.router.post('/', Auth.getAcl(), controller.create);
}
}- Add the appropriate resource-permission for this resource for each method (
list,create, in this case) and for each method the roles that will have access to.
POST api/resource-permissions
{
resourceName: 'articles',
methods: [
{
name: 'list',
roles: [PublicRoleId]
},
{
name: 'create',
roles: [AdminRoleId, RegisteredUserRoleId]
}
]
}Get Service
- Register any service in
server.ts, underregisterServicesfunction - Access it anywhere by destructuring it
const {uploadService} = req.app.get('services');
Get Authenticated User
- When a route is authenticated, you can access the authenticated user by
res.locals.authUser
Get Resource Permissions
- For optimization, resource-permissions are stored in
app.locals.resourcePermissions. If you update manually a resource (i.e. not via api call, but database manipulation), restart server or callAuth.updateAppPermissions()
Follow the structure below. It will keep things and your mind tidy πΌ
.
βββ dist # Compiled files ready to deploy `npm run test`
βββ uploads # When using local provider this is where uploads go
β
βββ src # Your code goes here
β βββ routes # Routes that define endpoints, methods, handlers and middleware
β βββ controllers # Controllers that handle functionality from routes
β βββ models # Mongoose models and typescript interfaces
β βββ middleware # Middleware functions
β βββ services # Services in OOP style that can be called in controllers
β βββ helpers # Exported functions that need no instantiation
β βββ server.ts # Server entrypoint file
β
βββ test # Automated tests `npm run test`
β
βββ swagger.yaml # Swagger documentation (`api/docs`) defined in yaml format
βββ LICENSE # License file
βββ README.md # This File
- Uploads can be stored locally or remotely, see
.env - To upload a file
POST api/uploadswithContent-Type: multipart/form-data;or use Postman - Field
fileis the file data,altenativeText,captionare optional strings PUT api/uploads/:idacceptsContent-Type: application/json;as onlyaltenativeText,captioncan be modified- If file of image type, a thumbnail (80x80) will be generated
Testing is based on Mocha, chai and chai-http
Run tests
$ npm testCoverage is based on nyc
Run coverage (generated under folder coverage)
$ npm run test:coverageTo debug TS without compiling it, you can setup your IDE of choice as in the example below
Note: Running older versions of node may require attaching --inspect before --require
Code released under the MIT license

