Skip to content

Commit

Permalink
Handle headers (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Sep 7, 2024
1 parent 5b3296b commit 9746654
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"args": [
"--sourcePath=sample.postman_collection.json",
"--targetPath=output",
"--ignoreHeaders=Ignore.*"
],
"request": "launch",
"runtimeArgs": [
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,4 +63,11 @@ Will be converted to:
// pm.environment.set("someId", pm.response.json().id);
//});
}}
```
```

## Limitations

There is no current support for:

* Non-javascript scripting languages
* Non-JSON body in POST/PUT requests
21 changes: 16 additions & 5 deletions sample.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"name": "V1",
"item": [
{
"name": "List Comments",
"name": "Get Comment",
"event": [
{
"listen": "test",
Expand All @@ -29,7 +29,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{host}}/v1/comments",
"raw": "{{host}}/v1/comments/{{commentId}}",
"host": [
"{{host}}"
],
Expand All @@ -49,7 +49,7 @@
"name": "V2",
"item": [
{
"name": "List Comments",
"name": "Get Comment",
"event": [
{
"listen": "test",
Expand All @@ -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}}"
],
Expand Down
36 changes: 36 additions & 0 deletions src/RequestDefinitionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ import { RequestDefinitionBuilder } from './RequestDefinitionBuilder';

const args = parse<IOptions>({
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();
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface IOptions {
sourcePath: string;
targetPath: string;
ignoreHeaders?: string[];
help?: boolean;
}

0 comments on commit 9746654

Please sign in to comment.