77
88import fs from 'node:fs' ;
99import got from 'got' ;
10- import { CookieJar } from 'tough-cookie' ;
1110import FormData from 'form-data' ;
1211import { SfError , Logger } from '@salesforce/core' ;
1312
@@ -17,6 +16,10 @@ export type SeedResponse = {
1716export type ServletResponse = {
1817 jwt : string ;
1918} ;
19+ export type AuthServletResponse = {
20+ statusCode : string ;
21+ body : string ;
22+ } ;
2023
2124export type PollSeedResponse = {
2225 execution_end_time : string ;
@@ -29,46 +32,33 @@ export type PollSeedResponse = {
2932
3033export type DataSeedingOperation = 'data-generation' | 'data-copy' ;
3134
32- // TODO Change to SFAP Endpoint
33- const baseUrl = process . env . SF_DATA_SEEDING_URL ?? 'https://data-seed-gid.sfdc-yfeipo.svc.sfdcfc.net' ;
34- const csrfUrl = `${ baseUrl } /get-csrf-token` ;
35+ const baseUrl = 'https://api.salesforce.com/platform/data-seed/v1' ;
3536const seedUrl = `${ baseUrl } /data-seed` ;
3637const pollUrl = `${ baseUrl } /status` ;
3738
38- export const getCookieJar = async ( ) : Promise < CookieJar > => {
39- const cookieJar = new CookieJar ( ) ;
40- await got ( csrfUrl , { cookieJar } ) ;
41- return cookieJar ;
42- } ;
43-
44- export const getCsrfToken = ( cookieJar : CookieJar ) : string => {
45- const csrfToken = cookieJar . getCookiesSync ( csrfUrl ) . find ( ( cookie ) => cookie . key === 'csrf_token' ) ?. value ;
46- if ( ! csrfToken ) throw new SfError ( 'Failed to obtain CSRF token' ) ;
47-
48- return csrfToken ;
49- } ;
50-
5139export const initiateDataSeed = async (
5240 config : string ,
5341 operation : DataSeedingOperation ,
54- jwt : string
42+ jwt : string ,
43+ srcOrgUrl : string ,
44+ srcAccessToken : string ,
45+ tgtOrgUrl : string ,
46+ tgtAccessToken : string
5547) : Promise < SeedResponse > => {
56- // const cookieJar = await getCookieJar();
57- // const csrf = getCsrfToken(cookieJar);
5848 const form = new FormData ( ) ;
5949 form . append ( 'config_file' , fs . createReadStream ( config ) ) ;
60- // TODO : Remove credential file once SFAP is active and dataseed endpoint accepts orgurl and token
61- form . append ( 'credentials_file' , fs . createReadStream ( 'ignore/credentials.txt' ) ) ;
6250 form . append ( 'operation' , operation ) ;
51+ form . append ( 'source_access_token' , srcAccessToken ) ;
52+ form . append ( 'source_instance_url' , srcOrgUrl ) ;
53+ form . append ( 'target_access_token' , tgtAccessToken ) ;
54+ form . append ( 'target_instance_url' , tgtOrgUrl ) ;
6355 // TODO: Update to use .json() instead of JSON.parse once the Error response is changed to be JSON
6456 // Update the return type as well
6557 const response = await got . post ( seedUrl , {
6658 throwHttpErrors : false ,
67- // cookieJar,
6859 headers : {
6960 ...form . getHeaders ( ) ,
70- // 'X-CSRFToken': csrf,
71- Authorization : 'Bearer ' + jwt ,
61+ Authorization : `Bearer ${ jwt } ` ,
7262 } ,
7363 body : form ,
7464 } ) ;
@@ -86,31 +76,38 @@ export const initiateJWTMint = async (
8676 tgtOrgUrl : string ,
8777 tgtAccessToken : string
8878) : Promise < ServletResponse > => {
89- const srcServletUrl = srcOrgUrl + ' /dataseed/auth' ;
90- const responseSrc = await got . post ( srcServletUrl , {
91- throwHttpErrors : false ,
92- headers : {
93- Authorization : 'Bearer ' + srcAccessToken ,
94- } ,
95- } ) ;
79+ const srcServletUrl = ` ${ srcOrgUrl } /dataseed/auth` ;
80+ const tgtServletUrl = ` ${ tgtOrgUrl } /dataseed/auth` ;
81+
82+ const [ responseSrc , responseTgt ] = await Promise . all ( [
83+ callAuthServlet ( srcServletUrl , srcAccessToken ) ,
84+ callAuthServlet ( tgtServletUrl , tgtAccessToken ) ,
85+ ] ) ;
9686
97- if ( responseSrc . statusCode !== 200 ) {
98- const tgtServletUrl = tgtOrgUrl + '/dataseed/auth' ;
99- const responseTgt = await got . post ( tgtServletUrl , {
100- throwHttpErrors : false ,
101- headers : {
102- Authorization : 'Bearer ' + tgtAccessToken ,
103- } ,
104- } ) ;
105- if ( responseTgt . statusCode !== 200 ) {
106- throw new SfError (
107- `Org permission for data seed not found in source & target org.\nSource Response: Error Code : ${ responseSrc . statusCode } - ${ responseSrc . body } . \nTarget Response: Error Code : ${ responseTgt . statusCode } - ${ responseTgt . body } `
108- ) ;
109- }
87+ if ( responseSrc . statusCode === '200' ) {
88+ return JSON . parse ( responseSrc . body ) as ServletResponse ;
89+ }
90+
91+ if ( responseTgt . statusCode === '200' ) {
11092 return JSON . parse ( responseTgt . body ) as ServletResponse ;
11193 }
11294
113- return JSON . parse ( responseSrc . body ) as ServletResponse ;
95+ throw new SfError (
96+ `Org permission for data seed not found in either the source or target org.\nSource Response: Error Code : ${ responseSrc . statusCode } - ${ responseSrc . body } . \nTarget Response: Error Code : ${ responseTgt . statusCode } - ${ responseTgt . body } `
97+ ) ;
98+ } ;
99+
100+ const callAuthServlet = async ( url : string , accessToken : string ) : Promise < AuthServletResponse > => {
101+ const response = await got . post ( url , {
102+ throwHttpErrors : false ,
103+ headers : {
104+ Authorization : `Bearer ${ accessToken } ` ,
105+ } ,
106+ } ) ;
107+ return {
108+ statusCode : response . statusCode . toString ( ) , // Convert to string
109+ body : response . body ,
110+ } ;
114111} ;
115112
116113export const pollSeedStatus = async ( jobId : string ) : Promise < PollSeedResponse > => {
0 commit comments