Skip to content

Commit dd4037a

Browse files
committed
fix most types
1 parent 1c51832 commit dd4037a

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

packages/insomnia/src/main/importers/entities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type * as Har from 'har-format';
22

3+
import type { SupportedAuthTypes } from '~/models/request';
4+
35
export interface Comment {
46
comment?: string;
57
}
@@ -12,7 +14,7 @@ export interface Authentication extends Comment {
1214
clientId?: string;
1315
clientSecret?: Variable;
1416
scope?: string;
15-
type?: 'basic' | 'oauth2';
17+
type?: SupportedAuthTypes;
1618
grantType?: 'authorization_code' | 'password' | 'client_credentials';
1719
disabled?: boolean;
1820
username?: string;

packages/insomnia/src/main/importers/importers/curl.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { URL } from 'node:url';
22

33
import * as curlconverter from 'curlconverter';
4+
import type { AuthType } from 'curlconverter/dist/src/Request';
45
import { type ControlOperator, parse, type ParseEntry } from 'shell-quote';
56

7+
import type { SupportedAuthTypes } from '~/models/request';
8+
69
import { type Converter, type ImportRequest, type Parameter } from '../entities';
710

811
export const id = 'curl';
@@ -443,31 +446,36 @@ const transformCurlDataObjectToInsomniaBody = (
443446
if (!data) {
444447
return { mimeType };
445448
}
449+
// Handle non-form-urlencoded bodies as text
446450
if (mimeType !== 'application/x-www-form-urlencoded') {
447451
return {
448452
text: new URLSearchParams(data).toString(),
449453
mimeType,
450454
};
451455
}
456+
// curlconverter gives us an object: {key: value}
452457
if (typeof data === 'object') {
453458
return {
454-
params: Object.entries(data).map(([name, value]) => ({ name, value })),
459+
params: Object.entries(data).map(([name, value]) => ({ name, value })) as Parameter[],
455460
mimeType,
456461
};
457462
}
463+
// curlconverter gives us a file upload string: key@filename
458464
if (data.includes('@')) {
459465
const [name, fileName] = data.split('@');
460466
return {
461467
params: [{ name, fileName, type: 'file' }],
462468
mimeType,
463469
};
464470
}
471+
// curlconverter gives us url params: key=value&key2=value2
465472
if (data.includes('&')) {
466473
return {
467474
params: [...new URLSearchParams(data).entries()].map(([name, value]) => ({ name, value })),
468475
mimeType,
469476
};
470477
}
478+
// curlconverter gives us a single binary param: key=value
471479
if (data.includes('=')) {
472480
const firstEqual = data.indexOf('=');
473481
const name = data.slice(0, firstEqual);
@@ -478,6 +486,7 @@ const transformCurlDataObjectToInsomniaBody = (
478486
};
479487
}
480488

489+
// we default to decoding uri components, unclear why it works this way but this preserves existing behaviour
481490
return {
482491
params: [{ name: '', value: decodeURIComponent(data) }],
483492
mimeType,
@@ -486,11 +495,30 @@ const transformCurlDataObjectToInsomniaBody = (
486495
const transformCurlObjectToInsomniaRequest = (output: curlconverter.JSONOutput): ImportRequest => {
487496
const mimeType = output.headers?.['Content-Type'] || '';
488497
const body = transformCurlDataObjectToInsomniaBody(output.data, mimeType);
498+
499+
// Match our auth types to curlconverter
500+
const mapAuthTypeToInsomniaAuthType: Record<AuthType, SupportedAuthTypes> = {
501+
'basic': 'basic',
502+
'bearer': 'bearer',
503+
'digest': 'digest',
504+
'ntlm': 'ntlm',
505+
'ntlm-wb': 'ntlm',
506+
'none': 'none',
507+
'negotiate': 'none', // not supported in Insomnia, may be similar to ntlm
508+
'aws-sigv4': 'none', // not supported in Insomnia, may be similar to hawk
509+
};
489510
const authentication = output.auth
490-
? { type: output.auth_type, username: output.auth.user, password: output.auth.password }
511+
? {
512+
type: output.auth_type ? mapAuthTypeToInsomniaAuthType[output.auth_type] : 'none',
513+
username: output.auth.user,
514+
password: output.auth.password,
515+
}
491516
: {};
517+
// Preserves existing behaviour of putting query params in the parameters section for GET requests, and in the body for other requests
492518
const parameters =
493-
output.method === 'GET' && output.data ? Object.entries(output.data).map(([name, value]) => ({ name, value })) : [];
519+
output.method === 'GET' && output.data
520+
? (Object.entries(output.data).map(([name, value]) => ({ name, value })) as Parameter[])
521+
: [];
494522
const count = requestCount++;
495523
return {
496524
_id: `__REQ_${count}__`,

packages/insomnia/src/models/request.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ export const canDuplicate = true;
3232

3333
export const canSync = true;
3434

35+
export type SupportedAuthTypes =
36+
| 'basic'
37+
| 'apikey'
38+
| 'oauth2'
39+
| 'hawk'
40+
| 'oauth1'
41+
| 'digest'
42+
| 'ntlm'
43+
| 'bearer'
44+
| 'iam'
45+
| 'netrc'
46+
| 'asap'
47+
| 'none'
48+
| 'singleToken';
3549
/**
3650
* Basic Authentication configuration
3751
* Uses username and password with optional ISO-8859-1 encoding

0 commit comments

Comments
 (0)