npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-api

v0.2.1

Published

Super easy promise-based api declaration

Downloads

5

Readme

logo

Express wrapper that provides simpler functions to declare API.

Current features

  • Proper promise handling
  • Automatic parameter retrieval
  • Automatic error handling, with custom error capability
  • Simpler static file delivering
  • Simpler file download
  • Simpler cross-site requests enabling

easy-api returns an enhanced version of Express object. Yet nothing is removed. All existing methods remain available.

build status

Install

npm install --save easy-api

Usage

Here are some examples for how you do things using easy-api

Initialization

var easyApi = require('easy-api')

var app = easyApi()

app.getAsync('/music/:musicId', function(musicId) {
    return db.get(musicId)
})

app.getAsync('/music/:musicId/stream', function(musicId) {
    return db.get(musicId).then(function(track) {
        return easyApi.download('audio/mpeg', track.file)
    })
})

app.static('/', './html')

// optional:
// app.enableCrossSiteRequests()

app.start(1337).then(function() {
    console.log('server is running')
})

Route declaration

Every HTTP method is available with the 'Async' suffix, which will provide you the easy-api encapsulation. If you do not want to use easy-api for some route, you can use the classic express methods.

app.getAsync('/message/:id', ...)

app.postAsync('/message/:id', ...)

app.putAsync('/message/:id', ...)

app.deleteAsync('/message/:id', ...)

app.patchAsync('/message/:id', ...)


// or if for some reason (like a feature not yet supported file download for
// instance) you do not wish to use easy-api for a specific route

app.get(...) // classic Express call

Route handler automatic parameter detection

The handler function you pass as parameter will have its parameters automatically fulfilled, with the parameters from the route and also from the body of the request.

app.postAsync('/message/:category', function(category, message) {
    // here you automatically have access to category and message
    //  (message should be a field of the body: see examples below)
})

Route handler Promise handling

Return value

The value returned by the handler is automatically sent in the response as JSON.

app.postAsync('/message/:category', function(category, message) {
    return {id: 42, succes : true}

    // or

    return createMessage(category, message).then(function() {
        return {id: 42, succes : true}
    })
})

When called, this route will set its response headers to 'applcation/json' and send the stringified version of the object, along with a status code of 200

Errors

If an error happens in the handler function, it is automatically caught and a status code 500 is sent, along with the stack of the error. If you want to customize the error, it is possible by .catch()-ing the exception and returning a custom one.

app.getAsync('/message/:id', function(id) {
    return getMessage(id).catch(function(e) {
        e.statusCode = 404
        e.message = 'Could not find message with id: ' + id
        throw e
    })
})

Delivring static content

If you want to deliver some static content, a function is available to do so:

app.static('/', './static/')

Allowing cross-site requests

If you want to enable requests from cross-sites origins, all you have to do is:

// this should be done before declaring any route
app.enableCrossSiteRequests()

Examples

You can browse to the examples folder of the project in order to check out some usage examples.

Features in progress

  • File upload
  • Automatic client generation
  • REST model declaration

Client-side (code snippets for convenience)

Using request (or request-promise)
request.get({
    url : '/message/1',
    json : true
})

request.post({
    uri: '/message/catSound',
    json : true,
    body : {
        message : 'Meow'
    }
})
Using XMLHttpRequest
request('GET', '/message/1')
request('POST', '/message/catSound', {
    message : 'meow'
})

function request(type, url, content) {
    return new Promise(function(resolve, reject) {
        var req = new XMLHttpRequest()
        req.open(type.toUpperCase(), url, true)
        var params = JSON.stringify(content)

        req.setRequestHeader("Content-type", "application/json")
        req.setRequestHeader("Content-length", params.length)
        req.setRequestHeader("Connection", "close")

        req.onreadystatechange = function(aEvt) {
            if (req.readyState == 4) {
                if (req.status == 200)
                    resolve(JSON.parse(req.responseText))
                else
                    reject()
            }
        }
        req.send(params)
    })
}

Tests

Code is thoroughly tested and has been written using TDD.

npm test

build status Please open issues in GitHub if you encounter any problem using this module.

Star the project if you like it :)

Thanks