particle-api-js 10.0.0 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/EventStream-e2e-browser.html +0 -1
  3. package/EventStream-e2e-node.js +2 -3
  4. package/README.md +2 -2
  5. package/dist/particle.min.js +1 -434
  6. package/dist/particle.min.js.map +1 -1
  7. package/docs/api.md +4916 -266
  8. package/karma.conf.js +18 -6
  9. package/package.json +18 -22
  10. package/src/Agent.js +407 -0
  11. package/src/Client.js +170 -0
  12. package/src/Defaults.js +7 -0
  13. package/src/EventStream.js +263 -0
  14. package/src/Library.js +33 -0
  15. package/src/Particle.js +2644 -0
  16. package/test/Agent.integration.js +2 -3
  17. package/test/Agent.spec.js +2 -2
  18. package/test/Client.spec.js +7 -7
  19. package/test/Defaults.spec.js +2 -2
  20. package/test/EventStream.spec.js +6 -4
  21. package/test/FakeAgent.js +2 -2
  22. package/test/Library.spec.js +2 -2
  23. package/test/Particle.integration.js +2 -3
  24. package/test/Particle.spec.js +54 -54
  25. package/test/fixtures/index.js +4 -18
  26. package/test/support/FixtureHttpServer.js +5 -3
  27. package/test/test-setup.js +5 -5
  28. package/tsconfig.json +14 -0
  29. package/webpack.config.js +45 -0
  30. package/.babelrc +0 -4
  31. package/lib/Agent.js +0 -615
  32. package/lib/Agent.js.map +0 -1
  33. package/lib/Client.js +0 -312
  34. package/lib/Client.js.map +0 -1
  35. package/lib/Defaults.js +0 -14
  36. package/lib/Defaults.js.map +0 -1
  37. package/lib/EventStream.js +0 -335
  38. package/lib/EventStream.js.map +0 -1
  39. package/lib/Library.js +0 -67
  40. package/lib/Library.js.map +0 -1
  41. package/lib/Particle.js +0 -3831
  42. package/lib/Particle.js.map +0 -1
  43. package/test/Client.integration.js +0 -69
  44. package/test/fixtures/tarball.tar.gz +0 -0
  45. package/test/fixtures/test-library-publish-0.0.1.tar.gz +0 -0
  46. package/test/fixtures/test-library-publish-0.0.2.tar.gz +0 -0
@@ -21,9 +21,8 @@
21
21
  * Tests for real the Agent class using an external service.
22
22
  */
23
23
 
24
- import { expect } from './test-setup';
25
- import Agent from '../src/Agent';
26
-
24
+ const { expect } = require('./test-setup');
25
+ const Agent = require('../src/Agent');
27
26
 
