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

@souzjfe/swax

v0.2.7

Published

CLI para integração entre Swagger e Axios

Downloads

185

Readme

Swax CLI

Swax CLI is a command-line tool for generating TypeScript types for API routes from a Swagger JSON file. The CLI can also create a default configuration file to help you get started.

For now, it is only possible to generate types to suggest possible endpoints in the API, but soon there will be versions for typing requests and responses.

How It Works

When you run the generate command, Swax CLI fetches the Swagger JSON from the specified API and generates a TypeScript declaration file, axios.d.ts. This file complements the type definitions for the Axios library by providing specific typings for your API routes. You can further customize these types as needed.

Installation

To install the CLI globally, run the following command:

npm install -D @souzjfe/swax

Usage

The CLI provides two main commands: generate and init.

init Command

The init command creates a default swax.config.ts configuration file in the root of your project.

Syntax

npx swax init

generate Command

The generate command fetches the Swagger JSON and generates TypeScript types for API routes and methods in axios.d.ts file.

Syntax

npx swax generate [options]

Options

  • -c, --config <path>: Path to the configuration file. Default is ./swax.config.ts.

Example

npx swax generate 

If you have changed the swax.config.ts file you will have to inform it:

npx swax generate -c ./path/to/swax.config.ts

Configuration File

The swax.config.ts file is a TypeScript module that should export an object satisfying the SwaxConfig interface. Here is an example of how the file might be structured:

import { SwaxConfig } from './src/types';

export default {
  baseURL: 'https://petstore.swagger.io/v2',
  jsonSchemaPath: '/swagger.json',
  outputTypesPath: '@types/',
  ignoredPaths: ['/pets', '/store'],
} as SwaxConfig;

Configuration Fields

  • baseURL: The base URL of your API.
  • jsonSchemaPath: The path to the Swagger JSON schema.
  • outputTypesPath: The path to the directory where generated types will be saved.
  • ignoredPaths: A list of paths that should be excluded when generating the route types. These paths will be stripped from the beginning of each route when generating the types.

Example: Multiple Axios Instances

If you want to create more than one axios isntance and concatenate the baseURL with some path, like the following example:

const httpPets = axios.create({ baseURL: baseURL + '/pets' });
const httpStore = axios.create({ baseURL: baseURL + '/store' });

You must insert each path that has been concatenated into ignoredPaths:

export default {
  baseURL: 'https://petstore.swagger.io/v2',
  jsonSchemaPath: '/swagger.json',
  outputTypesPath: '@types/',
  ignoredPaths: ['/pets', '/store'], // here
} as SwaxConfig;

Limitations and Future Versions

Currently, the CLI generates TypeScript types only for the API routes. The following features are planned for future releases:

  • Version 1.0.0: Type definitions for API responses and request data.

Contributing ❤️

If you wish to contribute to the project, please follow these steps:

  1. Fork the repository.
  2. Create a branch for your changes (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Open a pull request.

Project Structure

The project is organized as follows:

  • src/commands/: Contains files implementing the CLI commands.
    • generate.ts: Logic for generating TypeScript types.
    • init.ts: Logic for creating the default configuration file.
  • src/utils/: Contains utility functions used by the commands.
    • swaggerUtils.ts: Functions for normalizing API paths.
    • fileUtils.ts: Functions for file handling.
  • src/index.ts: The main file for the CLI that sets up and executes commands.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

For more information or inquiries, please contact Jeferson.