Skip to content

Commit 03f48eb

Browse files
allisonc07adolfoportilla
authored andcommitted
feat: add single select v1.2 params (#47)
* chore: add params for single select v1.2 * chore: add test to ^ coverage * chore: prepare release * chore: fix rebase issues * chore: add single_select=true as well when obj is used * chore: run prepare release * chore: update documentation * chore: update docs * chore: move null check for clarity * chore: add a test
1 parent cb80bd3 commit 03f48eb

File tree

6 files changed

+125
-18
lines changed

6 files changed

+125
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ npm install @smartcar/auth
2424
### Smartcar CDN
2525

2626
```html
27-
<script src="https://javascript-sdk.smartcar.com/2.3.0/sdk.js"></script>
27+
<script src="https://javascript-sdk.smartcar.com/2.4.0/sdk.js"></script>
2828
```
2929

3030
Before v2.2.0, the SDK was versioned as follows:
@@ -192,4 +192,4 @@ https://application-backend.com/page?error=access_denied&error_description=User+
192192
[tag-image]: https://img.shields.io/github/tag/smartcar/javascript-sdk.svg
193193

194194
<!-- Please do not modify or remove this, it is used by the build process -->
195-
[version]: 2.3.0
195+
[version]: 2.4.0

doc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Generates Smartcar OAuth URL.
6363
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
6464
| [options.forcePrompt] | <code>Boolean</code> | <code>false</code> | force permission approval screen to show on every authentication, even if the user has previously consented to the exact scope of permission |
6565
| [options.vehicleInfo.make] | <code>String</code> | | `vehicleInfo` is an object with an optional property `make`, which allows users to bypass the car brand selection screen. For a complete list of supported brands, please see our [API Reference](https://smartcar.com/docs/api#authorization) documentation. |
66-
| [options.singleSelect] | <code>Boolean</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. Defaults to `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
66+
| [options.singleSelect] | <code>Boolean</code> \| <code>Object</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. If `single_select` is passed in as an object with the property `vin`, Smartcar will only authorize the vehicle with the specified VIN. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
6767

6868
**Example**
6969
```js
@@ -89,7 +89,7 @@ Launches Smartcar Connect in a new window.
8989
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
9090
| [options.forcePrompt] | <code>Boolean</code> | <code>false</code> | force permission approval screen to show on every authentication, even if the user has previously consented to the exact scope of permission |
9191
| [options.vehicleInfo.make] | <code>String</code> | | `vehicleInfo` is an object with an optional property `make`, which allows users to bypass the car brand selection screen. For a complete list of supported makes, please see our [API Reference](https://smartcar.com/docs/api#authorization) documentation. |
92-
| [options.singleSelect] | <code>Boolean</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. Defaults to `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
92+
| [options.singleSelect] | <code>Boolean</code> \| <code>Object</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. If `single_select` is passed in as an object with the property `vin`, Smartcar will only authorize the vehicle with the specified VIN. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
9393

9494
<a name="Smartcar+addClickHandler"></a>
9595

@@ -107,7 +107,7 @@ On-click event calls openDialog when the specified element is clicked.
107107
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
108108
| [options.forcePrompt] | <code>Boolean</code> | <code>false</code> | force permission approval screen to show on every authentication, even if the user has previously consented to the exact scope of permission |
109109
| [options.vehicleInfo.make] | <code>String</code> | | `vehicleInfo` is an object with an optional property `make`, which allows users to bypass the car brand selection screen. For a complete list of supported makes, please see our [API Reference](https://smartcar.com/docs/api#authorization) documentation. |
110-
| [options.singleSelect] | <code>Boolean</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. Defaults to `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
110+
| [options.singleSelect] | <code>Boolean</code> \| <code>Object</code> | | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. If `single_select` is passed in as an object with the property `vin`, Smartcar will only authorize the vehicle with the specified VIN. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
111111

112112
<a name="Smartcar.AccessDenied"></a>
113113

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smartcar/auth",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "javascript auth sdk for the smartcar",
55
"main": "dist/npm/sdk.js",
66
"license": "MIT",

src/sdk.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ class Smartcar {
210210
* car brand selection screen. For a complete list of supported brands, please
211211
* see our [API Reference](https://smartcar.com/docs/api#authorization)
212212
* documentation.
213-
* @param {Boolean} [options.singleSelect] - An optional value that sets the
213+
* @param {Boolean|Object} [options.singleSelect] - An optional value that sets the
214214
* behavior of the grant dialog displayed to the user. If set to `true`,
215-
* `single_select` limits the user to selecting only one vehicle. Defaults to
216-
* `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/)
215+
* `single_select` limits the user to selecting only one vehicle. If `single_select`
216+
* is passed in as an object with the property `vin`, Smartcar will only authorize
217+
* the vehicle with the specified VIN. See the
218+
* [Single Select guide](https://smartcar.com/docs/guides/single-select/)
217219
* for more information.
218220
*
219221
* @return {String} Connect URL to redirect user to.
@@ -249,8 +251,24 @@ class Smartcar {
249251

250252
link += `&mode=${this.mode}`;
251253

252-
if (options.singleSelect !== undefined) {
253-
link += `&single_select=${options.singleSelect === true}`;
254+
if (options.singleSelect !== undefined && options.singleSelect !== null) {
255+
let singleSelectParamAdded = false;
256+
if (typeof options.singleSelect === 'object') {
257+
const availableParams = ['vin'];
258+
for (const param of availableParams) {
259+
if (param in options.singleSelect) {
260+
link += `&single_select_${param}=${options.singleSelect[param]}`;
261+
singleSelectParamAdded = true;
262+
}
263+
}
264+
if (!singleSelectParamAdded) {
265+
link += '&single_select=false';
266+
} else {
267+
link += '&single_select=true';
268+
}
269+
} else {
270+
link += `&single_select=${options.singleSelect === true}`;
271+
}
254272
}
255273

256274
if (options.state) {
@@ -282,10 +300,12 @@ class Smartcar {
282300
* car brand selection screen. For a complete list of supported makes, please
283301
* see our [API Reference](https://smartcar.com/docs/api#authorization)
284302
* documentation.
285-
* @param {Boolean} [options.singleSelect] - An optional value that sets the
303+
* @param {Boolean|Object} [options.singleSelect] - An optional value that sets the
286304
* behavior of the grant dialog displayed to the user. If set to `true`,
287-
* `single_select` limits the user to selecting only one vehicle. Defaults to
288-
* `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/)
305+
* `single_select` limits the user to selecting only one vehicle. If `single_select`
306+
* is passed in as an object with the property `vin`, Smartcar will only authorize
307+
* the vehicle with the specified VIN. See the
308+
* [Single Select guide](https://smartcar.com/docs/guides/single-select/)
289309
* for more information.
290310
*/
291311
openDialog(options) {
@@ -310,10 +330,12 @@ class Smartcar {
310330
* car brand selection screen. For a complete list of supported makes, please
311331
* see our [API Reference](https://smartcar.com/docs/api#authorization)
312332
* documentation.
313-
* @param {Boolean} [options.singleSelect] - An optional value that sets the
333+
* @param {Boolean|Object} [options.singleSelect] - An optional value that sets the
314334
* behavior of the grant dialog displayed to the user. If set to `true`,
315-
* `single_select` limits the user to selecting only one vehicle. Defaults to
316-
* `false`. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/)
335+
* `single_select` limits the user to selecting only one vehicle. If `single_select`
336+
* is passed in as an object with the property `vin`, Smartcar will only authorize
337+
* the vehicle with the specified VIN. See the
338+
* [Single Select guide](https://smartcar.com/docs/guides/single-select/)
317339
* for more information.
318340
*/
319341
addClickHandler(options) {

test/unit/sdk.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,91 @@ describe('sdk', () => {
742742
expect(link).toEqual(expectedLink);
743743
});
744744

745+
test('Adds single_select_vin=12345 when singleSelect included as an object', () => {
746+
const options = {
747+
clientId: 'clientId',
748+
redirectUri: 'https://smartcar.com',
749+
scope: ['read_vehicle_info', 'read_odometer'],
750+
onComplete: jest.fn(),
751+
testMode: false,
752+
};
753+
754+
const smartcar = new Smartcar(options);
755+
756+
const expectedLink =
757+
'https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=live&single_select_vin=12345&single_select=true&state=foobarbaz&make=TESLA';
758+
759+
const link = smartcar.getAuthUrl({
760+
state: 'foobarbaz',
761+
forcePrompt: true,
762+
vehicleInfo: {
763+
make: 'TESLA',
764+
},
765+
singleSelect: {
766+
vin: '12345',
767+
},
768+
});
769+
770+
expect(link).toEqual(expectedLink);
771+
});
772+
773+
test('Ignores junk properties when singleSelect is included as an object', () => {
774+
const options = {
775+
clientId: 'clientId',
776+
redirectUri: 'https://smartcar.com',
777+
scope: ['read_vehicle_info', 'read_odometer'],
778+
onComplete: jest.fn(),
779+
testMode: false,
780+
};
781+
782+
const smartcar = new Smartcar(options);
783+
784+
const expectedLink =
785+
'https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=live&single_select_vin=vin&single_select=true&state=foobarbaz&make=TESLA';
786+
787+
const link = smartcar.getAuthUrl({
788+
state: 'foobarbaz',
789+
forcePrompt: true,
790+
vehicleInfo: {
791+
make: 'TESLA',
792+
},
793+
singleSelect: {
794+
pizza: '12345',
795+
vin: 'vin',
796+
},
797+
});
798+
799+
expect(link).toEqual(expectedLink);
800+
});
801+
802+
test('Ignores junk properties when singleSelect is included as an object with only junk properties', () => {
803+
const options = {
804+
clientId: 'clientId',
805+
redirectUri: 'https://smartcar.com',
806+
scope: ['read_vehicle_info', 'read_odometer'],
807+
onComplete: jest.fn(),
808+
testMode: false,
809+
};
810+
811+
const smartcar = new Smartcar(options);
812+
813+
const expectedLink =
814+
'https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=live&single_select=false&state=foobarbaz&make=TESLA';
815+
816+
const link = smartcar.getAuthUrl({
817+
state: 'foobarbaz',
818+
forcePrompt: true,
819+
vehicleInfo: {
820+
make: 'TESLA',
821+
},
822+
singleSelect: {
823+
pizza: '12345',
824+
},
825+
});
826+
827+
expect(link).toEqual(expectedLink);
828+
});
829+
745830
test('Sets single_select=false with junk values passed to singleSelect', () => {
746831
const options = {
747832
clientId: 'clientId',

0 commit comments

Comments
 (0)