- Added schema generation

This commit is contained in:
Ferdi Koomen 2019-12-16 10:35:28 +01:00
parent 5193884511
commit 68ec47f367
23 changed files with 610 additions and 242 deletions

View File

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

@ -49,4 +49,7 @@ export enum ContentType {
APPLICATION_JSON = 'application/json',
TEXT_JSON = 'text/json',
TEXT_PAIN = 'text/plain',
MULTIPART_MIXED = 'multipart/mixed',
MULTIPART_RELATED = 'multipart/related',
MULTIPART_BATCH = 'multipart/batch',
}

View File

@ -18,5 +18,14 @@ export function getContent(openApi: OpenApi, content: Dictionary<OpenApiMediaTyp
) || (
content[ContentType.TEXT_PAIN] &&
content[ContentType.TEXT_PAIN].schema
) || (
content[ContentType.MULTIPART_MIXED] &&
content[ContentType.MULTIPART_MIXED].schema
) || (
content[ContentType.MULTIPART_RELATED] &&
content[ContentType.MULTIPART_RELATED].schema
) || (
content[ContentType.MULTIPART_BATCH] &&
content[ContentType.MULTIPART_BATCH].schema
) || null;
}

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */
@ -6,7 +8,10 @@ export let Dictionary;
(function (Dictionary) {
Dictionary.schema = {
type: 'Dictionary'
type: 'Dictionary',
item: {
type: 'any'
}
};
})(Dictionary || (Dictionary = {}));

View File

@ -1,3 +1,5 @@
'use strict';
/* istanbul ignore file */
/* eslint-disable */
/* prettier-ignore */

View File

@ -30,18 +30,21 @@ type ArraySchema<T> = FieldSchema & {
readonly item: Schema<T>;
}
type DictionarySchema<T> = FieldSchema & {
readonly item: Schema<T>;
}
type ObjectSchema<T> = FieldSchema & {
readonly [K in keyof T]: Schema<T[K]>;
}
export type Schema<T = any> =
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 Array<infer U> ? ArraySchema<U> :
// T extends Dictionary<infer U> ? ArraySchema<U> :
// Check if infer type!
T extends Dictionary<infer U> ? DictionarySchema<U> :
T extends Object ? ObjectSchema<T> :
FieldSchema

View File

@ -6,7 +6,7 @@
export { ApiError } from './core/ApiError';
export { isSuccess } from './core/isSuccess';
export { OpenAPI } from './core/OpenAPI';
import { FieldSchema, Schema } from './core/Schema';
import { Schema } from './core/Schema';
{{#if models}}
{{#each models}}
@ -27,13 +27,13 @@ export { {{{this}}} } from './services/{{{this}}}';
{{/if}}
{{#if models}}
const schemas: Schema = {
const schemas = {
{{#each models}}
'{{{this}}}': {{{this}}}.schema,
{{/each}}
};
export function getSchema<T>(schema: string): Schema<T> | null {
export function getSchema<K extends keyof typeof schemas>(schema: K) {
if (schemas.hasOwnProperty(schema)) {
return schemas[schema];
}

View File

@ -6,13 +6,22 @@
import { Schema } from '../core/Schema';
export interface Dictionary<T> {
/**
* @internal
*/
readonly __type: T,
[key: string]: T;
}
export namespace Dictionary {
export const schema: Schema<Dictionary> = {
type: 'Dictionary'
export const schema: Schema<Dictionary<any>> = {
type: 'Dictionary',
item: {
type: 'any'
}
};
}

View File

@ -59,7 +59,7 @@ describe('getModelNames', () => {
properties: [],
});
expect(getModelNames([])).toEqual([]);
expect(getModelNames(models)).toEqual(['Doe', 'Jane', 'John']);
expect(getModelNames([])).toEqual(['Dictionary']);
expect(getModelNames(models)).toEqual(['Dictionary', 'Doe', 'Jane', 'John']);
});
});

View File

@ -1,10 +1,9 @@
import { Language } from '../index';
import { Model } from '../client/interfaces/Model';
export function getModelNames(models: Model[], language: Language): string[] {
export function getModelNames(models: Model[]): string[] {
return models
.map(model => model.name)
.concat(language === Language.TYPESCRIPT ? ['Dictionary'] : [])
.concat('Dictionary')
.sort((a, b) => {
const nameA = a.toLowerCase();
const nameB = b.toLowerCase();

View File

@ -24,7 +24,7 @@ export function writeClientIndex(client: Client, language: Language, templates:
templates.index({
server: client.server,
version: client.version,
models: getModelNames(client.models, language),
models: getModelNames(client.models),
services: getServiceNames(client.services),
})
);

File diff suppressed because it is too large Load Diff

View File

@ -552,6 +552,43 @@
}
}
}
},
"/api/v{api-version}/multipart": {
"get": {
"tags": [
"multipart"
],
"operationId": "MultipartResponse",
"responses": {
"200": {
"description": "OK",
"content": {
"multipart/mixed": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
},
"metadata": {
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
},
"components": {