可以直接让用户把自己本地的 data 数据导入到 RAP 中
(data: JsonData, curUserId: number)
| 1177 | |
| 1178 | /** 可以直接让用户把自己本地的 data 数据导入到 RAP 中 */ |
| 1179 | public static async importRepoFromJSON(data: JsonData, curUserId: number) { |
| 1180 | function parseJSON(str: string) { |
| 1181 | try { |
| 1182 | const data = JSON5.parse(str) |
| 1183 | return _.isObject(data) ? data : {} |
| 1184 | } catch (error) { |
| 1185 | return {} |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | const repositoryId = data.id |
| 1190 | await Promise.all( |
| 1191 | data.modules.map(async (modData, index) => { |
| 1192 | const mod = await Module.create({ |
| 1193 | name: modData.name, |
| 1194 | description: modData.description || '', |
| 1195 | priority: index + 1, |
| 1196 | creatorId: curUserId, |
| 1197 | repositoryId, |
| 1198 | }) |
| 1199 | |
| 1200 | await Promise.all( |
| 1201 | modData.interfaces.map(async (iftData, index) => { |
| 1202 | let properties = iftData.properties |
| 1203 | |
| 1204 | const itf = await Interface.create({ |
| 1205 | moduleId: mod.id, |
| 1206 | name: iftData.name, |
| 1207 | description: iftData.description || '', |
| 1208 | url: iftData.url, |
| 1209 | priority: index + 1, |
| 1210 | creatorId: curUserId, |
| 1211 | repositoryId, |
| 1212 | method: iftData.method, |
| 1213 | }) |
| 1214 | |
| 1215 | if (!properties && (iftData.requestJSON || iftData.responseJSON)) { |
| 1216 | const reqData = parseJSON(iftData.requestJSON) |
| 1217 | const resData = parseJSON(iftData.responseJSON) |
| 1218 | properties = [ |
| 1219 | ...Tree.jsonToArray(reqData, { |
| 1220 | interfaceId: itf.id, |
| 1221 | moduleId: mod.id, |
| 1222 | repositoryId, |
| 1223 | scope: 'request', |
| 1224 | userId: curUserId, |
| 1225 | }), |
| 1226 | ...Tree.jsonToArray(resData, { |
| 1227 | interfaceId: itf.id, |
| 1228 | moduleId: mod.id, |
| 1229 | repositoryId, |
| 1230 | scope: 'response', |
| 1231 | userId: curUserId, |
| 1232 | }), |
| 1233 | ] |
| 1234 | } |
| 1235 | |
| 1236 | if (!properties) { |
no test coverage detected