|
| 1 | + |
| 2 | + angular.module('DataStudioWebui.AppEditor') |
| 3 | + .factory('ApiSchema', ApiSchemaFactory); |
| 4 | + |
| 5 | + ApiSchemaFactory.$inject = []; |
| 6 | + function ApiSchemaFactory () { |
| 7 | + |
| 8 | + class ApiSchema { |
| 9 | + constructor (operations, routes) { |
| 10 | + this.operations = operations; |
| 11 | + this.routes = routes; |
| 12 | + } |
| 13 | + toJSON () { |
| 14 | + let operations = this.operations; |
| 15 | + let routes = this.routes; |
| 16 | + let paths = {}; |
| 17 | + let routePaths = {}; |
| 18 | + |
| 19 | + routes.forEach(route => { |
| 20 | + let path = route.Path; |
| 21 | + paths[path] = {}; |
| 22 | + routePaths[route.Id] = path; |
| 23 | + }); |
| 24 | + |
| 25 | + operations.forEach(operation => { |
| 26 | + |
| 27 | + let op; |
| 28 | + let method; |
| 29 | + |
| 30 | + let targetPath = routePaths[operation.RouteId]; |
| 31 | + |
| 32 | + let pathExists = targetPath in paths; |
| 33 | + if (!pathExists) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + method = operation.Method; |
| 38 | + op = { |
| 39 | + operationId: operation.Name, |
| 40 | + summary: '', |
| 41 | + description: '', |
| 42 | + tags: [], |
| 43 | + consumes: [ |
| 44 | + 'application/json', |
| 45 | + ], |
| 46 | + produces: [ |
| 47 | + 'application/json', |
| 48 | + ], |
| 49 | + params: [], |
| 50 | + responses: {}, |
| 51 | + }; |
| 52 | + |
| 53 | + decorate(op, method, targetPath); |
| 54 | + |
| 55 | + paths[targetPath][method] = op; |
| 56 | + |
| 57 | + }); |
| 58 | + |
| 59 | + return { |
| 60 | + swagger: '2.0', |
| 61 | + info: { |
| 62 | + title: '', |
| 63 | + description: '', |
| 64 | + version: '0.0.0-ALPHA', |
| 65 | + contact: { |
| 66 | + email: '', |
| 67 | + }, |
| 68 | + license: { |
| 69 | + name: '', |
| 70 | + url: '', |
| 71 | + }, |
| 72 | + }, |
| 73 | + host: 'api.localhost', |
| 74 | + basePath: '/', |
| 75 | + tags: [], |
| 76 | + schemes: [ |
| 77 | + 'http', |
| 78 | + 'https', |
| 79 | + ], |
| 80 | + paths: paths, |
| 81 | + definitions: {}, |
| 82 | + }; |
| 83 | + |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + return ApiSchema; |
| 88 | + |
| 89 | + function decorate (op, method, uri) { |
| 90 | + |
| 91 | + let uriParts = uri.split(/\//g).slice(1); |
| 92 | + let className = ''; |
| 93 | + |
| 94 | + uriParts.forEach(uriPart => { |
| 95 | + let isVariable = ':' === uriPart[0]; |
| 96 | + if (!isVariable) { |
| 97 | + let c = uriPart[0].toUpperCase(); |
| 98 | + className = c + uriPart.substr(1); |
| 99 | + className = className.replace(/ies$/, 'y'); |
| 100 | + className = className.replace(/s$/, ''); |
| 101 | + op.tags.push(className); |
| 102 | + return; |
| 103 | + } |
| 104 | + op.params.push({ |
| 105 | + in: 'path', |
| 106 | + name: uriPart.substr(1), |
| 107 | + description: '', |
| 108 | + required: true, |
| 109 | + type: 'string', |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + if ('post' === method) { |
| 114 | + add303Response(op); |
| 115 | + add400Response(op); |
| 116 | + add401Response(op); |
| 117 | + add403Response(op); |
| 118 | + op.params.push({ |
| 119 | + in: 'body', |
| 120 | + name: 'body', |
| 121 | + description: `The new \`${className}\``, |
| 122 | + required: true, |
| 123 | + schema: { |
| 124 | + $ref: `#/definitions/New${className}`, |
| 125 | + }, |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + if ('get' === method) { |
| 130 | + add200Response(op); |
| 131 | + add404Response(op); |
| 132 | + } |
| 133 | + |
| 134 | + if ('put' === method) { |
| 135 | + add200Response(op); |
| 136 | + add202Response(op); |
| 137 | + add400Response(op); |
| 138 | + add401Response(op); |
| 139 | + add403Response(op); |
| 140 | + op.params.push({ |
| 141 | + in: 'body', |
| 142 | + name: 'body', |
| 143 | + description: `The \`${className}\` data to save`, |
| 144 | + required: true, |
| 145 | + schema: { |
| 146 | + $ref: `#/definitions/${className}`, |
| 147 | + }, |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + if ('delete' === method) { |
| 152 | + add204Response(op); |
| 153 | + add401Response(op); |
| 154 | + add403Response(op); |
| 155 | + add404Response(op); |
| 156 | + } |
| 157 | + |
| 158 | + function add200Response (op) { |
| 159 | + op.responses['200'] = { |
| 160 | + description: 'OK', |
| 161 | + schema: { |
| 162 | + $ref: `#/definitions/${className}`, |
| 163 | + }, |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + function add202Response (op) { |
| 168 | + op.responses['202'] = { |
| 169 | + description: 'Accepted', |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + function add204Response (op) { |
| 174 | + op.responses['204'] = { |
| 175 | + description: 'No Content', |
| 176 | + }; |
| 177 | + } |
| 178 | + |
| 179 | + function add303Response (op) { |
| 180 | + op.responses['303'] = { |
| 181 | + description: 'See Other', |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + function add400Response (op) { |
| 186 | + op.responses['400'] = { |
| 187 | + description: 'Bad Request', |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + function add401Response (op) { |
| 192 | + op.responses['401'] = { |
| 193 | + description: 'Unauthorized', |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + function add403Response (op) { |
| 198 | + op.responses['403'] = { |
| 199 | + description: 'Forbidden', |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + function add404Response (op) { |
| 204 | + op.responses['404'] = { |
| 205 | + description: 'Not Found', |
| 206 | + }; |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + } |
0 commit comments