@@ -18,7 +18,17 @@ error:
1818 message: 'You are not allowed to access this resource'
1919 */
2020
21- import { Constraint } from '../lib/validators/constraints' ;
21+ // This is the model of our built result
22+ // It will be serialized and deserialized by class-transformer
23+ // https://github.com/typestack/class-transformer/tree/master
24+ // So we should use classes instead of interfaces.
25+
26+ import {
27+ Constraint ,
28+ ConstraintDiscriminator ,
29+ } from '../lib/validators/constraints' ;
30+ import { Type } from 'class-transformer' ;
31+ import 'reflect-metadata' ;
2232
2333// Pagination mode should always be UPPERCASE because schema parser will transform the user inputs.
2434export enum PaginationMode {
@@ -39,62 +49,69 @@ export enum FieldDataType {
3949 STRING = 'STRING' ,
4050}
4151
42- export interface ValidatorDefinition < T = any > {
43- name : string ;
44- args : T ;
52+ export class ValidatorDefinition < T = any > {
53+ name ! : string ;
54+ args ! : T ;
4555}
4656
47- export interface RequestSchema {
48- fieldName : string ;
57+ export class RequestSchema {
58+ fieldName ! : string ;
4959 // the field put in query parameter or headers
50- fieldIn : FieldInType ;
51- description : string ;
52- type : FieldDataType ;
53- validators : Array < ValidatorDefinition > ;
54- constraints : Array < Constraint > ;
60+ fieldIn ! : FieldInType ;
61+ description ! : string ;
62+ type ! : FieldDataType ;
63+ validators ! : Array < ValidatorDefinition > ;
64+ @Type ( ( ) => Constraint , {
65+ discriminator : ConstraintDiscriminator ,
66+ } )
67+ constraints ! : Array < Constraint > ;
5568}
5669
57- export interface ResponseProperty {
58- name : string ;
70+ export class ResponseProperty {
71+ name ! : string ;
5972 description ?: string ;
60- type : FieldDataType | Array < ResponseProperty > ;
73+ type ! : FieldDataType | Array < ResponseProperty > ;
6174 required ?: boolean ;
6275}
6376
64- export interface PaginationSchema {
65- mode : PaginationMode ;
77+ export class PaginationSchema {
78+ mode ! : PaginationMode ;
6679 // The key name used for do filtering by key for keyset pagination.
6780 keyName ?: string ;
6881}
6982
70- export interface ErrorInfo {
71- code : string ;
72- message : string ;
83+ export class ErrorInfo {
84+ code ! : string ;
85+ message ! : string ;
7386}
7487
75- export interface Sample {
76- profile : string ;
77- parameters : Record < string , any > ;
88+ export class Sample {
89+ profile ! : string ;
90+ parameters ! : Record < string , any > ;
7891}
7992
80- export interface APISchema {
93+ export class APISchema {
8194 // graphql operation name
82- operationName : string ;
95+ operationName ! : string ;
8396 // restful url path
84- urlPath : string ;
97+ urlPath ! : string ;
8598 // template, could be name or path
86- templateSource : string ;
87- request : Array < RequestSchema > ;
88- errors : Array < ErrorInfo > ;
89- response : Array < ResponseProperty > ;
99+ templateSource ! : string ;
100+ @Type ( ( ) => RequestSchema )
101+ request ! : Array < RequestSchema > ;
102+ @Type ( ( ) => ErrorInfo )
103+ errors ! : Array < ErrorInfo > ;
104+ @Type ( ( ) => ResponseProperty )
105+ response ! : Array < ResponseProperty > ;
90106 description ?: string ;
91107 // The pagination strategy that do paginate when querying
92108 // If not set pagination, then API request not provide the field to do it
93109 pagination ?: PaginationSchema ;
94110 sample ?: Sample ;
95- profiles : Array < string > ;
111+ profiles ! : Array < string > ;
96112}
97113
98- export interface BuiltArtifact {
99- apiSchemas : Array < APISchema > ;
114+ export class BuiltArtifact {
115+ @Type ( ( ) => APISchema )
116+ schemas ! : Array < APISchema > ;
100117}
0 commit comments