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

openfetch

v0.1.3

Published

Fetch-like OpenAPI 3 client for JS

Downloads

7

Readme

OpenFetch

ci

Fetch-like OpenAPI client library. Supports OpenAPI 3 only.

Install from NPM: npm install openfetch

Usage

import * as openfetch from 'openfetch';

// Build an API based on the spec. Shown below are the default options:
const api = openfetch.create(SPEC, {
  // The base URL from which to make requests.
  url,
  // Enable logging. Validation errors will be logged to the console.
  logging: false,
  // The console, providing the log methods (only `warn` is used).
  console: window.console,
  // The default implementation of fetch to use for making requests.
  fetch: window.fetch,
});

// If your spec is hosted, it can be retrieved automatically.
const api = await openfetch.hosted('http://example.com', {
  // ... same opts as above
});

// Create an invocation context with credentials and such. The keys of the credentials
// object are names of security schemes, and the values are their values...
// *   HTTP Basic Auth expects the value to be `{ user, pass }`
// *   HTTP Bearer Auth expects the value to be just the token (i.e. not including the "Bearer" prefix)
// *   Other HTTP auth expects the full header value (i.e. including the scheme name)
// *   OAuth2 will pass the token via Autorization header
const invoke = openfetch.client({
  // Override the base URL from which to make requests
  url,
  // Override the implementation of `fetch` again. This value takes precedence over the one
  // passed to `create`, if both were provided.
  fetch,
  // The credentials to use to satisfy security requirements
  credentials: {},
});

// Invoke an operation by `operationId` (here: `getUser`):
// *   Parameter are supplied by name
// *   Options are the same as fetch, with a few being supplied automatically:
//     *   `Content-Type` will be determined automatically if the spec only defines one request body
//         type, otherwise it must be supplied via `headers`.
//     *   If `Content-Type` is JSON, `body` will be passed through `JSON.stringify`.
//         No processing will be done to any other bodies.
//     *   The `Authorization` header will be set automatically based on the security requirements.
//     *   Set the `Accept` header manually to specify which response format to receive.
const response = await invoke(api.getUser({ id: 'foxfriends' }, { headers, body }));

// The response is whatever is returned by the provided implementation of `fetch`. Refer to the
// relevant documentation on how to handle that response. In particular:
// *   The response body is not interpreted at all (e.g. JSON is not parsed automatically)
// *   The response status is not interpreted at all (e.g. 4XX/5XX reponses do not throw)
console.assert(response instanceof Response)

Points to note:

  • This package assumes your OpenAPI spec is valid/correct, and that you are (for the most part) calling it with sensible values. Undefined behaviour will occur if you deviate from spec.
  • The servers field of the spec is ignored. Provide a correct url on your own.
  • There is currently no support for any extensions, but to be able to implement those as plugins is something that is being considered.

Testing

So far... very little testing has been done. Just a bit of manual stuff. Trust this project at your own risk for now, until I feel like writing a proper test suite.

Contributing

Contributions are welcome! Please send a PR or create issues if you would like something improved.