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

ee-soa-request

v0.2.22

Published

System internal request response implementation.

Downloads

14

Readme

#ee-soa-request

Application internal, protocol independent request/response implementation.

installation

npm install ee-soa-request

build status

Build Status

usage

var Request = require('ee-soa-request');

var myRequest = new Request();

var myReadRequest = new Request.ReadRequest();

api

Some of the important methods the read request object provides (incomplete):

Base Accessors

// needs a callback to support asynchronous loading in the future
request.getContent(callback)
request.setContent(content)

request.setContentType(type)
request.getContentType()

Type

The type of the request is based on the action code (for crud/rest mapping, analogous to http methods).

request.setAction(actionCode)
request.getAction()

request.isOfType(actionCode)

request.isReadRequest()
request.isCreateRequest()
request.isUpdateRequest()
request.isDeleteRequest()
request.isWriteRequest()
request.isInfoRequest()
request.isOptionsRequest()

Relations

A request is often created to query specific relations/models.

request.getCollection() :string
// alias
request.getController() :string
request.setCollection(collection:string)
request.queriesCollection() :boolean

request.getResourceId()
request.setResourceId(id:mixed)
request.hasResourceId()

request.hasRelatedTo()
request.getRelatedTo() : {model:string, id: mixed}
request.setRelatedTo(model, id)

Accessors and Helpers

request.getVersion()                    // api version
request.setVersion(version)
request.versionCompare(version)         // 0 if equal, -1 if request version is smaller, 1 if bigger

request.addFormat(type, subtype)        // e.g. 'application' 'json'
request.acceptsFormat(type, subtype)

request.setLanguages(languageArray)
request.getLanguages()
request.acceptsLanguage(languageString)

request.hasRange()
request.setRange(from, to)
request.getRange(): {from:mixed, to:mixed}  // Ranges (from, to) are set to null if not present

// filters
// projections
// orderings
// orderings

Types and Dispatching

The following request types are defined (based on the http verbs for rest interfaces), please use them and not the generic Request.

  • DeleteRequest DELETE
  • InfoRequest HEAD
  • CreateRequest PUT
  • OptionsRequest OPTIONS
  • ReadRequest GET
  • UpdateRequest PATCH
  • WriteRequest POST

The different types of requests implement a dispatch method which takes a handler as its argument (the request performs a so called double-dispatch, this allows you to easily handle the request without knowing its type upfront). A handler is described by the following set of methods (to say it in terms of duck-typing)

var handler = {
    handleCreateRequest: function(req, options){ ... }
    handleDeleteRequest: function(req, options){ ... }
    handleInfoRequest: function(req, options){ ... }
    handleOptionsRequest: function(req, options){ ... }
    handleReadRequest: function(req, options){ ... }
    handleUpdateRequest: function(req, options){ ... }
    handleWriteRequest: function(req, options){ ... }
}

You can use the options parameter to pass in additional data e.g. the request if you want to send the your data directly from your handler. See the following (rudimentary) example for a read request:

var handler = {
    handleReadRequest: function(req, options){
        // list
        var   query = 'SELECT * FROM :collection:'
            , params = {collection: req.getCollection()};

        // listOne
        if(req.hasResourceId()){
            params['id'] = req.getResourceId();
            query += 'WHERE `id`= :id:';
        }

        var data = this.performQuery(query, params);
        options.response.send(options.response.OK, data);
    }
};

request.dispatch(handler, {response: response});

Format

Formats are the internal representation of the internet media types (currently not supporting parameters such as the encoding). All types of Request objects provide easy accessors to preserve you from dealing with the internal data format.

addFormat(type, subtype)

Add a format to the current format collection of the request (type and subtype are strings the asterisk represents the wildcard.

acceptsFormat(type, subtype)

This method can be used to to check if a request accepts a specific response format based on the internet media type (type, subtype). It returns the priority of the passed format to allow your service or controller to check which formats are accepted, and which should be delivered (the relation is covariant, so image/* accepts image/jpg but not the other way round). Lets consider an example for an simple image service:

// The accepted formats are image/jpg, application/*
// We can create jpgs and json
var acceptsJPG  = request.acceptsFormat('image', 'jpg'),
    acceptsJSON = request.acceptsFormat('application', 'json');

if(acceptsJPG === false && acceptsJSON === false){
    return this.sendUnsatisfiableResponse();
}

if(acceptsJPEG > acceptsJSON){
    return this.sendImageResponse();
}

return this.sendJSONResponse();

One can immediately see, that we need the priority to be able to distinguish which response to send (because the request accepts both). More formats can easily be checked in a loop.