Code Monkey home page Code Monkey logo

openapi-client's Introduction

OpenAPI Client

Generate ES6 or Typescript service integration code from an OpenAPI 2.0 spec.

Also supports optional Redux action creator generation.

Tested against JSON services.

Install

In your project

npm install openapi-client --save-dev

Or globally to run CLI from anywhere

npm install openapi-client -g

Type Dependencies

If targeting TypeScript you'll also need to install the isomorphic-fetch typings in your project.

npm install @types/isomorphic-fetch --save-dev

Usage – Generating the API client

openapi-client generates action creators in the outDir of your choosing. The rest of the examples assume that you've set --outDir api-client. You can generate the api-client either using the CLI, or in code.

CLI

Usage: openapi [options]

Options:

  -h, --help              output usage information
  -V, --version           output the version number
  -s, --src <url|path>    The url or path to the Open API spec file
  -o, --outDir <dir>      The path to the directory where files should be generated
  -l, --language <js|ts>  The language of code to generate
  --redux                 True if wanting to generate redux action creators

Code

const openapi = require('openapi-client')
openapi.genCode({
  src: 'http://petstore.swagger.io/v2/swagger.json',
  outDir: './src/service',
  language: 'ts',
  redux: true
})
.then(complete, error)

function complete(spec) {
  console.info('Service generation complete')
}

function error(e) {
  console.error(e.toString())
}

Usage – Integrating into your project

Initialization

If you don't need authorization, or to override anything provided by your OpenAPI spec, you can use the actions generated by openapi-client directly. However, most of the time you'll need to perform some authorization to use your API. If that's the case, you can initialize the client, probably in the index.js of your client-side app:

import serviceGateway from './path/to/service/gateway';

serviceGateway.init({
  url: 'https://service.com/api', // set your service url explicitly. Defaults to the one generated from your OpenAPI spec
  getAuthorization // Add a `getAuthorization` handler for when a request requires auth credentials
});

// The param 'security' represents the security definition in your OpenAPI spec a request is requiring
// For bearer type it has two properties:
// 1. id - the name of the security definition from your OpenAPI spec
// 2. scopes - the token scope(s) required
// Should return a promise
function getAuthorization(security) {
  switch (security.id) {
    case 'account': return getAccountToken(security);
    // case 'api_key': return getApiKey(security); // Or any other securityDefinitions from your OpenAPI spec
    default: throw new Error(`Unknown security type '${security.id}'`)
  }
};

function getAccountToken(security) {
  const token = findAccountToken(security); // A utility function elsewhere in your application that returns a string containing your token – possibly from Redux or localStorage
  if (token) return Promise.resolve({ token: token.value });
  else throw new Error(`Token ${type} ${security.scopes} not available`);
}

Initialization Options

The full set of gateway initialization options.

export interface ServiceOptions {
  /**
   * The service url.
   *
   * If not specified then defaults to the one defined in the Open API
   * spec used to generate the service api.
   */
  url?: string${ST}
  /**
   * Fetch options object to apply to each request e.g 
   * 
   *     { mode: 'cors', credentials: true }
   * 
   * If a headers object is defined it will be merged with any defined in
   * a specific request, the latter taking precedence with name collisions.
   */
  fetchOptions?: any${ST}
  /**
   * Function which should resolve rights for a request (e.g auth token) given
   * the OpenAPI defined security requirements of the operation to be executed.
   */
  getAuthorization?: (security: OperationSecurity, 
                      securityDefinitions: any,
                      op: OperationInfo) => Promise<OperationRightsInfo>${ST}
  /**
   * Given an error response, custom format and return a ServiceError
   */
  formatServiceError?: (response: FetchResponse, data: any) => ServiceError${ST}
  /**
   * Before each Fetch request is dispatched this function will be called if it's defined.
   * 
   * You can use this to augment each request, for example add extra query parameters.
   * 
   *     const params = reqInfo.parameters;
   *     if (params && params.query) {
   *       params.query.lang = "en"
   *     }
   *     return reqInfo
   */
  processRequest?: (op: OperationInfo, reqInfo: RequestInfo) => RequestInfo${ST}
  /**
   * If you need some type of request retry behavior this function
   * is the place to do it.
   * 
   * The response is promise based so simply resolve the "res" parameter
   * if you're happy with it e.g.
   * 
   *     if (!res.error) return Promise.resolve({ res });
   * 
   * Otherwise return a promise which flags a retry.
   * 
   *     return Promise.resolve({ res, retry: true })
   * 
   * You can of course do other things before this, like refresh an auth
   * token if the error indicated it expired.
   * 
   * The "attempt" param will tell you how many times a retry has been attempted.
   */
  processResponse?: (req: api.ServiceRequest,
                    res: Response<any>,
                    attempt: number) => Promise<api.ResponseOutcome>${ST}
  /**
   * If a fetch request fails this function gives you a chance to process
   * that error before it's returned up the promise chain to the original caller.
   */
  processError?: (req: api.ServiceRequest,
                  res: api.ResponseOutcome) => Promise<api.ResponseOutcome>${ST}
  /**
   * By default the authorization header name is "Authorization".
   * This property allows you to override it.
   * 
   * One place this can come up is where your API is under the same host as
   * a website it powers. If the website has Basic Auth in place then some
   * browsers will override your "Authorization: Bearer <token>" header with
   * the Basic Auth value when calling your API. To counter this we can change
   * the header, e.g.
   * 
   *     authorizationHeader = "X-Authorization"
   * 
   * The service must of course accept this alternative.
   */
  authorizationHeader?: string${ST}
}

