mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Updated and removed schema
This commit is contained in:
parent
0f98a13b0e
commit
22c98ef426
@ -10,8 +10,8 @@ program
|
||||
.version(pkg.version)
|
||||
.option('--input [value]', 'Path to swagger specification', './spec.json')
|
||||
.option('--output [value]', 'Output directory', './generated')
|
||||
.option('--language', 'Language to generate [typescript, javascript]', 'typescript')
|
||||
.option('--http-client', 'HTTP client to generate [fetch, xhr]', 'fetch')
|
||||
.option('--language [value]', 'Language to generate [typescript, javascript]', 'typescript')
|
||||
.option('--http-client [value]', 'HTTP client to generate [fetch, xhr]', 'fetch')
|
||||
.parse(process.argv);
|
||||
|
||||
const SwaggerCodegen = require(path.resolve(__dirname, '../dist/index.js'));
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "openapi-typescript-codegen",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.5",
|
||||
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
|
||||
"author": "Ferdi Koomen",
|
||||
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
|
||||
@ -70,7 +70,6 @@
|
||||
"@types/mkdirp": "0.5.2",
|
||||
"@types/node": "12.12.14",
|
||||
"@types/rimraf": "2.0.3",
|
||||
"@types/yup": "0.26.26",
|
||||
"@typescript-eslint/eslint-plugin": "2.10.0",
|
||||
"@typescript-eslint/parser": "2.10.0",
|
||||
"camelcase": "5.3.1",
|
||||
@ -89,7 +88,6 @@
|
||||
"path": "0.12.7",
|
||||
"prettier": "1.19.1",
|
||||
"rimraf": "3.0.0",
|
||||
"typescript": "3.7.2",
|
||||
"yup": "0.27.0"
|
||||
"typescript": "3.7.2"
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ export { OpenAPI } from './core/OpenAPI';
|
||||
{{#if models}}
|
||||
|
||||
{{#each models}}
|
||||
{{#notEquals this 'Dictionary'}}export { {{{this}}} } from './models/{{{this}}}';{{/notEquals}}
|
||||
export { {{{this}}} } from './models/{{{this}}}';
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
{{#if services}}
|
||||
|
||||
@ -4,15 +4,12 @@
|
||||
|
||||
{{#if imports}}
|
||||
{{#each imports}}
|
||||
{{#notEquals this 'Dictionary'}}import { {{{this}}} } from '../models/{{{this}}}';{{/notEquals}}
|
||||
import { {{{this}}} } from '../models/{{{this}}}';
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
import * as yup from 'yup';
|
||||
|
||||
{{#equals export 'interface'}}
|
||||
{{>exportInterface}}
|
||||
{{>exportEnums}}
|
||||
{{else equals export 'enum'}}
|
||||
{{>exportEnum}}
|
||||
{{else}}
|
||||
{{>exportGeneric}}
|
||||
{{/equals}}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
{{#if default}} = {{{default}}}{{/if}}
|
||||
@ -10,14 +10,4 @@ export let {{{name}}};
|
||||
{{{../name}}}.{{{name}}} = {{{value}}};
|
||||
{{/each}}
|
||||
|
||||
{{{name}}}.schema = {{>validation}};
|
||||
|
||||
{{{name}}}.validate = async function(value) {
|
||||
return {{{name}}}.schema.validate(value, { strict: true });
|
||||
};
|
||||
|
||||
{{{name}}}.validateSync = function(value) {
|
||||
return {{{name}}}.schema.validateSync(value, { strict: true });
|
||||
};
|
||||
|
||||
})({{{name}}} || ({{{name}}} = {}));
|
||||
|
||||
@ -19,14 +19,4 @@ export let {{{name}}};
|
||||
};
|
||||
|
||||
{{/each}}
|
||||
{{{name}}}.schema = {{>validation parent=name}};
|
||||
|
||||
{{{name}}}.validate = async function(value) {
|
||||
return {{{name}}}.schema.validate(value, { strict: true });
|
||||
};
|
||||
|
||||
{{{name}}}.validateSync = function(value) {
|
||||
return {{{name}}}.schema.validateSync(value, { strict: true });
|
||||
};
|
||||
|
||||
})({{{name}}} || ({{{name}}} = {}));
|
||||
@ -1,19 +0,0 @@
|
||||
{{#if description}}
|
||||
/**
|
||||
* {{{description}}}
|
||||
*/
|
||||
{{/if}}
|
||||
export let {{{name}}};
|
||||
(function ({{{name}}}) {
|
||||
|
||||
{{{name}}}.schema = {{>validation}};
|
||||
|
||||
{{{name}}}.validate = async function(value) {
|
||||
return {{{name}}}.schema.validate(value, { strict: true });
|
||||
};
|
||||
|
||||
{{{name}}}.validateSync = function(value) {
|
||||
return {{{name}}}.schema.validateSync(value, { strict: true });
|
||||
};
|
||||
|
||||
})({{{name}}} || ({{{name}}} = {}));
|
||||
@ -1,13 +0,0 @@
|
||||
{{#equals export 'interface'}}
|
||||
{{>validationInterface}}
|
||||
{{else equals export 'reference'}}
|
||||
{{>validationReference}}
|
||||
{{else equals export 'enum'}}
|
||||
{{>validationEnum}}
|
||||
{{else equals export 'array'}}
|
||||
{{>validationArray}}
|
||||
{{else equals export 'dictionary'}}
|
||||
{{>validationDictionary}}
|
||||
{{else}}
|
||||
{{>validationGeneric}}
|
||||
{{/equals}}
|
||||
@ -1,5 +0,0 @@
|
||||
{{~#if link~}}
|
||||
yup.array().of({{>validation link}}){{>validationBasics}}
|
||||
{{~else~}}
|
||||
yup.array().of({{{base}}}.schema){{>validationBasics}}
|
||||
{{~/if~}}
|
||||
@ -1 +0,0 @@
|
||||
{{#if isNullable}}.nullable(){{/if}}{{#if isRequired}}.required(){{/if}}
|
||||
@ -1,19 +0,0 @@
|
||||
{{~#if link~}}
|
||||
yup.lazy(value => {
|
||||
return yup.object().shape(
|
||||
Object.keys(value).reduce((object, key) => ({
|
||||
...object,
|
||||
[key]: {{>validation link}}
|
||||
}), {})
|
||||
).noUnknown();
|
||||
})
|
||||
{{~else~}}
|
||||
yup.lazy(value => {
|
||||
return yup.object().shape(
|
||||
Object.keys(value).reduce((key, item) => ({
|
||||
...object,
|
||||
[key]: {{{base}}}.schema
|
||||
}), {})
|
||||
).noUnknown();
|
||||
})
|
||||
{{~/if~}}
|
||||
@ -1,13 +0,0 @@
|
||||
{{~#equals parent name~}}
|
||||
yup.mixed().oneOf([
|
||||
{{#each enum}}
|
||||
{{{../parent}}}.{{{name}}}{{>comma}}
|
||||
{{/each}}
|
||||
]){{>validationBasics}}
|
||||
{{~else~}}
|
||||
yup.mixed().oneOf([
|
||||
{{#each enum}}
|
||||
{{{value}}}{{>comma}}
|
||||
{{/each}}
|
||||
]){{>validationBasics}}
|
||||
{{~/equals~}}
|
||||
@ -1,9 +0,0 @@
|
||||
{{~#equals type 'boolean'~}}
|
||||
yup.boolean(){{>validationBasics}}
|
||||
{{~else equals type 'number'~}}
|
||||
yup.number(){{>validationBasics}}
|
||||
{{~else equals type 'string'~}}
|
||||
yup.string(){{>validationBasics}}
|
||||
{{~else~}}
|
||||
yup.mixed(){{>validationBasics}}
|
||||
{{~/equals~}}
|
||||
@ -1,25 +0,0 @@
|
||||
(
|
||||
{{#if extends}}
|
||||
{{#each extends}}
|
||||
{{{this}}}.schema.concat(
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
{{#if properties}}
|
||||
yup.object().shape({
|
||||
{{#each properties}}
|
||||
{{#equals ../parent name}}
|
||||
{{{name}}}: {{>validation parent=name}}{{>comma}}
|
||||
{{else}}
|
||||
{{{name}}}: {{>validation}}{{>comma}}
|
||||
{{/equals}}
|
||||
{{/each}}
|
||||
}).noUnknown()
|
||||
{{else}}
|
||||
yup.object()
|
||||
{{/if}}
|
||||
{{#if extends}}
|
||||
{{#each extends}}
|
||||
)
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
){{>validationBasics}}
|
||||
@ -1 +0,0 @@
|
||||
{{{base}}}.schema{{>validationBasics}}
|
||||
@ -3,7 +3,7 @@
|
||||
/* prettier-ignore */
|
||||
|
||||
import { ApiError, catchGenericError } from '../core/ApiError';
|
||||
import { request } from '../core/request';
|
||||
import { request as $request } from '../core/request';
|
||||
import { OpenAPI } from '../core/OpenAPI';
|
||||
|
||||
export class {{{name}}} {
|
||||
@ -31,11 +31,11 @@ export class {{{name}}} {
|
||||
*/
|
||||
static async {{{name}}}({{#if parameters}}
|
||||
{{#each parameters}}
|
||||
{{{name}}}{{>defaultValue}}{{>comma}}
|
||||
{{{name}}}{{#if default}} = {{{default}}}{{/if}}{{>comma}}
|
||||
{{/each}}
|
||||
{{/if}}) {
|
||||
|
||||
const result = await request({
|
||||
const result = await $request({
|
||||
method: '{{{method}}}',
|
||||
path: `{{{path}}}`{{#if parametersCookie}},
|
||||
cookies: {
|
||||
|
||||
@ -2,13 +2,12 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
/* prettier-ignore */
|
||||
|
||||
{{#if imports}}
|
||||
|
||||
{{#each imports}}
|
||||
import { {{{this}}} } from '../models/{{{this}}}';
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
import * as yup from 'yup';
|
||||
|
||||
{{#equals export 'interface'}}
|
||||
{{>exportInterface}}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
{{#if default}} = {{{default}}}{{/if}}
|
||||
@ -8,16 +8,3 @@ export enum {{{name}}} {
|
||||
{{{name}}} = {{{value}}}{{>comma}}
|
||||
{{/each}}
|
||||
}
|
||||
|
||||
export namespace {{{name}}} {
|
||||
|
||||
export const schema = {{>validation parent=name}};
|
||||
|
||||
export async function validate(value: any): Promise<{{{name}}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: any): {{{name}}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,16 +4,3 @@
|
||||
*/
|
||||
{{/if}}
|
||||
export type {{{name}}} = {{>type}};
|
||||
|
||||
export namespace {{{name}}} {
|
||||
|
||||
export const schema = {{>validation}};
|
||||
|
||||
export async function validate(value: any): Promise<{{{name}}}{{>isNullable}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: any): {{{name}}}{{>isNullable}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,13 +29,4 @@ export namespace {{{name}}} {
|
||||
}
|
||||
|
||||
{{/each}}
|
||||
export const schema: yup.ObjectSchema<{{{name}}}> = {{>validation parent=name}};
|
||||
|
||||
export async function validate(value: any): Promise<{{{name}}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: any): {{{name}}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
{{#equals export 'interface'}}
|
||||
{{>validationInterface}}
|
||||
{{else equals export 'reference'}}
|
||||
{{>validationReference}}
|
||||
{{else equals export 'enum'}}
|
||||
{{>validationEnum}}
|
||||
{{else equals export 'array'}}
|
||||
{{>validationArray}}
|
||||
{{else equals export 'dictionary'}}
|
||||
{{>validationDictionary}}
|
||||
{{else}}
|
||||
{{>validationGeneric}}
|
||||
{{/equals}}
|
||||
@ -1,6 +0,0 @@
|
||||
{{~#if link~}}
|
||||
yup.array<{{>type link}}>().of({{>validation link}})
|
||||
{{~else~}}
|
||||
yup.array<{{{type}}}{{>isNullable}}>().of({{{base}}}.schema)
|
||||
{{~/if~}}
|
||||
{{>validationBasics}}
|
||||
@ -1 +0,0 @@
|
||||
{{#if isNullable}}.nullable(){{/if}}{{#if isRequired}}.required(){{/if}}
|
||||
@ -1,19 +0,0 @@
|
||||
{{~#if link~}}
|
||||
yup.lazy<Dictionary<{{>type link}}>>(value => {
|
||||
return yup.object<Dictionary<{{>type link}}>>().shape(
|
||||
Object.keys(value).reduce((object, key) => ({
|
||||
...object,
|
||||
[key]: {{>validation link}}
|
||||
}), {})
|
||||
).noUnknown();
|
||||
})
|
||||
{{~else~}}
|
||||
yup.lazy<Dictionary<{{{type}}}{{>isNullable}}>>(value => {
|
||||
return yup.object<Dictionary<{{{type}}}{{>isNullable}}>>().shape(
|
||||
Object.keys(value).reduce((object, key) => ({
|
||||
...object,
|
||||
[key]: {{{base}}}.schema
|
||||
}), {})
|
||||
).noUnknown();
|
||||
})
|
||||
{{~/if~}}
|
||||
@ -1,14 +0,0 @@
|
||||
{{~#equals parent name~}}
|
||||
yup.mixed<{{{name}}}>().oneOf([
|
||||
{{#each enum}}
|
||||
{{{../parent}}}.{{{name}}}{{>comma}}
|
||||
{{/each}}
|
||||
]){{>validationBasics}}
|
||||
{{~else~}}
|
||||
yup.mixed<{{{type}}}{{>isNullable}}>().oneOf([
|
||||
{{#each enum}}
|
||||
{{{value}}}{{>comma}}
|
||||
{{/each}}
|
||||
]){{>validationBasics}}
|
||||
{{~/equals~}}
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
{{~#equals type 'boolean'~}}
|
||||
yup.boolean(){{>validationBasics}}
|
||||
{{~else equals type 'number'~}}
|
||||
yup.number(){{>validationBasics}}
|
||||
{{~else equals type 'string'~}}
|
||||
yup.string(){{>validationBasics}}
|
||||
{{~else~}}
|
||||
yup.mixed<{{{type}}}{{>isNullable}}>(){{>validationBasics}}
|
||||
{{~/equals~}}
|
||||
@ -1,25 +0,0 @@
|
||||
(
|
||||
{{#if extends}}
|
||||
{{#each extends}}
|
||||
{{{this}}}.schema.concat(
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
{{#if properties}}
|
||||
yup.object().shape({
|
||||
{{#each properties}}
|
||||
{{#equals ../parent name}}
|
||||
{{{name}}}: {{>validation parent=name}}{{>comma}}
|
||||
{{else}}
|
||||
{{{name}}}: {{>validation}}{{>comma}}
|
||||
{{/equals}}
|
||||
{{/each}}
|
||||
}).noUnknown()
|
||||
{{else}}
|
||||
yup.object<any>()
|
||||
{{/if}}
|
||||
{{#if extends}}
|
||||
{{#each extends}}
|
||||
)
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
){{>validationBasics}}
|
||||
@ -1 +0,0 @@
|
||||
{{{base}}}.schema{{>validationBasics}}
|
||||
@ -9,7 +9,7 @@ import { {{{this}}} } from '../models/{{{this}}}';
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
import { ApiError, catchGenericError } from '../core/ApiError';
|
||||
import { request } from '../core/request';
|
||||
import { request as $request } from '../core/request';
|
||||
import { OpenAPI } from '../core/OpenAPI';
|
||||
|
||||
export class {{{name}}} {
|
||||
@ -37,11 +37,11 @@ export class {{{name}}} {
|
||||
*/
|
||||
public static async {{{name}}}({{#if parameters}}
|
||||
{{#each parameters}}
|
||||
{{{name}}}{{>isRequired}}: {{>type}}{{>defaultValue}}{{>comma}}
|
||||
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}}{{>comma}}
|
||||
{{/each}}
|
||||
{{/if}}): Promise<{{>result}}> {
|
||||
|
||||
const result = await request({
|
||||
const result = await $request({
|
||||
method: '{{{method}}}',
|
||||
path: `{{{path}}}`{{#if parametersCookie}},
|
||||
cookies: {
|
||||
|
||||
13
src/utils/getImports.ts
Normal file
13
src/utils/getImports.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import * as glob from 'glob';
|
||||
import * as path from 'path';
|
||||
|
||||
export function getImports(folder: string): string[] {
|
||||
const files = glob.sync('**/*.{ts,js}', { cwd: folder });
|
||||
return files
|
||||
.map(file => path.basename(file))
|
||||
.sort((a, b) => {
|
||||
const nameA = a.toLowerCase();
|
||||
const nameB = b.toLowerCase();
|
||||
return nameA.localeCompare(nameB, 'en');
|
||||
});
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
import { getModelNames } from './getModelNames';
|
||||
|
||||
describe('getModelNames', () => {
|
||||
it('should return sorted list', () => {
|
||||
const models = new Map<string, Model>();
|
||||
models.set('John', {
|
||||
export: 'interface',
|
||||
name: 'John',
|
||||
type: 'John',
|
||||
base: 'John',
|
||||
template: null,
|
||||
link: null,
|
||||
description: null,
|
||||
isProperty: false,
|
||||
isReadOnly: false,
|
||||
isRequired: false,
|
||||
isNullable: false,
|
||||
imports: [],
|
||||
extends: [],
|
||||
enum: [],
|
||||
enums: [],
|
||||
properties: [],
|
||||
});
|
||||
models.set('Jane', {
|
||||
export: 'interface',
|
||||
name: 'Jane',
|
||||
type: 'Jane',
|
||||
base: 'Jane',
|
||||
template: null,
|
||||
link: null,
|
||||
description: null,
|
||||
isProperty: false,
|
||||
isReadOnly: false,
|
||||
isRequired: false,
|
||||
isNullable: false,
|
||||
imports: [],
|
||||
extends: [],
|
||||
enum: [],
|
||||
enums: [],
|
||||
properties: [],
|
||||
});
|
||||
models.set('Doe', {
|
||||
export: 'interface',
|
||||
name: 'Doe',
|
||||
type: 'Doe',
|
||||
base: 'Doe',
|
||||
template: null,
|
||||
link: null,
|
||||
description: null,
|
||||
isProperty: false,
|
||||
isReadOnly: false,
|
||||
isRequired: false,
|
||||
isNullable: false,
|
||||
imports: [],
|
||||
extends: [],
|
||||
enum: [],
|
||||
enums: [],
|
||||
properties: [],
|
||||
});
|
||||
|
||||
expect(getModelNames(new Map<string, Model>())).toEqual(['Dictionary']);
|
||||
expect(getModelNames(models)).toEqual(['Dictionary', 'Doe', 'Jane', 'John']);
|
||||
});
|
||||
});
|
||||
@ -1,12 +0,0 @@
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
|
||||
export function getModelNames(models: Map<string, Model>): string[] {
|
||||
return Array.from(models.values())
|
||||
.map(model => model.name)
|
||||
.concat(['Dictionary'])
|
||||
.sort((a, b) => {
|
||||
const nameA = a.toLowerCase();
|
||||
const nameB = b.toLowerCase();
|
||||
return nameA.localeCompare(nameB, 'en');
|
||||
});
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
import { Service } from '../client/interfaces/Service';
|
||||
import { getServiceNames } from './getServiceNames';
|
||||
|
||||
describe('getServiceNames', () => {
|
||||
it('should return sorted list', () => {
|
||||
const services = new Map<string, Service>();
|
||||
services.set('John', {
|
||||
name: 'John',
|
||||
operations: [],
|
||||
imports: [],
|
||||
});
|
||||
services.set('Jane', {
|
||||
name: 'Jane',
|
||||
operations: [],
|
||||
imports: [],
|
||||
});
|
||||
services.set('Doe', {
|
||||
name: 'Doe',
|
||||
operations: [],
|
||||
imports: [],
|
||||
});
|
||||
|
||||
expect(getServiceNames(new Map<string, Service>())).toEqual([]);
|
||||
expect(getServiceNames(services)).toEqual(['Doe', 'Jane', 'John']);
|
||||
});
|
||||
});
|
||||
@ -1,11 +0,0 @@
|
||||
import { Service } from '../client/interfaces/Service';
|
||||
|
||||
export function getServiceNames(services: Map<string, Service>): string[] {
|
||||
return Array.from(services.values())
|
||||
.map(service => service.name)
|
||||
.sort((a, b) => {
|
||||
const nameA = a.toLowerCase();
|
||||
const nameB = b.toLowerCase();
|
||||
return nameA.localeCompare(nameB, 'en');
|
||||
});
|
||||
}
|
||||
12
src/utils/shouldExportModel.ts
Normal file
12
src/utils/shouldExportModel.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Language } from '../index';
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
|
||||
export function shouldExportModel(model: Model, language: Language): boolean {
|
||||
if (language === Language.JAVASCRIPT) {
|
||||
const hasEnum = model.enum.length > 0;
|
||||
const hasEnums = model.enums.length > 0;
|
||||
return hasEnum || hasEnums;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -53,10 +53,10 @@ export function writeClient(client: Client, language: Language, httpClient: Http
|
||||
|
||||
// Write the client files
|
||||
try {
|
||||
writeClientIndex(client, language, templates, outputPath);
|
||||
writeClientSettings(client, language, httpClient, templates, outputPathCore);
|
||||
writeClientModels(client.models, language, templates, outputPathModels);
|
||||
writeClientServices(client.services, language, templates, outputPathServices);
|
||||
writeClientSettings(client, language, httpClient, templates, outputPathCore);
|
||||
writeClientIndex(client, language, templates, outputPathModels, outputPathServices, outputPath);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import * as fs from 'fs';
|
||||
import * as glob from 'glob';
|
||||
import { Client } from '../client/interfaces/Client';
|
||||
import { Language } from '../index';
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
@ -7,8 +8,10 @@ import { Templates } from './readHandlebarsTemplates';
|
||||
import { writeClientIndex } from './writeClientIndex';
|
||||
|
||||
jest.mock('fs');
|
||||
jest.mock('glob');
|
||||
|
||||
const fsWriteFileSync = fs.writeFileSync as jest.MockedFunction<typeof fs.writeFileSync>;
|
||||
const globSync = glob.sync as jest.MockedFunction<typeof glob.sync>;
|
||||
|
||||
describe('writeClientIndex', () => {
|
||||
it('should write to filesystem', () => {
|
||||
@ -18,13 +21,18 @@ describe('writeClientIndex', () => {
|
||||
models: new Map<string, Model>(),
|
||||
services: new Map<string, Service>(),
|
||||
};
|
||||
|
||||
const templates: Templates = {
|
||||
index: () => 'dummy',
|
||||
model: () => 'dummy',
|
||||
service: () => 'dummy',
|
||||
settings: () => 'dummy',
|
||||
};
|
||||
writeClientIndex(client, Language.TYPESCRIPT, templates, '/');
|
||||
|
||||
globSync.mockReturnValue([]);
|
||||
|
||||
writeClientIndex(client, Language.TYPESCRIPT, templates, '/', '/', '/');
|
||||
|
||||
expect(fsWriteFileSync).toBeCalledWith('/index.ts', 'dummy');
|
||||
});
|
||||
});
|
||||
|
||||
@ -4,8 +4,7 @@ import { Client } from '../client/interfaces/Client';
|
||||
import { Language } from '../index';
|
||||
import { Templates } from './readHandlebarsTemplates';
|
||||
import { getFileName } from './getFileName';
|
||||
import { getModelNames } from './getModelNames';
|
||||
import { getServiceNames } from './getServiceNames';
|
||||
import { getImports } from './getImports';
|
||||
|
||||
/**
|
||||
* Generate the OpenAPI client index file using the Handlebar template and write it to disk.
|
||||
@ -14,22 +13,23 @@ import { getServiceNames } from './getServiceNames';
|
||||
* @param client Client object, containing, models, schemas and services.
|
||||
* @param language The output language (Typescript or javascript).
|
||||
* @param templates The loaded handlebar templates.
|
||||
* @param outputPathModels
|
||||
* @param outputPathServices
|
||||
* @param outputPath
|
||||
*/
|
||||
export function writeClientIndex(client: Client, language: Language, templates: Templates, outputPath: string): void {
|
||||
export function writeClientIndex(client: Client, language: Language, templates: Templates, outputPathModels: string, outputPathServices: string, outputPath: string): void {
|
||||
const fileName = getFileName('index', language);
|
||||
// try {
|
||||
console.log(fileName);
|
||||
fs.writeFileSync(
|
||||
path.resolve(outputPath, fileName),
|
||||
templates.index({
|
||||
server: client.server,
|
||||
version: client.version,
|
||||
models: getModelNames(client.models),
|
||||
services: getServiceNames(client.services),
|
||||
})
|
||||
);
|
||||
// } catch (e) {
|
||||
// throw new Error(`Could not write index: "${fileName}"`);
|
||||
// }
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.resolve(outputPath, fileName),
|
||||
templates.index({
|
||||
server: client.server,
|
||||
version: client.version,
|
||||
models: getImports(outputPathModels),
|
||||
services: getImports(outputPathServices),
|
||||
})
|
||||
);
|
||||
} catch (e) {
|
||||
throw new Error(`Could not write index: "${fileName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,13 +29,16 @@ describe('writeClientModels', () => {
|
||||
enums: [],
|
||||
properties: [],
|
||||
});
|
||||
|
||||
const templates: Templates = {
|
||||
index: () => 'dummy',
|
||||
model: () => 'dummy',
|
||||
service: () => 'dummy',
|
||||
settings: () => 'dummy',
|
||||
};
|
||||
|
||||
writeClientModels(models, Language.TYPESCRIPT, templates, '/');
|
||||
|
||||
expect(fsWriteFileSync).toBeCalledWith('/Item.ts', 'dummy');
|
||||
});
|
||||
});
|
||||
|
||||
@ -6,6 +6,7 @@ import { Templates } from './readHandlebarsTemplates';
|
||||
import { exportModel } from './exportModel';
|
||||
import { format } from './format';
|
||||
import { getFileName } from './getFileName';
|
||||
import { shouldExportModel } from './shouldExportModel';
|
||||
|
||||
/**
|
||||
* Generate Models using the Handlebar template and write to disk.
|
||||
@ -16,14 +17,15 @@ import { getFileName } from './getFileName';
|
||||
*/
|
||||
export function writeClientModels(models: Map<string, Model>, language: Language, templates: Templates, outputPath: string): void {
|
||||
models.forEach(model => {
|
||||
const fileName = getFileName(model.name, language);
|
||||
// try {
|
||||
console.log(fileName);
|
||||
const templateData = exportModel(model);
|
||||
const templateResult = templates.model(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
// } catch (e) {
|
||||
// throw new Error(`Could not write model: "${fileName}"`);
|
||||
// }
|
||||
if (shouldExportModel(model, language)) {
|
||||
const fileName = getFileName(model.name, language);
|
||||
try {
|
||||
const templateData = exportModel(model);
|
||||
const templateResult = templates.model(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
} catch (e) {
|
||||
throw new Error(`Could not write model: "${fileName}"`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -16,13 +16,16 @@ describe('writeClientServices', () => {
|
||||
operations: [],
|
||||
imports: [],
|
||||
});
|
||||
|
||||
const templates: Templates = {
|
||||
index: () => 'dummy',
|
||||
model: () => 'dummy',
|
||||
service: () => 'dummy',
|
||||
settings: () => 'dummy',
|
||||
};
|
||||
|
||||
writeClientServices(services, Language.TYPESCRIPT, templates, '/');
|
||||
|
||||
expect(fsWriteFileSync).toBeCalledWith('/Item.ts', 'dummy');
|
||||
});
|
||||
});
|
||||
|
||||
@ -17,13 +17,12 @@ import { getFileName } from './getFileName';
|
||||
export function writeClientServices(services: Map<string, Service>, language: Language, templates: Templates, outputPath: string): void {
|
||||
services.forEach(service => {
|
||||
const fileName = getFileName(service.name, language);
|
||||
// try {
|
||||
console.log(fileName);
|
||||
const templateData = exportService(service);
|
||||
const templateResult = templates.service(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
// } catch (e) {
|
||||
// throw new Error(`Could not write service: "${fileName}"`);
|
||||
// }
|
||||
try {
|
||||
const templateData = exportService(service);
|
||||
const templateResult = templates.service(templateData);
|
||||
fs.writeFileSync(path.resolve(outputPath, fileName), format(templateResult));
|
||||
} catch (e) {
|
||||
throw new Error(`Could not write service: "${fileName}"`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
51
yarn.lock
51
yarn.lock
@ -653,13 +653,6 @@
|
||||
"@babel/helper-plugin-utils" "^7.0.0"
|
||||
"@babel/plugin-transform-typescript" "^7.7.4"
|
||||
|
||||
"@babel/runtime@^7.0.0":
|
||||
version "7.7.4"
|
||||
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.4.tgz#b23a856751e4bf099262f867767889c0e3fe175b"
|
||||
integrity sha512-r24eVUUr0QqNZa+qrImUk8fn5SPhHq+IfYvIoIMg0do3GdK9sMdiLKP3GYVVaxpPKORgm8KRKaNTEhAjgIpLMw==
|
||||
dependencies:
|
||||
regenerator-runtime "^0.13.2"
|
||||
|
||||
"@babel/template@^7.4.0", "@babel/template@^7.7.4":
|
||||
version "7.7.4"
|
||||
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.7.4.tgz#428a7d9eecffe27deac0a98e23bf8e3675d2a77b"
|
||||
@ -985,11 +978,6 @@
|
||||
dependencies:
|
||||
"@types/yargs-parser" "*"
|
||||
|
||||
"@types/yup@0.26.26":
|
||||
version "0.26.26"
|
||||
resolved "https://registry.yarnpkg.com/@types/yup/-/yup-0.26.26.tgz#2e7065384ac2b7711271d8790f26ac7d73f6a33d"
|
||||
integrity sha512-5cLJLd8NIT7OfJLi7ScquRn/NWfmewBqJ92nT/FYfdpgKzyUNcR4n2BKEOQ7mOG8WuJXgomIvNl5P3sn9Akd4A==
|
||||
|
||||
"@typescript-eslint/eslint-plugin@2.10.0":
|
||||
version "2.10.0"
|
||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.10.0.tgz#c4cb103275e555e8a7e9b3d14c5951eb6d431e70"
|
||||
@ -2091,11 +2079,6 @@ flatted@^2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08"
|
||||
integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==
|
||||
|
||||
fn-name@~2.0.1:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/fn-name/-/fn-name-2.0.1.tgz#5214d7537a4d06a4a301c0cc262feb84188002e7"
|
||||
integrity sha1-UhTXU3pNBqSjAcDMJi/rhBiAAuc=
|
||||
|
||||
for-in@^1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
|
||||
@ -3208,7 +3191,7 @@ lodash.unescape@4.0.1:
|
||||
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
|
||||
integrity sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw=
|
||||
|
||||
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15:
|
||||
lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15:
|
||||
version "4.17.15"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
|
||||
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
|
||||
@ -3813,11 +3796,6 @@ prompts@^2.0.1:
|
||||
kleur "^3.0.3"
|
||||
sisteransi "^1.0.3"
|
||||
|
||||
property-expr@^1.5.0:
|
||||
version "1.5.1"
|
||||
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-1.5.1.tgz#22e8706894a0c8e28d58735804f6ba3a3673314f"
|
||||
integrity sha512-CGuc0VUTGthpJXL36ydB6jnbyOf/rAHFvmVrJlH+Rg0DqqLFQGAP6hIaxD/G0OAmBJPhXDHuEJigrp0e0wFV6g==
|
||||
|
||||
psl@^1.1.24, psl@^1.1.28:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/psl/-/psl-1.5.0.tgz#47fd1292def7fdb1e138cd78afa8814cebcf7b13"
|
||||
@ -3910,11 +3888,6 @@ regenerate@^1.4.0:
|
||||
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
|
||||
integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==
|
||||
|
||||
regenerator-runtime@^0.13.2:
|
||||
version "0.13.3"
|
||||
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
|
||||
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
|
||||
|
||||
regenerator-transform@^0.14.0:
|
||||
version "0.14.1"
|
||||
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"
|
||||
@ -4501,11 +4474,6 @@ symbol-tree@^3.2.2:
|
||||
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
|
||||
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==
|
||||
|
||||
synchronous-promise@^2.0.6:
|
||||
version "2.0.10"
|
||||
resolved "https://registry.yarnpkg.com/synchronous-promise/-/synchronous-promise-2.0.10.tgz#e64c6fd3afd25f423963353043f4a68ebd397fd8"
|
||||
integrity sha512-6PC+JRGmNjiG3kJ56ZMNWDPL8hjyghF5cMXIFOKg+NiwwEZZIvxTWd0pinWKyD227odg9ygF8xVhhz7gb8Uq7A==
|
||||
|
||||
table@^5.2.3:
|
||||
version "5.4.6"
|
||||
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
|
||||
@ -4596,11 +4564,6 @@ to-regex@^3.0.1, to-regex@^3.0.2:
|
||||
regex-not "^1.0.2"
|
||||
safe-regex "^1.1.0"
|
||||
|
||||
toposort@^2.0.2:
|
||||
version "2.0.2"
|
||||
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
|
||||
integrity sha1-riF2gXXRVZ1IvvNUILL0li8JwzA=
|
||||
|
||||
tough-cookie@^2.3.3, tough-cookie@^2.3.4:
|
||||
version "2.5.0"
|
||||
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
|
||||
@ -4931,15 +4894,3 @@ yargs@^13.3.0:
|
||||
which-module "^2.0.0"
|
||||
y18n "^4.0.0"
|
||||
yargs-parser "^13.1.1"
|
||||
|
||||
yup@0.27.0:
|
||||
version "0.27.0"
|
||||
resolved "https://registry.yarnpkg.com/yup/-/yup-0.27.0.tgz#f8cb198c8e7dd2124beddc2457571329096b06e7"
|
||||
integrity sha512-v1yFnE4+u9za42gG/b/081E7uNW9mUj3qtkmelLbW5YPROZzSH/KUUyJu9Wt8vxFJcT9otL/eZopS0YK1L5yPQ==
|
||||
dependencies:
|
||||
"@babel/runtime" "^7.0.0"
|
||||
fn-name "~2.0.1"
|
||||
lodash "^4.17.11"
|
||||
property-expr "^1.5.0"
|
||||
synchronous-promise "^2.0.6"
|
||||
toposort "^2.0.2"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user