Skip to content

Commit

Permalink
Merge branch 'bryanYao-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosn committed Jul 21, 2020
2 parents 0b71d7f + cf2bf67 commit 7af8fee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/config/config.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/models/bo/property.ts
Original file line number Diff line number Diff line change
@@ -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 })
Expand Down
150 changes: 82 additions & 68 deletions src/service/export/postman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostmanCollection> {
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<PostmanCollection> {
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'
}))
}

0 comments on commit 7af8fee

Please sign in to comment.