Using generated Redux action creators

You can use the generated API client directly. However, if you pass --redux or redux: true to openapi-client, you will have generated Redux action creators to call your API (using a wrapper around fetch). The following example assumes that you're using react-redux to wrap action creators in dispatch. You also need to use for example redux-thunk as middleware to allow async actions.

In your component:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import functional from 'react-functional';

import { getPetById } from '../api-client/action/pet';

const Pet = ({ actions, pet }) => (
  <div>
    {pet.name}
  </div>
)

// Dispatch an action to get the pet when the component mounts. Here we're using 'react-functional', but this could also be done using the class componentDidMount method
Pet.componentDidMount = ({ actions }) => actions.getPetById(id);

const mapStateToProps = state => (
  {
    pet: getPet(state) // a function that gets 
  }
);

const mapDispatchToProps = dispatch => (
  {
    actions: bindActionCreators({ getPetById }, dispatch)
  }
);

export default connect( mapStateToProps, mapDispatchToProps)(functional(Pet));

The client can't generate your reducer for you as it doesn't know how merge the returned object into state, so you'll need to add a something to your reducer, such as:

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case GET_PET_BY_ID_START:
      return state.set('isFetching', true);
    case GET_PET_BY_ID: // When we actually have a pet returned
      if(!action.error){
        return state.merge({
          isFetching: false,
          pet: action.payload,
          error: null,
        });
      }
      else{ // handle an error
        return state.merge({
          isFetching: false,
          error: action.error,
        });
      }
    default:
      return state;
  }
}

openapi-client's People

Contributors

gazab avatar hallian avatar jbowes avatar johnsusek avatar kapulara avatar laszlo-vadasz-deltatre avatar maxinteger avatar mike-stead-deltatre avatar mikestead avatar mtribes-sdk-bot avatar pgr86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openapi-client's Issues

Dot (.) in function and parameter name.

Thanks for the package.

my problem is that it is generates classes with dot in the parameters and functions:

export const _PASSPORT_CTRL.LOGIN_START = 's/PassportCtrl/_PASSPORT_CTRL.LOGIN_START'

export function PassportCtrl.login(body: api.UserLogin): Promise<api.Response<api.User>> {
  const parameters: api.OperationParamGroups = {
    body: {
      body
    }
  }
  return gateway.request(PassportCtrl.loginOperation, parameters)
}

and my swagger.json:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Api documentation",
    "description": "",
    "termsOfService": ""
  },
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "securityDefinitions": {},
  "tags": [
    {
      "name": "PassportCtrl"
    }
  ],
  "paths": {
    "/rest/auth/login": {
      "post": {
        "operationId": "PassportCtrl.login",
        "tags": [
          "PassportCtrl"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/UserLogin"
            }
          }
        ],
        "consumes": [],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/User"
            }
          },
          "400": {
            "description": "Missing required parameter"
          }
        },
        "security": [],
        "produces": [],
        "description": "Description of this route",
        "summary": "Summary of this route"
      }
    },
    "/rest/auth/isAuthenticated": {
      "post": {
        "operationId": "PassportCtrl.isAuthenticated",
        "tags": [
          "PassportCtrl"
        ],
        "parameters": [],
        "consumes": [],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/User"
            }
          }
        },
        "security": [],
        "produces": [],
        "description": "return the user if is Authenticated and  exists in database",
        "summary": "Check if is user Authenticated"
      }
    },
    "/rest/auth/logout": {
      "get": {
        "operationId": "PassportCtrl.logout",
        "tags": [
          "PassportCtrl"
        ],
        "parameters": [],
        "consumes": [],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "security": [],
        "produces": []
      }
    }
  },
  "definitions": {
    "User": {
      "properties": {
        "_id": {
          "minLength": 3,
          "maxLength": 20,
          "type": "string"
        },
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "password": {
          "type": "string"
        }
      },
      "required": [
        "_id"
      ],
      "type": "object"
    },
    "UserLogin": {
      "properties": {
        "email": {
          "format": "email",
          "type": "string"
        },
        "password": {
          "minLength": 6,
          "type": "string"
        }
      },
      "required": [
        "email",
        "password"
      ],
      "type": "object"
    }
  }
}

generic request function is not correct in TypeScript

Hey,

the request function request in the generated typescript files doesn't seem to be right:

export function request<T>(op: api.OperationInfo, parameters?: api.OperationParamGroups, attempt = 1): Promise<T> {
	if (!attempt) attempt = 1;
	return acquireRights(op, spec, options)
		.then(rights => {
			parameters = parameters || {}
			const baseUrl = getBaseUrl(spec)
			let reqInfo = { parameters, baseUrl }
			if (options.processRequest) {
				reqInfo = options.processRequest(op, reqInfo)
			}
			const req = buildRequest(op, reqInfo.baseUrl, reqInfo.parameters, rights)
			return makeFetchRequest(req)
				.then(res => processResponse(req, <any>res, attempt, options), e => processError(req, e))
				.then(outcome => outcome.retry ? request(op, parameters, attempt + 1) : outcome.res)
		})
}

acquireRights returns Promise<api.OperationRights> but request<T> is of type Promise<T>.

Request: docs on using generated code, in particular, auth

Most of the code that the package generates is pretty dead ahead, but for the life of me I can't figure out how to use my bearer token with Redux. This line hints that there should be an options.getAuthorization, but I can't work out how to do that with my Redux action.

If you can give me some quick tips, I can then make a PR to update the docs. If I get the time, I'll also try to add a few more examples about how to use the generated code.

Feature request: dispatch an _ERROR action if there was an API error

You're going to love me @mikestead .

When I've used Redux before, one of the starters that I used had a really nice model, that was, when making AJAX requests, to dispatch _REQUEST, _SUCCESS and _FAILURE actions depending on what happened in the api service. Originally I thought that this was daft, but actually, you want to be able to handle errors in different kinds of requests differently.

It looks like it should be a pretty easy change as you'll just have to add a .catch() on the dispatch around here – and dispatch a new action, such MY_ACTION_NAME_ERROR. If I'm not mistaken, makeFetchRequest already returns a fetch promise, which should already reject if there's an error, which means that there shouldn't be too much working through the entire codebase.

Are my assumptions above correct? If so, would you accept a PR for that?

Outdir is relative to package.json, rather than the file

When I use openapi.genCode(), it outputs the file relative to package.json, rather than the file in which it was called. Is this the expected behaviour? It's a bit different from what most packages do.

Otherwise, great package! Love it!

File upload missing

I have a problem with file upload through formData with generated openapi-client.

  • I create a new post with type 'file':

screen shot 2017-03-27 at 15 43 12

  • All good in swagger UI:
    screen shot 2017-03-27 at 15 45 13

  • openapi-client generates Typescript type error "[ts] Cannot find name 'file'.":

