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

@insertish/oapi

v0.1.18

Published

API Library Generator for OpenAPI

Downloads

563

Readme

OAPI Client

This is a CLI for auto-generating an API library for a given OpenAPI spec which uses JSON primarily and conforms to Revolt's API style.

Thank you to bree for helping me figure out all the especially difficult types.

Goals

  • Commit programming atrocities internally.
  • Pretend everything is fine externally.
  • Provide a simple strongly typed API experience to the user.

Example

Here's what you can achieve with this library:

import { API } from 'your-api';

let id = 'user_id';

// By default, we use the first server specified in the API spec.
new API()
    // Path parameters are specified using template strings.
    .get(`/users/${id}`)
    .then(user => {
        // User is still fully typed!
        return user.username;
    })
    .then(console.log);

Conflict resolution is also automatic, so conflicting prefixes will not cause issues:

import { API } from 'your-api';

// For a route /some/{string}:
new API()
    .get('/some/this is an example')
    .then(x => {
        x // number
    })

// For a route /some/{string}/conflicting:
new API()
    .get('/some/this is an example/conflicting')
    .then(x => {
        x // string
    })

You can also provide your query and body parameters at the same time:

import { API } from 'your-api';

// PATCH /users/@me?preserve=true
// Body: { username: string }
new API()
    .patch(`/users/@me`, {
        // Specify query parameters
        preserve: true,
        // Or body parameters
        username: 'something'
    });

This removes the overhead of having to remember exactly what goes where and provides a much nicer (and still strongly typed) API experience.

Currently this only supports rauth and Revolt authentication, but you can still provide your own Axios config or add your own authentication.

new API({
    baseURL: 'https://example.com',
    authentication: {
        rauth: 'session token'
    }
})

Setup Guide

Create a new project and configure it:

yarn init
yarn add @insertish/oapi axios lodash.defaultsdeep
yarn add --dev typescript openapi-typescript @types/lodash.defaultsdeep

Place your OpenAPI specification at the root of your API library at OpenAPI.json.

Update your package.json to include the following:

{
    [...],
    "scripts": {
        "build": "oapilib && tsc"
    }
}

Note: you may want to use STRICT=1 opailib, see "Strict Mode" below.

Setup Typescript:

yarn exec tsc -- --init

Change tsconfig.json to include:

{
    "compilerOptions": {
        "outDir": "./dist",
        "rootDir": "./src",
        "declaration": true
    }
}

Create a new src directory.

Now generate the library: (src folder will be overwritten!)

yarn build

Replace anyOf with oneOf

If your spec uses anyOf, the values are mapped as an intersection while you may actually want all of these values to be treated as if they were oneOf, or otherwise a union.

You can specify the environment variable REWRITE_ANYOF to any truthy value.

REWRITE_ANYOF=1 oapilib