From cf2bf672206408c6eb339a7e20c23a2ef6d4ff59 Mon Sep 17 00:00:00 2001 From: "huoyong.msb" Date: Tue, 21 Jul 2020 10:20:50 +0800 Subject: [PATCH] feat: merged manually --- src/config/config.dev.ts | 2 +- src/models/bo/property.ts | 4 +- src/service/export/postman.ts | 150 +++++++++++++++++++--------------- 3 files changed, 86 insertions(+), 70 deletions(-) diff --git a/src/config/config.dev.ts b/src/config/config.dev.ts index 22804b4..15bd585 100644 --- a/src/config/config.dev.ts +++ b/src/config/config.dev.ts @@ -17,7 +17,7 @@ const config: IConfigOptions = { host: process.env.MYSQL_URL ?? "localhost", port: (process.env.MYSQL_PORT && parseInt(process.env.MYSQL_PORT)) || 3306, username: process.env.MYSQL_USERNAME ?? "root", - password: process.env.MYSQL_PASSWD ?? "29837DF983(*&34kfjD(*3kjf(", + password: process.env.MYSQL_PASSWD ?? "", database: process.env.MYSQL_SCHEMA ?? "RAP2_DELOS_APP", pool: { max: 10, diff --git a/src/models/bo/property.ts b/src/models/bo/property.ts index d0ca8b8..84442ce 100644 --- a/src/models/bo/property.ts +++ b/src/models/bo/property.ts @@ -1,13 +1,15 @@ import { Table, Column, Model, AutoIncrement, PrimaryKey, AllowNull, DataType, Default, BelongsTo, ForeignKey } from 'sequelize-typescript' import { User, Interface, Module, Repository } from '../' -export enum SCOPES { REQUEST = 'request', RESPONSE = 'response' } +export enum SCOPES { REQUEST = 'request', RESPONSE = 'response', SCRIPT = 'script' } export enum TYPES { STRING = 'String', NUMBER = 'Number', BOOLEAN = 'Boolean', OBJECT = 'Object', ARRAY = 'Array', FUNCTION = 'Function', REGEXP = 'RegExp', Null = 'Null' } export enum REQUEST_PARAMS_TYPE { HEADERS = 1, QUERY_PARAMS = 2, BODY_PARAMS = 3, + PRE_REQUEST_SCRIPT = 4, + TESTS = 5 } @Table({ paranoid: true, freezeTableName: false, timestamps: true }) diff --git a/src/service/export/postman.ts b/src/service/export/postman.ts index 1ce692a..cd892c4 100644 --- a/src/service/export/postman.ts +++ b/src/service/export/postman.ts @@ -7,86 +7,100 @@ import UrlUtils from "../../routes/utils/url" const SCHEMA_V_2_1_0 = 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json' export default class PostmanService { - public static async export(repositoryId: number): Promise { - const repo = await Repository.findByPk(repositoryId, { - include: [{ - model: Module, - as: 'modules', - include: [{ - model: Interface, - as: 'interfaces', - include: [{ - model: Property, - as: 'properties', - }] - }] - }] - }) - const result: PostmanCollection = { - info: { - name: `RAP2 Pack ${repo.name}`, - schema: SCHEMA_V_2_1_0, - }, - item: [] - } + public static async export(repositoryId: number): Promise { + const repo = await Repository.findByPk(repositoryId, { + include: [{ + model: Module, + as: 'modules', + include: [{ + model: Interface, + as: 'interfaces', + include: [{ + model: Property, + as: 'properties', + }] + }] + }] + }) + const result: PostmanCollection = { + info: { + name: `RAP2 Pack ${repo.name}`, + schema: SCHEMA_V_2_1_0, + }, + item: [] + } - for (const mod of repo.modules) { - const modItem: Folder = { - name: mod.name, - item: [], - } + for (const mod of repo.modules) { + const modItem: Folder = { + name: mod.name, + item: [], + } - for (const itf of mod.interfaces) { - const interfaceId = itf.id - const requestParams = await Property.findAll({ - where: { interfaceId, scope: 'request' } - }) - const responseParams = await Property.findAll({ - where: { interfaceId, scope: 'response' } - }) + for (const itf of mod.interfaces) { + const interfaceId = itf.id + const requestParams = await Property.findAll({ + where: {interfaceId, scope: 'request'} + }) + const responseParams = await Property.findAll({ + where: {interfaceId, scope: 'response'} + }) + const eventScript = await Property.findAll({ + where: {interfaceId, scope: 'script'} + }) - const relativeUrl = UrlUtils.getRelative(itf.url) - const parseResult = url.parse(itf.url) - const itfItem: Item = { - name: itf.name, - request: { - method: itf.method as any, - header: getHeader(requestParams), - body: getBody(requestParams), - url: { - raw: `{{url}}${relativeUrl}`, - host: '{{url}}', - port: parseResult.port || undefined, - hash: parseResult.hash, - path: [parseResult.path], - query: getQuery(requestParams), - }, - description: itf.description, - }, - response: responseParams.map(x => ({ key: x.name, value: x.value })), + const relativeUrl = UrlUtils.getRelative(itf.url) + const parseResult = url.parse(itf.url) + const itfItem: Item = { + name: itf.name, + request: { + method: itf.method as any, + header: getHeader(requestParams), + body: getBody(requestParams), + url: { + raw: `{{url}}${relativeUrl}`, + host: '{{url}}', + port: parseResult.port || '', + hash: parseResult.hash, + path: [parseResult.path], + query: getQuery(requestParams), + }, + description: itf.description, + }, + response: responseParams.map(x => ({key: x.name, value: x.value})), + event: getEvent(eventScript) + } + modItem.item.push(itfItem) + } + result.item.push(modItem) } - modItem.item.push(itfItem) - } - result.item.push(modItem) + return result } - return result - } } function getBody(pList: Property[]) { - return { - "mode": "formdata" as "formdata", - "formdata": pList.filter(x => x.pos === REQUEST_PARAMS_TYPE.BODY_PARAMS) - .map(x => ({ key: x.name, value: x.value, description: x.description, type: "text" as "text"})), - } + return { + "mode": "formdata" as "formdata", + "formdata": pList.filter(x => x.pos === REQUEST_PARAMS_TYPE.BODY_PARAMS) + .map(x => ({key: x.name, value: x.value, description: x.description, type: "text" as "text"})), + } } function getQuery(pList: Property[]) { - return pList.filter(x => x.pos === null || x.pos === REQUEST_PARAMS_TYPE.QUERY_PARAMS) - .map(x => ({ key: x.name, value: x.value, description: x.description })) + return pList.filter(x => x.pos === null || x.pos === REQUEST_PARAMS_TYPE.QUERY_PARAMS) + .map(x => ({key: x.name, value: x.value, description: x.description})) } function getHeader(pList: Property[]) { - return pList.filter(x => x.pos === REQUEST_PARAMS_TYPE.HEADERS) - .map(x => ({ key: x.name, value: x.value, description: x.description })) + return pList.filter(x => x.pos === REQUEST_PARAMS_TYPE.HEADERS) + .map(x => ({key: x.name, value: x.value, description: x.description})) +} + +function getEvent(pList: Property[]) { + return pList.filter(x => (x.pos === REQUEST_PARAMS_TYPE.PRE_REQUEST_SCRIPT || x.pos === REQUEST_PARAMS_TYPE.TESTS)) + .map(x => ({ + key: x.name, + script: {key: x.name, type: 'text/javascript', exec: x.value}, + disabled: false, + listen: x.pos === REQUEST_PARAMS_TYPE.PRE_REQUEST_SCRIPT ? 'prerequest' : 'test' + })) } \ No newline at end of file