-
Notifications
You must be signed in to change notification settings - Fork 7
/
spec.ts
202 lines (187 loc) · 8.49 KB
/
spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import utils from '../../commons/utils/utils';
import config from '../../configs/config';
import { Schema, Table, RAMLApplicationInfo } from '../../commons/types';
/**
* Class which holds the logic for generating raml spec.
*
* @export
* @class SpecGenerator
*/
export class SpecGenerator {
/**
* Generates the api spec for the whole schema.
*
* @param {Schema} schema - entire collection of tables
* @param {RAMLApplicationInfo} options - holds the title of the app, the version and the base url
* @return {string} - returns the api spec
*/
public generateContent(schema: Schema, options: RAMLApplicationInfo): string {
let spec: string = this.addHeaderContent(options) + this.addDataTypes(schema);
schema.forEach((entity: Table) => {
spec += this.addSpecForEntity(entity);
});
return spec;
}
/**
* Generates the header content
*
* @param {RAMLApplicationInfo} options - holds the title of the app, the version and the base url
* @return {string} returns the formatted header
*/
public addHeaderContent(options: RAMLApplicationInfo): string {
return utils.formatLine('', config.TABS.LEVEL_0, '#%RAML 1.0') +
utils.formatLine('', config.TABS.LEVEL_0, `title: ${options.name}`) +
utils.formatLine('', config.TABS.LEVEL_0, `version: ${options.version}`) +
utils.formatLine('', config.TABS.LEVEL_0, `baseUri: ${options.url}{version}`) +
utils.formatLine('', config.TABS.LEVEL_0, 'protocols: [HTTP, HTTPS]');
}
/**
* Adds the data types based on schema
*
* @param {Schema} schema - entire collection of tables
* @return {string} returns the formatted data types
*/
public addDataTypes(schema: Schema): string {
let types: string = `types:${config.END_OF_LINE}`;
schema.forEach((entity: Table) => {
types += utils.formatLine(
config.DEFAULT_INDENTATION,
0,
`${utils.toTitleCase(entity.name)}: !include types/${utils.toTitleCase(entity.name)}.raml`,
);
});
return types;
}
/**
* Generates the get all spec
*
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted spec for the entity
*/
public addSpecForEntity(entity: Table): string {
let spec: string = `/${entity.name}:${config.END_OF_LINE}`;
spec += this.addGetAllSpec(config.DEFAULT_INDENTATION, entity);
spec += this.addCreateSpec(config.DEFAULT_INDENTATION, entity);
spec += utils.formatLine(config.DEFAULT_INDENTATION, config.TABS.LEVEL_0, `/{${utils.singular(entity.name)}Id}:`);
spec += this.addGetOneSpec(config.DEFAULT_INDENTATION, entity);
spec += this.addUpdateSpec(config.DEFAULT_INDENTATION, entity);
spec += this.addDeleteSpec(config.DEFAULT_INDENTATION, entity);
return spec;
}
/**
* Generates the get all spec
*
* @param {string} initialIndentation - holds the initial indentation
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted get all spec
*/
public addGetAllSpec(initialIndentation: string, entity: Table): string {
return utils.formatLine(initialIndentation, config.TABS.LEVEL_0, 'get:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_1, `description: Get a list of all ${entity.name}`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'responses:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, '200:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_4, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_5, `type: ${utils.toTitleCase(entity.name)}[]`) +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_5,
`example: !include examples/${utils.pluralize(utils.toTitleCase(entity.name))}.json`,
);
}
/**
* Generates the create spec
*
* @param {string} initialIndentation - holds the initial indentation
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted create spec
*/
public addCreateSpec(initialIndentation: string, entity: Table): string {
return utils.formatLine(initialIndentation, config.TABS.LEVEL_0, 'post:') +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_1,
`description: Create a ${utils.singular(entity.name)}`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, `type: ${utils.toTitleCase(entity.name)}`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'responses:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, '201:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_4, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_5, `type: ${utils.toTitleCase(entity.name)}`) +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_5,
`example: !include examples/${utils.toTitleCase(entity.name)}.json`,
);
}
/**
* Generates the get one spec
*
* @param {string} initialIndentation - holds the initial indentation
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted get one spec
*/
public addGetOneSpec(initialIndentation: string, entity: Table): string {
return utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'get:') +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_2,
`description: Get an instance of ${utils.singular(entity.name)} based on it's id.`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, 'responses:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, '200:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_4, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_5, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_6, `type: ${utils.toTitleCase(entity.name)}`) +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_6,
`example: !include examples/${utils.toTitleCase(entity.name)}.json`,
);
}
/**
* Generates the update spec
*
* @param {string} initialIndentation - holds the initial indentation
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted update spec
*/
public addUpdateSpec(initialIndentation: string, entity: Table): string {
return utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'put:') +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_2,
`description: Update an instance of ${utils.singular(entity.name)}.`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_4, `type: ${utils.toTitleCase(entity.name)}`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, 'responses:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, '200:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_4, 'body:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_5, 'application/json:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_6, `type: ${utils.toTitleCase(entity.name)}`) +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_6,
`example: !include examples/${utils.toTitleCase(entity.name)}.json`,
);
}
/**
* Generates the delete spec
*
* @param {string} initialIndentation - holds the initial indentation
* @param {Table} entity - holds the entity definition
* @return {string} - returns the formatted delete spec
*/
public addDeleteSpec(initialIndentation: string, entity: Table): string {
return utils.formatLine(initialIndentation, config.TABS.LEVEL_1, 'delete:') +
utils.formatLine(
initialIndentation,
config.TABS.LEVEL_2,
`description: Delete an instance of ${utils.singular(entity.name)} based on it's id.`) +
utils.formatLine(initialIndentation, config.TABS.LEVEL_2, 'responses:') +
utils.formatLine(initialIndentation, config.TABS.LEVEL_3, '204:');
}
}
export default new SpecGenerator();