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

mm-http

v3.1.2

Published

Matter in motion http transport

Downloads

30

Readme

Matter In Motion. HTTP transport

NPM Version NPM Downloads

Http transport extension for matter in motion framework

Usage

Transport installation instructions

Http transport adds root property to the app. It is just an express instance. Also, you can use app.use method to add a contract or any other express-compatible handler

Protocol

To use all advantages of HTTP protocol:

  • GET
    1. Add call in the URL after API URL
    2. Add body as query string
  • POST
    1. Add special MM header with call as JSON string
    2. Send body as string, number, boolean, or JSON string in request body

Put meta field in the standard Authorization: Bearer header.

HTTP request requires Accept and Content-Type headers to be set to application/json

The response will come as JSON string.

From the browser looks like this:

GET request:

const xhr = new XMLHttpRequest();
xhr.open('GET', '/api/world.hello?name=John', true);
xhr.setRequestHeader('Authorization', 'Bearer ' + meta);
xhr.setRequestHeader('Accept', 'application/json');
xhr.send();
xhr.onload = function() {
  if (xhr.status === 200) {
    let msg = JSON.parse(xhr.responseText);
    console.log(msg);
  } else {
    console.log(xhr.status);
  }
}

POST request:

const xhr = new XMLHttpRequest();
xhr.open('POST', '/api', true);
xhr.setRequestHeader('MM', JSON.stringify({ call: 'world.hello' }) );
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(body);
xhr.onload = function() {
  if (xhr.status === 200) {
    let msg = JSON.parse(xhr.responseText);
    console.log(msg);
  } else {
    console.log(xhr.status);
  }
}

If you make POST request with content type set to multipart/* message will be marked as raw and request object will be passed into your API call method, so you can parse it any way you want.

OPTIONS request

This is similar to the POST but it will transform into question request and body will be ignored

Settings

Only port option is required. Everything else is optional.

  • port — number. Accepting connections on the specified port
  • host — string, '0.0.0.0'. Accepting connections on the specified hostname.
  • limit — number, null. Limit content length for the api request
  • encoding — string, 'utf8'. Encoding of the api request
  • cors — defines the cross-origin HTTP request control. Every option adds corresponding http headers for preflight OPTIONS request and for actual request
    • allowOrigin — Recomended for the matter in motion protocol: '*',
    • allowMethods — Recomended for the matter in motion protocol: 'GET, POST, OPTIONS',
    • allowHeaders — Recomended for the matter in motion protocol: 'Authorization, Origin, Content-Type, Accept, MM',
    • maxAge — Recomended for the matter in motion protocol: 1728000
  • tls — instead of http server will create https server, this should be the https server settings
  • static — adds express.static static files handler. For more info look into official documentation
    • url — string, static url path
    • root — string, root directory from which the static assets are to be served
    • dotfiles — string, 'ignore'. Option for serving dotfiles. Possible values are “allow”, “deny”, and “ignore”
    • etag — boolean, true. Enable or disable etag generation
    • extensions — Sets file extension fallbacks: If a file is not found, search for files with the specified extensions and serve the first one found. Example: ['html', 'htm'].
    • index — string, 'index.html'. Sends the specified directory index file. Set to false to disable directory indexing.
    • lastModified — boolean, true. Set the Last-Modified header to the last modified date of the file on the OS.
    • maxAge — number, 0. Set the max-age property of the Cache-Control header in milliseconds or a string in ms format
    • redirect — boolean, true. Redirect to trailing “/” when the pathname is a directory.
    • setHeaders — function for setting HTTP headers to serve with the file.

Contract

The contract is a subclass of the express Router class that makes easy to add unit views.

To use as sub contract define handle method as express-like middleware

addView(path, view)

  • path — path to add view to
  • view — name of the view unit

addViews(obj)

Adds all views from obj:

this.addViews({
  '/?': 'index',
  '/contacts/?': 'contacts'
})

HTTP Methods

All the standart http methods (check the require('http').METHODS), except OPTIONS can be used as methods for your resource. To do that, you need to use uppercase method name as an API call declaration. Example:

  GET: function() {
    return {
      title: 'HTTP Test',
      description: 'returns the get request data',
      request: {},
      response: {},
      call: (auth, data) => data
    }
  }

And call it as GET /api/resource.

License: MIT

© velocityzen