Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adv-express

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adv-express

ADV integration with Express

  • 2.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

adv-express

Build Status

ADV integration with Express

Usage

const express = require('express');

require('adv-express')(express);

const users = express.Router();

app.use(express.json());
app.use(users);

users
    .baseUrl('/users')
    .callback(require('some-middleware'));

users
    .schema(`Success = {success: true}`)
    .schema(Error => ({success: false, message: string}))
    .schema(() => User = {
       id: number,
       name: string.minLength(3).maxLength(20),
    });

users
    .url('/')
    .query(`User.props('name')`)
    .then(req => Users.findByName(req.query.name))
    .response(`[User]`);

users
    .url('/:id')
    .params(() => User.props('id'))
    .callback(require('some-local-middleware'))
    .then(req => Users.findById(req.params.id))
    .catch((err, req, res) => {
    	res.status(500);
    	
    	return {error: true, message: err.message};
    })
    .response(`User`)
    .response(500, () => ({error: true, message: string}));

users
    .url('POST /:id')
    .params(`User.props('id')`)
    .body(`User.omit('id')`)
    .callback(function (req, res) {
        Users
            .findById(req.params.id)
            .then(user => user.update(req.body))
            .then(() => res.json({success: true}))
            .catch(err => res.status(500).json({success: false, message: err.message}));
    })
    .response(`Success`)
    .response(`5xx Error`);

Arguments

  1. express - express module
  2. options
    • schemas - object where key is schema name and value is ajv json schema. Defaults to clone of adv-parser/schemas
    • ajv - Ajv instance. Defaults to new Ajv({coerceTypes: true}).
    • defaultMethod - http method name. Default is GET.
    • parseEndpoints - should all schemas to be parsed on next process tick. Default is true.

Share schemas

Share schemas with adv-sequelize

const defaultSchemas = require('adv-parser/schemas');
const schemas = {...defaultSchemas};
const express = require('express');
require('adv-express')(express, {schemas});
const createModel = require('adv-sequelize');
const define = code => createModel(code, {schemas});

const User = define(`User = {
    id: id.primaryKey(), 
    name: string.minLength(3).maxLength(20),
}`);

app
    .url('/users/:id')
    .params(`User.props('id')`)
    .then(req => User.findByPk(req.params.id))
    .response(`User`);

Keywords

FAQs

Package last updated on 13 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc