karlvr/openapi-generator-plus-express-passport

Sample implementation

magichome opened this issue · 1 comments

The template seems to create good code from my openapi yaml. My problem is integrating this code into an operational Express application. I'm sure this is due to my less than optimal experience with Typescript. Does anyone have an example where they have integrated the generated Typescript with an Express application?

I figured it out. My root index looks like this...

// Import the express in typescript file
import express from 'express'
import http from 'http';

// Generated code initialization
import serviceApi from '../dist/index';

// Service implementation code
import { ServiceImpl } from './impl/types';

// Initialize the express engine
const app = express();

// Take a port 3000 for running server.
const PORT: number = 3000;

// Service implementations
const impl = new ServiceImpl();

// call generated code default function to initialize
serviceApi(app, impl);

// Server setup
// Start server (app.listen can also be used)    
http.createServer(app).listen(PORT, () =>  
  console.log(`Service running at http://localhost:${PORT}/`));

Then my implementation looks like this...

import { ApiImplementation } from '../../dist/types';
import { TestApiImpl } from './test/types';

// Attempt to implement /test functionality
export class ServiceImpl implements ApiImplementation {
    test: TestApiImpl = new TestApiImpl;
}

The test implement looks like this...

import { GetTest200Response, GetTestResponse, TestApi } from '../../../dist/api/test/types';

export class TestApiImpl implements TestApi {
    getTest(): Promise<GetTestResponse> {
        return new Promise<GetTestResponse>((resolve, reject) => {
            const tr = <GetTest200Response>{
                status: 200,
                body: {
                    message: "hi"
                }
            }
            resolve(tr)
        });
    }
}