Skip to content

Commit

Permalink
fix: schemas for api docs and json model for version 3.0.3 were fixed (
Browse files Browse the repository at this point in the history
  • Loading branch information
harvic3 committed Jul 9, 2023
1 parent eb3c772 commit 19b9fc6
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 120 deletions.
113 changes: 71 additions & 42 deletions openapi.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"openapi": "3.1.0",
"title": "NodeTSkeleton API",
"version": "1.0.0",
"description": "Api documentation for NodeTSkeleton project",
"contact": {
"name": "TSK Support",
"url": "https://github.com/harvic3/nodetskeleton",
"email": "harvic3@protonmail.com"
},
"license": {
"name": "MIT",
"identifier": "MIT"
"openapi": "3.0.3",
"info": {
"title": "NodeTSkeleton API",
"version": "1.0.0",
"description": "Api documentation for NodeTSkeleton project",
"contact": {
"name": "TSK Support",
"url": "https://github.com/harvic3/nodetskeleton",
"email": "harvic3@protonmail.com"
},
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "localhost:3003",
"url": "localhost:3003/api",
"description": "Local server"
}
],
Expand All @@ -28,7 +29,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ResultT<TokenDto>"
"$ref": "#/components/schemas/ResultTTokenDto"
}
}
}
Expand All @@ -38,7 +39,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ResultT<TokenDto>"
"$ref": "#/components/schemas/ResultTTokenDto"
}
}
}
Expand All @@ -53,8 +54,7 @@
}
}
}
},
"parameters": []
}
}
},
"/ping": {
Expand All @@ -71,9 +71,7 @@
}
}
}
},
"requestBody": {},
"parameters": []
}
}
},
"/v1/users/sign-up": {
Expand Down Expand Up @@ -110,40 +108,71 @@
}
}
}
},
"requestBody": {},
"parameters": []
}
}
}
},
"components": {
"schemas": {
"TokenDto": {
"token": "string",
"expiresIn": "number"
"type": "object",
"properties": {
"token": {
"type": "string"
},
"expiresIn": {
"type": "number"
}
}
},
"ResultT<TokenDto>": {
"message": "string",
"statusCode": "string",
"error": "string",
"success": "boolean",
"data": {
"$ref": "#/components/schemas/TokenDto"
"ResultTTokenDto": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"statusCode": {
"type": "string"
},
"error": {
"type": "string"
},
"success": {
"type": "boolean"
},
"data": {
"$ref": "#/components/schemas/TokenDto"
}
}
},
"Credentials": {
"email": "string",
"passwordB64": "string"
},
"string": {
"primitive": "string"
"type": "object",
"properties": {
"email": {
"type": "string"
},
"passwordB64": {
"type": "string"
}
}
},
"Result": {
"message": "string",
"statusCode": "string",
"error": "string",
"success": "boolean"
"type": "object",
"properties": {
"message": {
"type": "string"
},
"statusCode": {
"type": "string"
},
"error": {
"type": "string"
},
"success": {
"type": "boolean"
}
}
}
}
}
}
}
10 changes: 9 additions & 1 deletion src/adapters/controllers/base/context/apiDoc/SchemasStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { PropTypeEnum } from "./TypeDescriber";

