|
3 | 3 |
|
4 | 4 | import { type ControlOperator, parse, type ParseEntry } from 'shell-quote'; |
5 | 5 |
|
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[]; }; |
13 | 31 |
|
14 | 32 | const SUPPORTED_ARGS = [ |
15 | 33 | 'url', |
@@ -37,7 +55,7 @@ type Pair = string | boolean; |
37 | 55 |
|
38 | 56 | type PairsByName = Record<string, Pair[]>; |
39 | 57 |
|
40 | | -const importCommand = (parseEntries: ParseEntry[]): ImportRequest => { |
| 58 | +const importCommand = (parseEntries: ParseEntry[]): ConvertedRequest => { |
41 | 59 | // ~~~~~~~~~~~~~~~~~~~~~ // |
42 | 60 | // Collect all the flags // |
43 | 61 | // ~~~~~~~~~~~~~~~~~~~~~ // |
@@ -98,8 +116,7 @@ const importCommand = (parseEntries: ParseEntry[]): ImportRequest => { |
98 | 116 | const { searchParams, href, search } = new URL(urlValue); |
99 | 117 | parameters = Array.from(searchParams.entries()).map(([name, value]) => ({ |
100 | 118 | name, |
101 | | - value, |
102 | | - disabled: false, |
| 119 | + value |
103 | 120 | })); |
104 | 121 |
|
105 | 122 | url = href.replace(search, '').replace(/\/$/, ''); |
@@ -220,12 +237,7 @@ const importCommand = (parseEntries: ParseEntry[]): ImportRequest => { |
220 | 237 | method = 'text' in body || 'params' in body ? 'POST' : 'GET'; |
221 | 238 | } |
222 | 239 |
|
223 | | - const count = requestCount++; |
224 | 240 | return { |
225 | | - _id: `__REQ_${count}__`, |
226 | | - _type: 'request', |
227 | | - parentId: '__WORKSPACE_ID__', |
228 | | - name: url || `cURL Import ${count}`, |
229 | 241 | parameters, |
230 | 242 | url, |
231 | 243 | method, |
@@ -359,9 +371,7 @@ const getPairValue = <T extends string | boolean>(parisByName: PairsByName, defa |
359 | 371 | return defaultValue; |
360 | 372 | }; |
361 | 373 |
|
362 | | -export const convert: Converter = rawData => { |
363 | | - requestCount = 1; |
364 | | - |
| 374 | +export const convert = (rawData: string) => { |
365 | 375 | if (!rawData.match(/^\s*curl /)) { |
366 | 376 | return null; |
367 | 377 | } |
@@ -418,7 +428,7 @@ export const convert: Converter = rawData => { |
418 | 428 | // Push the last unfinished command |
419 | 429 | commands.push(currentCommand); |
420 | 430 |
|
421 | | - const requests: ImportRequest[] = commands.filter(command => command[0] === 'curl').map(importCommand); |
| 431 | + const requests: ConvertedRequest[] = commands.filter(command => command[0] === 'curl').map(importCommand); |
422 | 432 |
|
423 | 433 | return requests; |
424 | 434 | }; |
|
0 commit comments