Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function generateApi(
e: nunjucks.Environment,
specFileName: string,
root: string
): Promise {
const api: Spec = await SwaggerParser.bundle(`api/${specFileName}.yaml`);
const specCode = `
/* tslint:disable:object-literal-sort-keys */
/* tslint:disable:no-duplicate-string */
// DO NOT EDIT
// auto-generated by generated_model.ts from ${specFileName}.yaml
export const specs = ${JSON.stringify(api)};
`;
await fs.writeFile(
`${root}/${specFileName}.ts`,
prettier.format(specCode, {
parser: "typescript"
})
);
async.eachSeries(files, function parseSwagger(file, swagCallback) {
var handlerName = path.basename(file); //use the swagger filename as our handler module id
handlerName = handlerName.substring(0, handlerName.lastIndexOf('.')); //strip extensions
var derefPromise = parser.validate(file);
parser.bundle(file)
.then(function (bundledApi) {
specs.bundled[handlerName] = bundledApi;
return derefPromise;
})
.then(function (dereferencedApi) {
specs.dereferenced[handlerName] = dereferencedApi;
if (polymorphicValidation !== 'off') {
preparePathsForPolymorphicValidation(dereferencedApi.paths, responseModelValidationLevel);
}
prepareDefinitionsForPolymorphicValidation(dereferencedApi.definitions);
specs.names.push(handlerName);
return swagCallback();
})
.catch(function (error) {
// don't generate an error if it was a non-Swagger Spec JSON file
var swagErr = error;
run(config = {}) {
let configFormatters = (config.formatters ? config.formatters : DEFAULT_RUN_CONFIG.formatters);
let configMiddleware = (config.middleware ? config.middleware : DEFAULT_RUN_CONFIG.middleware);
// apply any registered formatters
this._formatters = configure(this._formatters, configFormatters);
this._formatters.forEach(({ formatName, callback }) => jsf.format(formatName, callback));
return SwaggerParser.bundle(this._pathToFile)
.then(api => {
this._middleware = configure(this._middleware, configMiddleware);
let modifiedApi = Object.assign({}, api);
this._middleware.forEach(func => modifiedApi = func(modifiedApi));
return modifiedApi;
})
.then(api => SwaggerParser.dereference(api))
.then(api => this._parsedFile = api)
.catch((err) => { throw new Error(`Error has occured when trying to bundle and dereference the OpenAPI / Swagger object. \n Error: ${err}`); });
}
export function build(swaggerSchema: string | Swagger, config: BuildOptions = {}): Promise {
const { formatters, middleware } = config;
const configurationF = (formatters ? formatters : DEFAULT_CONFIG_FORMATTER);
const configurationM = (middleware ? middleware : DEFAULT_CONFIG_MIDDLEWARE);
// create a registered array of formatters based on the configuration
const _formatters = configure(CORE_FORMATTERS, configurationF);
// apply any registered formatters
_formatters.forEach(({ formatName, callback }) => jsf.format(formatName, callback));
return SwaggerParser.bundle(swaggerSchema)
.then((api: Swagger) => {
// create a registered array of middleware based on the configuration
const _middleware = configure(CORE_MIDDLEWARE, configurationM);
let modifiedApi = Object.assign({}, api);
//apply any registered middleware
_middleware.forEach(m => modifiedApi = m(modifiedApi));
return modifiedApi;
})
.then((api: Swagger) => SwaggerParser.dereference(api))
.catch((err: Error) => {
throw new Error(`Error has occured when trying to bundle and dereference the OpenAPI / Swagger object. \n Error: ${err}`);
});
}
export async function generateTypesForDocument(definition: Document | string) {
const api = new OpenAPIClientAxios({ definition });
await api.init();
const processor = new WriteProcessor({ indentSize: 2, indentChar: ' ' });
const resolver = new ReferenceResolver();
const convertor = new SchemaConvertor(processor);
const rootSchema = await SwaggerParser.bundle(definition);
resolver.registerSchema(parseSchema(rootSchema));
const generator = new DtsGenerator(resolver, convertor);
const schemaTypes = await generator.generate();
const exportedTypes = convertor.getExports();
const operationTypings = generateOperationMethodTypings(api, exportedTypes);
const imports = [
'import {',
' OpenAPIClient,',
' Parameters,',
' UnknownParamsObject,',
' OperationResponse,',
' AxiosRequestConfig,',
`} from 'openapi-client-axios';`,
].join('\n');
_bundle (schema){
return SwaggerParser.bundle(schema, this.options);
}
validate(): Promise {
return bundle(this.toJSON())
}