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

swagger-js-api-generator

v0.0.1

Published

This tool allows you to automatically generate an api object from a given swagger schema. Since this tool doesn't define how resource should be fetched, it is intended to be composed in to a platform or framework specific resource handler.

Downloads

1

Readme

Swagger JS API Generator

This tool allows you to automatically generate an api object from a given swagger schema. Since this tool doesn't define how resource should be fetched, it is intended to be composed in to a platform or framework specific resource handler.

If this library is require'd from Node, it comes with .fetch and .save methods to allow you to pull a schema from a given swagger resource listing. When the library is used client side, it is only a factory function creates an API object from a schema object. The common flow would be to generate the schema during your build process and embed it in to your client-side app.

This tool differs from swagger-js in that it doesn't define how the resources will be fetched, instead it takes in a callback function which is called when ever a resource is being called, making it simple to tie the request logic to the platform or framework of your choosing. This library is also an order of magnitude smaller, weighing in at ~1kb, this is primarily because it doesn't ship with the methods of interacting with the API server as swagger-js does.

Example

// in nodejs
var schema = require('swagger-schema');
schema.save('http://api.app.com/api-docs', 'my-schema.json');
// in a browser

// Assuming you've loaded my-schema as mySchema
var api = swaggerSchema(mySchema, function(operation){
  return function(data){
    console.log(operation.nickname, 'called with', data);
  }
});

api.user.create('kanye');
// console: 'create called with kanye';

Browser API

var api = swaggerSchema(schemaObject, operationHandler)

schemaObject (an object)

An object created by the swagger-schema nodejs library. It's basically a single json object of the resource listing and all associated api declarations.

operationHandler (a function)

Operation handler is a function which creates the request handler for a given operation. The request handler is a function which is called whenever a client interacts with the api (e.g. api.user.get() would call the request handler that was returned for the 'get' operation).

For example, api.user.get('kanye', kanyeHandler) calls the operation with the nickname 'get' on the 'user' resource with 'kanye' as the first argument and a callback as the second. A very basic handler may look like this:

function operationHandler(operation){
  var endpoint = operation.apiObject.basePath + operation.path;

  return function(data, callback){
    var url = endpoint.replace('{userName}', data);

    var req = new XMLHttpRequest();
    req.onload = function(){
      callback(this.responseText);
    };
    req.open(this.method, url, true);
    req.send();
  }
}

function kanyeHandler(text){
  console.log('kanye is', text);
}

From the this variable in the operation handler you can access the entire schema of the API: