Skip to content

Commit

Permalink
feat: add single select v1.2 params (#47)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
allisonc07 authored and adolfoportilla committed Jul 30, 2019
1 parent cb80bd3 commit 03f48eb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install @smartcar/auth
### Smartcar CDN

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

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

<!-- Please do not modify or remove this, it is used by the build process -->
[version]: 2.3.0
[version]: 2.4.0
6 changes: 3 additions & 3 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Generates Smartcar OAuth URL.
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
| [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 |
| [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. |
| [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. |
| [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. |

**Example**
```js
Expand All @@ -89,7 +89,7 @@ Launches Smartcar Connect in a new window.
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
| [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 |
| [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. |
| [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. |
| [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. |

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

Expand All @@ -107,7 +107,7 @@ On-click event calls openDialog when the specified element is clicked.
| [options.state] | <code>String</code> | | arbitrary state passed to redirect uri |
| [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 |
| [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. |
| [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. |
| [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. |

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

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@smartcar/auth",
"version": "2.3.0",
"version": "2.4.0",
"description": "javascript auth sdk for the smartcar",
"main": "dist/npm/sdk.js",
"license": "MIT",
Expand Down
44 changes: 33 additions & 11 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ class Smartcar {
* car brand selection screen. For a complete list of supported brands, please
* see our [API Reference](https://smartcar.com/docs/api#authorization)
* documentation.
* @param {Boolean} [options.singleSelect] - An optional value that sets the
* @param {Boolean|Object} [options.singleSelect] - 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/)
* `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.
*
* @return {String} Connect URL to redirect user to.
Expand Down Expand Up @@ -249,8 +251,24 @@ class Smartcar {

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

if (options.singleSelect !== undefined) {
link += `&single_select=${options.singleSelect === true}`;
if (options.singleSelect !== undefined && options.singleSelect !== null) {
let singleSelectParamAdded = false;
if (typeof options.singleSelect === 'object') {
const availableParams = ['vin'];
for (const param of availableParams) {
if (param in options.singleSelect) {
link += `&single_select_${param}=${options.singleSelect[param]}`;
singleSelectParamAdded = true;
}
}
if (!singleSelectParamAdded) {
link += '&single_select=false';
} else {
link += '&single_select=true';
}
} else {
link += `&single_select=${options.singleSelect === true}`;
}
}

if (options.state) {
Expand Down Expand Up @@ -282,10 +300,12 @@ class Smartcar {
* car brand selection screen. For a complete list of supported makes, please
* see our [API Reference](https://smartcar.com/docs/api#authorization)
* documentation.
* @param {Boolean} [options.singleSelect] - An optional value that sets the
* @param {Boolean|Object} [options.singleSelect] - 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/)
* `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.
*/
openDialog(options) {
Expand All @@ -310,10 +330,12 @@ class Smartcar {
* car brand selection screen. For a complete list of supported makes, please
* see our [API Reference](https://smartcar.com/docs/api#authorization)
* documentation.
* @param {Boolean} [options.singleSelect] - An optional value that sets the
* @param {Boolean|Object} [options.singleSelect] - 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/)
* `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.
*/
addClickHandler(options) {
Expand Down
85 changes: 85 additions & 0 deletions test/unit/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,91 @@ describe('sdk', () => {
expect(link).toEqual(expectedLink);
});

test('Adds single_select_vin=12345 when singleSelect included as an object', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
};

const smartcar = new Smartcar(options);

const expectedLink =
'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';

const link = smartcar.getAuthUrl({
state: 'foobarbaz',
forcePrompt: true,
vehicleInfo: {
make: 'TESLA',
},
singleSelect: {
vin: '12345',
},
});

expect(link).toEqual(expectedLink);
});

test('Ignores junk properties when singleSelect is included as an object', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
};

const smartcar = new Smartcar(options);

const expectedLink =
'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';

const link = smartcar.getAuthUrl({
state: 'foobarbaz',
forcePrompt: true,
vehicleInfo: {
make: 'TESLA',
},
singleSelect: {
pizza: '12345',
vin: 'vin',
},
});

expect(link).toEqual(expectedLink);
});

test('Ignores junk properties when singleSelect is included as an object with only junk properties', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
};

const smartcar = new Smartcar(options);

const expectedLink =
'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';

const link = smartcar.getAuthUrl({
state: 'foobarbaz',
forcePrompt: true,
vehicleInfo: {
make: 'TESLA',
},
singleSelect: {
pizza: '12345',
},
});

expect(link).toEqual(expectedLink);
});

test('Sets single_select=false with junk values passed to singleSelect', () => {
const options = {
clientId: 'clientId',
Expand Down

0 comments on commit 03f48eb

Please sign in to comment.