0.22.0 • Published 6 months ago

@cblx-br/openapi-typescript v0.22.0

Weekly downloads
119
License
MIT
Repository
github
Last release
6 months ago

openapi-typescript

npm

openapi client generator for typescript

Install

npm install @cblx-br/openapi-typescript --save-dev

create an openapi-typescript.config.js

var config = {
    url: "<url>/swagger.json",
    outputDir: "./src/client",
};

module.exports = config;

execute it:

npx openapi-typescript

Then create an implementation for your connector using the tool of your choice (fetch, jquery ajax, etc...).

export class MyAppConnector implements OpenApiConnector {
     async request(method: string, path: string, parameters: any, body: any) {
         ... implementation ...
     }
}

Use your connector with your client api service.

var myApi = new MyApiClient(new MyAppConnector());
let result = await myApi.get();

Examples: Fetch, Angular

Friendly function names

For friendly function names, set the desired name in the operantionId field of each path definition.

Enum names

This tool supports the 'x-enum-varnames' extension

Fetch connector example

import { OpenApiConnector } from "@cblx-br/openapi-typescript";

class MyAppConnector extends OpenApiConnector {
    async request(method: string, path: string, parameters: any, body: any) {
        var url = new URL(location.origin + '/' + path);
        if (parameters) {
            Object.keys(parameters).forEach(key => {
                let value = parameters[key];
                if (value === null || value === undefined) { value = ''; }
                url.searchParams.append(key, value);
            });
        }

        let response = await fetch(url.toString(), {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            method: method,
            body: body ? JSON.stringify(body) : undefined
        });

        if (response.status != 200) {
            throw await response.json();
        }

        const contentType = response.headers.get('content-type');
        if (contentType && contentType.indexOf('application/json') == 0) {
            return await response.json();
        }
    }
}

export const connector = new MyConnector();

Angular connector example

create an app-connector.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { OpenApiConnector } from '@cblx-br/openapi-typescript';

@Injectable({
    providedIn: 'root'
})
export class MyAppConnector implements OpenApiConnector {

    constructor(private http: HttpClient) {}

    async request(method: string, path: string, parameters: any, body: any) {
        return await this.http.request(method, '/' + path, {
            body: body,
            params: parameters
        }).toPromise();
    }
}

app.module.ts

@NgModule({
  declarations: [...],
  imports: [
    ...
    HttpClientModule
  ],
  providers: [
    {
      provide: OpenApiConnector,
      useClass: MyAppConnector
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

openapi-typescript.config.js

var config = {
    url: "<url>/swagger.json",
    outputDir: "./src/client",
    hooks: {
          writingClient(client, context) {
            client.importsSection.push(`import { Injectable } from '@angular/core';`);
            client.decoratorsSection.push(`@Injectable({ providedIn: 'root' })`);
        }
    }
};

module.exports = config;

Then inject your client api services wherever you need...

@Component(...)
export class MyComponent{
    constructor(myApiClient: MyApiClient){
        ...
    }
}

Advanced options

The openapi-typescript.config.js supports some experimental options. These options can be found here:

https://github.com/cblx/openapi-typescript/blob/main/tool/config.ts

0.21.7

6 months ago

0.21.6

6 months ago

0.21.5

6 months ago

0.22.0

6 months ago

0.21.4

2 years ago

0.21.3

2 years ago

0.21.2

2 years ago

0.21.1

2 years ago

0.21.0

2 years ago

0.21.0-dev.15

2 years ago

0.21.0-dev.6

2 years ago

0.21.0-dev.14

2 years ago

0.21.0-dev.12

2 years ago

0.21.0-dev.13

2 years ago

0.21.0-dev.9

2 years ago

0.21.0-dev.8

2 years ago

0.21.0-dev.7

2 years ago

0.21.0-dev.2

2 years ago

0.21.0-dev.1

2 years ago

0.21.0-dev.5

2 years ago

0.21.0-dev.4

2 years ago

0.21.0-dev.3

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago

0.20.4

2 years ago

0.20.3

2 years ago

0.20.2

2 years ago

0.20.0-dev.19

2 years ago

0.20.0-dev.18

3 years ago

0.20.0-dev.17

3 years ago

0.20.0-dev.16

3 years ago

0.20.0-dev.14

3 years ago

0.20.0-dev.13

3 years ago

0.20.0-dev.12

3 years ago

0.20.0-dev.11

3 years ago

0.20.0-dev.15

3 years ago

0.20.0-dev.9

3 years ago

0.20.0-dev.8

3 years ago

0.20.0-dev.10

3 years ago

0.20.0-dev.7

3 years ago

0.20.0-dev.6

3 years ago

0.20.0-dev.4

3 years ago

0.20.0-dev.3

3 years ago

0.20.0-dev.2

3 years ago

0.19.1

3 years ago

0.20.0-dev.1

3 years ago

0.19.0

3 years ago

0.19.0-dev.1

3 years ago

0.18.3

3 years ago

0.18.3-dev.1

3 years ago

0.18.0-dev.4

3 years ago

0.18.0-dev.3

3 years ago

0.18.0-dev.2

3 years ago

0.18.0-dev.1

3 years ago

0.17.1

3 years ago

0.18.1

3 years ago

0.18.2

3 years ago

0.18.0

3 years ago

0.17.1-dev.1

3 years ago

0.17.1-dev.2

3 years ago

0.17.0

3 years ago

0.17.0-dev.3

3 years ago

0.17.0-dev.2

3 years ago

0.17.0-dev.1

3 years ago

0.16.0

4 years ago

0.16.0-dev.9

4 years ago

0.16.0-dev.8

4 years ago

0.16.0-dev.7

4 years ago

0.16.0-dev.6

4 years ago

0.16.0-dev.5

4 years ago

0.16.0-dev.4

4 years ago

0.16.0-dev.3

4 years ago

0.16.0-dev.2

4 years ago

0.16.0-dev.1

4 years ago

0.15.1-dev

4 years ago

0.0.0-merge.0

4 years ago

0.15.0

4 years ago

0.13.0

4 years ago

0.14.0

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.4

4 years ago

0.0.0-master.6

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.0

4 years ago

0.9.1

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.5.2

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.5.1

4 years ago

0.4.2

4 years ago

0.4.0

4 years ago