28
27
  describe('Agent', () => {
29
28
  if (!process.env.SKIP_AGENT_TEST){
@@ -1,5 +1,5 @@
1
- import { sinon, expect } from './test-setup';
2
- import Agent from '../src/Agent.js';
1
+ const { sinon, expect } = require('./test-setup');
2
+ const Agent = require('../src/Agent.js');
3
3
 
4
4
  describe('Agent', () => {
5
5
  beforeEach(() => {
@@ -1,7 +1,7 @@
1
- import { expect, sinon } from './test-setup';
2
- import Client from '../src/Client';
3
- import * as fixtures from './fixtures';
4
- import Library from '../src/Library';
1
+ const { expect, sinon } = require('./test-setup');
2
+ const Client = require('../src/Client');
3
+ const fixtures = require('./fixtures');
4
+ const Library = require('../src/Library');
5
5
 
6
6
  let api;
7
7
  const token = 'tok';
@@ -25,7 +25,7 @@ describe('Client', () => {
25
25
 
26
26
  describe('libraries', () => {
27
27
  it('resolves to a list of Library objects', () => {
28
- api.listLibraries = () => Promise.resolve({ body: fixtures.readJSON('libraries.json') });
28
+ api.listLibraries = () => Promise.resolve({ body: fixtures.read('libraries.json') });
29
29
  return client.libraries().then(libraries => {
30
30
  expect(libraries.length).to.equal(1);
31
31
  expect(libraries[0].name).to.equal('neopixel');
@@ -35,7 +35,7 @@ describe('Client', () => {
35
35
 
36
36
  describe('library', () => {
37
37
  it('resolves to a Library objects', () => {
38
- api.getLibrary = () => Promise.resolve({ body: fixtures.readJSON('library.json') });
38
+ api.getLibrary = () => Promise.resolve({ body: fixtures.read('library.json') });
39
39
  return client.library('neopixel').then(library => {
40
40
  expect(library.name).to.equal('neopixel');
41
41
  });
@@ -44,7 +44,7 @@ describe('Client', () => {
44
44
 
45
45
  describe('libraryVersions', () => {
46
46
  it('resolves to a Library objects', () => {
47
- api.getLibraryVersions = () => Promise.resolve({ body: fixtures.readJSON('libraryVersions.json') });
47
+ api.getLibraryVersions = () => Promise.resolve({ body: fixtures.read('libraryVersions.json') });
48
48
  return client.libraryVersions().then(libraries => {
49
49
  expect(libraries.length).to.equal(9);
50
50
  expect(libraries[0].name).to.equal('neopixel');
@@ -1,5 +1,5 @@
1
- import { expect } from './test-setup';
2
- import Defaults from '../src/Defaults';
1
+ const { expect } = require('./test-setup');
2
+ const Defaults = require('../src/Defaults');
3
3
 
4
4
  describe('Default Particle constructor options', () => {
5
5
  it('includes baseUrl', () => {
@@ -1,8 +1,8 @@
1
- import { sinon, expect } from './test-setup';
2
- import http from 'http';
3
- import { EventEmitter } from 'events';
1
+ const { sinon, expect } = require('./test-setup');
2
+ const http = require('http');
3
+ const { EventEmitter } = require('events');
4
4
 
5
- import EventStream from '../src/EventStream';
5
+ const EventStream = require('../src/EventStream');
6
6
 
7
7
  describe('EventStream', () => {
8
8
  afterEach(() => {
@@ -32,6 +32,7 @@ describe('EventStream', () => {
32
32
 
33
33
  describe('connect', () => {
34
34
  it('successfully connects to http', () => {
35
+ sinon.useFakeTimers({ shouldAdvanceTime: true });
35
36
  const fakeRequest = makeRequest();
36
37
  sinon.stub(http, 'request').callsFake(() => {
37
38
  setImmediate(() => {
@@ -57,6 +58,7 @@ describe('EventStream', () => {
57
58
  });
58
59
 
59
60
  it('returns http errors on connect', () => {
61
+ sinon.useFakeTimers({ shouldAdvanceTime: true });
60
62
  const fakeRequest = makeRequest();
61
63
  sinon.stub(http, 'request').callsFake(() => {
62
64
  setImmediate(() => {
package/test/FakeAgent.js CHANGED
@@ -1,4 +1,4 @@
1
- export default class FakeAgent {
1
+ class FakeAgent {
2
2
  get({ uri, auth, headers, query, context }){
3
3
  return this.request({ uri, method: 'get', auth, headers, query, context });
4
4
  }
@@ -23,4 +23,4 @@ export default class FakeAgent {
23
23
  return new Promise((resolve) => resolve(opts));
24
24
  }
25
25
  }
26
-
26
+ module.exports = FakeAgent;
@@ -1,5 +1,5 @@
1
- import { expect } from './test-setup';
2
- import Library from '../src/Library';
1
+ const { expect } = require('./test-setup');
2
+ const Library = require('../src/Library');
3
3
 
4
4
  let client = {};
5
5
 
@@ -1,6 +1,5 @@
1
- import { expect, sinon } from './test-setup';
2
- import Particle from '../src/Particle';
3
-
1
+ const { expect, sinon } = require('./test-setup');
2
+ const Particle = require('../src/Particle');
4
3
 
5
4
  describe('Particle', () => {
6
5
  let api;
@@ -1,10 +1,10 @@
1
- import should from 'should'; // monkeypatch the world~!1
2
- import Particle from '../src/Particle';
3
- import Defaults from '../src/Defaults';
4
- import Client from '../src/Client';
5
- import EventStream from '../src/EventStream';
6
- import FakeAgent from './FakeAgent';
7
- import { sinon, expect } from './test-setup';
1
+ const should = require('should'); // monkeypatch the world~!1
2
+ const Particle = require('../src/Particle');
3
+ const Defaults = require('../src/Defaults');
4
+ const Client = require('../src/Client');
5
+ const EventStream = require('../src/EventStream');
6
+ const FakeAgent = require('./FakeAgent');
7
+ const { sinon, expect } = require('./test-setup');
8
8
 
9
9
  let api;
10
10
 
@@ -96,25 +96,25 @@ const props = {
96
96
  dateRange: '2020-05-15T18:29:45.000Z,2020-05-19T18:29:45.000Z',
97
97
  rectBl: '56.185412,-4.049868',
98
98
  rectTr: '56.571537,-5.385920',
99
- blockId: 1,
100
- runId: 1,
101
- block: {
99
+ logicFunctionId: 1,
100
+ logicRunId: 1,
101
+ logicFunction: {
102
102
  enabled: true,
103
- name: 'block-1',
103
+ name: 'function-1',
104
104
  description: 'hello world',
105
- logic: {
105
+ source: {
106
106
  type: 'JavaScript',
107
- code: 'console.log("hello from block-1");'
107
+ code: 'console.log("hello from function-1");'
108
108
  },
109
- matchers: [
109
+ logic_triggers: [
110
110
  {
111
- type: 'PubSub',
111
+ type: 'Event',
112
112
  enabled: true,
113
113
  product_id: 9001,
114
114
  event_name: 'main',
115
115
  },
116
116
  {
117
- type: 'Chron',
117
+ type: 'Scheduled',
118
118
  enabled: true,
119
119
  cron: '0 0 1 * *',
120
120
  start_at: '2021-05-15T18:29:45.000Z',
@@ -2574,31 +2574,31 @@ describe('ParticleAPI', () => {
2574
2574
  });
2575
2575
  });
2576
2576
 
2577
- describe('.createLogicBlock', () => {
2577
+ describe('.createLogicFunction', () => {
2578
2578
  it('generates request', () => {
2579
- return api.createLogicBlock(propsWithOrg).then((results) => {
2579
+ return api.createLogicFunction(propsWithOrg).then((results) => {
2580
2580
  results.should.match({
2581
2581
  method: 'post',
2582
- uri: `/v1/orgs/${org}/blocks`,
2582
+ uri: `/v1/orgs/${org}/logic/functions`,
2583
2583
  auth: props.auth,
2584
2584
  data: {
2585
- block: {
2585
+ logic_function: {
2586
2586
  enabled: true,
2587
- name: 'block-1',
2587
+ name: 'function-1',
2588
2588
  description: 'hello world',
2589
- logic: {
2589
+ source: {
2590
2590
  type: 'JavaScript',
2591
- code: 'console.log("hello from block-1");'
2591
+ code: 'console.log("hello from function-1");'
2592
2592
  },
2593
- matchers: [
2593
+ logic_triggers: [
2594
2594
  {
2595
- type: 'PubSub',
2595
+ type: 'Event',
2596
2596
  enabled: true,
2597
2597
  product_id: parseInt(props.productId),
2598
2598
  event_name: props.event,
2599
2599
  },
2600
2600
  {
2601
- type: 'Chron',
2601
+ type: 'Scheduled',
2602
2602
  enabled: true,
2603
2603
  cron: '0 0 1 * *',
2604
2604
  start_at: '2021-05-15T18:29:45.000Z',
@@ -2612,43 +2612,43 @@ describe('ParticleAPI', () => {
2612
2612
  });
2613
2613
  });
2614
2614
 
2615
- describe('.getLogicBlock', () => {
2615
+ describe('.getLogicFunction', () => {
2616
2616
  it('generates request', () => {
2617
- return api.getLogicBlock(propsWithOrg).then((results) => {
2617
+ return api.getLogicFunction(propsWithOrg).then((results) => {
2618
2618
  results.should.match({
2619
2619
  method: 'get',
2620
- uri: `/v1/orgs/${org}/blocks/${props.blockId}`,
2620
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2621
2621
  auth: props.auth
2622
2622
  });
2623
2623
  });
2624
2624
  });
2625
2625
  });
2626
2626
 
2627
- describe('.updateLogicBlock', () => {
2627
+ describe('.updateLogicFunction', () => {
2628
2628
  it('generates request', () => {
2629
- return api.updateLogicBlock(propsWithOrg).then((results) => {
2629
+ return api.updateLogicFunction(propsWithOrg).then((results) => {
2630
2630
  results.should.match({
2631
2631
  method: 'put',
2632
- uri: `/v1/orgs/${org}/blocks/${props.blockId}`,
2632
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2633
2633
  auth: props.auth,
2634
2634
  data: {
2635
- block: {
2635
+ logic_function: {
2636
2636
  enabled: true,
2637
- name: 'block-1',
2637
+ name: 'function-1',
2638
2638
  description: 'hello world',
2639
- logic: {
2639
+ source: {
2640
2640
  type: 'JavaScript',
2641
- code: 'console.log("hello from block-1");'
2641
+ code: 'console.log("hello from function-1");'
2642
2642
  },
2643
- matchers: [
2643
+ logic_triggers: [
2644
2644
  {
2645
- type: 'PubSub',
2645
+ type: 'Event',
2646
2646
  enabled: true,
2647
2647
  product_id: parseInt(props.productId),
2648
2648
  event_name: props.event,
2649
2649
  },
2650
2650
  {
2651
- type: 'Chron',
2651
+ type: 'Scheduled',
2652
2652
  enabled: true,
2653
2653
  cron: '0 0 1 * *',
2654
2654
  start_at: '2021-05-15T18:29:45.000Z',
@@ -2662,60 +2662,60 @@ describe('ParticleAPI', () => {
2662
2662
  });
2663
2663
  });
2664
2664
 
2665
- describe('.deleteLogicBlock', () => {
2665
+ describe('.deleteLogicFunction', () => {
2666
2666
  it('generates request', () => {
2667
- return api.deleteLogicBlock(propsWithOrg).then((results) => {
2667
+ return api.deleteLogicFunction(propsWithOrg).then((results) => {
2668
2668
  results.should.match({
2669
2669
  method: 'delete',
2670
- uri: `/v1/orgs/${org}/blocks/${props.blockId}`,
2670
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2671
2671
  auth: props.auth
2672
2672
  });
2673
2673
  });
2674
2674
  });
2675
2675
  });
2676
2676
 
2677
- describe('.listLogicBlocks', () => {
2677
+ describe('.listLogicFunctions', () => {
2678
2678
  it('generates request', () => {
2679
- return api.listLogicBlocks(propsWithOrg).then((results) => {
2679
+ return api.listLogicFunctions(propsWithOrg).then((results) => {
2680
2680
  results.should.match({
2681
2681
  method: 'get',
2682
- uri: `/v1/orgs/${org}/blocks`,
2682
+ uri: `/v1/orgs/${org}/logic/functions`,
2683
2683
  auth: props.auth
2684
2684
  });
2685
2685
  });
2686
2686
  });
2687
2687
  });
2688
2688
 
2689
- describe('.listBlockRuns', () => {
2689
+ describe('.listLogicRuns', () => {
2690
2690
  it('generates request', () => {
2691
- return api.listBlockRuns(propsWithOrg).then((results) => {
2691
+ return api.listLogicRuns(propsWithOrg).then((results) => {
2692
2692
  results.should.match({
2693
2693
  method: 'get',
2694
- uri: `/v1/orgs/${org}/blocks/${props.blockId}/runs`,
2694
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs`,
2695
2695
  auth: props.auth,
2696
2696
  });
2697
2697
  });
2698
2698
  });
2699
2699
  });
2700
2700
 
2701
- describe('.getBlockRun', () => {
2701
+ describe('.getLogicRun', () => {
2702
2702
  it('generates request', () => {
2703
- return api.getBlockRun(propsWithOrg).then((results) => {
2703
+ return api.getLogicRun(propsWithOrg).then((results) => {
2704
2704
  results.should.match({
2705
2705
  method: 'get',
2706
- uri: `/v1/orgs/${org}/blocks/${props.blockId}/runs/${props.runId}`,
2706
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs/${props.logicRunId}`,
2707
2707
  auth: props.auth,
2708
2708
  });
2709
2709
  });
2710
2710
  });
2711
2711
  });
2712
2712
 
2713
- describe('.getBlockRunLog', () => {
2713
+ describe('.getLogicRunLogs', () => {
2714
2714
  it('generates request', () => {
2715
- return api.getBlockRunLog(propsWithOrg).then((results) => {
2715
+ return api.getLogicRunLogs(propsWithOrg).then((results) => {
2716
2716
  results.should.match({
2717
2717
  method: 'get',
2718
- uri: `/v1/orgs/${org}/blocks/${props.blockId}/runs/${props.runId}/logs`,
2718
+ uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs/${props.logicRunId}/logs`,
2719
2719
  auth: props.auth,
2720
2720
  });
2721
2721
  });
@@ -1,16 +1,8 @@
1
- /* In order for the tests to run in the browser the fixture files must
2
- * be loaded statically into an object. The 'brfs' module will replace
3
- * the fs.readFileSync('static_path') call by the contents of the file.
4
- */
5
- const fs = require('fs'); // import syntax doesn't work inside karma
6
-
7
1
 
8
2
  const fixtures = {
9
- 'libraries.json': fs.readFileSync(__dirname + '/libraries.json'),
10
- 'library.json': fs.readFileSync(__dirname + '/library.json'),
11
- 'libraryVersions.json': fs.readFileSync(__dirname + '/libraryVersions.json'),
12
- 'test-library-publish-0.0.1.tar.gz': fs.readFileSync(__dirname + '/test-library-publish-0.0.1.tar.gz'),
13
- 'test-library-publish-0.0.2.tar.gz': fs.readFileSync(__dirname + '/test-library-publish-0.0.2.tar.gz'),
3
+ 'libraries.json': require('./libraries.json'),
4
+ 'library.json': require('./library.json'),
5
+ 'libraryVersions.json': require('./libraryVersions.json')
14
6
  };
15
7
 
16
8
  function read(filename) {
@@ -20,10 +12,4 @@ function read(filename) {
20
12
  return fixtures[filename];
21
13
  }
22
14
 
23
- function readJSON(filename) {
24
- return JSON.parse(read(filename));
25
- }
26
-
27
- export {
28
- read, readJSON
29
- };
15
+ module.exports = { read };
@@ -1,9 +1,9 @@
1
1
  // Serve files from the fixture folder
2
- import express from 'express';
3
- import * as fixtures from '../fixtures';
2
+ const express = require('express');
3
+ const fixtures = require('../fixtures');
4
4
 
5
5
 
6
- export default class FixtureHttpServer {
6
+ class FixtureHttpServer {
7
7
  constructor(){
8
8
  this.app = express();
9
9
  this.app.get('/:filename', (req, res) => {
@@ -23,3 +23,5 @@ export default class FixtureHttpServer {
23
23
  return `http://localhost:${this.server.address().port}`;
24
24
  }
25
25
  }
26
+
27
+ module.exports = FixtureHttpServer;
@@ -1,16 +1,16 @@
1
1
  // Set up the Mocha test framework with the Chai assertion library and
2
2
  // the Sinon mock library
3
3
 
4
- import chai from 'chai';
5
- import sinon from 'sinon';
6
- import sinonChai from 'sinon-chai';
7
- import chaiAsPromised from 'chai-as-promised';
4
+ const chai = require('chai');
5
+ const sinon = require('sinon');
6
+ const sinonChai = require('sinon-chai');
7
+ const chaiAsPromised = require('chai-as-promised');
8
8
 
9
9
  chai.use(sinonChai);
10
10
  chai.use(chaiAsPromised);
11
11
  const expect = chai.expect;
12
12
 
13
- export {
13
+ module.exports = {
14
14
  chai,
15
15
  sinon,
16
16
  expect
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es6",
5
+ "allowJs": true,
6
+ "checkJs": true,
7
+ "skipLibCheck": true,
8
+ "esModuleInterop": true,
9
+ "resolveJsonModule": true
10
+ },
11
+ "types": ["node"],
12
+ "include": [ "src" ],
13
+ "exclude": ["node_modules"]
14
+ }
@@ -0,0 +1,45 @@
1
+ const path = require('path');
2
+ const webpack = require('webpack');
3
+ const TerserPlugin = require('terser-webpack-plugin');
4
+
5
+ module.exports = (env) => {
6
+ return {
7
+ mode: env.mode,
8
+ target: 'web',
9
+ entry: './src/Particle.js',
10
+ devtool: 'source-map',
11
+ output: {
12
+ filename: `particle${env.mode === 'production' ? '.min' : ''}.js`,
13
+ path: path.resolve(__dirname, 'dist'),
14
+ clean: true,
15
+ library: {
16
+ name: 'Particle',
17
+ type: 'var'
18
+ }
19
+ },
20
+ optimization: {
21
+ minimize: env.mode === 'production',
22
+ minimizer: [new TerserPlugin({
23
+ extractComments: false,
24
+ terserOptions: {
25
+ format: {
26
+ comments: false
27
+ }
28
+ }
29
+ })]
30
+ },
31
+ resolve: {
32
+ fallback: {
33
+ buffer: require.resolve('buffer'),
34
+ events: require.resolve('events'),
35
+ url: require.resolve('url')
36
+ }
37
+ },
38
+ plugins: [
39
+ new webpack.ProvidePlugin({
40
+ Buffer: ['buffer', 'Buffer'],
41
+ process: 'process/browser',
42
+ })
43
+ ]
44
+ };
45
+ };
package/.babelrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "presets": ["es2015"],
3
- "plugins": ["add-module-exports", "transform-runtime"]
4
- }