Coder Social home page Coder Social logo

mikestead / openapi-client Goto Github PK

View Code? Open in Web Editor NEW
93.0 93.0 34.0 107 KB

Generate ES6 or Typescript service integration code from an OpenAPI 2 spec

License: MIT License

TypeScript 100.00%
code-generation javascript openapi swagger typescript

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

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?

parameter name with '.'

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

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

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

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>.

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
        }
      }))
  }
}

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" },

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;
}

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!

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"
    }
  }
}

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?

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?

Usage with Angular2

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

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.

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!

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"

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.

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.

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)

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 ?

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.