Skip to content

Commit 4a9094e

Browse files
authored
feat: use jsforce v2 (#457)
* feat: use jsforce v2 * chore: remove typeRoots * chore: temporarily disable deploy-retrieve-metadata nuts * chore: pin jsforce to 2.0.0-beta.7 * chore: reenable deploy-retrieve-metadata nuts * chore: run deploy-retrieve-metadtata nuts on main
1 parent 72856d2 commit 4a9094e

29 files changed

+588
-868
lines changed

.circleci/config.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ jobs:
5353
- release-management/install-sfdx:
5454
version: <<parameters.sfdx_version>>
5555
os: <<parameters.os>>
56-
- run: git clone <<parameters.external_project_git_url>> $(pwd)
56+
- run:
57+
name: 'Clone repo'
58+
command: |
59+
url=$(echo <<parameters.external_project_git_url>> | cut -d "#" -f 1)
60+
branch=main
61+
if [[ "<<parameters.external_project_git_url>>" == *"#"* ]]; then
62+
branch=$(echo <<parameters.external_project_git_url>> | cut -d "#" -f 2)
63+
fi
64+
echo URL: $url
65+
echo BRANCH: $branch
66+
git clone -b $branch $url $(pwd)
5767
- run:
5868
name: Install dependencies
5969
command: yarn
@@ -68,9 +78,9 @@ jobs:
6878
yarn build
6979
working_directory: node_modules/@salesforce/core
7080
- run:
71-
name: remove command/core
72-
# deletes command/core to prevent ts conflicts
73-
command: rm -rf node_modules/\@salesforce/command/node_modules/\@salesforce/core
81+
name: remove sf-plugins-core/core
82+
# deletes sf-plugins-core/core to prevent ts conflicts
83+
command: rm -rf node_modules/\@salesforce/sf-plugins-core/node_modules/\@salesforce/core
7484
- run:
7585
name: Build
7686
command: |
@@ -122,8 +132,9 @@ workflows:
122132
node_version: [lts]
123133
external_project_git_url:
124134
[
135+
'https://github.com/salesforcecli/plugin-config#v2',
125136
'https://github.com/salesforcecli/plugin-env',
126-
'https://github.com/salesforcecli/plugin-login',
137+
'https://github.com/salesforcecli/plugin-login#mdonnalley/jsforce',
127138
'https://github.com/salesforcecli/plugin-deploy-retrieve',
128139
'https://github.com/salesforcecli/plugin-deploy-retrieve-metadata',
129140
]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@
4848
"@types/mkdirp": "^1.0.1",
4949
"change-case": "^4.1.2",
5050
"debug": "^3.1.0",
51+
"form-data": "^4.0.0",
5152
"graceful-fs": "^4.2.4",
5253
"jsen": "0.6.6",
53-
"jsforce": "^1.10.1",
54+
"jsforce": "2.0.0-beta.7",
5455
"jsonwebtoken": "8.5.0",
5556
"mkdirp": "1.0.4",
5657
"sfdx-faye": "^1.0.9",

src/deviceOauthService.ts

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
/* eslint-disable camelcase */
88
/* eslint-disable @typescript-eslint/ban-types */
99

10-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
11-
// @ts-ignore
12-
import * as Transport from 'jsforce/lib/transport';
10+
import { URLSearchParams } from 'url';
11+
import Transport from 'jsforce/lib/transport';
1312
import { AsyncCreatable, Duration, parseJsonMap } from '@salesforce/kit';
14-
import { OAuth2Options } from 'jsforce';
13+
import { OAuth2Config } from 'jsforce/lib/oauth2';
14+
import { HttpRequest } from 'jsforce';
1515
import { Nullable, ensureString, JsonMap } from '@salesforce/ts-types';
1616
import { Logger } from './logger';
1717
import { AuthInfo, DEFAULT_CONNECTED_APP_INFO } from './org/authInfo';
@@ -40,35 +40,13 @@ export interface DeviceCodePollingResponse extends JsonMap {
4040
issued_at: string;
4141
}
4242

43-
interface DeviceLoginOptions {
44-
url: string;
45-
headers: object;
46-
method: string;
47-
form: {
48-
client_id: string;
49-
response_type: string;
50-
scope: string;
51-
};
52-
}
53-
54-
interface DevicePollingOptions {
55-
url: string;
56-
headers: object;
57-
method: string;
58-
form: {
59-
client_id: string;
60-
grant_type: string;
61-
code: string;
62-
};
63-
}
64-
6543
async function wait(ms = 1000): Promise<void> {
6644
return new Promise((resolve) => {
6745
setTimeout(resolve, ms);
6846
});
6947
}
7048

71-
async function makeRequest<T extends JsonMap>(options: DeviceLoginOptions | DevicePollingOptions): Promise<T> {
49+
async function makeRequest<T extends JsonMap>(options: HttpRequest): Promise<T> {
7250
const rawResponse = await new Transport().httpRequest(options);
7351
const response = parseJsonMap<T>(rawResponse.body);
7452
if (response.error) {
@@ -96,17 +74,17 @@ async function makeRequest<T extends JsonMap>(options: DeviceLoginOptions | Devi
9674
* const authInfo = await deviceOauthService.authorizeAndSave(approval);
9775
* ```
9876
*/
99-
export class DeviceOauthService extends AsyncCreatable<OAuth2Options> {
77+
export class DeviceOauthService extends AsyncCreatable<OAuth2Config> {
10078
public static RESPONSE_TYPE = 'device_code';
10179
public static GRANT_TYPE = 'device';
10280
public static SCOPE = 'refresh_token web api';
10381
private static POLLING_COUNT_MAX = 100;
10482

10583
private logger!: Logger;
106-
private options: OAuth2Options;
84+
private options: OAuth2Config;
10785
private pollingCount = 0;
10886

109-
public constructor(options: OAuth2Options) {
87+
public constructor(options: OAuth2Config) {
11088
super(options);
11189
this.options = options;
11290
if (!this.options.clientId) this.options.clientId = DEFAULT_CONNECTED_APP_INFO.clientId;
@@ -161,42 +139,42 @@ export class DeviceOauthService extends AsyncCreatable<OAuth2Options> {
161139
this.logger.debug(`this.options.loginUrl: ${this.options.loginUrl}`);
162140
}
163141

164-
private getLoginOptions(url: string): DeviceLoginOptions {
142+
private getLoginOptions(url: string): HttpRequest {
143+
const body = new URLSearchParams();
144+
body.append('client_id', ensureString(this.options.clientId));
145+
body.append('response_type', DeviceOauthService.RESPONSE_TYPE);
146+
body.append('scope', DeviceOauthService.SCOPE);
165147
return {
166148
url,
167149
headers: SFDX_HTTP_HEADERS,
168150
method: 'POST',
169-
form: {
170-
client_id: ensureString(this.options.clientId),
171-
response_type: DeviceOauthService.RESPONSE_TYPE,
172-
scope: DeviceOauthService.SCOPE,
173-
},
151+
body,
174152
};
175153
}
176154

177-
private getPollingOptions(url: string, code: string): DevicePollingOptions {
155+
private getPollingOptions(url: string, code: string): HttpRequest {
156+
const body = new URLSearchParams();
157+
body.append('client_id', ensureString(this.options.clientId));
158+
body.append('grant_type', DeviceOauthService.GRANT_TYPE);
159+
body.append('code', code);
178160
return {
179161
url,
180162
headers: SFDX_HTTP_HEADERS,
181163
method: 'POST',
182-
form: {
183-
code,
184-
grant_type: DeviceOauthService.GRANT_TYPE,
185-
client_id: ensureString(this.options.clientId),
186-
},
164+
body,
187165
};
188166
}
189167

190168
private getDeviceFlowRequestUrl(): string {
191169
return `${ensureString(this.options.loginUrl)}/services/oauth2/token`;
192170
}
193171

194-
private async poll(pollingOptions: DevicePollingOptions): Promise<Nullable<DeviceCodePollingResponse>> {
172+
private async poll(httpRequest: HttpRequest): Promise<Nullable<DeviceCodePollingResponse>> {
195173
this.logger.debug(
196174
`polling for device approval (attempt ${this.pollingCount} of ${DeviceOauthService.POLLING_COUNT_MAX})`
197175
);
198176
try {
199-
return await makeRequest<DeviceCodePollingResponse>(pollingOptions);
177+
return await makeRequest<DeviceCodePollingResponse>(httpRequest);
200178
} catch (e) {
201179
const err = e.data;
202180
if (err.error && err.status === 400 && err.error === 'authorization_pending') {
@@ -218,13 +196,13 @@ export class DeviceOauthService extends AsyncCreatable<OAuth2Options> {
218196
}
219197

220198
private async pollForDeviceApproval(
221-
pollingOptions: DevicePollingOptions,
199+
httpRequest: HttpRequest,
222200
interval: number
223201
): Promise<Nullable<DeviceCodePollingResponse>> {
224202
this.logger.debug('BEGIN POLLING FOR DEVICE APPROVAL');
225203
let result: Nullable<DeviceCodePollingResponse>;
226204
while (this.shouldContinuePolling()) {
227-
result = await this.poll(pollingOptions);
205+
result = await this.poll(httpRequest);
228206
if (result) {
229207
this.logger.debug('POLLING FOR DEVICE APPROVAL SUCCESS');
230208
break;

src/exported.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { Messages } from './messages';
99
Messages.importMessagesDirectory(__dirname);
1010

11-
export { OAuth2Options } from 'jsforce';
11+
export { OAuth2Config } from 'jsforce';
1212

1313
export { ConfigFile } from './config/configFile';
1414

@@ -32,7 +32,7 @@ export {
3232

3333
export { ConfigInfo, ConfigAggregator } from './config/configAggregator';
3434

35-
export { AuthFields, AuthInfo, OAuth2WithVerifier, OrgAuthorization } from './org/authInfo';
35+
export { AuthFields, AuthInfo, OrgAuthorization } from './org/authInfo';
3636

3737
export { AuthRemover } from './org/authRemover';
3838

@@ -73,11 +73,9 @@ export { SchemaValidator } from './schema/validator';
7373

7474
export { SfdxError } from './sfdxError';
7575

76-
export { StatusResult } from './status/client';
77-
7876
export { PollingClient } from './status/pollingClient';
7977

80-
export { CometClient, CometSubscription, StreamingClient } from './status/streamingClient';
78+
export { CometClient, CometSubscription, StreamingClient, StatusResult } from './status/streamingClient';
8179

8280
export { MyDomainResolver } from './status/myDomainResolver';
8381

0 commit comments

Comments
 (0)