44 * Licensed under the BSD 3-Clause license.
55 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66 */
7- import { EOL } from 'node:os ' ;
8- import { createWriteStream } from 'node:fs ' ;
9- import got , { Headers } from 'got' ;
7+ import { createWriteStream , readFileSync , existsSync } from 'node:fs ' ;
8+ import { join } from 'node:path ' ;
9+ import got from 'got' ;
1010import type { AnyJson } from '@salesforce/ts-types' ;
1111import { ProxyAgent } from 'proxy-agent' ;
1212import { Flags , SfCommand } from '@salesforce/sf-plugins-core' ;
13- import { Messages , Org , SfError } from '@salesforce/core' ;
13+ import { Messages , Org , SFDX_HTTP_HEADERS , SfError } from '@salesforce/core' ;
1414import { Args } from '@oclif/core' ;
1515import ansis from 'ansis' ;
16+ import { getHeaders } from '../../../shared/methods.js' ;
1617
1718Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
1819const messages = Messages . loadMessages ( '@salesforce/plugin-api' , 'rest' ) ;
@@ -23,12 +24,7 @@ export class Rest extends SfCommand<void> {
2324 public static state = 'beta' ;
2425 public static enableJsonFlag = false ;
2526 public static readonly flags = {
26- // TODO: getting a false positive from this eslint rule.
27- // summary is already set in the org flag.
28- // eslint-disable-next-line sf-plugin/flag-summary
29- 'target-org' : Flags . requiredOrg ( {
30- helpValue : 'username' ,
31- } ) ,
27+ 'target-org' : Flags . requiredOrg ( ) ,
3228 include : Flags . boolean ( {
3329 char : 'i' ,
3430 summary : messages . getMessage ( 'flags.include.summary' ) ,
@@ -67,45 +63,38 @@ export class Rest extends SfCommand<void> {
6763 } ) ,
6864 } ;
6965
70- private static getHeaders ( keyValPair : string [ ] ) : Headers {
71- const headers : { [ key : string ] : string } = { } ;
72-
73- for ( const header of keyValPair ) {
74- const [ key , ...rest ] = header . split ( ':' ) ;
75- const value = rest . join ( ':' ) . trim ( ) ;
76- if ( ! key || ! value ) {
77- throw new SfError ( `Failed to parse HTTP header: "${ header } ".` , 'Failed To Parse HTTP Header' , [
78- 'Make sure the header is in a "key:value" format, e.g. "Accept: application/json"' ,
79- ] ) ;
80- }
81- headers [ key ] = value ;
82- }
83-
84- return headers ;
85- }
86-
8766 public async run ( ) : Promise < void > {
8867 const { flags, args } = await this . parse ( Rest ) ;
8968
9069 const org = flags [ 'target-org' ] ;
9170 const streamFile = flags [ 'stream-to-file' ] ;
71+ const headers = flags . header ? getHeaders ( flags . header ) : { } ;
72+
73+ const url = new URL ( `${ org . getField < string > ( Org . Fields . INSTANCE_URL ) } /${ args . endpoint } ` ) ;
74+ const body =
75+ flags . method === 'GET'
76+ ? undefined
77+ : // if they've passed in a file name, check and read it
78+ existsSync ( join ( process . cwd ( ) , flags . body ?? '' ) )
79+ ? readFileSync ( join ( process . cwd ( ) , flags . body ?? '' ) )
80+ : // otherwise it's a stdin, and we use it directly
81+ flags . body ;
9282
9383 await org . refreshAuth ( ) ;
9484
95- const url = `${ org . getField < string > ( Org . Fields . INSTANCE_URL ) } /${ args . endpoint } ` ;
96-
9785 const options = {
9886 agent : { https : new ProxyAgent ( ) } ,
9987 method : flags . method ,
10088 headers : {
89+ ...SFDX_HTTP_HEADERS ,
10190 Authorization : `Bearer ${
10291 // we don't care about apiVersion here, just need to get the access token.
10392 // eslint-disable-next-line sf-plugin/get-connection-with-version
10493 org . getConnection ( ) . getConnectionOptions ( ) . accessToken !
10594 } `,
106- ...( flags . header ? Rest . getHeaders ( flags . header ) : { } ) ,
95+ ...headers ,
10796 } ,
108- body : flags . method === 'GET' ? undefined : flags . body ,
97+ body,
10998 throwHttpErrors : false ,
11099 followRedirect : false ,
111100 } ;
@@ -127,12 +116,10 @@ export class Rest extends SfCommand<void> {
127116
128117 // Print HTTP response status and headers.
129118 if ( flags . include ) {
130- let httpInfo = `HTTP/${ res . httpVersion } ${ res . statusCode } ${ EOL } ` ;
131-
132- for ( const [ header ] of Object . entries ( res . headers ) ) {
133- httpInfo += `${ ansis . blue . bold ( header ) } : ${ res . headers [ header ] as string } ${ EOL } ` ;
134- }
135- this . log ( httpInfo ) ;
119+ this . log ( `HTTP/${ res . httpVersion } ${ res . statusCode } ` ) ;
120+ Object . entries ( res . headers ) . map ( ( [ header , value ] ) => {
121+ this . log ( `${ ansis . blue . bold ( header ) } : ${ Array . isArray ( value ) ? value . join ( ',' ) : value ?? '<undefined>' } ` ) ;
122+ } ) ;
136123 }
137124
138125 try {
0 commit comments