Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaffolding to publish types #161

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ lib/
/.idea
*.tgz
.vscode
tmp
tmp
/types
2 changes: 1 addition & 1 deletion dist/particle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/particle.min.js.map

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"version": "9.4.1",
"description": "Particle API Client",
"main": "lib/Particle.js",
"types": "types/index.d.ts",
"scripts": {
"babel-watch": "babel src -d lib --watch --source-maps",
"prepublish": "npm run lint && npm run compile",
"compile": "babel src -sd lib",
"compile": "npm run generate-types && babel src -sd lib",
"test": "npm run lint && npm run test:unit",
"test:ci": "npm run lint && npm run test:unit -- --forbid-only && npm run coverage",
"test:unit": "mocha test/ -R spec --compilers js:babel-register",
Expand All @@ -22,7 +23,8 @@
"preversion": "npm run test && npm run prepublish",
"reinstall": "rm -rf ./node_modules && npm i",
"version": "npm run build && npm run docs && npm run update-changelog && git add dist/* docs/*",
"update-changelog": "VERSION=`node -p -e \"require('./package.json').version\"` bash -c 'read -p \"Update CHANGELOG.md for version $VERSION and press ENTER when done.\"' && git add CHANGELOG.md"
"update-changelog": "VERSION=`node -p -e \"require('./package.json').version\"` bash -c 'read -p \"Update CHANGELOG.md for version $VERSION and press ENTER when done.\"' && git add CHANGELOG.md",
"generate-types": "tsc"
},
"repository": {
"type": "git",
Expand All @@ -46,6 +48,7 @@
],
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^16.18.32",
"babel-cli": "^6.9.0",
"babel-eslint": "^6.0.4",
"babel-plugin-add-module-exports": "^0.1.2",
Expand Down Expand Up @@ -73,6 +76,7 @@
"should": "^9.0.0",
"sinon": "^7.2.5",
"sinon-chai": "^3.7.0",
"typescript": "^5.0.4",
"watchify": "^3.7.0"
},
"dependencies": {
Expand Down
36 changes: 21 additions & 15 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ class Particle {
* Claim a device to the account. The device must be online and unclaimed.
* @param {Object} options Options for this API call
* @param {String} options.deviceId Device ID
* @param {Boolean} [options.requestTransfer] Request the device be transferred to the account doing the claim
* @param {String} options.auth Access Token
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
Expand Down Expand Up @@ -587,8 +588,8 @@ class Particle {
* @param {Object} [options.context] Request context
* @returns {Promise} A promise
*/
lockDeviceProductFirmware({ deviceId, desiredFirmwareVersion, flash, product, auth, context }){
return this.updateDevice({ deviceId, desiredFirmwareVersion, flash, product, auth, context });
lockDeviceProductFirmware({ deviceId, desiredFirmwareVersion, flash, product, auth, context, headers }){
return this.updateDevice({ deviceId, desiredFirmwareVersion, flash, product, auth, context, headers });
}

/**
Expand Down Expand Up @@ -1207,6 +1208,7 @@ class Particle {
* @param {String} options.iccid ICCID of the SIM card
* @param {Array<String>} options.iccids (Product only) ICCID of multiple SIM cards to import
* @param {String} options.country The ISO country code for the SIM cards
* @param {String} [options.promoCode] (Product only) Promo code for the SIM cards
* @param {String} [options.product] SIM cards for this product ID or slug
* @param {String} options.auth Access Token
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
Expand Down Expand Up @@ -1322,7 +1324,6 @@ class Particle {
* - 'verified' - list only verified libraries
* - 'featured' - list only featured libraries
* @param {String} options.excludeScopes list of scopes to exclude
* @param {String} options.category Category to filter
* @param {String} options.auth Access Token
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
Expand Down Expand Up @@ -2003,7 +2004,7 @@ class Particle {
* @param {Object} options Options for this API call
* @param {String} options.product Config for this product ID or slug
* @param {String} options.auth Access Token
* @param {Object} opitons.config Product configuration to update
* @param {Object} options.config Product configuration to update
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
* @returns {Promise} A promise
Expand All @@ -2023,7 +2024,7 @@ class Particle {
* @param {Object} options Options for this API call
* @param {String} options.product Config for this product ID or slug
* @param {String} options.auth Access Token
* @param {Object} opitons.config Product configuration to update
* @param {Object} options.config Product configuration to update
* @param {String} options.deviceId Device ID to access
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
Expand Down Expand Up @@ -2086,8 +2087,6 @@ class Particle {
* @param {String} options.deviceId Device ID to query
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
* @returns {Promise} A promise
*/
getProductDeviceLocations({ auth, product, dateRange, rectBl, rectTr, deviceId, headers, context }){
Expand Down Expand Up @@ -2120,7 +2119,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
*
* @returns {Promise<{body: {block: ResponseBlock}, statusCode: int}>} A promise that resolves to the created logic block data.
* @returns {Promise<{body: {block: BlockResponse}, statusCode: number}>} A promise that resolves to the created logic block data.
*/
createLogicBlock({ auth, org, block, headers, context }) {
return this.post({
Expand All @@ -2142,7 +2141,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
*
* @returns {Promise<{body: {block: ResponseBlock}, statusCode: int}>} A promise that resolves to the specified logic block data.
* @returns {Promise<{body: {block: BlockResponse}, statusCode: number}>} A promise that resolves to the specified logic block data.
*/
getLogicBlock({ auth, org, blockId, headers, context }) {
return this.get({
Expand All @@ -2158,6 +2157,9 @@ class Particle {
*
* If you include an id on a matcher, it will update the matcher in place.
*
* @typedef {import('./types/blocks').Block} Block
* @typedef {import('./types/blocks').BlockResponse} BlockResponse
*
* @param {Object} options The options for updating the logic block.
* @param {Object} options.auth The authentication object with the API key.
* @param {string} options.org The unique identifier of the organization.
Expand All @@ -2166,7 +2168,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context.
*
* @returns {Promise<{body: {block: ResponseBlock}, statusCode: int}>} A promise that resolves to the updated logic block data.
* @returns {Promise<{body: {block: BlockResponse}, statusCode: number}>} A promise that resolves to the updated logic block data.
*/
updateLogicBlock({ auth, org, blockId, block, headers, context }) {
return this.put({
Expand All @@ -2188,7 +2190,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context.
*
* @returns {Promise<{body: {block_id: int}, statusCode: int}>} A promise that resolves to an object containing the deleted block ID.
* @returns {Promise<{body: {block_id: number}, statusCode: number}>} A promise that resolves to an object containing the deleted block ID.
*/
deleteLogicBlock({ auth, org, blockId, headers, context }) {
return this.delete({
Expand All @@ -2208,7 +2210,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context.
*
* @returns {Promise<{body: {block: ResponseBlock[]}, statusCode: int}>} A promise that resolves to an array of logic block data.
* @returns {Promise<{body: {block: BlockResponse[]}, statusCode: number}>} A promise that resolves to an array of logic block data.
*/
listLogicBlocks({ auth, org, headers, context }) {
return this.get({
Expand All @@ -2229,7 +2231,9 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
*
* @returns {Promise<{body: {blocks: BlockRun[]}, statusCode: int}>} A promise that resolves to an array of block run data.
* @typedef {import('./types/blocks').BlockRun} BlockRun
*
* @returns {Promise<{body: {blocks: BlockRun[]}, statusCode: number}>} A promise that resolves to an array of block run data.
*/
listBlockRuns({ auth, org, blockId, headers, context }) {
return this.get({
Expand All @@ -2251,7 +2255,7 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
*
* @returns {Promise<{body: {block_runs: BlockRun[]}, statusCode: int}>} A promise that resolves to an array of block run data for the specified block run ID.
* @returns {Promise<{body: {block_runs: BlockRun[]}, statusCode: number}>} A promise that resolves to an array of block run data for the specified block run ID.
*/
getBlockRun({ auth, org, blockId, runId, headers, context }) {
return this.get({
Expand All @@ -2273,7 +2277,9 @@ class Particle {
* @param {Object} [options.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
* @param {Object} [options.context] Request context
*
* @returns {Promise<{body: {block_run_log: BlockRunLog}, statusCode: int}>} A promise that resolves to the logs for the specified block run ID.
* @typedef {import('./types/blocks').BlockRunLog} BlockRunLog
*
* @returns {Promise<{body: {block_run_log: BlockRunLog}, statusCode: number}>} A promise that resolves to the logs for the specified block run ID.
*/
getBlockRunLog({ auth, org, blockId, runId, headers, context }) {
return this.get({
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Builds into index.d.ts and serves as the type definition for the package
import Particle from './Particle';
export default Particle;

export * from './types/blocks';
92 changes: 92 additions & 0 deletions src/types/blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Types for the Particle Blocks API
* Responses have the same structure as the request, with additional fields
*/

/**
* @typedef {Object} Block
* @property {boolean} enabled Whether the block is enabled
* @property {string} name The name of the block
* @property {string} description A description of the block
* @property {Logic} logic The logic to run for the block
* @property {Matcher[]} matchers The matchers for the block
*
* @typedef {Object} Logic
* @property {"JavaScript"} type Must be "JavaScript"
* @property {string} code The JavaScript code to run
*
* @typedef {Object} PubSubMatcher
* @property {"PubSub"} type Must be "PubSub"
* @property {boolean} enabled Whether the matcher is enabled
* @property {number} product_id The ID of the product to match
* @property {string} event_name The name of the event to match
*
* @typedef {Object} ChronMatcher
* @property {"Chron"} type Must be "Chron"
* @property {boolean} enabled Whether the matcher is enabled
* @property {string} cron The cron schedule for the matcher
* @property {string} [start_at] The start time for the matcher (ISO 8601 format)
* @property {string} [end_at] The end time for the matcher (ISO 8601 format)
*
* @typedef {(PubSubMatcher | ChronMatcher)} Matcher
*
* @typedef {Object} BlockRun
* @property {number} id The ID of the block run
* @property {number} block_id The ID of the block
* @property {"PubSub"|"Chron"} matcher_type The type of the matcher
* @property {number} matcher_id The ID of the matcher
* @property {"Success"|"Failure"} status The status of the block run
* @property {string} started_at The start time of the block run (ISO 8601 format)
* @property {string} finished_at The end time of the block run (ISO 8601 format)
* @property {string} log_filename The name of the log file
*
* @typedef {Object} Log
* @property {"Info"|"Warn"|"Error"} level The level of the log
* @property {string} timestamp The time of the log (ISO 8601 format)
* @property {any[]} args The arguments for the log
*
* @typedef {Object} BlockRunLog
* @property {Log[]} logs An array of logs for the block run
* @property {string} [err] The error message, if the block execution failed
*/

/**
* @typedef {Object} BlockResponse
* @property {boolean} enabled Whether the block is enabled
* @property {string} name The name of the block
* @property {string} description A description of the block
* @property {Logic} logic The logic to run for the block
* @property {number} id The id of the block
* @property {string} owner_id The id of the owner of the block
* @property {string} version The version of the block
* @property {string} created_at The creation date of the block (ISO 8601 format)
* @property {string} updated_at The update date of the block (ISO 8601 format)
* @property {string} created_by The creator of the block
* @property {string} updated_by The updater of the block
* @property {MatcherResponse[]} matchers The matchers associated with the block
*
* @typedef {Object} PubSubMatcherResponse
* @property {"PubSub"} type Must be "PubSub"
* @property {boolean} enabled Whether the matcher is enabled
* @property {number} product_id The ID of the product to match
* @property {string} event_name The name of the event to match
* @property {string} id The id of the matcher
* @property {string} block_id The id of the block
* @property {number} version The version of the matcher
* @property {string} last_scheduled_at The last time the matcher was scheduled (ISO 8601 format)
* @property {string} next_unscheduled_at The next time the matcher will be scheduled (ISO 8601 format)
*
* @typedef {Object} ChronMatcherResponse
* @property {"Chron"} type Must be "Chron"
* @property {boolean} enabled Whether the matcher is enabled
* @property {string} cron The cron schedule for the matcher
* @property {string} start_at The start time for the matcher (ISO 8601 format)
* @property {string} end_at The end time for the matcher (ISO 8601 format)
* @property {string} id The id of the matcher
* @property {string} block_id The id of the block
*
* @typedef {(PubSubMatcherResponse | ChronMatcherResponse)} MatcherResponse
*/

// export all JSDoc types
module.exports = {};
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"declarationMap": true,
"outDir": "./types",
"allowJs": true,
"checkJs": false
},
"include": ["src/**/*.js"],
"exclude": ["node_modules", "test"]
}