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

swaxios

v0.2.5

Published

Swagger API client generator based on axios and TypeScript.

Downloads

14

Readme

Swaxios

Swaxios

A Swagger (OpenAPI v2) API client generator based on axios and written in TypeScript.

Motivation

The Swaxios project automates the creation of an API client for TypeScript applications, which can be used in web browsers and Node.js environments.

At the time of writing this tool (2019-04-26), the Swagger Codegen project only provided separate SDK generators for browsers and Node.js (typescript-fetch and typescript-node). Unfortunately, the typescript-fetch generator for browsers is not compatible with all Node.js applications since the fetch API became globally available only as an experimental feature in Node.js 18 (2022-04-19).

Swaxios offers an API generator that works in both environments (browsers & Node.js 16+) by utilizing the flexible axios request library. API calls are routed through axios and can be easily customized using request interceptors.

Limitations

This library can only work with a valid OpenAPI Version 2 specification, which must be in the form of a file or URL. It does not support OpenAPI Version 3 specifications.

If you need a generator for Open API v3 specs, you can test typescript-axios from Swagger Codegen (added on 2020-09-21).

java -jar swagger-codegen-cli-3.0.24.jar generate -l typescript-axios -i ./swagger.json -o ./api-client

There is also an npm package (@openapitools/openapi-generator-cli) which exposes this generator. You can use it the following way in your scripts:

{
  "scripts": {
    "generate": "openapi-generator-cli generate -i ./swagger.json -g typescript-axios -p \"withoutRuntimeChecks=true,withInterfaces=true\" -o ./api-client"
  }
}

Installation

You can install Swaxios globally (npm i -g swaxios) or add it to your devDependencies.

Usage

Display all CLI options:

swaxios --help

If you pass an OpenAPI v2 specification (JSON or YAML) to Swaxios, then it generates a fully typed API client for you which uses axios under the hood:

# Provide a Swagger input file (JSON or YAML)
swaxios -i ./path/to/swagger.json -o ./path/to/output/directory
swaxios -i ./path/to/swagger.yml -o ./path/to/output/directory

# Alternative: Provide a URL to a Swagger endpoint
swaxios -i http://127.0.0.1:3000/documentation-json -o ./path/to/output/directory

With the -f option, you can force Swaxios to overwrite existing files in the output path:

swaxios -i ./path/to/swagger.json -o ./path/to/output/directory -f

Examples

You can find many examples of generated API client code in our snapshots section.

Here is a basic example:

ExchangeService.ts

/* tslint:disable */

/**
 * This file was automatically generated by "Swaxios".
 * It should not be modified by hand.
 */

import {AxiosInstance, AxiosRequestConfig} from 'axios';

export class ExchangeService {
  private readonly apiClient: AxiosInstance;

  constructor(apiClient: AxiosInstance) {
    this.apiClient = apiClient;
  }

  deleteExchange = async (id: number): Promise<void> => {
    const config: AxiosRequestConfig = {
      method: 'delete',
      url: `/api/v1/exchange/${id}`,
    };
    await this.apiClient.request(config);
  };
}

It has been generated from the following path:

swagger.json

{
  // ...
  "paths": {
    "/api/v1/exchange/{id}": {
      "delete": {
        "consumes": ["application/json"],
        "operationId": "deleteExchange",
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "type": "number"
          }
        ],
        "produces": ["application/json"],
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    }
  }
  // ...
}