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

schematicsjs

v1.1.1

Published

An API communication experiment

Downloads

15

Readme

Schematics.js [WIP]

An API communication experiment

What is it?

The idea behind Schematics is to keep everything API in the API and provide the front-end with all the information it needs to know what the API wants. This in oppose to traditional ways in which there is basically initially no real communication between server and client on what the client can expect.

Examples

Check out the Web and Node.js examples.

Usage

#1 Schema

The first step is creating a schema which lays out what endpoints your API has and how to use them. You can use these schema's to navigate through your API.

Basically every property in a GET response body named $schema is recognized as a schema so that you can send them along with your usual responses.

{
    "$schema": {
        "users": "https://api.github.com/users{/username}{?limit,offset}",
        "emojis": "https://api.github.com/emojis",
        "events": {
            "post": {
                "href": "https://api.github.com/events",
                "params": {
                    "name": "String",
                    "date": { "type": "String", "optional": true }
                }
            },
            "get": "https://api.github.com/events"
        },
        "repos": "http://kvendrik.github.io/schematicsjs/examples/schema-repos.json"
    }
}

#2 Library

Grab the library:

  • npm i schematicsjs --save
  • bower i schematicsjs --save
  • Grab the source from the dist/ folder

Perfect! Now give the library the URL to your intial schema (or a method to get it) and your HTTP method and start hacking. :)

new Schematics('http://kvendrik.github.io/schematicsjs/examples/schema.json', httpMethod)
.then(function({ api, body }){

    api.users.get({ username: 'kvendrik', limit: 5 })
    .then((data) => console.log(data))
    .catch((err) => console.log(err));

    api.events.post({ name: 'Koen' })
    .then((data) => console.log(data));

    //With GET requests you can also give in an array with fields you want 
    //back from the server. The server will receive these as a GET parameter 
    //named `return_fields[]`
    api.users.get({ username: 'kvendrik' }, ['id', 'url']);

    //get the raw schema object using getSchema
    api.getSchema();

    //and some nested schemas awesomeness
    api.repos.get()
    .then(({ api, body }) => {
        api.issues.get({ repoName: body.data[0].name })
            .then(({ body: issues }) => console.log(issues));
    });

});

Your HTTP method

While we aim to make things as easy for you as possible we also want to keep things as minimal and flexible as possible. Thats why we ask from you you provide the library with your own method to do HTTP requests.

Using a framework or library?

You might be able to simply use your framework's HTTP method. Using for example Angular? You can just pass in Angular's $http method. Using jQuery? With one extra line of code you can use jQuery's $.ajax.

Example with jQuery

new Schematics('https://api.someurl.com', function(settings){
    //use a native JavaScript promise instead of jQuery's version
    return new Promise((resolve, reject) => $.ajax(settings).done(resolve).fail(reject));
});

Example with Fetch

new Schematics('https://api.someurl.com', function(settings){
    return fetch(settings.url, { method: settings.type, body: JSON.stringify(settings.data) })
});

Custom method

A few requirements: your HTTP method should:

  • Accept a settings object with:
    • type: the request type e.g. GET
    • url: the URL to send the request to
    • data: an object to store the request body or GET parameters in
  • Return a Promise

Example

new Schematics('https://api.someurl.com', function(settings){
    let doRequest = function(resolve, reject){
        //do request based on settings
    };
    return new Promise(doRequest);
});

Get Initial Schema Method

Instead of a URL to get your initial schema you can also pass in a method to get it. This is basically the callback to a promise which should be resolved with a json object containing a $schema object.

Example

new Schematics((resolve, reject) => {
    resolve({
        $schema: {
            users: "https://api.github.com/users"
        }
    });
}, httpMethod)
.then(({ api, body }) => console.log(api, body));

Say you would have your API return the schema in the response on your API authentication endpoint you could save your users the extra get schema request.

Example

new Schematics((resolve, reject, http) => {
    http({
        url: 'https://api.someurl.com/authorize',
        type: 'POST',
        data: {
            username: 'xxx',
            password: 'xxx'
        }
    })
    .then(resolve)
    .catch(reject);
}, httpMethod)
.then(({ api, body }) => console.log(api, body));

Schema Syntax

Some things you might find useful to know:

  • GET
    • A details object with a href is optional
    • Queries can be defined with just their key e.g. {?offset&limit}
    • Queries are always optional
    • Optional parameters can be defined using brackets e.g. {/username}
    • Required parameters can be defined using brackets e.g. /{username}
  • POST, PUT & DELETE
    • These all require a details object
    • This should contain a href String and a params Object
    • Params
      • An object with the properties that should go into the request body
      • As value you specify the data type the property should be
      • Currently only JavaScript data types are supported, these include: String, Number, Date, Array, Object and Boolean
      • These properties are required by default
      • If you would like to make a property optional define a details Object instead of the data type String e.g. { "type": "String", "optional": true }