Skip to content

Commit

Permalink
Merge pull request #192 from particle-iot/add-context-docs
Browse files Browse the repository at this point in the history
docs: Add context docs
  • Loading branch information
cole-abbeduto-particle authored Dec 6, 2024
2 parents 354d401 + 4ec6a37 commit 8e52094
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class Particle {
*
* Create a new Particle object and call methods below on it.
*
* @param {Object} options Options for this API call Options to be used for all requests (see [Defaults](../src/Defaults.js))
* @param {Object} options Options for this API call Options to be used for all requests (see [Defaults](../src/Defaults.js))
* @param {string} [options.baseUrl]
* @param {string} [options.clientSecret]
* @param {string} [options.clientId]
* @param {number} [options.tokenDuration]
* @param {string} [options.auth] The access token. If not specified here, will have to be added to every request
* @param {string} [options.auth] The access token. If not specified here, will have to be added to every request
*/
constructor(options = {}){
if (options.auth) {
Expand All @@ -37,13 +37,33 @@ class Particle {
// todo - this seems a bit dangerous - would be better to put all options/context in a contained object
Object.assign(this, Defaults, options);
this.context = {};

this.agent = new Agent(this.baseUrl);
}

_isValidContext(name, context){
return (name === 'tool' || name === 'project') && context !== undefined;
}

/**
* @typedef {Object} ToolContext
* @property {string} name
* @property {string | number} [version]
* @property {Omit<ToolContext, 'components'>[]} [components]
*/

/**
* @typedef {Record<string, string | number>} ProjectContext
* @property {string} name
*/

/**
* Allows setting a tool or project context which will be sent as headers with every request.
* Tool- x-particle-tool
* Project- x-particle-project
* @param {'tool' | 'project'} name
* @param {ToolContext | ProjectContext | undefined} context
*/
setContext(name, context){
if (context !== undefined){
if (this._isValidContext(name, context)){
Expand Down

0 comments on commit 8e52094

Please sign in to comment.