tools/oas-configurable-proxy/index.js
/**
 * Generate Proxy Config from OAS
 *
 * @param {Object} config OAS generator config
 */
Generate Proxy Config from OAS
@param {Object} config OAS generator config
Generate Proxy Config from OAS @param { Object } config OAS generator config
async function generateFromOAS(config) {
    try {
        const api = await SwaggerParser.validate(config.oasPath);
        const oasVersionString = api.openapi || api.swagger;
        const oasVersionSplit = oasVersionString.split(".");
        const oasVersionMajor = parseInt(oasVersionSplit[0], 10);
        const proxyName = config.name || api.info.title.replace(/ /g, '-').toLowerCase();
        const basepath = config.basepath || `/${proxyName}/v1`
        let targetService = 'https://notfound.apigee.google.com # TODO replace me'
        if (oasVersionMajor === 2 && api.host) {
            const schema = Array.isArray(api.schemes) && api.schemes.includes('https') ? 'https' : 'http';
            const basePath = api.basePath || ''
            targetService = `${schema}://${api.host}${basePath}`
        } else if (oasVersionMajor === 3 && api.servers) {
            targetService = `${api.servers[0].url}`
        }
        const operations = [];
        for (const [requestPath, pathObject] of Object.entries(api.paths || {})) {
            for (const [method, pathItem] of Object.entries(pathObject || {})) {
                operations.push({
                    id: `${method}-${pathItem.operationId.replace(/ /g, '-').toLowerCase()}`,
                    httpMethod: method.toUpperCase(),
                    requestPath: requestPath,
                    consumerAuthorization: translateOasSecurity(pathItem.security, api.components)
                })
            }
        }
        if (oasVersionMajor === 3) {
            consumerAuthorization = translateOasSecurity(api.security, api.components)
        }
        data = {
            basepath,
            operations,
            targetService,
            consumerAuthorization
        }
        const template = handlebars.compile(fs.readFileSync('./proxy.yaml.handlebars').toString());
        const generatedProxyConfig = template(data)
        if (config.outFolder) {
            const proxyFolder = path.join(outFolder, 'src', 'main', 'apigee', 'apiproxies', proxyName);
            createFolderIfNotExists(proxyFolder);
            fs.writeFileSync(path.join(proxyFolder, 'proxy.yaml'), generatedProxyConfig);
            const envs = (config.envs || '').split(',')
            envs.forEach(env => {
                const envFolder = path.join(outFolder, 'src', 'main', 'apigee', 'environments', env);
                createFolderIfNotExists(envFolder);
                const envDeployments = path.join(envFolder, "deployments.json");
                let deployments = { proxies: [] }
                if (fs.existsSync(envDeployments)) {
                    deployments = JSON.parse(fs.readFileSync(envDeployments))
                }
                const uniqueProxies = new Set(deployments.proxies);
                uniqueProxies.add(proxyName);
                deployments.proxies = [...uniqueProxies];
                fs.writeFileSync(envDeployments, JSON.stringify(deployments, null, 2));
            });
        } else {
            console.log(generatedProxyConfig);
        }
    }
    catch (err) {
        console.error(err);
    }
}
async function generateFromOAS ( config ) { try { const api = await SwaggerParser . validate ( config . oasPath ) ; const oasVersionString = api . openapi || api . swagger ; const oasVersionSplit = oasVersionString . split ( "." ) ; const oasVersionMajor = parseInt ( oasVersionSplit [ 0 ] , 10 ) ; const proxyName = config . name || api . info . title . replace ( / / g , '-' ) . toLowerCase ( ) ; const basepath = config . basepath || ` ${ proxyName } ` let targetService = 'https://notfound.apigee.google.com # TODO replace me' if ( oasVersionMajor === 2 && api . host ) { const schema = Array . isArray ( api . schemes ) && api . schemes . includes ( 'https' ) ? 'https' : 'http' ; const basePath = api . basePath || '' targetService = ` ${ schema } ${ api . host } ${ basePath } ` } else if ( oasVersionMajor === 3 && api . servers ) { targetService = ` ${ api . servers [ 0 ] . url } ` } const operations = [ ] ; for ( const [ requestPath , pathObject ] of Object . entries ( api . paths || { } ) ) { for ( const [ method , pathItem ] of Object . entries ( pathObject || { } ) ) { operations . push ( { id : ` ${ method } ${ pathItem . operationId . replace ( / / g , '-' ) . toLowerCase ( ) } ` , httpMethod : method . toUpperCase ( ) , requestPath : requestPath , consumerAuthorization : translateOasSecurity ( pathItem . security , api . components ) } ) } } if ( oasVersionMajor === 3 ) { consumerAuthorization = translateOasSecurity ( api . security , api . components ) } data = { basepath , operations , targetService , consumerAuthorization } const template = handlebars . compile ( fs . readFileSync ( './proxy.yaml.handlebars' ) . toString ( ) ) ; const generatedProxyConfig = template ( data ) if ( config . outFolder ) { const proxyFolder = path . join ( outFolder , 'src' , 'main' , 'apigee' , 'apiproxies' , proxyName ) ; createFolderIfNotExists ( proxyFolder ) ; fs . writeFileSync ( path . join ( proxyFolder , 'proxy.yaml' ) , generatedProxyConfig ) ; const envs = ( config . envs || '' ) . split ( ',' ) envs . forEach ( env => { const envFolder = path . join ( outFolder , 'src' , 'main' , 'apigee' , 'environments' , env ) ; createFolderIfNotExists ( envFolder ) ; const envDeployments = path . join ( envFolder , "deployments.json" ) ; let deployments = { proxies : [ ] } if ( fs . existsSync ( envDeployments ) ) { deployments = JSON . parse ( fs . readFileSync ( envDeployments ) ) } const uniqueProxies = new Set ( deployments . proxies ) ; uniqueProxies . add ( proxyName ) ; deployments . proxies = [ ... uniqueProxies ] ; fs . writeFileSync ( envDeployments , JSON . stringify ( deployments , null , 2 ) ) ; } ) ; } else { console . log ( generatedProxyConfig ) ; } } catch ( err ) { console . error ( err ) ; } }
Generate Proxy Config from OAS
 @param {Object} config OAS generator config
Generate Proxy Config from OAS @param { Object } config OAS generator config
// target service // consumer auth // Print to file or stdout