# sails-hook-openapi

> Install: ========

Latest version **1.1.7** (published 2018-08-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install sails-hook-openapi
pnpm add sails-hook-openapi
yarn add sails-hook-openapi
bun add sails-hook-openapi
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2018-08-17 |
| First published | 2017-08-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | rponcin |

## Links

- npm: https://www.npmjs.com/package/sails-hook-openapi
- npm.io page: https://npm.io/package/sails-hook-openapi

## Recent versions

- 1.1.7 (latest) — 2018-08-17
- 1.1.6 — 2018-06-01
- 1.1.5 — 2017-11-16
- 1.1.4 — 2017-11-16
- 1.1.3 — 2017-11-15
- 1.1.2 — 2017-11-15
- 1.1.1 — 2017-09-05
- 1.1.0 — 2017-09-05
- 1.0.5 — 2017-09-04
- 1.0.4 — 2017-08-11
- 1.0.3 — 2017-08-11
- 1.0.2 — 2017-08-10
- 1.0.1 — 2017-08-10

## README

Install:
========

`npm install --save sails-hook-openapi`

------------------------------------------------------------

This module adds a route in a sails project, which allows to make a FIND query on any model of the sails backend, with filters:

`GET {{url}}/api/v1/request/:model`  

or

`POST {{url}}/api/v1/request/:model`  

------------------------------------------------------------

Configuration
=============

> In `./config/openapi.js` file :

``` javascript
module.exports.openapi = {

    //POLICIES

    //By default:
    policies: {
        production: false,
        staging: false,
        development: true
    },

    /* Works the same as sails policies
        policies: false,
        policies: true,
        policies: {
            production: "tokenAuth",
            staging: "tokenAuth",
            development: true,
        }
        policies: ["tokenAuth", "otherPolicy"],
    */

    ///////////////////////////////////////////////////////////////////////

    //ROUTES

    //By default:
    //routes: ["get /api/v1/request/:model", "post /api/v1/request/:model"]

    /*  
        Routes can be overwritten (it must contain :model in the route name or it can be included as a parameter)
        routes: "get /api/v1/openapi/:model"
        routes: ["get /:model", "post /:model"]
    */

};
```


> In `./config/env/[development|staging|production].js` file :

``` javascript

module.exports = {

    openapi: {
        policies: [...],
        routes: [...]
    }

}
```

------------------------------------------------------------

Parameters for all queries  
=====================

- **model** : name of the model to query on (case insensisitve) **required**
- **limit** : defaults to 10, min 1, max 50
- **offset** : defaults to 0
- **sort**: defaults to "createdAt desc"

------------------------------------------------------------

Parameters specific to a Model
==============

GET query
----------

> You can filter on any **string, number and boolean** fields of the model (except for previous parameters: model, limit, offset and sort).  
> For **strings**, the value can contain '%' to make a "LIKE" query on the field  
> Numbers sent in parameters are casted as number, so the query on a number saved as a string won't work. To avoid that, add a "%" before or after the number.  

Example:
------
- **first_name** : Fred%
- **last_name** : %iso%
- **activated** : true


POST query
----------

> For native query, sort option must be an object | 1 = asc / -1 = desc | {"email": 1} 

Examples:
--------

``` json
{
    "first_name": { "like": "Fred%" },
    "activated" : true,
    "limit": 5,
    "sort": "createdAt"
}
```

``` json
{
    "native": true,
    "query": { "email": {"$regex":".*name.*"} },
    "limit": 8,
    "sort": { "email" : 1 } 
}
```

``` json
{
    "native": "aggregate",
    "query": [
        {
            "$group": {
                "_id": "$first_name"
            }
        }
    ]
}
```

------------------------------------------------------------

Response:
=====================

> It always return an array, even with limit=1

``` json
{
	"success": true,
	"result": []
}
```

------------------------------------------------------------

Coming soon:
============

- **populate** parameter to chose which fields to populate in the result

------------------------------------------------------------

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