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

@simplesmiler/taxios-generate

v0.3.0-alpha.1

Published

Typed wrapper around axios

Downloads

232

Readme

@simplesmiler/taxios-generate

Generate API typings for Taxios from Swagger/OpenAPI.

Generate

CLI

taxios-generate [options] <input-file-or-url>
Options:
  -o, --out FILE                    Write into this file
  -e, --export NAME                 Export generated definition under this name
      --skip-validation             Skip strict schema validation
      --named-enums                 [0.2.4+] Generate named enums instead of union types when possible
      --skip-additional-properties  [0.2.5+] Skip generating`[k: string]: unknown` for objects, unless explicitly asked
      --sort-fields                 [0.2.10+] Sort fields in interfaces instead of keeping the order from source
      --ignore-min-max-items        [0.2.14+] Ignore min and max items for arrays, preventing tuples being generated

As module (programmatically)

export interface GenerateProps {
  exportName: string;
  input: string | OpenAPI.Document; // https://apitools.dev/swagger-parser/docs/swagger-parser.html#parseapi-options-callback
  outputPath?: string;
  skipValidate?: boolean;
  sortFields?: boolean;
  unionEnums?: boolean;
  keepAdditionalProperties?: boolean;
  ignoreMinMaxItems?: boolean;
}

export function generate(options: GenerateProps): Promise<string>;

Example

Swagger: https://petstore.swagger.io/

CLI

taxios-generate -o PetStore.ts -e PetStore https://petstore.swagger.io/v2/swagger.json

[0.3.0] As module (programmatically)

import { generate } from '@simplesmiler/taxios-generate';
// import taxiosGenerate from '@simplesmiler/taxios-generate';

const result = await generate({
  exportName: 'PetStore',
  input: 'https://petstore.swagger.io/v2/swagger.json',
  outputPath: path.resolve('./PetStore.ts'), // optional
});

Result: PetStore.ts

Use

import axios from 'axios';
import { Taxios } from '@simplesmiler/taxios';
import { PetStore } from './PetStore';

const taxios = new Taxios<PetStore>(axios.create({ baseURL: 'https://petstore.swagger.io/v2' }));
const pet = await taxios.get('/pet/{petId}', { params: { petId: 1 } });

See @simplesmiler/taxios package for details.

[0.2.4+] Union types and Named enums

WARNING: The following only applies to "standalone" enums. Inline enums can only be expressed as inline union types.

WARNING: Since 0.3.0 --named-enums is the default behavior, and to generate union types use --union-enums.

Look at the following OpenAPI snippet:

components:
  schemas:
    PetStatus:
      enum: [Placed, Approved, Delivered]

Prior to 0.2.4 taxios-generate would generate a union type:

export type OrderStatus = 'Placed' | 'Approved' | 'Delivered';

Since 0.2.4 you can use --named-enums option to generate a named enum instead:

export enum OrderStatus {
  Placed = 'Placed',
  Approved = 'Approved',
  Delivered = 'Delivered',
}

By default, enum names will be the same as values, assuming they are valid identifiers.

Names can also be given explicitly using x-enumNames. This is useful for numeric enums:

components:
  schemas:
    StatusCode:
      enum: [200, 400]
      x-enumNames: [Ok, BadRequest]

If no valid names are available, taxios-generate will fallback to generating a union type.

[0.2.5+] Additional properties

WARNING: Since 0.3.0 --skip-additional-properties is the default behavior, and to generate additional properties use --keep-additional-properties.

Look at the following OpenAPI snippet:

components:
  schemas:
    User:
      type: object
      properties:
        name:
          type: string

Prior to 0.2.5 taxios-generate would generate an interface with explicitly allowed additional properties:

export interface User {
  name?: string;
  [k: string]: unknown;
}

Since 0.2.5 you can use --skip-additional-properties option to generate an interface without then:

export interface User {
  name?: string;
}

This option treats unspecified value of OpenAPI additionalProperties field as false.

If additionalProperties is explicitly given as true, then [k: string]: unknown will still be added.

components:
  schemas:
    User:
      type: object
      properties:
        name:
          type: string
      additionalProperties: true

[0.2.10+] Sort fields

Look at the following OpenAPI snippet:

components:
  schemas:
    User:
      type: object
      properties:
        name:
          type: string
        email:
          type: string
      required:
        - name
        - email
      additionalProperties: false

Prior to 0.2.10 fields in generated interface would always be in the same order as in the source:

export interface User {
  name: string;
  email: string;
}

Since 0.2.10 you can use --sort-fields option to enforce the alphabetical order of fields:

export interface User {
  email: string;
  name: string;
}

This option is useful if you want to minimize merge conflicts, and do have not control over the source OpenAPI document.