7
7
8
8
import fs from 'node:fs' ;
9
9
import got from 'got' ;
10
- import { CookieJar } from 'tough-cookie' ;
11
10
import FormData from 'form-data' ;
12
11
import { SfError , Logger } from '@salesforce/core' ;
13
12
@@ -17,6 +16,10 @@ export type SeedResponse = {
17
16
export type ServletResponse = {
18
17
jwt : string ;
19
18
} ;
19
+ export type AuthServletResponse = {
20
+ statusCode : string ;
21
+ body : string ;
22
+ } ;
20
23
21
24
export type PollSeedResponse = {
22
25
execution_end_time : string ;
@@ -29,46 +32,33 @@ export type PollSeedResponse = {
29
32
30
33
export type DataSeedingOperation = 'data-generation' | 'data-copy' ;
31
34
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' ;
35
36
const seedUrl = `${ baseUrl } /data-seed` ;
36
37
const pollUrl = `${ baseUrl } /status` ;
37
38
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
-
51
39
export const initiateDataSeed = async (
52
40
config : string ,
53
41
operation : DataSeedingOperation ,
54
- jwt : string
42
+ jwt : string ,
43
+ srcOrgUrl : string ,
44
+ srcAccessToken : string ,
45
+ tgtOrgUrl : string ,
46
+ tgtAccessToken : string
55
47
) : Promise < SeedResponse > => {
56
- // const cookieJar = await getCookieJar();
57
- // const csrf = getCsrfToken(cookieJar);
58
48
const form = new FormData ( ) ;
59
49
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' ) ) ;
62
50
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 ) ;
63
55
// TODO: Update to use .json() instead of JSON.parse once the Error response is changed to be JSON
64
56
// Update the return type as well
65
57
const response = await got . post ( seedUrl , {
66
58
throwHttpErrors : false ,
67
- // cookieJar,
68
59
headers : {
69
60
...form . getHeaders ( ) ,
70
- // 'X-CSRFToken': csrf,
71
- Authorization : 'Bearer ' + jwt ,
61
+ Authorization : `Bearer ${ jwt } ` ,
72
62
} ,
73
63
body : form ,
74
64
} ) ;
@@ -86,31 +76,38 @@ export const initiateJWTMint = async (
86
76
tgtOrgUrl : string ,
87
77
tgtAccessToken : string
88
78
) : 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
+ ] ) ;
96
86
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' ) {
110
92
return JSON . parse ( responseTgt . body ) as ServletResponse ;
111
93
}
112
94
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
+ } ;
114
111
} ;
115
112
116
113
export const pollSeedStatus = async ( jobId : string ) : Promise < PollSeedResponse > => {
0 commit comments