UNPKG

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