smartcar / node-sdk

Smartcar Node.js SDK
MIT License
50 stars 14 forks source link

feat: add lock status to Node SDK #163

Closed hhovsepi closed 1 year ago

hhovsepi commented 1 year ago

In implementing this PR I followed these steps:

lib/vehicle.js

  1. Updated the methods map to have a lockStatus entry.
  2. Added JSDoc example for the new security function.

test/end-to-end/vehicle.js

  1. Wrote an individual endpoint test, and ran npm run test:e2e to assert success: image
  2. Wrote a batch request test, and ran npm run test:e2e to assert success: image

Prior to writing the tests, I created server.js file in the root of the repository and tested the response locally to assert success, the response was the following:

{
  "isLocked": true,
  "doors": [
    {
      "type": "frontLeft",
      "status": "CLOSED"
    },
    {
      "type": "frontRight",
      "status": "CLOSED"
    },
    {
      "type": "backLeft",
      "status": "CLOSED"
    },
    {
      "type": "backRight",
      "status": "CLOSED"
    }
  ],
  "windows": [
    {
      "type": "frontLeft",
      "status": "CLOSED"
    },
    {
      "type": "frontRight",
      "status": "CLOSED"
    },
    {
      "type": "backLeft",
      "status": "CLOSED"
    },
    {
      "type": "backRight",
      "status": "CLOSED"
    }
  ],
  "sunroof": [
    {
      "type": "sunroof",
      "status": "CLOSED"
    }
  ],
  "storage": [
    {
      "type": "front",
      "status": "CLOSED"
    },
    {
      "type": "rear",
      "status": "CLOSED"
    }
  ],
  "chargingPort": [],
  "meta": {
    "dataAge": "2023-08-18T15:32:39.000Z",
    "requestId": "879bd16f-9260-42c0-8c47-3a469e866382"
  }
}

Here is the contents of my server.js file:

/* eslint-disable no-console */
'use strict';

const smartcar = require('smartcar');
const express = require('express');

const app = express();

const port = 4040;

const client = new smartcar.AuthClient({
  clientId: '5d9f7b24-68c7-4c51-b1fd-8fc44826f496',
  clientSecret: <redacted>,
  redirectUri: 'http://localhost:4040/callback',
  mode: 'live', // launch Smartcar Connect in test mode
});

console.log(client);

// Redirect to Smartcar Connect
app.get('/login', function(req, res) {
  const link = client.getAuthUrl(['read_security']);
  console.log(link);
  res.redirect(link);
});

app.get('/callback', async function(req, res, next) {

  if (req.query.error) {
    return next(new Error(req.query.error));
  }

  const tokens = await client.exchangeCode(req.query.code);
  console.log('tokens', tokens);
  const vehicles = await smartcar.getVehicles(tokens.accessToken);
  const vehicle = new smartcar.Vehicle(
    vehicles.vehicles[0],
    tokens.accessToken,
  );
  console.log(JSON.stringify(vehicle, null, 2));
  const lockStatus = await vehicle.lockStatus();
  console.log(JSON.stringify(lockStatus, null, 2));
});

app.listen(port, () => console.log(`Listening on port ${port}`));
codecov[bot] commented 1 year ago

Codecov Report

Patch and project coverage have no change.

Comparison is base (cb3c2f2) 99.64% compared to head (5a40cf1) 99.64%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #163 +/- ## ======================================= Coverage 99.64% 99.64% ======================================= Files 6 6 Lines 280 280 ======================================= Hits 279 279 Misses 1 1 ``` | [Files Changed](https://app.codecov.io/gh/smartcar/node-sdk/pull/163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=smartcar) | Coverage Δ | | |---|---|---| | [lib/vehicle.js](https://app.codecov.io/gh/smartcar/node-sdk/pull/163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=smartcar#diff-bGliL3ZlaGljbGUuanM=) | `100.00% <ø> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

smartcar-ci commented 1 year ago

:tada: This PR is included in version 9.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: