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 | var transform = require('qewd-transform-json').transform;
|
32 | var traverse = require('traverse');
|
33 | var fs = require('fs');
|
34 | var headings = require('qewd-ripple/lib/headings/headings').headings;
|
35 | delete headings.transfers;
|
36 | delete headings.counts;
|
37 |
|
38 | var path = 'www/swagger/';
|
39 |
|
40 | function getFile(file) {
|
41 | file = path + file;
|
42 | var text = fs.readFileSync(file, 'utf-8');
|
43 |
|
44 | return JSON.parse(text);
|
45 | }
|
46 |
|
47 | function json() {
|
48 | return ["application/json"];
|
49 | }
|
50 |
|
51 | function auth() {
|
52 | return [{QEWDToken: []}];
|
53 | }
|
54 |
|
55 | function schema(ref) {
|
56 | console.log('schema: ref= ' + ref);
|
57 | return {
|
58 | $ref: '#/definitions/' + ref
|
59 | };
|
60 | }
|
61 |
|
62 | function headingPostSchema(heading) {
|
63 | return {
|
64 | $ref: '#/definitions/' + heading + 'Schema'
|
65 | };
|
66 | }
|
67 |
|
68 | function headingSummaryDescription(heading) {
|
69 | return 'Get ' + heading + ' summary data';
|
70 | }
|
71 |
|
72 | function headingPostDescription(heading) {
|
73 | return 'Save a new ' + heading + ' record';
|
74 | }
|
75 |
|
76 | function headingDetailDescription(heading) {
|
77 | return 'Get complete ' + heading + ' data';
|
78 | }
|
79 |
|
80 | function getPostFields(headingObj, headingName) {
|
81 |
|
82 | var templateObj = headingObj.post.transformTemplate;
|
83 | var fields = [];
|
84 | traverse(templateObj).map(function(node) {
|
85 |
|
86 | if (typeof node === 'string') {
|
87 | var pieces;
|
88 | var name;
|
89 |
|
90 | if (node.indexOf('{{') !== -1) {
|
91 | pieces = node.split('{{');
|
92 | pieces = pieces[1].split('}}');
|
93 | name = pieces[0].trim();
|
94 | fields.push(name);
|
95 | }
|
96 | if (node.indexOf('=>') !== -1) {
|
97 | pieces = node.split('(');
|
98 | pieces = pieces[1].split(',');
|
99 | name = pieces[0].trim();
|
100 | if (name !== ')') fields.push(name);
|
101 | }
|
102 | }
|
103 | });
|
104 | return fields;
|
105 | }
|
106 |
|
107 | function get(file, ...arr) {
|
108 | console.log('arr = ' + JSON.stringify(arr));
|
109 | var params = {};
|
110 | if (Array.isArray(arr)) {
|
111 | params[arr[0]] = arr[1];
|
112 | if (arr[2]) params[arr[2]] = arr[3];
|
113 | if (arr[4]) params[arr[4]] = arr[5];
|
114 | }
|
115 |
|
116 | if (params.headingDetail) {
|
117 | params.desc = params.desc + ' for ' + params.headingDetail;
|
118 | params.ref = params.headingDetail + 'Detail' + params.ref;
|
119 | }
|
120 |
|
121 | if (params.headingSummary) {
|
122 | params.desc = params.desc + ' for ' + params.headingSummary;
|
123 | params.ref = params.headingSummary + 'Summary' + params.ref;
|
124 | }
|
125 |
|
126 | console.log('get: file = ' + file + '; params: ' + JSON.stringify(params));
|
127 | var obj = getFile(file);
|
128 |
|
129 | return transform(obj, params, {get, schema, json, auth, headingSummaryDescription, headingDetailDescription, headingPostDescription, headingPostSchema});
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | var json1 = get('swaggerTemplate.json');
|
135 |
|
136 | var json2;
|
137 | var headingObj;
|
138 | var summaryFields;
|
139 | var detailFields;
|
140 | var template;
|
141 | var properties;
|
142 | var name;
|
143 |
|
144 | for (var heading in headings) {
|
145 | console.log('--- ' + heading + ' ----');
|
146 |
|
147 | json2 = get('heading-summary.json', 'headingName', heading);
|
148 | json1.paths['/api/patients/{patientId}/' + heading] = {
|
149 | get: json2
|
150 | };
|
151 |
|
152 | headingObj = require('qewd-ripple/lib/headings/' + heading);
|
153 | summaryFields = headingObj.headingTableFields;
|
154 | //console.log('template for ' + heading + ': ' + JSON.stringify(template));
|
155 | properties = {
|
156 | source: {
|
157 | description: 'Source OpenEHR System',
|
158 | type: 'string'
|
159 | },
|
160 | sourceId: {
|
161 | description: 'Source OpenEHR Composition Id',
|
162 | type: 'string'
|
163 | }
|
164 | };
|
165 | summaryFields.forEach(function(name) {
|
166 | properties[name] = {
|
167 | type: 'string'
|
168 | };
|
169 | });
|
170 |
|
171 |
|
172 | json1.definitions[heading + 'SummaryResponse'] = {
|
173 | properties: properties
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 | json2 = get('heading-detail.json', 'headingName', heading);
|
179 | json1.paths['/api/patients/{patientId}/' + heading + '/{sourceId}'] = {
|
180 | get: json2
|
181 | }
|
182 |
|
183 | detailFields = headingObj.get.transformTemplate;
|
184 | properties = {
|
185 | source: {
|
186 | description: 'Source OpenEHR System',
|
187 | type: 'string'
|
188 | },
|
189 | sourceId: {
|
190 | description: 'Source OpenEHR Composition Id',
|
191 | type: 'string'
|
192 | }
|
193 | };
|
194 | for (name in detailFields) {
|
195 | properties[name] = {
|
196 | type: 'string'
|
197 | };
|
198 | };
|
199 |
|
200 |
|
201 | json1.definitions[heading + 'DetailResponse'] = {
|
202 | properties: properties
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 | if (headingObj.post) {
|
208 |
|
209 | var fields = getPostFields(headingObj, heading);
|
210 | properties = {};
|
211 | fields.forEach(function(name) {
|
212 | properties[name] = {
|
213 | type: 'string'
|
214 | };
|
215 | });
|
216 |
|
217 | json2 = get('heading-post.json', 'headingName', heading);
|
218 | json1.paths['/api/patients/{patientId}/' + heading].post = json2;
|
219 |
|
220 | json1.definitions[heading + 'Schema'] = {
|
221 | properties: properties
|
222 | }
|
223 | }
|
224 |
|
225 | }
|
226 |
|
227 | var text = JSON.stringify(json1, null, 2) + '\n';
|
228 |
|
229 | console.log('text: ' + text);
|
230 |
|
231 | fs.writeFileSync(path + 'swagger.json', text, 'utf-8');
|
232 |
|
233 |
|
234 |
|
235 |
|