swagapi
SWAGAPI framework
Last updated a year ago by amunhoz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install swagapi 
SYNC missed versions from official npm registry.

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)

Current Tags

  • 0.4.0                                ...           latest (a year ago)

45 Versions

  • 0.4.0                                ...           a year ago
  • 0.3.45                                ...           a year ago
  • 0.3.44                                ...           a year ago
  • 0.3.43                                ...           a year ago
  • 0.3.42                                ...           a year ago
  • 0.3.41                                ...           a year ago
  • 0.3.40                                ...           a year ago
  • 0.3.39                                ...           a year ago
  • 0.3.38                                ...           a year ago
  • 0.3.37                                ...           a year ago
  • 0.3.36                                ...           a year ago
  • 0.3.35                                ...           a year ago
  • 0.3.34                                ...           2 years ago
  • 0.3.33                                ...           2 years ago
  • 0.3.32                                ...           2 years ago
  • 0.3.31                                ...           2 years ago
  • 0.3.30                                ...           2 years ago
  • 0.3.29                                ...           2 years ago
  • 0.3.28                                ...           2 years ago
  • 0.3.27                                ...           2 years ago
  • 0.3.26                                ...           2 years ago
  • 0.3.25                                ...           2 years ago
  • 0.3.24                                ...           2 years ago
  • 0.3.23                                ...           2 years ago
  • 0.3.22                                ...           2 years ago
  • 0.3.21                                ...           2 years ago
  • 0.3.20                                ...           2 years ago
  • 0.3.19                                ...           2 years ago
  • 0.3.18                                ...           2 years ago
  • 0.3.17                                ...           2 years ago
  • 0.3.16                                ...           2 years ago
  • 0.3.15                                ...           2 years ago
  • 0.3.14                                ...           2 years ago
  • 0.3.13                                ...           2 years ago
  • 0.3.12                                ...           2 years ago
  • 0.3.11                                ...           2 years ago
  • 0.3.10                                ...           2 years ago
  • 0.3.8                                ...           2 years ago
  • 0.3.7                                ...           2 years ago
  • 0.3.6                                ...           2 years ago
  • 0.3.5                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 5
Last Day 0
Last Week 2
Last Month 47
Dependencies (34)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |