# coffeerest-api

> Api scaffolding from a model specification in few lines of coffeescript OH MY

Latest version **0.1.35** (published 2015-11-08) · BSD license · 0 weekly downloads

## Install

```sh
npm install coffeerest-api
pnpm add coffeerest-api
yarn add coffeerest-api
bun add coffeerest-api
```

## 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.1.35 |
| Published | 2015-11-08 |
| First published | 2015-09-10 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | coderofsalvation |
| Keywords | coffeerest, coffeescript, api, rest, model, specification, generator, generate, scaffolding, skeleton, jsonschema |

## Links

- npm: https://www.npmjs.com/package/coffeerest-api
- Repository: https://github.com/coderofsalvation/coffeerest-api
- Homepage: https://github.com/coderofsalvation/coffeerest-api#readme
- Issues: https://github.com/coderofsalvation/coffeerest-api/issues
- npm.io page: https://npm.io/package/coffeerest-api

## Dependencies (6)

- [ajv](https://npm.io/package/ajv.md) ^1.4.2
- [glob](https://npm.io/package/glob.md) ^5.0.15
- [extend](https://npm.io/package/extend.md) ^3.0.0
- [json-ref-lite](https://npm.io/package/json-ref-lite.md) ^1.0.69
- [async-eventemitter](https://npm.io/package/async-eventemitter.md) ^0.2.2
- [json-schema-defaults](https://npm.io/package/json-schema-defaults.md) ^0.1.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.35 (latest) — 2015-11-08
- 0.1.34 — 2015-11-05
- 0.1.33 — 2015-10-09
- 0.1.30 — 2015-10-05
- 0.1.29 — 2015-10-05
- 0.1.28 — 2015-09-28
- 0.1.27 — 2015-09-28
- 0.1.25 — 2015-09-11
- 0.1.2 — 2015-09-11
- 0.1.1 — 2015-09-10
- 0.1.0 — 2015-09-10

## README

coffeerest-api
==============
Unfancy rest apis

<img alt="" src="https://github.com/coderofsalvation/coffeerest-api/raw/master/coffeerest.png" width="20%" />

## Ouch! Is it that simple?

Your `model.coffee` specification 

    module.exports = 
      host: process.env.HOST || "localhost:8080"
      name: "project foo"
      resources:
        '/book/:category':
          post:
            description: 'adds a book'
            payload:
              foo: { type: "string", minLength: 5, required: true }
            function: (req, res, next,lib, reply) ->
              category = req.params.category
              reply.data = [1,2, lib.foo() ]
              return reply 
              
      replyschema:
        type: 'object'
        required: ['code','message','kind','data']
        messages:
          0: 'feeling groovy'
          1: 'unknown error'
          2: 'your payload is invalid (is object? content-type is application/json?)'                                                                                           
          3: 'data error'
          4: 'access denied'
        payload:
          code:       { type: 'integer', default: 0 }
          message:    { type: 'string',  default: 'feeling groovy' }
          kind:       { type: 'string',  default: 'default', enum: ['book','default'] }
          data:       { type: 'object' }
          errors:     { type: 'object' }
## Extensions 

* [coffeerest-api-doc](https://www.npmjs.com/package/coffeerest-api-doc): automatic html and markdown REST-documentation
* coffeerest-api-clients:  automatic REST clients) *TODO*
* coffeerest-api-db: ORM to connect any database (using waterline) which automatically generates collection-to-restapi-endpoints 

## Example servercode 

    restify    = require('restify')        # here we use restify but express should be fine too
    coffeerest = require('coffeerest-api')
    model      = require('./model.coffee')
    lib        = 
      foo: () -> return 3

    server = restify.createServer { name:model.name }
    server.use restify.queryParser()
    server.use restify.bodyParser()
    server.use coffeerest server, { "/v1":model }, lib  # multiversion support
    server.listen process.env.PORT || 8080, () ->
      console.log '%s listening at %s', server.name, server.url

Done!

    $ coffee server.coffee
    registering REST resource: /v1/books/:action (post)
    registering REST resource: /v1/book (post)
    project foo listening at http://0.0.0.0:4455

    $ curl -H 'Content-Type: application/json' http://localhost:$PORT/v1/book -X POST --data '{"foo":"foobar"}'
    {"code":0,"message":"feeling groovy","kind":"default","data":[1,2,3],"errors":{}}

    $ curl -H 'Content-Type: application/json' http://localhost:$PORT/v1/book -X POST --data '{"foo":"foo"}'
    {"code":2,"message":"your payload is invalid (is object? content-type is application/json?)","kind":"default","data":{},"errors":[{"uri":"/foo","message":"String is less than the required minimum length","attribute":"minLength","details":5}]}
    
    $ curl -H 'Content-Type: application/json' http://localhost:$PORT/v1/book -X POST --data '{}'
    "code":2,"message":"your payload is invalid (is object? content-type is application/json?)","kind":"default","data":{},"errors":[{"uri":"/foo","message":"Property is required","attribute":"required","details":true}]}

## Philosophy

Dont generate code based on config (Yeoman etc), but instead extend both code and config.
Oh..and jsonschema, because jsonschema.

## Magic 

* ignore certain extensions 

| pass environment var COFFEEREST_EXT_IGNORE with value '(db|frontend)' to ignore all coffeerest extensions which match either 'db' or 'frontend'

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