# pure-json-api

> Help you maintain a single endpoint api with dynamic handlers.

Latest version **1.0.9** (published 2015-10-24) · 0 weekly downloads

## Install

```sh
npm install pure-json-api
pnpm add pure-json-api
yarn add pure-json-api
bun add pure-json-api
```

## 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.0.9 |
| Published | 2015-10-24 |
| First published | 2015-10-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >0.10.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Shlomi haver |
| Maintainers | havershlomi |
| Keywords | api, pureApi, rest, rest api, json api, json |

## Links

- npm: https://www.npmjs.com/package/pure-json-api
- Repository: https://github.com/havershlomi/pureApi
- Issues: https://github.com/havershlomi/pureApi/issues
- npm.io page: https://npm.io/package/pure-json-api

## Dependencies (2)

- [extend](https://npm.io/package/extend.md) 2.0.0
- [require-dir](https://npm.io/package/require-dir.md) ^0.3.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.9 (latest) — 2015-10-24
- 1.0.8 — 2015-10-24
- 1.0.7 — 2015-10-19
- 1.0.6 — 2015-10-19
- 1.0.5 — 2015-10-19
- 1.0.4 — 2015-10-13
- 1.0.3 — 2015-10-06
- 1.0.2 — 2015-10-06
- 1.0.1 — 2015-10-06
- 1.0.0 — 2015-10-06

## README

# Pure json api

Pure json api is module that allows you to create a single endpoint api that handles your code with dynamic handlers,
without the need of custom routing to each request.
 
## How? 

```javascript
 npm install pure-json-api --save 
```

```javascript

 var options = {
     handlersDir: "./myHandlersDir",
     validateReqFunc: function (req) {
         var data = req.body || {};
         return (data["action_str"] !== undefined && typeof data["action_str"] === "string")
             && (data["data_type"] !== undefined && typeof data["data_type"] === "string")
             && (data["log_list"] !== undefined && typeof data["log_list"] === "object")
             && (data["request_map"] !== undefined && typeof data["request_map"] === "object")
             && (data["trans_map"] !== undefined && typeof data["trans_map"] === "object");
     },
     dataType: "",
     actionStr: "",
     debug: true
 };
 
 app.post("/myEndpoint", pureApi.endPoint(options));
```

## The flow
> All the requests will be made to the same endpoint using post request only, the custom validation function 
> will make sure that all the needed data is supplied within the json data object that was sent to the server.
> Then check for the data type handler and the data type action within the object.
> Your data type callback handler will get this function 
 ```javascript
 function (req, res, next) {
      ...
    next(null,responseObj);
    //next first param is an error object
    //and the second obj is the response that will be sent to the client
 }
 ```
> and will preform the appropriate actions and return the appropriate response using res.send function.


## Options

### default options object
 
```javascript
var defaults = {
    handlersDir: "/handlers",
    validateReqFunc: function (req) {
        return true;
    },
    dataType: "data_type",
    actionStr: "action_str",
    debug: false
};
```

### handlersDir
> A path to the directory which contains the custom handlers.  

### validateReqFunc 
> A custom function which get the request object and allows you to check if the request is valid.
> if true continue with the process else throws an error. 
   
### dataType
> Supply here the name of the field in the data object which corresponds to the handler file name.

### actionStr
> Supply here the name of the field in the data object which corresponds to the specific function within the handler. 

### debug
> A flag if set to true will show log of request and response messages, (default to false). 


## Handler example
> This handler file is located in my handlers dir under the name person.js.
```javascript
exports.retrieve = function (req, res, next) {
    ...
    next(null, new pureApi.ApiResponse("RETRIEVED", "person"));
};
```
> In order to execute this action I am sending to the endpoint this object:

```javascript
 { action_str    : "retrieve",  //this verb is case sensitive and should be the same as the function name
     data_type     : "person",  //this verb is case sensitive and should be the same as the handler file name
     log_list      : [ /* application messages   */ ],
     request_map   : { /* request parameters     */ },
     trans_map     : { /* transaction meta-data  */ }
 }
 ```
 >

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