screen shot 2017-03-27 at 15 41 51

  • I resolved the type error by having "file" replaced with "File" (big F https://developer.mozilla.org/en/docs/Web/API/File) with following PR #11.

  • But the actual file upload still doesn't work since request headers doesn't get Content-Type: "multipart/form-data" and there is no request payload either.
    screen shot 2017-03-27 at 15 49 59

  • It should look something like this to work:
    screen shot 2017-03-27 at 15 53 07
    screen shot 2017-03-27 at 15 56 12

  • File upload feature would be very handy to have in this openapi-client!

The models are also generated ?

I have a server which is OpenAPI v3.0 compatible.

I tried to generate the functions and it worked. Looking into the code I see that I will have to write the "models" by myself. Is it in purpose or because some issue the "models" are not generated ?

Generator is lacking sanitization of identifiers

I have a spec with bits like that:

        "parameters": [
          {
            "name": "Body",
            "in": "body",
            "required": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/(fo)RegisterUserrequest"
            }
          },
          {
            "name": "Content-Type",
            "in": "header",
            "required": true,
            "type": "string",
            "description": ""
          },
          {
            "name": "x-api-key",
            "in": "header",
            "required": true,
            "type": "string",
            "description": ""
          }
        ],

or

    "(apple)VerifyReceiptWithApplerequest": {
      "title": "(APPLE) Verify Receipt with AppleRequest",
      "example": {
        "exclude-old-transactions": false,
        "password": "",
        "receipt-data": ""
      },
      "type": "object",
      "properties": {
        "exclude-old-transactions": {
          "description": "",
          "example": false,
          "type": "boolean"
        },

Generated code is invalid because of broken identifiers for:

  • types ((fo)RegisterUserrequest),
  • parameters (x-api-key),
  • fields (exclude-old-transactions)

Certain POST configurations result in an action creator that doesn't work

Let me see how well I can explain this!

If I have a Swagger spec that contains this:

operationId: addOpportunity
x-swagger-router-controller: opportunity.controller
description: Add a new opportunity. Returns a complete representation of the added opportunity.
tags:
  - opportunity
security: 
  - auth0:
    - openid
parameters:
  - name: opportunity
    description: Properties to add to the opportunity
    in: body
    required: true
    schema:
      $ref: "./Opportunity.yaml"
responses:
  "200":
    description: Success
    schema:
      allOf:
      - $ref: '../shared/newObject.yaml'
      - $ref: './Opportunity.yaml'
  default:
    description: Error
    schema:
      $ref: "../shared/errorResponse.yaml"

I get:

export function addOpportunity(opportunity, info) {
  return dispatch => {
    dispatch({ type: ADD_OPPORTUNITY_START, meta: { info } })
    return opportunity.addOpportunity(opportunity)
      .then(response => dispatch({
        type: ADD_OPPORTUNITY,
        payload: response.data,
        error: response.error,
        meta: {
          res: response.raw,
          info
        }
      }))
  }
}

As you can see, this isn't going to work: return opportunity.addOpportunity(opportunity). It's because the parameter passed to addOpportunity (opportunity, in this case) is also the same as the named import at the top of the file:

import * as opportunity from '../opportunity'

... and badness ensues. It's because my tags and params both have a thing called opportunity. I can work around this by changing the name of my param, but it's not unreasonable to name the parameter for a 'new opportunity' as opportunity.

Is this a quick/obvious fix for you? I'm happy to look into it, but I don't currently understand the guts of your templating system, so it might take me a while to get up to speed.

Only expose interfaces and types

I don't have a particular need for the additional interfaces provided by this library (i.e. ServiceRequest). I'm mostly interested in the typescript interface generation. Is there a way that logic can be split up?

parameter name with '.'

I stumbled over a parameter 'fooNr.' which translates to

    query: {
      'Foo-Nr.': options.Foo-Nr.,
    },

Make fetch'es 'credentials option' avail.

In my setup, I need to use "credentials = true" as option to fetch, in order to work with cookie based auth. Its as easy as change the makeRequest method, but it would be fine to be able to "pass it in", not change the gateway/index.js "by hand"

Custom authorization header

In some case the Authorization header is not appropriate and we must use different request header to send the auth token.
For example if the site use Basic auth (to protect index page) and also oauth for user authentication. The Basic auth token is stored by the browser and it will send with every request automatically (it is normal), but In some browsers (Safari, IE and Edge) the app can not change the Authorization header value because the browser overwrite it with the basic auth token.

https://stackoverflow.com/a/42558891

redux action params are not converted to camelCase

First off all, thanks for this great project! I'm really happy to get all the API stuff and even the redux actions generated :-)

I found a small bug within the generated redux action code.

Our API consumes a user token via http header - we call it 'X-token'.
This is a mandatory param for our action.

The code generation will output something like this:

export function createUser(XToken, options, info) {
  return dispatch => {
    dispatch({ type: CREATE_USER_START, meta: { info } })
    return user.createUser(X-token, options)
      .then(response => dispatch({
        type: CREATE_USER,
        payload: response.data,
        error: response.error,
        meta: {
          res: response.raw,
          info
        }
      }))
  }
}

The param in the exported function will be correctly transformed from 'X-token' to XToken, but within the return statement the param name gets not transformed in this way. This will obviously throw an error.

I created a pull request #37 for that.

Properties are not type checked for propLines constant

in gentTypes.ts:

264:1 const propLines = Object.keys(def.properties).map(prop => {

def.properties can be undefined if there are no properties for a given object/stub.

which will throw an exception:

TypeError: Cannot convert undefined or null to object at Function.keys (<anonymous>) at renderTypeDoc (....\node_modules\openapi-client\dist\gen\js\genTypes.js:259:28)

json schema entry for repro

"myStubObject": { "title": "My Stub Object", "type": "object" },

Usage with Angular2

Hello.
Could you tell me how I can use it with Angular2+? Is openapi-client support third version of the openapi?

Optional `date` parameter throws error

If you specify an a parameter which is optional and of format: date or date-time and then you do not pass this parameter when invoking the operation, it is still passed through the date formatting function which throws an error Cannot read property 'toISOString' of undefined

Example spec:

swagger: "2.0"
info:
  title: Sample API
  version: 1.0.0

paths:
  /something:
    get:
      parameters:
        - name: date
          description: optional date string
          in: query
          type: string
          format: date
      responses:
        '200':
          description: ''
          schema:
            type: object

produces:

// default.ts
/**
 * @param {object} options Optional options
 * @param {date} [options.date] optional date string
 * @return {Promise<object>} 
 */
export function getSomething(options?: GetSomethingOptions): Promise<api.Response<any>> {
  if (!options) options = {}
  const parameters: api.OperationParamGroups = {
    query: {
      date: gateway.formatDate(options.date, 'date')
    }
  }
  return gateway.request(getSomethingOperation, parameters)
}

then the following causes the error

import { getSomething } from './out/default'
getSomething({})

as gateway.formatDate expects a Date object:

export function formatDate(date: Date, format: 'date'|'date-time'): string {
	const str = date.toISOString();
	return (format === 'date') ? str.split('T')[0] : str;
}

Problems with dash (-) in url

Hello,

When I'm using a dash it doesn't seem to work.
I'll try to fix this & submit PR.

// Authorization.js
export function postV1UserReset-password() {
  return gateway.request(postV1UserReset-passwordOperation)
}

const postV1UserReset-passwordOperation = {
  path: '/v1/user/reset-password',
  method: 'post'
}


// action/Authorization.js
export const POST_V1_USER_RESET-PASSWORD_START = 's/Authorization/POST_V1_USER_RESET-PASSWORD_START'
export const POST_V1_USER_RESET-PASSWORD = 's/Authorization/POST_V1_USER_RESET-PASSWORD'


export function postV1UserReset-password(info) {
  return dispatch => {
    dispatch({ type: POST_V1_USER_RESET-PASSWORD_START, meta: { info } })
    return Authorization.postV1UserReset-password()
      .then(response => dispatch({
        type: POST_V1_USER_RESET-PASSWORD,
        payload: response.data,
        error: response.error,
        meta: {
          res: response.raw,
          info
        }
      }))
  }
}

Customizing the output of redux actions

Hey there! I've been investigating a number of different ways to integrate swagger with our React+Redux app and this library is looking pretty good. Looking at some of the alternative generators like https://github.com/swagger-api/swagger-codegen and https://www.npmjs.com/package/swagger-js-codegen, they seem to offer some form of template extensibility through Mustache templates. I'm wondering if that's something you considered for this design and if not, how can I control what is generated especially for the redux actions side of things?

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.