particle-api-js 11.1.0 → 11.1.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "particle-api-js",
3
- "version": "11.1.0",
3
+ "version": "11.1.2",
4
4
  "description": "Particle API Client",
5
5
  "main": "src/Particle.js",
6
6
  "scripts": {
@@ -26,10 +26,12 @@ class EventStream extends EventEmitter {
26
26
 
27
27
  const isSecure = protocol === 'https:';
28
28
  const requestor = isSecure ? https : http;
29
+ const nonce = global.performance ? global.performance.now() : 0;
29
30
  const req = requestor.request({
30
31
  hostname,
31
32
  protocol,
32
- path,
33
+ // Firefox has issues making multiple fetch requests with the same parameters so add a nonce
34
+ path: `${path}?nonce=${nonce}`,
33
35
  headers: {
34
36
  'Authorization': `Bearer ${this.token}`
35
37
  },
package/src/Particle.js CHANGED
@@ -22,12 +22,12 @@ class Particle {
22
22
  *
23
23
  * Create a new Particle object and call methods below on it.
24
24
  *
25
- * @param {Object} options Options for this API call Options to be used for all requests (see [Defaults](../src/Defaults.js))
25
+ * @param {Object} options Options for this API call Options to be used for all requests (see [Defaults](../src/Defaults.js))
26
26
  * @param {string} [options.baseUrl]
27
27
  * @param {string} [options.clientSecret]
28
28
  * @param {string} [options.clientId]
29
29
  * @param {number} [options.tokenDuration]
30
- * @param {string} [options.auth] The access token. If not specified here, will have to be added to every request
30
+ * @param {string} [options.auth] The access token. If not specified here, will have to be added to every request
31
31
  */
32
32
  constructor(options = {}){
33
33
  if (options.auth) {
@@ -37,6 +37,7 @@ class Particle {
37
37
  // todo - this seems a bit dangerous - would be better to put all options/context in a contained object
38
38
  Object.assign(this, Defaults, options);
39
39
  this.context = {};
40
+
40
41
  this.agent = new Agent(this.baseUrl);
41
42
  }
42
43
 
@@ -44,6 +45,25 @@ class Particle {
44
45
  return (name === 'tool' || name === 'project') && context !== undefined;
45
46
  }
46
47
 
48
+ /**
49
+ * @typedef {Object} ToolContext
50
+ * @property {string} name
51
+ * @property {string | number} [version]
52
+ * @property {Omit<ToolContext, 'components'>[]} [components]
53
+ */
54
+
55
+ /**
56
+ * @typedef {Record<string, string | number>} ProjectContext
57
+ * @property {string} name
58
+ */
59
+
60
+ /**
61
+ * Allows setting a tool or project context which will be sent as headers with every request.
62
+ * Tool- x-particle-tool
63
+ * Project- x-particle-project
64
+ * @param {'tool' | 'project'} name
65
+ * @param {ToolContext | ProjectContext | undefined} context
66
+ */
47
67
  setContext(name, context){
48
68
  if (context !== undefined){
49
69
  if (this._isValidContext(name, context)){
@@ -49,7 +49,7 @@ describe('EventStream', () => {
49
49
  expect(http.request).to.have.been.calledWith({
50
50
  hostname: 'hostname',
51
51
  protocol: 'http:',
52
- path: '/path',
52
+ path: '/path?nonce=0',
53
53
  headers: {
54
54
  'Authorization': 'Bearer token'
55
55
  },