Skip to content

Commit 39988e1

Browse files
chore: default to services/data/vXX.0
1 parent 594bd27 commit 39988e1

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/commands/api/request/rest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Rest extends SfCommand<void> {
2525
public static enableJsonFlag = false;
2626
public static readonly flags = {
2727
'target-org': Flags.requiredOrg(),
28+
'api-version': Flags.orgApiVersion(),
2829
include: Flags.boolean({
2930
char: 'i',
3031
summary: messages.getMessage('flags.include.summary'),
@@ -70,7 +71,14 @@ export class Rest extends SfCommand<void> {
7071
const streamFile = flags['stream-to-file'];
7172
const headers = flags.header ? getHeaders(flags.header) : {};
7273

73-
const url = new URL(`${org.getField<string>(Org.Fields.INSTANCE_URL)}/${args.endpoint}`);
74+
// replace first '/' to create valid URL
75+
const endpoint = args.endpoint.startsWith('/') ? args.endpoint.replace('/', '') : args.endpoint;
76+
const url = new URL(
77+
`${org.getField<string>(Org.Fields.INSTANCE_URL)}/services/data/v${
78+
flags['api-version'] ?? (await org.retrieveMaxApiVersion())
79+
}/${endpoint}`
80+
);
81+
7482
const body =
7583
flags.method === 'GET'
7684
? undefined

test/commands/api/request/rest.nut.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ skipIfWindows('api:request:rest NUT', () => {
4848

4949
describe('std out', () => {
5050
it('get result in json format', () => {
51-
const result = execCmd("api request rest 'services/data/v60.0/limits'").shellOutput.stdout;
51+
const result = execCmd("api request rest 'limits'").shellOutput.stdout;
5252

5353
// make sure we got a JSON object back
5454
expect(Object.keys(JSON.parse(result) as Record<string, unknown>)).to.have.length;
5555
});
5656

5757
it('should pass headers', () => {
58-
const result = execCmd("api request rest 'services/data/v60.0/limits' -H 'Accept: application/xml'").shellOutput
59-
.stdout;
58+
const result = execCmd("api request rest 'limits' -H 'Accept: application/xml'").shellOutput.stdout;
6059

6160
// the headers will change this to xml
6261
expect(result.startsWith('<?xml version="1.0" encoding="UTF-8"?><LimitsSnapshot>')).to.be.true;
@@ -65,8 +64,7 @@ skipIfWindows('api:request:rest NUT', () => {
6564

6665
describe('stream-to-file', () => {
6766
it('get result in json format', () => {
68-
const result = execCmd("api request rest 'services/data/v60.0/limits' --stream-to-file out.txt").shellOutput
69-
.stdout;
67+
const result = execCmd("api request rest 'limits' --stream-to-file out.txt").shellOutput.stdout;
7068

7169
expect(result.trim()).to.equal('File saved to out.txt');
7270

@@ -76,9 +74,8 @@ skipIfWindows('api:request:rest NUT', () => {
7674
});
7775

7876
it('should pass headers', () => {
79-
const result = execCmd(
80-
"api request rest 'services/data/v60.0/limits' -H 'Accept: application/xml' --stream-to-file out.txt"
81-
).shellOutput.stdout;
77+
const result = execCmd("api request rest 'limits' -H 'Accept: application/xml' --stream-to-file out.txt")
78+
.shellOutput.stdout;
8279

8380
expect(result.trim()).to.equal('File saved to out.txt');
8481

test/commands/api/request/rest.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,22 @@ describe('rest', () => {
4343
it('should request org limits and default to "GET" HTTP method', async () => {
4444
nock(testOrg.instanceUrl).get('/services/data/v56.0/limits').reply(200, orgLimitsResponse);
4545

46-
await Rest.run(['services/data/v56.0/limits', '--target-org', '[email protected]']);
46+
await Rest.run(['--api-version', '56.0', 'limits', '--target-org', '[email protected]']);
47+
48+
expect(uxStub.styledJSON.args[0][0]).to.deep.equal(orgLimitsResponse);
49+
});
50+
51+
it("should strip leading '/'", async () => {
52+
nock(testOrg.instanceUrl).get('/services/data/v56.0/limits').reply(200, orgLimitsResponse);
53+
54+
await Rest.run(['--api-version', '56.0', '/limits', '--target-org', '[email protected]']);
4755

4856
expect(uxStub.styledJSON.args[0][0]).to.deep.equal(orgLimitsResponse);
4957
});
5058

5159
it('should throw error for invalid header args', async () => {
5260
try {
53-
await Rest.run(['services/data/v56.0/limits', '--target-org', '[email protected]', '-H', 'myInvalidHeader']);
61+
await Rest.run(['limits', '--target-org', '[email protected]', '-H', 'myInvalidHeader']);
5462
assert.fail('the above should throw');
5563
} catch (e) {
5664
expect((e as SfError).name).to.equal('Failed To Parse HTTP Header');
@@ -64,7 +72,15 @@ describe('rest', () => {
6472
it('should redirect to file', async () => {
6573
nock(testOrg.instanceUrl).get('/services/data/v56.0/limits').reply(200, orgLimitsResponse);
6674
const writeSpy = $$.SANDBOX.stub(process.stdout, 'write');
67-
await Rest.run(['services/data/v56.0/limits', '--target-org', '[email protected]', '--stream-to-file', 'myOutput.txt']);
75+
await Rest.run([
76+
'--api-version',
77+
'56.0',
78+
'limits',
79+
'--target-org',
80+
81+
'--stream-to-file',
82+
'myOutput.txt',
83+
]);
6884

6985
// gives it a second to resolve promises and close streams before we start asserting
7086
await sleep(1000);
@@ -93,10 +109,20 @@ describe('rest', () => {
93109
accept: 'application/xml',
94110
},
95111
})
96-
.get('/services/data')
112+
.get('/services/data/v42.0/')
97113
.reply(200, xmlRes);
98114

99-
await Rest.run(['services/data', '--header', 'Accept: application/xml', '--target-org', '[email protected]']);
115+
await Rest.run([
116+
'',
117+
'--api-version',
118+
'42.0',
119+
'--method',
120+
'GET',
121+
'--header',
122+
'Accept: application/xml',
123+
'--target-org',
124+
125+
]);
100126

101127
const output = stripAnsi(writeSpy.args.flat().join(''));
102128

@@ -126,7 +152,7 @@ describe('rest', () => {
126152
location: `${testOrg.instanceUrl}/services/data/v56.0/limits`,
127153
});
128154

129-
await Rest.run(['services/data/v56.0/limites', '--target-org', '[email protected]']);
155+
await Rest.run(['limites', '--api-version', '56.0', '--target-org', '[email protected]']);
130156

131157
expect(uxStub.styledJSON.args[0][0]).to.deep.equal(orgLimitsResponse);
132158
});

0 commit comments

Comments
 (0)