Skip to content

Commit 4aff5ff

Browse files
committed
Flatten types into insomnia parser, get a first test passing
1 parent 360d1c8 commit 4aff5ff

File tree

5 files changed

+76
-148
lines changed

5 files changed

+76
-148
lines changed

entities.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
export const hi = true;
1+
import type * as Har from 'har-format';
2+
3+
import { convert } from './parser.js';
4+
5+
export class HarParseError extends Error {
6+
constructor(message: string) {
7+
super(message);
8+
this.name = 'HarParseError';
9+
}
10+
}
11+
12+
export function parseCurlCommand(curlCommand: string): Har.Request[] {
13+
const parserResult = convert(curlCommand);
14+
15+
if (parserResult === null) {
16+
throw new HarParseError('Failed to parse cURL command');
17+
}
18+
19+
return parserResult.map((req): Har.Request => {
20+
return {
21+
method: req.method,
22+
url: req.url,
23+
httpVersion: 'HTTP/1.1',
24+
cookies: [],
25+
headers: req.headers,
26+
queryString: [],
27+
headersSize: -1,
28+
bodySize: 0
29+
};
30+
});
31+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"devDependencies": {
3434
"@types/chai": "^5.2.3",
35+
"@types/har-format": "^1.2.16",
3536
"@types/mocha": "^10.0.10",
3637
"@types/shell-quote": "^1.7.5",
3738
"chai": "^6.2.0",

parser.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33

44
import { type ControlOperator, parse, type ParseEntry } from 'shell-quote';
55

6-
import { type Converter, type ImportRequest, type Parameter } from './entities.ts';
7-
8-
export const id = 'curl';
9-
export const name = 'cURL';
10-
export const description = 'cURL command line tool';
11-
12-
let requestCount = 1;
6+
export interface ConvertedRequest {
7+
method: string;
8+
url: string;
9+
headers: Header[];
10+
parameters: Parameter[];
11+
authentication: { username: string, password?: string } | {};
12+
body: Body;
13+
}
14+
15+
interface Header {
16+
name: string;
17+
value: string;
18+
}
19+
20+
interface Parameter {
21+
name: string;
22+
type?: 'file' | 'text';
23+
value?: string;
24+
fileName?: string;
25+
}
26+
27+
type Body =
28+
| {}
29+
| { mimeType: string; text: string; }
30+
| { mimeType: string; params: Parameter[]; };
1331

1432
const SUPPORTED_ARGS = [
1533
'url',
@@ -37,7 +55,7 @@ type Pair = string | boolean;
3755

3856
type PairsByName = Record<string, Pair[]>;
3957

40-
const importCommand = (parseEntries: ParseEntry[]): ImportRequest => {
58+
const importCommand = (parseEntries: ParseEntry[]): ConvertedRequest => {
4159
// ~~~~~~~~~~~~~~~~~~~~~ //
4260
// Collect all the flags //
4361
// ~~~~~~~~~~~~~~~~~~~~~ //
@@ -98,8 +116,7 @@ const importCommand = (parseEntries: ParseEntry[]): ImportRequest => {
98116
const { searchParams, href, search } = new URL(urlValue);
99117
parameters = Array.from(searchParams.entries()).map(([name, value]) => ({
100118
name,
101-
value,
102-
disabled: false,
119+
value
103120
}));
104121

105122
url = href.replace(search, '').replace(/\/$/, '');
@@ -220,12 +237,7 @@ const importCommand = (parseEntries: ParseEntry[]): ImportRequest => {
220237
method = 'text' in body || 'params' in body ? 'POST' : 'GET';
221238
}
222239

223-
const count = requestCount++;
224240
return {
225-
_id: `__REQ_${count}__`,
226-
_type: 'request',
227-
parentId: '__WORKSPACE_ID__',
228-
name: url || `cURL Import ${count}`,
229241
parameters,
230242
url,
231243
method,
@@ -359,9 +371,7 @@ const getPairValue = <T extends string | boolean>(parisByName: PairsByName, defa
359371
return defaultValue;
360372
};
361373

362-
export const convert: Converter = rawData => {
363-
requestCount = 1;
364-
374+
export const convert = (rawData: string) => {
365375
if (!rawData.match(/^\s*curl /)) {
366376
return null;
367377
}
@@ -418,7 +428,7 @@ export const convert: Converter = rawData => {
418428
// Push the last unfinished command
419429
commands.push(currentCommand);
420430

421-
const requests: ImportRequest[] = commands.filter(command => command[0] === 'curl').map(importCommand);
431+
const requests: ConvertedRequest[] = commands.filter(command => command[0] === 'curl').map(importCommand);
422432

423433
return requests;
424434
};

tests.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { expect } from 'chai';
2-
import { hi } from './index.js';
2+
import { parseCurlCommand } from './index.js';
33

4-
describe("test", () => {
5-
it("should pass", () => {
6-
expect(hi).to.equal(true);
4+
describe("Curl parsing", () => {
5+
it("should be able to parse a minimal GET request", () => {
6+
expect(
7+
parseCurlCommand('curl http://example.com')
8+
).to.deep.equal([{
9+
method: 'GET',
10+
url: 'http://example.com',
11+
httpVersion: 'HTTP/1.1',
12+
cookies: [],
13+
headers: [],
14+
queryString: [],
15+
headersSize: -1,
16+
bodySize: 0,
17+
}]);
718
})
819
})

0 commit comments

Comments
 (0)