UNPKG

6.8 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Rob Tweed, M/Gateway Developments Ltd |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 8 June 2017
28
29*/
30
31var transform = require('qewd-transform-json').transform;
32var traverse = require('traverse');
33var fs = require('fs');
34var headings = require('qewd-ripple/lib/headings/headings').headings;
35delete headings.transfers;
36delete headings.counts;
37
38var path = 'www/swagger/';
39
40function getFile(file) {
41 file = path + file;
42 var text = fs.readFileSync(file, 'utf-8');
43 //console.log('text: ' + text);
44 return JSON.parse(text);
45}
46
47function json() {
48 return ["application/json"];
49}
50
51function auth() {
52 return [{QEWDToken: []}];
53}
54
55function schema(ref) {
56 console.log('schema: ref= ' + ref);
57 return {
58 $ref: '#/definitions/' + ref
59 };
60}
61
62function headingPostSchema(heading) {
63 return {
64 $ref: '#/definitions/' + heading + 'Schema'
65 };
66}
67
68function headingSummaryDescription(heading) {
69 return 'Get ' + heading + ' summary data';
70}
71
72function headingPostDescription(heading) {
73 return 'Save a new ' + heading + ' record';
74}
75
76function headingDetailDescription(heading) {
77 return 'Get complete ' + heading + ' data';
78}
79
80function 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
107function 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 //console.log('json: ' + JSON.stringify(obj));
129 return transform(obj, params, {get, schema, json, auth, headingSummaryDescription, headingDetailDescription, headingPostDescription, headingPostSchema});
130}
131
132
133
134var json1 = get('swaggerTemplate.json');
135
136var json2;
137var headingObj;
138var summaryFields;
139var detailFields;
140var template;
141var properties;
142var name;
143
144for (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 // detailed heading data...
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 // post headings
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
227var text = JSON.stringify(json1, null, 2) + '\n';
228
229console.log('text: ' + text);
230
231fs.writeFileSync(path + 'swagger.json', text, 'utf-8');
232
233
234//console.log('ok');
235//console.log(text);