- Added new way to generate schemas

This commit is contained in:
Ferdi Koomen 2019-12-22 20:25:12 +01:00
parent b110b22921
commit 9a1fa39ada
8 changed files with 404 additions and 281 deletions

View File

@ -1,6 +1,6 @@
{
"name": "openapi-typescript-codegen",
"version": "0.1.6",
"version": "0.1.7",
"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",

View File

@ -1,50 +0,0 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
import { Dictionary } from '../models/Dictionary';
export type FieldDefinition = {
readonly type?: string;
readonly isReadOnly?: boolean;
readonly isRequired?: boolean;
readonly isNullable?: boolean;
readonly format?: string;
readonly maximum?: number;
readonly exclusiveMaximum?: boolean;
readonly minimum?: number;
readonly exclusiveMinimum?: boolean;
readonly multipleOf?: number;
readonly maxLength?: number;
readonly minLength?: number;
readonly pattern?: string;
readonly maxItems?: number;
readonly minItems?: number;
readonly uniqueItems?: boolean;
readonly maxProperties?: number;
readonly minProperties?: number;
}
type ArrayDefinition<T> = FieldDefinition & {
readonly item: Definition<T>;
}
type DictionaryDefinition<T> = FieldDefinition & {
readonly item: Definition<T>;
}
type ObjectDefinition<T> = FieldDefinition & {
readonly [K in keyof T]: Definition<T[K]>;
}
export type Definition<T> =
T extends string ? FieldDefinition :
T extends number ? FieldDefinition :
T extends boolean ? FieldDefinition :
T extends File ? FieldDefinition :
T extends Blob ? FieldDefinition :
T extends Array<infer U> ? ArrayDefinition<U> :
T extends Dictionary<infer U> ? DictionaryDefinition<U> :
T extends Object ? ObjectDefinition<T> :
FieldDefinition

View File

@ -0,0 +1,40 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
export type FieldSchema = {
readonly type?: string;
readonly isReadOnly?: boolean;
readonly isRequired?: boolean;
readonly isNullable?: boolean;
readonly format?: string;
readonly maximum?: number;
readonly exclusiveMaximum?: boolean;
readonly minimum?: number;
readonly exclusiveMinimum?: boolean;
readonly multipleOf?: number;
readonly maxLength?: number;
readonly minLength?: number;
readonly pattern?: string;
readonly maxItems?: number;
readonly minItems?: number;
readonly uniqueItems?: boolean;
readonly maxProperties?: number;
readonly minProperties?: number;
}
export type ObjectSchema<T> = FieldSchema & {
properties?: {
readonly [K in keyof T]: Schema<T[K]>;
}
}
export type Schema<T> =
T extends string ? FieldSchema :
T extends number ? FieldSchema :
T extends boolean ? FieldSchema :
T extends File ? FieldSchema :
T extends Blob ? FieldSchema :
T extends Object ? ObjectSchema<T> :
FieldSchema

View File

@ -9,7 +9,7 @@ import * as schemas from '../schemas';
* Get a schema object for a given model name.
* @param model The model name to return the schema from.
*/
export function getSchema<K extends keyof typeof schemas>(model: K) {
export function getSchema<K extends keyof typeof schemas, T>(model: K) {
if (schemas.hasOwnProperty(model)) {
return schemas[model];
}

View File

@ -7,5 +7,6 @@ export { ApiError } from './core/ApiError';
export { getSchema } from './core/getSchema';
export { isSuccess } from './core/isSuccess';
export { OpenAPI } from './core/OpenAPI';
export { FieldSchema, Schema, ObjectSchema } from './core/Schema';
export * from './models/';
export * from './services/';

View File

@ -8,5 +8,7 @@
import { ${{{this}}} } from './${{{this}}}';
{{/each}}
{{/if}}
import { Schema as __Schema } from '../core/Schema';
import { {{{name}}} } from '../models/{{{name}}}';
export const ${{{name}}} = {{>schema}};
export const ${{{name}}}: __Schema<{{{name}}}> = {{>schema}};

View File

@ -9,7 +9,7 @@ import { {{{this}}} } from '../models/{{{this}}}';
{{/each}}
{{/if}}
import { ApiError, catchGenericError } from '../core/ApiError';
import { request as $request } from '../core/request';
import { request as __request } from '../core/request';
import { OpenAPI } from '../core/OpenAPI';
export class {{{name}}} {
@ -41,7 +41,7 @@ export class {{{name}}} {
{{/each}}
{{/if}}): Promise<{{>result}}> {
const result = await $request({
const result = await __request({
method: '{{{method}}}',
path: `{{{path}}}`,
{{#if parametersCookie~}}

File diff suppressed because it is too large Load Diff