This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
postman.ts
154 lines (142 loc) · 4.74 KB
/
postman.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// import * as recursiveWalk from 'recursive-readdir';
import * as path from 'path';
import * as fs from 'fs';
import chalk from 'chalk';
// import * as yaml from 'js-yaml';
import { writeErrorMessage } from './handler';
import { writeMessage } from './handler';
// var mkdirp = require('mkdirp');
// import * as mkdirp from mkdirp;
/**
* Convert Postman collection to stREST requests
* @param collection_file [optional] Target dir with .strest.yaml files in it
*/
export const convert = async (collection_file: string) => {
let isFile = false
let cwd_file
// if a custom path was defined
if(collection_file !== null){
cwd_file = path.join(process.cwd(), collection_file);
if(!fs.existsSync(cwd_file)){
writeErrorMessage(`Path ${chalk.underline(path.join(process.cwd(), collection_file))} does not exist`);
return 1
}
isFile = fs.statSync(cwd_file).isFile();
} else {
writeErrorMessage(`Postman collection not specified`);
return 1
}
// if the path is a directory, walk through it and find all test files
if(!isFile) {
writeErrorMessage(`Specified collection ${chalk.underline(path.join(process.cwd(), collection_file))} is not a file.`);
return 1
}
let collection
try{
collection = require(cwd_file);
}catch(e){
if (e instanceof ReferenceError) {
writeErrorMessage(`ReferenceError. Is it a json file?`);
return 1;
}else{
throw e
}
}
// Light validation on the collection
if(collection.info.schema == "https://schema.getpostman.com/json/collection/v2.1.0/collection.json") {
writeMessage(`Postman schema 2.1 confirmed`);
} else{
writeErrorMessage(`Postman schema not 2.1`);
return 1
}
// await(exporter(collection.item))
let result:any[] = []
let cwd = path.join(process.cwd(),collection.info.name);
result = await(buildDirs(collection.item, cwd, result))
console.log(result)
return 0
}
/**
* Recurse Collection Items
* @param item
*/
const buildDirs = async (items: Array<any>, working_dir: any, result: any[]) => {
function IsJsonString(str:string) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
for (let i of items) {
const new_working_dir = path.join(working_dir, i.name.replace(/[\ +\/+]/g,"_"));
if(i.item || i._postman_isSubFolder) {
result = await(buildDirs(i.item, new_working_dir, result))
}else{
let request_name = i.name.replace(/[\ +\/+]/g,"_") + ".strest.yml"
let request: any = {}
request.path = working_dir
request.request = request_name
request[request_name] = {}
request[request_name].url = i.request.url.raw
request[request_name].method = i.request.method
if (i.request.header){
for (let header of i.request.header){
// console.log(header)
}
}
if (i.request.body){
request[request_name].data = {}
if(i.request.body.mode == "raw"){
if(IsJsonString(i.request.body.raw)){
request[request_name].data.json = JSON.parse(i.request.body.raw)
}else{
request[request_name].data.raw = i.request.body.raw
}
}
}
// convert scripts
try {
const script = i.event[0].script.exec.toString();
const codeReg = /pm\.response\.to\.have\.status\(\d+\)/gm;
const rawBodyReg = /pm\.response\.to\.have\.body\(.*\);/gm;
const marksReg1 = /\"(.*?)\"/;
const marksReg2 = /\`(.*?)\`/;
const innerReg = /\((.*?)\)/
const codeMatch = codeReg.exec(script);
if(codeMatch !== null) {
const innerVal = innerReg.exec(codeMatch[0]);
const codeToProof = innerVal![1];
if(typeof request[request_name].validate === 'undefined') {
request[request_name].validate = {};
}
request[request_name].validate.code = codeToProof;
}
const rawBodyMatch = rawBodyReg.exec(script);
if(rawBodyMatch !== null) {
const innerVal = innerReg.exec(rawBodyMatch[0]);
const rawWithQuo = innerVal![1]
let rawToProof
const quoMatch1 = marksReg2.exec(rawWithQuo);
// check for ` marks
if(quoMatch1 !== null) {
rawToProof = quoMatch1[1];
} else {
// if there are no ` marks, check for normal "" quotation marks
const quoMatch2 = marksReg1.exec(rawWithQuo);
rawToProof = quoMatch2![1];
}
if(typeof request[request_name].validate === 'undefined') {
request[request_name].validate = {};
}
request[request_name].validate.raw = rawToProof;
}
} catch(e) {
// no scripts found
}
result.push(request)
}
}
return result
}