Giter VIP home page Giter VIP logo

swagger-ts-template's Introduction

swagger-ts-template

This fork's npm package:

yarn add @proerd/swagger-ts-template

Quick 'n dirty solution to integrate swagger v2 into a typescript codebase.

  • Generates an (opinionated) typescript consumer API. Call REST APIs just as functions. Forget about HTTP details. Get doc suggestions and typed responses.
var generator = require("swagger-ts-template")
var swaggerFile = require("./api.json")

generator.genPaths(swaggerFile, { output: "./api" }).then(() => console.log("okay"))

What's different on this one?

This has been serving me great for a couple of years.

  • The scaffold gives almost no opinion on how should you do your requests. Receive the processed playload, do the request with whatever fetch or axios or any other thing, return a response or throw an error;
  • The default "payload processing" is also easily extensible;
  • You can add extra input or output arguments through interface augmenting, which works fine for covering special cases;

(detailed article with motivations)

genPaths options

Generates a typed API consumer on a defined folder. genTypes is already included.

export async function genPaths(swaggerDoc: SwaggerDoc, opts: genPathsOpts): Promise<void>

type genPathsOpts = {
  output: string //output folder path
  moduleStyle: "commonjs" | "esm"
  failOnMissingOperationId?: boolean
  typesOpts?: genTypesOpts
  mapOperation?: (
    operation: Operation,
    pathItem: SwaggerIo.V2.SchemaJson.Definitions.PathItem,
    pathKey: string,
    methodKey: string
  ) => Operation
  templateString?: string
}

Handling incomplete APIs

swagger-ts-template relies on the presence of operationId and tags on the swagger definition.

If those are missing, it will attempt to generate default values from the path names.

The default generated values may not fit an API. In that case, you can complement a swagger definition by adding yourelf those fields though mapOperation.

Setting up the consumer API

You have to bootstrap the api skeleton telling how should you run the requests.

This setting is global and must be run before the 1st request takes place.

import { SwaggerRequester, settings as swaggerSettings, IRequest } from "./swagger/api-common"

class MyRequester extends SwaggerRequester {
  handler(request: IRequest) {
    const opts = request.options || {}
    const resp = await doTheRequest(
      request.verb as any,
      request.url,
      request.query,
      request.body,
      opts
    )
    return resp || {}
  }
}

const myRequester = new MyRequester()
swaggerSettings.getRequester = () => myRequester

Generated consumer API

  • All declared types are exported as interface's or type aliases when necessary;
  • "Anonymous" types (actually, the request and response types) will also get an interface for them, with a generated name (if they are not already present in "declarations");
  • Methods are grouped in files by their tag and named by their operationId;
  • The transport is abstracted away. All inputs are passed by their names, there is no distiction whether they are in the path, query parameters or body; The method declarations carry the necessary metadata to achieve this.

Sample generated file excerpt:

import * as Types from "../api-types"
import * as ApiCommon from "../api-common"

export type getPriceEstimates_Type = {
  end_latitude: number
  end_longitude: number
  start_latitude: number
  start_longitude: number
}
export const getPriceEstimates = ApiCommon.requestMaker<
  getPriceEstimates_Type,
  Types.getPriceEstimates_Response
>({
  id: "getPriceEstimates",
  path: "/estimates/price",
  verb: "GET",
  parameters: [
    { name: "end_latitude", required: true, in: "query" },
    { name: "end_longitude", required: true, in: "query" },
    { name: "start_latitude", required: true, in: "query" },
    { name: "start_longitude", required: true, in: "query" }
  ]
})

///...

Sample consumer invocation:

import CustomerApi = require("./api/modules/Customer")
let customer = await CustomerApi.getCustomer({
  customerId: 999
})

See also the samples path in this repo.

Personalization

  • The input parameters from genPaths may be used to tweak the generation a bit; Most notably, you can remap the operation objects to do things such as renaming methods;
  • You can extend the request and response types by augmenting either GApiCommon#MergeToRequest or GApiCommon#MergeToResponse global interfaces.
declare global {
  namespace GApiCommon {
    interface MergeToRequest {
      _allowCache: true
    }

    interface MergeToResponse {
      page: number
      numPages: number
    }
  }
}

swagger-ts-template's People

Contributors

wkrueger avatar everton-amorim avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.