export class SchemasStore {
static #store: { schemas: Record<string, any> } = { schemas: {} };

static add(key: string, schema: any): void {
static add(
key: string,
schema: {
type: PropTypeEnum;
properties: any;
},
): void {
this.#store.schemas[key] = schema;
}

Expand Down
106 changes: 71 additions & 35 deletions src/adapters/controllers/base/context/apiDoc/TypeDescriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export enum PropFormatEnum {
PASSWORD = "password",
}

type ClassProperty = {
export type ClassProperty = {
type: PropTypeEnum;
format?: PropFormatEnum;
nullable?: boolean;
readonly?: boolean;
required?: boolean;
Expand Down Expand Up @@ -63,7 +64,11 @@ export class ResultDescriber {
readonly: true,
},
};
readonly schema: { name: string; definition: Record<keyof ResultWrapper, PropTypeEnum> };
readonly schema: {
name: string;
type: PropTypeEnum;
properties: Record<keyof ResultWrapper, ClassProperty>;
};

constructor(obj: {
type: PropTypeEnum.OBJECT;
Expand All @@ -76,14 +81,26 @@ export class ResultDescriber {
if (obj.props?.success) this.properties.success = obj.props.success;
this.schema = {
name: "Result",
definition: {
message: PropTypeEnum.STRING,
statusCode: PropTypeEnum.STRING,
error: PropTypeEnum.STRING,
success: PropTypeEnum.BOOLEAN,
type: PropTypeEnum.OBJECT,
properties: {
message: {
type: PropTypeEnum.STRING,
},
statusCode: {
type: PropTypeEnum.STRING,
},
error: {
type: PropTypeEnum.STRING,
},
success: {
type: PropTypeEnum.BOOLEAN,
},
},
};
SchemasStore.add(this.schema.name, this.schema.definition);
SchemasStore.add(this.schema.name, {
type: this.schema.type,
properties: this.schema.properties,
});
}
}

Expand Down Expand Up @@ -117,7 +134,8 @@ export class ResultTDescriber<T> {
};
readonly schema: {
name: string;
definition: Record<keyof ResultWrapper, PropTypeEnum> & {
type: PropTypeEnum;
properties: Record<keyof ResultWrapper, ClassProperty> & {
data:
| { $ref: string }
| { type: PropTypeEnum.OBJECT | PropTypeEnum.ARRAY; items: { $ref: string } };
Expand All @@ -139,19 +157,34 @@ export class ResultTDescriber<T> {
if (obj.props?.success) this.properties.success = obj.props.success;
const reference = "#/components/schemas/" + obj.props.data.schema.name;
this.schema = {
name: `ResultT<${this.name}>`,
definition: {
message: PropTypeEnum.STRING,
statusCode: PropTypeEnum.STRING,
error: PropTypeEnum.STRING,
success: PropTypeEnum.BOOLEAN,
name:
obj.props.data.type === PropTypeEnum.ARRAY
? `ResultT${this.name}Array`
: `ResultT${this.name}`,
type: PropTypeEnum.OBJECT,
properties: {
message: {
type: PropTypeEnum.STRING,
},
statusCode: {
type: PropTypeEnum.STRING,
},
error: {
type: PropTypeEnum.STRING,
},
success: {
type: PropTypeEnum.BOOLEAN,
},
data:
obj.props.data.type === PropTypeEnum.ARRAY
? { type: PropTypeEnum.ARRAY, items: { $ref: reference } }
: { $ref: reference },
},
};
SchemasStore.add(this.schema.name, this.schema.definition);
SchemasStore.add(this.schema.name, {
type: this.schema.type,
properties: this.schema.properties,
});
}
}

Expand All @@ -161,12 +194,16 @@ type Primitive =
| PropTypeEnum.BOOLEAN
| PropTypeEnum.NULL
| PropTypeEnum.UNDEFINED;
type PrimitiveDefinition = { primitive: Primitive };
type PrimitiveDefinition = { primitive: Primitive; format?: PropFormatEnum };

export class TypeDescriber<T> {
readonly type: PropTypeEnum.OBJECT | PropTypeEnum.ARRAY | PropTypeEnum.PRIMITIVE;
readonly properties: Record<keyof T, ClassProperty | TypeDescriber<any>> | PrimitiveDefinition;
readonly schema: { name: string; definition: Record<string, string> | Record<string, string>[] };
readonly schema: {
name: string;
type: PropTypeEnum;
properties: Record<string, ClassProperty> | { type: PropTypeEnum };
};

constructor(obj: {
name: string;
Expand All @@ -181,38 +218,37 @@ export class TypeDescriber<T> {
});
this.schema = {
name: obj.name,
definition: {},
type: obj.type,
properties: {},
};

if (this.type === PropTypeEnum.PRIMITIVE) {
this.type = (this.properties as PrimitiveDefinition).primitive as any;
this.schema.definition = { primitive: (this.properties as PrimitiveDefinition).primitive };
SchemasStore.add(this.schema.name, this.schema.definition);
return;
}

if (!Object.keys(props).length) return;

const schemaType: Record<string, string> = {};
const schemaType: Record<string, ClassProperty> = {};
Object.keys(props).forEach((key) => {
if (props[key].type) {
schemaType[key] = props[key].type;
schemaType[key] = {
type: props[key].type,
format: props[key].format,
};
}
});

if (this.type === PropTypeEnum.ARRAY) {
this.schema = {
name: obj.name,
definition: [schemaType],
};
} else {
this.schema = {
name: obj.name,
definition: schemaType,
};
}
this.schema = {
name: obj.name,
type: PropTypeEnum.OBJECT,
properties: schemaType,
};

SchemasStore.add(this.schema.name, this.schema.definition);
SchemasStore.add(this.schema.name, {
type: this.schema.type,
properties: this.schema.properties,
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/app/AppWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class AppWrapper {

constructor(controllers?: BaseController[]) {
this.setup();
this.apiDocGenerator = new ApiDocGenerator(AppSettings.Environment);
this.apiDocGenerator = new ApiDocGenerator(AppSettings.Environment, config.Params.ApiDocsInfo);
this.app = AppServer();
this.app.set("trust proxy", true);
this.loadMiddleware();
Expand Down
Loading

0 comments on commit 19b9fc6

Please sign in to comment.