From e41aaeb0b7974bab9500dc084282b4c71da9e15a Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:37:30 +0200 Subject: [PATCH 1/7] feat(unit test edit): unit test edit unit test edit --- test/devices.test.ts | 34 ++++++++++++++++++++++++++++++++++ test/sigfox-js.test.ts | 24 ------------------------ 2 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 test/devices.test.ts diff --git a/test/devices.test.ts b/test/devices.test.ts new file mode 100644 index 0000000..739e2e2 --- /dev/null +++ b/test/devices.test.ts @@ -0,0 +1,34 @@ +import * as nock from 'nock' + +import { API_CONFIG } from '../src/config/constants' +import { Devices } from '../src/modules/devices' + +describe('Devices Test', () => { + it('should be a class', () => { + const devices = new Devices({} as any) + expect(devices instanceof Devices).toBeTruthy() + }) + + describe('getDeviceInfo() call', () => { + it('should return device obj', async () => { + nock(API_CONFIG.baseURL) + .get(`/devices/9E1F98`) + .reply(200) + const devices = new Devices({} as any) + const result = await devices.getDeviceInfo('9E1F98') + expect(result).toMatchSnapshot() + }) + + it('should return error', async () => { + const devices = new Devices({} as any) + try { + nock(API_CONFIG.baseURL) + .get(`/devices/9E1F98`) + .reply(404) + await devices.getDeviceInfo('9E1F98') + } catch (error) { + expect(error).toMatchSnapshot() + } + }) + }) +}) diff --git a/test/sigfox-js.test.ts b/test/sigfox-js.test.ts index 99fc3b6..1a6d920 100644 --- a/test/sigfox-js.test.ts +++ b/test/sigfox-js.test.ts @@ -2,7 +2,6 @@ import * as nock from 'nock' import SigfoxApi from '../src/sigfox-js' import { ConfigParams } from '../src/types/config-params' -import { API_CONFIG } from '../src/config/constants' /** * SigfoxApi test @@ -19,27 +18,4 @@ describe('SigfoxApi test', () => { expect(new SigfoxApi({} as ConfigParams)).toBeInstanceOf(SigfoxApi) }) }) - - describe('getDeviceInfo() call', () => { - it('should return device obj', async () => { - nock(API_CONFIG.baseURL) - .get(`/devices/9E1F98`) - .reply(200) - const sigfox = new SigfoxApi({} as ConfigParams) - const result = await sigfox.getDeviceInfo('9E1F98') - expect(result).toMatchSnapshot() - }) - - it('should return error', async () => { - const sigfox = new SigfoxApi({} as ConfigParams) - try { - nock(API_CONFIG.baseURL) - .get(`/devices/9E1F98`) - .reply(404) - await sigfox.getDeviceInfo('9E1F98') - } catch (error) { - expect(error).toMatchSnapshot() - } - }) - }) }) From 9251f7d785cc5f85be8df6bd678182ac0d29920a Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:38:19 +0200 Subject: [PATCH 2/7] feat(edit tools setup): edit tools setup edit tools setup --- tools/gh-pages-publish.ts | 4 +-- tools/semantic-release-prepare.ts | 55 +++++++++++-------------------- 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/tools/gh-pages-publish.ts b/tools/gh-pages-publish.ts index 53f0fee..1f140b4 100644 --- a/tools/gh-pages-publish.ts +++ b/tools/gh-pages-publish.ts @@ -1,9 +1,9 @@ const { cd, exec, echo, touch } = require('shelljs') -const { readFileSync } = require('fs') +import { readFileSync } from 'fs' const url = require('url') let repoUrl -let pkg = JSON.parse(readFileSync('package.json') as any) +const pkg = JSON.parse(readFileSync('package.json') as any) if (typeof pkg.repository === 'object') { if (!pkg.repository.hasOwnProperty('url')) { throw new Error('URL does not exist in repository section') diff --git a/tools/semantic-release-prepare.ts b/tools/semantic-release-prepare.ts index db14dd7..19f9252 100644 --- a/tools/semantic-release-prepare.ts +++ b/tools/semantic-release-prepare.ts @@ -1,54 +1,39 @@ -const path = require("path") -const { fork } = require("child_process") -const colors = require("colors") +const path = require('path') +const { fork } = require('child_process') +const colors = require('colors') -const { readFileSync, writeFileSync } = require("fs") -const pkg = JSON.parse( - readFileSync(path.resolve(__dirname, "..", "package.json")) -) +const { readFileSync, writeFileSync } = require('fs') +const pkg = JSON.parse(readFileSync(path.resolve(__dirname, '..', 'package.json'))) -pkg.scripts.prepush = "npm run test:prod && npm run build" -pkg.scripts.commitmsg = "commitlint -E HUSKY_GIT_PARAMS" +pkg.scripts.prepush = 'npm run test:prod && npm run build' +pkg.scripts.commitmsg = 'commitlint -E HUSKY_GIT_PARAMS' -writeFileSync( - path.resolve(__dirname, "..", "package.json"), - JSON.stringify(pkg, null, 2) -) +writeFileSync(path.resolve(__dirname, '..', 'package.json'), JSON.stringify(pkg, null, 2)) // Call husky to set up the hooks -fork(path.resolve(__dirname, "..", "node_modules", "husky", "lib", "installer", 'bin'), ['install']) +fork(path.resolve(__dirname, '..', 'node_modules', 'husky', 'lib', 'installer', 'bin'), ['install']) console.log() -console.log(colors.green("Done!!")) +console.log(colors.green('Done!!')) console.log() if (pkg.repository.url.trim()) { - console.log(colors.cyan("Now run:")) - console.log(colors.cyan(" npm install -g semantic-release-cli")) - console.log(colors.cyan(" semantic-release-cli setup")) + console.log(colors.cyan('Now run:')) + console.log(colors.cyan(' npm install -g semantic-release-cli')) + console.log(colors.cyan(' semantic-release-cli setup')) console.log() - console.log( - colors.cyan('Important! Answer NO to "Generate travis.yml" question') - ) + console.log(colors.cyan('Important! Answer NO to "Generate travis.yml" question')) console.log() console.log( - colors.gray( - 'Note: Make sure "repository.url" in your package.json is correct before' - ) + colors.gray('Note: Make sure "repository.url" in your package.json is correct before') ) } else { - console.log( - colors.red( - 'First you need to set the "repository.url" property in package.json' - ) - ) - console.log(colors.cyan("Then run:")) - console.log(colors.cyan(" npm install -g semantic-release-cli")) - console.log(colors.cyan(" semantic-release-cli setup")) + console.log(colors.red('First you need to set the "repository.url" property in package.json')) + console.log(colors.cyan('Then run:')) + console.log(colors.cyan(' npm install -g semantic-release-cli')) + console.log(colors.cyan(' semantic-release-cli setup')) console.log() - console.log( - colors.cyan('Important! Answer NO to "Generate travis.yml" question') - ) + console.log(colors.cyan('Important! Answer NO to "Generate travis.yml" question')) } console.log() From 67f3f318d18692667d3447d35dbebf5bf0635869 Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:39:11 +0200 Subject: [PATCH 3/7] feat(edit tools setup): edit tools setup edit tools setup --- .gitignore | 1 + package-lock.json | 2 +- package.json | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d626d09..879f43a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ compiled .awcache .rpt2_cache docs +test/__snapshots__ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5b1da9d..40111e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "sigfox-js", - "version": "0.0.0", + "version": "0.0.0-development", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f854232..87ad842 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,9 @@ "commit": "git-cz", "semantic-release": "semantic-release", "semantic-release-prepare": "ts-node tools/semantic-release-prepare", - "travis-deploy-once": "travis-deploy-once" + "travis-deploy-once": "travis-deploy-once", + "prepush": "npm run test:prod && npm run build", + "commitmsg": "commitlint -E HUSKY_GIT_PARAMS" }, "lint-staged": { "{src,test}/**/*.ts": [ From fedff0ee6b552ca47cc731096a9e1c7a9d9a0e57 Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:39:42 +0200 Subject: [PATCH 4/7] feat(new device module): new device mod new device module wrapper rest --- src/modules/devices.ts | 153 +++++++++++++++++++++++++++++ src/types/device.ts | 5 + src/types/devices/getAllDevices.ts | 57 +++++++++++ src/types/devices/modem.ts | 23 +++++ 4 files changed, 238 insertions(+) create mode 100644 src/modules/devices.ts create mode 100644 src/types/devices/getAllDevices.ts create mode 100644 src/types/devices/modem.ts diff --git a/src/modules/devices.ts b/src/modules/devices.ts new file mode 100644 index 0000000..0b48aab --- /dev/null +++ b/src/modules/devices.ts @@ -0,0 +1,153 @@ +import axios, { AxiosInstance } from 'axios' + +import { RootObject, ModemCertificate } from '../types/devices/getAllDevices' +import { API_CONFIG } from '../config/constants' + +export class Devices { + rest: AxiosInstance + config: object + + constructor(config: object) { + this.rest = axios.create({ baseURL: API_CONFIG.baseURL }) + this.config = config + } + + /** + * The endpoint to get Device Info. + * HTTP GET /devices/${deviceId} + * @returns {device} + */ + public getDeviceInfo(deviceId: string): Promise { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * The endpoint to get info from all devices. + * HTTP GET /devices/?deep=false&sort=name&limit=100&offset=0 + * To Do - query + params customizable + * @returns [Devices] + */ + public getAllDevices(): Promise { + return this.rest + .get( + API_CONFIG.baseURL + `/devices/?deep=false&sort=name&limit=100&offset=0`, + this.config + ) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve a list of undelivered callbacks and errors for a given device, in reverse chronological order (most recent message first). + * HTTP GET /devices/${deviceId}/callbacks-not-delivered + * @returns [Callbacks - Errors] + */ + public getCallbacksNotDelivered(deviceId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/callbacks-not-delivered`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve the modem certificate associated with a device. + * HTTP GET /devices/${deviceId}/certificate/modem + * @returns {ModemCertificate} + */ + public getModemCert(deviceId: string): Promise { + return this.rest + .get( + API_CONFIG.baseURL + `/devices/${deviceId}/certificate/modem`, + this.config + ) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve the product certificate associated with a device already registered. + * HTTP GET /devices/${deviceId}/certificate/product + * @returns {ProductCertificate} + */ + public getProductCert(deviceId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/certificate/product`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve a device's consumption for a given year. + * HTTP GET /devices/${deviceId}/consumption/${year} + * @returns {id, deviceConsumptions} + */ + public getDeviceConsYearly(deviceId: string, year: number) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/consumption/${year}`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve a device's consumption for a given month during a given year. + * HTTP GET /devices/${deviceId}/consumption/${year}/${month} + * @returns {id, deviceConsumptions} + */ + public getDeviceConsMonthly(deviceId: string, year: number, month: number) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/consumption/${year}/${month}`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve a list of messages for a given device according to request filters, with a 3-day history. + * HTTP GET /devices/${deviceId}/messages + * @returns {infoMessages} + */ + public getMessagesList(deviceId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/messages`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Return the number of messages for a given device, for the last day, last week and last month. + * HTTP GET /devices/${deviceId}/messages/metric + * @returns {lastDay, lastWeek, lastMonth} + */ + public getMessagesMetric(deviceId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/messages/metric`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve a list of location data of a device according to request filters. + * HTTP GET /devices/${deviceId}/locations + * @returns {data} + */ + public getLocations(deviceId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/${deviceId}/locations`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } + + /** + * Retrieve the async job status for an unsubscribe devices action. + * HTTP GET /devices/bulk/unsubscribe/${jobId} + * @returns {jobDone, states} + */ + public unsubscribeActions(jobId: string) { + return this.rest + .get(API_CONFIG.baseURL + `/devices/bulk/unsubscribe/${jobId}`, this.config) + .then(response => response.data) + .catch(error => error.response.statusText) + } +} diff --git a/src/types/device.ts b/src/types/device.ts index 26206f2..c926502 100644 --- a/src/types/device.ts +++ b/src/types/device.ts @@ -22,6 +22,11 @@ export interface Device { activable: boolean } +export interface DeviceSingle { + id: string + name: string +} + export interface Location { lat: number lng: number diff --git a/src/types/devices/getAllDevices.ts b/src/types/devices/getAllDevices.ts new file mode 100644 index 0000000..2137f69 --- /dev/null +++ b/src/types/devices/getAllDevices.ts @@ -0,0 +1,57 @@ +export interface Location { + lat: number + lng: number +} + +export interface DeviceType { + id: string +} + +export interface Group { + id: string +} + +export interface Token { + state: number + detailMessage: string + end: any +} + +export interface Contract { + id: string +} + +export interface ModemCertificate { + id: string +} + +export interface Datum { + id: string + name: string + lastCom: any + state: number + comState: number + location: Location + deviceType: DeviceType + group: Group + lqi: number + activationTime: any + token: Token + contract: Contract + creationTime: any + modemCertificate: ModemCertificate + prototype: boolean + automaticRenewal: boolean + automaticRenewalStatus: number + createdBy: string + lastEditionTime: any + lastEditedBy: string + activable: boolean +} + +export interface Paging {} + +export interface RootObject { + data: Datum[] + paging: Paging +} diff --git a/src/types/devices/modem.ts b/src/types/devices/modem.ts new file mode 100644 index 0000000..fad717a --- /dev/null +++ b/src/types/devices/modem.ts @@ -0,0 +1,23 @@ +export interface Manufacturer { + id: string +} + +export interface RadioConfiguration { + id: number + outputPower: number + balancedLinkBudget: boolean +} + +export interface ModemCertificate { + id: string + manufacturer: Manufacturer + name: string + key: string + version: string + description: string + status: number + radioConfigurations: RadioConfiguration[] + inputSensitivity: number + modes: number[] + repeaterFunction: boolean +} From a91d7f1e415c1e2267f0c0b2af81ca2aef09ff96 Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:40:16 +0200 Subject: [PATCH 5/7] feat(add instance device to main): add instance device add instance device to main class --- src/sigfox-js.ts | 19 +++---------------- test/__snapshots__/sigfox-js.test.ts.snap | 3 --- 2 files changed, 3 insertions(+), 19 deletions(-) delete mode 100644 test/__snapshots__/sigfox-js.test.ts.snap diff --git a/src/sigfox-js.ts b/src/sigfox-js.ts index d0a0672..6176fb7 100644 --- a/src/sigfox-js.ts +++ b/src/sigfox-js.ts @@ -1,8 +1,6 @@ -import axios, { AxiosInstance } from 'axios' - import { ConfigParams } from './types/config-params' -import { Device } from './types/device' import { API_CONFIG } from './config/constants' +import { Devices } from './modules/devices' /** * @class SigfoxApiWrapper @@ -10,29 +8,18 @@ import { API_CONFIG } from './config/constants' */ export default class SigfoxApi { - public rest: AxiosInstance public customParams: object + public devices: Devices /** * @constructor * @param {ConfigParams} customParams ID Client Setup */ constructor(ConfigParams: ConfigParams) { - this.rest = axios.create({ baseURL: API_CONFIG.baseURL }) this.customParams = { auth: ConfigParams, API_CONFIG } - } - - /** - * The endpoint to get Device Info. - * @returns {device} - */ - public getDeviceInfo(deviceId: string) { - return this.rest - .get(API_CONFIG.baseURL + `/devices/${deviceId}`, this.customParams) - .then(response => response.data) - .catch(error => error.response.statusText) + this.devices = new Devices(this.customParams) } } diff --git a/test/__snapshots__/sigfox-js.test.ts.snap b/test/__snapshots__/sigfox-js.test.ts.snap deleted file mode 100644 index 86a779b..0000000 --- a/test/__snapshots__/sigfox-js.test.ts.snap +++ /dev/null @@ -1,3 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`SigfoxApi test getDeviceInfo() call should return device obj 1`] = `""`; From eb786183185638d26cf87d25a2a5615f5060c491 Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:49:11 +0200 Subject: [PATCH 6/7] feat(readme mod): readme mod readme mod --- README.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ecafd9a..70a559f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,25 @@ -#SigfoxJS +# SigfoxJS + +![Sigfox Logo img](https://temp.iotdk.dk/wp-content/uploads/2018/01/Sigfox_Logo_RGB_R-300x175.png ) ## [WIP] -- Work in Progress \ No newline at end of file +```javascript +const SigfoxApi = require('sigfox-js'); + +const apiConnection = new SigfoxApi({ + username: 'username', + password: 'password' +}); + +const infoDevices = await connection.devices.getAllDevices(); +``` + +#### To Do: + +- Users [x] +- Coverages [ ] +- Devices [ ] +- Devices Types [ ] +- Groups [ ] +- Tiles [ ] From e7495a27da54ad8b6eb7400667396c330d53a4cf Mon Sep 17 00:00:00 2001 From: David ML Date: Sun, 5 May 2019 19:57:44 +0200 Subject: [PATCH 7/7] fix(edit unit test): edit unit test edit unit test --- test/devices.test.ts | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/test/devices.test.ts b/test/devices.test.ts index 739e2e2..3f91dfe 100644 --- a/test/devices.test.ts +++ b/test/devices.test.ts @@ -8,27 +8,4 @@ describe('Devices Test', () => { const devices = new Devices({} as any) expect(devices instanceof Devices).toBeTruthy() }) - - describe('getDeviceInfo() call', () => { - it('should return device obj', async () => { - nock(API_CONFIG.baseURL) - .get(`/devices/9E1F98`) - .reply(200) - const devices = new Devices({} as any) - const result = await devices.getDeviceInfo('9E1F98') - expect(result).toMatchSnapshot() - }) - - it('should return error', async () => { - const devices = new Devices({} as any) - try { - nock(API_CONFIG.baseURL) - .get(`/devices/9E1F98`) - .reply(404) - await devices.getDeviceInfo('9E1F98') - } catch (error) { - expect(error).toMatchSnapshot() - } - }) - }) })