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

oas-client

v0.0.8

Published

OpenAPI client generator. Creates at runtime a fully functional api client based on an OpenAPI Specification. Providing automatic methods creation parameters definitions and validations.

Downloads

8

Readme

oas-client

Build Status npm npm

OpenAPI client generator. Creates, at runtime, a fully functional api client based on an OpenAPI Specification v3. Providing automatic methods creation parameters definitions and validations. The goal is to provide consistency and a fast way to create and maintain api clients. It reduces the need of writing and preparing by hand all the necessary code to handle the communication with such servers.

  • Lightweight
  • Node.js and Browser ready
  • Automatic methods creation and parameters definitions
  • Built-in parameters validation based on the parameters spec
  • Body Schema validation (without injecting a new dependency on the project by default)
  • Path Templating handling
  • Server Variables replacement following enum specification
  • Strict mode (allowing to pass only what is presented on the specification)

Installation

npm install oas-client --no-optional

--no-optional because ajv (to validate body schema) and yamljs (to read the spec from .yml format) are optional.

Usage

Basic Example



const specExample = {
    "openapi": "3.0.0",
    "info": {
        "version": "1.0.0",
    },
    "servers": [
        {
            "url": "https://github.com"
        }
    ],
    "paths": {
        "/{profile}": {
            "get": {
				"operationId": "fetchProfile",
				"parameters": [
                    {
                        "name": "profile",
                        "in": "path",
                        "required": true,
                        "description": "The profile to retrieve",
                        "schema": {
                            "type": "string"
                        }
                    }
                ]
			}
		},
		"/{profile}/{repository}": {
            "get": {
                "operationId": "fetchRepository"
			},
			"parameters": [
				{
					"name": "profile",
					"in": "path",
					"required": true,
					"description": "The profile to retrieve",
					"schema": {
						"type": "string"
					}
				},
				{
					"name": "repository",
					"in": "path",
					"required": true,
					"description": "The repository to retrieve",
					"schema": {
						"type": "string"
					}
				}
			]
        }
    }
};

const githubClient = oasClient.create(specExample);

// http://github.com/DiegZoracKy will be requested
githubClient.fetchProfile({data: {profile: 'DiegoZoracKy'}})
	.then(console.log)
	.catch(console.error);

// http://github.com/DiegZoracKy/oas-client will be requested
githubClient.fetchRepository({data: {profile: 'DiegoZoracKy', repository: 'oas-client'}})
	.then(console.log)
	.catch(console.error);

How it works

All the paths and operations (http methods) presented on the specification becomes a method accessible by their operationId (when present) or by its pathOperation (e.g. "GET /path") on the client object generated. When passing data for a method, the specification is what tells the client what to do with it. If it should be set as a querystring, as a path templating, etc.

On the following example:

githubClient.fetchProfile({data: {profile: 'DiegoZoracKy'}})

The request GET https://github.com/DiegoZoracky will be issued, as the specification tells that fetchProfile is the operationId related to the path /{profile} when being called via GET. Also it is defined that the parameter profile is present on the path. Finally enters the information contained at servers, which instructs to where the request should be made.

The same operation can be called by its pathOperation (essential to when there is not an operationId defined):

githubClient['GET /{profile}']({data: {profile: 'DiegoZoracKy'}})

Options / Config

The create method accepts a second parameter with a config object:

oasClient.create(specification, options);

Validate Body (default: false)

In order to validate the post body following the schema defined at the property requestBody, set:

validateBody: true.

Install de optionalDependency ajv to enable this feature.

Validate Parameters (default: true)

By default the client will validate the parameters defined as required on the specification. When they are not passed in, an error will be returned and the request will not be performed. To turn off this validation set:

validateParameters: false.

Default Parameters

Default parameters can be set at once to all requests to be made (useful to set Authorization tokens):

{
	defaultParameters: {
		cookie: { Authorization: 'Token!' },
	}
}

or by specific paths.

{
	paths: {
		'GET /search': {
			defaultParameters: {
				query: { 
					limit: '20' 
				}
			}
		}
	}
}