11import { URL } from 'node:url' ;
22
33import * as curlconverter from 'curlconverter' ;
4+ import type { AuthType } from 'curlconverter/dist/src/Request' ;
45import { type ControlOperator , parse , type ParseEntry } from 'shell-quote' ;
56
7+ import type { SupportedAuthTypes } from '~/models/request' ;
8+
69import { type Converter , type ImportRequest , type Parameter } from '../entities' ;
710
811export 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 = (
486495const 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 } __` ,
0 commit comments