-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE `interfaces` | ||
ADD COLUMN `bodyOption` VARCHAR(255) NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ALTER TABLE `RAP2_DELOS_APP`.`Properties` | ||
MODIFY COLUMN `scope` enum('request','response','script') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL | ||
DEFAULT 'response' COMMENT 'property owner' AFTER `id`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,106 @@ | ||
import { PostmanCollection, Folder, Item } from "../../types/postman" | ||
import { Repository, Interface, Module, Property } from "../../models" | ||
import * as url from 'url' | ||
import { REQUEST_PARAMS_TYPE } from "../../models/bo/property" | ||
import { POS_TYPE } from "../../models/bo/property" | ||
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'} | ||
}) | ||
const eventScript = await Property.findAll({ | ||
where: {interfaceId, scope: 'script'} | ||
}) | ||
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 || '', | ||
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) | ||
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) | ||
} | ||
return result | ||
modItem.item.push(itfItem) | ||
} | ||
result.item.push(modItem) | ||
} | ||
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 === POS_TYPE.BODY) | ||
.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 === POS_TYPE.QUERY) | ||
.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 === POS_TYPE.HEADER) | ||
.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' | ||
})) | ||
return pList.filter(x => (x.pos === POS_TYPE.PRE_REQUEST_SCRIPT || x.pos === POS_TYPE.TEST)) | ||
.map(x => ({ | ||
key: x.name, | ||
script: { key: x.name, type: 'text/javascript', exec: x.value }, | ||
disabled: false, | ||
listen: x.pos === POS_TYPE.PRE_REQUEST_SCRIPT ? 'prerequest' : 'test' | ||
})) | ||
} |