@@ -14,6 +14,9 @@ import { SfError, Logger } from '@salesforce/core';
1414export type SeedResponse = {
1515 request_id : string ;
1616} ;
17+ export type ServletResponse = {
18+ jwt : string ;
19+ } ;
1720
1821export type PollSeedResponse = {
1922 execution_end_time : string ;
@@ -26,7 +29,8 @@ export type PollSeedResponse = {
2629
2730export type DataSeedingOperation = 'data-generation' | 'data-copy' ;
2831
29- const baseUrl = process . env . SF_DATA_SEEDING_URL ?? 'https://data-seed-scratchpad5.sfdc-3vx9f4.svc.sfdcfc.net' ;
32+ // TODO Change to SFAP Endpoint
33+ const baseUrl = process . env . SF_DATA_SEEDING_URL ?? 'https://data-seed-gid.sfdc-yfeipo.svc.sfdcfc.net' ;
3034const csrfUrl = `${ baseUrl } /get-csrf-token` ;
3135const seedUrl = `${ baseUrl } /data-seed` ;
3236const pollUrl = `${ baseUrl } /status` ;
@@ -44,23 +48,23 @@ export const getCsrfToken = (cookieJar: CookieJar): string => {
4448 return csrfToken ;
4549} ;
4650
47- export const initiateDataSeed = async ( config : string , operation : DataSeedingOperation ) : Promise < SeedResponse > => {
48- const cookieJar = await getCookieJar ( ) ;
49- const csrf = getCsrfToken ( cookieJar ) ;
50-
51+ export const initiateDataSeed = async ( config : string , operation : DataSeedingOperation , jwt : string ) : Promise < SeedResponse > => {
52+ //const cookieJar = await getCookieJar();
53+ //const csrf = getCsrfToken(cookieJar);
5154 const form = new FormData ( ) ;
5255 form . append ( 'config_file' , fs . createReadStream ( config ) ) ;
56+ // TODO : Remove credential file once SFAP is active and dataseed endpoint accepts orgurl and token
5357 form . append ( 'credentials_file' , fs . createReadStream ( 'ignore/credentials.txt' ) ) ;
5458 form . append ( 'operation' , operation ) ;
55-
5659 // TODO: Update to use .json() instead of JSON.parse once the Error response is changed to be JSON
5760 // Update the return type as well
5861 const response = await got . post ( seedUrl , {
5962 throwHttpErrors : false ,
60- cookieJar,
63+ // cookieJar,
6164 headers : {
6265 ...form . getHeaders ( ) ,
63- 'X-CSRFToken' : csrf ,
66+ // 'X-CSRFToken': csrf,
67+ 'Authorization' : 'Bearer ' + jwt ,
6468 } ,
6569 body : form ,
6670 } ) ;
@@ -72,6 +76,33 @@ export const initiateDataSeed = async (config: string, operation: DataSeedingOpe
7276 return JSON . parse ( response . body ) as SeedResponse ;
7377} ;
7478
79+ export const initiateJWTMint = async ( src_org_url : string , src_access_token : string , tgt_org_url : string , tgt_access_token : string ) : Promise < ServletResponse > => {
80+
81+ const src_servlet_url = src_org_url + '/dataseed/auth'
82+ const response_src = await got . post ( src_servlet_url , {
83+ throwHttpErrors : false ,
84+ headers : {
85+ 'Authorization' : 'Bearer ' + src_access_token ,
86+ } ,
87+ } ) ;
88+
89+ if ( response_src . statusCode !== 200 ) {
90+ const tgt_servlet_url = tgt_org_url + '/dataseed/auth'
91+ const response_tgt = await got . post ( tgt_servlet_url , {
92+ throwHttpErrors : false ,
93+ headers : {
94+ 'Authorization' : 'Bearer ' + tgt_access_token ,
95+ } ,
96+ } ) ;
97+ if ( response_tgt . statusCode !== 200 ) {
98+ throw new SfError ( `Org permission for data seed not found in source & target org.\nSource Response: Error Code : ${ response_src . statusCode } - ${ response_src . body } . \nTarget Response: Error Code : ${ response_tgt . statusCode } - ${ response_tgt . body } ` ) ;
99+ }
100+ return JSON . parse ( response_tgt . body ) as ServletResponse ;
101+ }
102+
103+ return JSON . parse ( response_src . body ) as ServletResponse ;
104+ } ;
105+
75106export const pollSeedStatus = async ( jobId : string ) : Promise < PollSeedResponse > => {
76107 const logger = await Logger . child ( 'PollSeedStatus' ) ;
77108
0 commit comments