# swagapi

> SWAGAPI framework

Latest version **0.4.0** (published 2019-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install swagapi
pnpm add swagapi
yarn add swagapi
bun add swagapi
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2019-04-23 |
| First published | 2017-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 34 |
| Unpacked size | 72 KB |
| Known vulnerabilities | 0 (+7 in 6 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | A. Munhoz |
| Maintainers | amunhoz |

## Links

- npm: https://www.npmjs.com/package/swagapi
- Repository: https://github.com/amunhoz/swagapi
- Issues: https://github.com/amunhoz/swagapi/issues
- npm.io page: https://npm.io/package/swagapi

## Dependencies (34)

- [ejs](https://npm.io/package/ejs.md) ^2.5.7
- [hoek](https://npm.io/package/hoek.md) ^6.1.2
- [clone](https://npm.io/package/clone.md) ^2.1.1
- [lusca](https://npm.io/package/lusca.md) ^1.4.1
- [merge](https://npm.io/package/merge.md) ^1.2.1
- [helmet](https://npm.io/package/helmet.md) ^3.8.1
- [mathjs](https://npm.io/package/mathjs.md) ^3.19.0
- [moment](https://npm.io/package/moment.md) ^2.20.1
- [morgan](https://npm.io/package/morgan.md) ^1.9.1
- [set-tz](https://npm.io/package/set-tz.md) 0.0.3
- [express](https://npm.io/package/express.md) ^4.15.3
- [matcher](https://npm.io/package/matcher.md) ^1.0.0
- [request](https://npm.io/package/request.md) ^2.81.0
- [winston](https://npm.io/package/winston.md) ^2.3.1
- [hjsonfile](https://npm.io/package/hjsonfile.md) 0.0.5
- [waterline](https://npm.io/package/waterline.md) ^0.13.6
- [micromatch](https://npm.io/package/micromatch.md) ^3.1.5
- [body-parser](https://npm.io/package/body-parser.md) ^1.17.2
- [compression](https://npm.io/package/compression.md) ^1.6.2
- [require-dir](https://npm.io/package/require-dir.md) ^0.3.2
- [app-root-dir](https://npm.io/package/app-root-dir.md) ^1.0.2
- [mount-routes](https://npm.io/package/mount-routes.md) ^1.0.8
- [randomstring](https://npm.io/package/randomstring.md) ^1.1.5
- [serve-static](https://npm.io/package/serve-static.md) ^1.12.3
- [cache-manager](https://npm.io/package/cache-manager.md) ^2.6.0
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.3
- [eventemitter2](https://npm.io/package/eventemitter2.md) ^4.1.0
- [cookie-session](https://npm.io/package/cookie-session.md) ^2.0.0-beta.2
- [express-session](https://npm.io/package/express-session.md) ^1.15.3
- [swagger-ui-express](https://npm.io/package/swagger-ui-express.md) ^2.0.14
- [swaggerize-express](https://npm.io/package/swaggerize-express.md) ^4.0.5
- [express-async-errors](https://npm.io/package/express-async-errors.md) ^3.0.0
- [recursive-readdir-sync](https://npm.io/package/recursive-readdir-sync.md) ^1.0.6
- [swagger-schema-official](https://npm.io/package/swagger-schema-official.md) ^2.0.0-d79c205

## Recent versions

- 0.4.0 (latest) — 2019-04-23
- 0.3.45 — 2018-12-09
- 0.3.44 — 2018-12-06
- 0.3.43 — 2018-12-06
- 0.3.42 — 2018-12-05
- 0.3.41 — 2018-12-05
- 0.3.40 — 2018-12-05
- 0.3.39 — 2018-12-05
- 0.3.38 — 2018-12-05
- 0.3.37 — 2018-12-05
- 0.3.36 — 2018-12-05
- 0.3.35 — 2018-12-05
- 0.3.34 — 2018-08-09
- 0.3.33 — 2018-08-06
- 0.3.32 — 2018-07-25
- … 30 more at https://npm.io/package/swagapi/versions

## README

SWAGAPI is a boilerplate to reduce programming time for node APIs using some shortcuts to make life easier.
The SWAGAPI is based in some concepts:
	- Use directory structure for routes as possible to make easier to read the code
	- A event structure to customize behaviours without complexity
	- Blueprints with commom functions for apis to speed up
	- Keep some control of the modules with some predefined ones
	- Use a simple ROUTES+LIBRARIES+MIDDLEWARE+BOOT concept to make everything.
  

ROUTES
-----------------------------------------------------------------------
Using swagger
	1. Create a swagger file and put in /app/config
	2. Configure api.jon with the correct information
	3. Put the files for your route in /app/routesApi with a method for each request type

		module.exports = {
			post: async function (req, res) {
				//code for post
			},
			get: async function (req, res) {
				//code for get
			}
		};
	4. Security policy - Just put a file with the same name of policy inside /app/security
	
	5. Use the example code above for security policy:
		
		async function authorize(req, res, next) {
			 if (true) {
					 return next();
			 } else {
				 res.status(403).send({sucess:false, error:"Unauthorized."});
			 }
		}
		module.exports = authorize;
	
	
Using only js files
	1. Just put the file in /app/routesDir and it will create a route automaticaly
	2. Use the follow structure

		var router = require('express').Router();
		router.get('/', swagapi.security.apikey, function(req, res, next) {
				//code for get
		});


VIEWS
-----------------------------------------------------------------------
You can acces render views with
	req.render(viewName, data)

Template language
	We use the EJS module for it ( http://ejs.co/ )

View events

	views.viewName.before
	[views]		- event area
	[viewName]	- view name as used in render() funciton
	[moment]	- before or after
	[*] 		- can use wild cards in parameters
	
	eventFunction (ctx) {
		// the ctx parameter will come with details like req, res, viewName, data
	
	}
	

MODELS - Use Waterline documentation
-----------------------------------------------------------------------
You can acces models from 
	swagapi.models  	- Waterline models
	swagapi.imodels	- custom interface with event trigger
	
Models events

	models.modelName.before.create	- This event will trigger before create a register in modelName
	[models] - event area
	[modelName] - model name
	[before] 	- moment, it can be 'before' or 'after'
	[create]	- operation, can be create, find, findOne, update and delete
	[*] 		- can use wild cards in parameters

	eventFunction (ctx) {
		// the ctx parameter will come with details like req, res, criteria, model, modelName, data...
	
	}
	
	
MODULES
-----------------------------------------------------------------------
The system auto load some modules, wich you can control or configure from
	/app/config/models.hjson  (commented json)
	
	
	
	
INIT - Run every script in /app/init at start
-----------------------------------------------------------------------
Put the file there with the proper config and it will run at app start
Follow the structure:	

	module.exports = {
		priority: 999,	// priority among others
		enabled: true,	// you can enable/disable
		name: "Tests",  // avoid same name of others
		run: async function () {
			console.log(" your code here");
		}
	}
	
Middleware - Run every script in /app/middleware at start
-----------------------------------------------------------------------
Put the file there with the proper config and it will run at app start express
Follow the structure:	

	module.exports = {
		priority: 999,	// priority among others
		enabled: true,	// you can enable/disable
		name: "Tests",  // avoid same name of others
		run: async function (appExpress) {
			console.log(" your code here");
		}
	}
	
LIB - Auto load libraries from /app/lib
-----------------------------------------------------------------------
All libs will be loaded into swagapi.lib.YourLibName.YourFunction();
Follow the structure:	

	function myLib() {
		return true;
	};
	myLib.funcUtil = async function (url, query, headers) {
		console.log(url);
	};
	module.exports = myLib;
	
	
PUBLIC - Serve your static content
-----------------------------------------------------------------------
Just put inside /app/public
		
	
	
BLUEPRINTS - Deal with request complexity in a easy way
-----------------------------------------------------------------------
Has the functions: create, update, delete, find, count and findOne

	module.exports = {
		post: async function (req, res) {
			swagapi.swagapi.lib.blueprints.create({ req: req, res: res, modelName: "tag" });
		},
		get: async function (req, res) {
			swagapi.swagapi.lib.blueprints.find({ req: req, res: res, modelName: "tag" });
		}

	};
You can check additional parameters inside the code in \SWAGAPI\lib\blueprints\[operation].js
	 
	
	
GLOBAL ERROR HANDLING -
-----------------------------------------------------------------------
 There is a global error threatment with express..
 To get better error messages:
	• you DONT need to use try/cath
	• always use await in front the async functions (blueprints, imodels, etc)

---
_Source: https://npm.io/package/swagapi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
