diff --git a/.vscode/launch.json b/.vscode/launch.json index e5f70ea..e018bd9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,6 +8,7 @@ "args": [ "--sourcePath=sample.postman_collection.json", "--targetPath=output", + "--ignoreHeaders=Ignore.*" ], "request": "launch", "runtimeArgs": [ diff --git a/README.md b/README.md index 92235b0..4a57638 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ CLI script to convert a postman collection to httpyac file or files. * sourcePath - path to the postman collection json file * targetPath - path to the root of the .http files, will be created if it doesn't exist +* ignoreHeaders - optional list of headers to ignore, useful when using default headers. Supports regex patterns ## Request Lines @@ -62,4 +63,11 @@ Will be converted to: // pm.environment.set("someId", pm.response.json().id); //}); }} -``` \ No newline at end of file +``` + +## Limitations + +There is no current support for: + +* Non-javascript scripting languages +* Non-JSON body in POST/PUT requests \ No newline at end of file diff --git a/sample.postman_collection.json b/sample.postman_collection.json index ab91fed..0792031 100644 --- a/sample.postman_collection.json +++ b/sample.postman_collection.json @@ -11,7 +11,7 @@ "name": "V1", "item": [ { - "name": "List Comments", + "name": "Get Comment", "event": [ { "listen": "test", @@ -29,7 +29,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{host}}/v1/comments", + "raw": "{{host}}/v1/comments/{{commentId}}", "host": [ "{{host}}" ], @@ -49,7 +49,7 @@ "name": "V2", "item": [ { - "name": "List Comments", + "name": "Get Comment", "event": [ { "listen": "test", @@ -66,9 +66,20 @@ ], "request": { "method": "GET", - "header": [], + "header": [ + { + "key": "If-Match", + "value": "xxx", + "type": "text" + }, + { + "key": "IgnoreThisHeader", + "value": "shouldnotseeme", + "type": "text" + } + ], "url": { - "raw": "{{host}}/v2/comments", + "raw": "{{host}}/v2/comments/{{commentId}}", "host": [ "{{host}}" ], diff --git a/src/RequestDefinitionBuilder.ts b/src/RequestDefinitionBuilder.ts index d7e6076..c8275f2 100644 --- a/src/RequestDefinitionBuilder.ts +++ b/src/RequestDefinitionBuilder.ts @@ -3,9 +3,11 @@ import { Event, Item } from 'postman-collection'; export class RequestDefinitionBuilder { _definition: string; _item: Item + _ignoreHeaders: string[]; constructor() { this._definition = ''; + this._ignoreHeaders = []; } from(item: Item): RequestDefinitionBuilder { @@ -18,6 +20,12 @@ export class RequestDefinitionBuilder { return this; } + ignoreHeaders(headers: string[]): RequestDefinitionBuilder { + this._ignoreHeaders.push(...headers); + + return this; + } + appendName(): RequestDefinitionBuilder { this._definition += `### ${this._item.name}` return this; @@ -118,6 +126,34 @@ export class RequestDefinitionBuilder { return this; } + appendHeaders(): RequestDefinitionBuilder { + if (this._item.request.headers === undefined) { + return this; + } + + for (const header of this._item.request.headers.all()) { + if (!this.shouldInclude(header.key)) { + continue; + } + + this._definition += '\n'; + this._definition += `${header.key}: ${header.value}` + } + + return this; + } + + shouldInclude(header: string): boolean { + for (const ignoreHeader of this._ignoreHeaders) { + if (header.match(ignoreHeader)) { + console.log(`Ignoring header ${header}...`); + return false; + } + } + + return true; + } + appendBody(): RequestDefinitionBuilder { if (this._item.request.body === undefined) { return this; diff --git a/src/app.ts b/src/app.ts index 58d571c..d93307f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,11 +8,26 @@ import { RequestDefinitionBuilder } from './RequestDefinitionBuilder'; const args = parse({ sourcePath: { - type: String, alias: 's', optional: true as const + type: String, alias: 's', optional: true as const, description: 'Path to the exported postman_collection.json' }, targetPath: { - type: String, alias: 'd', optional: true as const + type: String, alias: 'd', optional: true as const, description: 'Path to the root directory to output the .http files' }, + ignoreHeaders: { + type: String, + alias: 'i', + multiple: true, + optional: true as const, + description: 'List of headers to ignore, useful when using default headers. Supports regex patterns', + defaultValue: [] + }, + help: { + type: Boolean, optional: true, alias: 'h', description: 'Prints this usage guide' + }, +}, +{ + helpArg: 'help', + headerContentSections: [{ header: 'Postman 2 HttpYac', content: 'Converts Postman collections to HttpYac format' }] }); const sourcePostmanCollectionPath = args.sourcePath.toString(); @@ -50,9 +65,11 @@ function processItem(item : Item) { console.log('Writing request definition...'); const requestDefinition = new RequestDefinitionBuilder() + .ignoreHeaders(args.ignoreHeaders) .from(item) .appendName() .appendRequest() + .appendHeaders() .appendBody() .appendPreRequestScript() .appendTestScript() diff --git a/src/options.ts b/src/options.ts index 63c1333..640e8c3 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,4 +1,6 @@ export interface IOptions { sourcePath: string; targetPath: string; + ignoreHeaders?: string[]; + help?: boolean; } \ No newline at end of file