Skip to content

Commit c00db93

Browse files
authored
Merge pull request #170 from particle-iot/feature/sc-121448/configure-typescript-with-just-checkjs
feature/sc-121448/configure-typescript-with-just-checkjs
2 parents f202a67 + 1b939e4 commit c00db93

File tree

8 files changed

+229
-125
lines changed

8 files changed

+229
-125
lines changed

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
"babel-watch": "babel src -d lib --watch --source-maps",
88
"prepublish": "npm run lint && npm run compile",
99
"compile": "babel src -sd lib",
10-
"test": "npm run lint && npm run test:unit",
10+
"test": "npm run lint && npm run typecheck && npm run test:unit",
1111
"test:ci": "npm run lint && npm run test:unit -- --forbid-only && npm run coverage",
1212
"test:unit": "mocha test/ -R spec --compilers js:babel-register",
1313
"test:unit:silent": "npm run test:unit > tmp/test-unit-log.txt 2>&1",
1414
"test:browser": "karma start --single-run",
1515
"test:watch": "npm run test:unit -- --watch",
16+
"typecheck": "tsc --noEmit",
1617
"coverage": "nyc --reporter=text --include='src/**/*.js' --temp-dir=./tmp/ --check-coverage --lines 91 npm run test:unit:silent",
1718
"lint": "eslint . --ext .js --format unix --ignore-path .gitignore --ignore-pattern \"dist/*\"",
1819
"lint:fix": "npm run lint -- --fix",
@@ -46,6 +47,7 @@
4647
],
4748
"license": "Apache-2.0",
4849
"devDependencies": {
50+
"@types/node": "^20.5.9",
4951
"babel-cli": "^6.9.0",
5052
"babel-eslint": "^6.0.4",
5153
"babel-plugin-add-module-exports": "^0.1.2",
@@ -73,6 +75,7 @@
7375
"should": "^9.0.0",
7476
"sinon": "^7.2.5",
7577
"sinon-chai": "^3.7.0",
78+
"typescript": "^5.2.2",
7679
"watchify": "^3.7.0"
7780
},
7881
"dependencies": {

src/Agent.js

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import packageJson from '../package.json';
3232

3333
/**
3434
* The possible response from an API request
35-
* @typedef {JSONResponse|Buffer|arrayBuffer} RequestResponse The type is based on
35+
* @typedef {JSONResponse|Buffer|ArrayBuffer} RequestResponse The type is based on
3636
* the request config and whether is on browser or node
3737
*/
3838

@@ -57,11 +57,12 @@ export default class Agent {
5757

5858
/**
5959
* Make a GET request
60-
* @param {string} uri The URI to request
61-
* @param {string} [auth] Authorization token to use
62-
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
63-
* @param {object} [query] Key/Value pairs of query params
64-
* @param {object} [context] The invocation context, describing the tool and project
60+
* @param {object} params Configurations to customize the request
61+
* @param {string} params.uri The URI to request
62+
* @param {string|object} [params.auth] Authorization token to use
63+
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
64+
* @param {string|object} [params.query] Key/Value pairs of query params or a correctly formatted string
65+
* @param {object} [params.context] The invocation context, describing the tool and project
6566
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
6667
*/
6768
get({ uri, auth, headers, query, context }) {
@@ -70,11 +71,12 @@ export default class Agent {
7071

7172
/**
7273
* Make a HEAD request
73-
* @param {string} uri The URI to request
74-
* @param {string} [auth] Authorization token to use
75-
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
76-
* @param {object} [query] Key/Value pairs of query params
77-
* @param {object} [context] The invocation context, describing the tool and project
74+
* @param {object} params Configurations to customize the request
75+
* @param {string} params.uri The URI to request
76+
* @param {string|object} [params.auth] Authorization token to use
77+
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
78+
* @param {string|object} [params.query] Key/Value pairs of query params or a correctly formatted string
79+
* @param {object} [params.context] The invocation context, describing the tool and project
7880
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
7981
*/
8082
head({ uri, auth, headers, query, context }) {
@@ -83,11 +85,12 @@ export default class Agent {
8385

8486
/**
8587
* Make a POST request
86-
* @param {string} uri The URI to request
87-
* @param {string} [auth] Authorization token to use
88-
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
89-
* @param {object} [data] Payload to send in the request body in JSON format
90-
* @param {object} [context] The invocation context, describing the tool and project
88+
* @param {object} params Configurations to customize the request
89+
* @param {string} params.uri The URI to request
90+
* @param {string|object} [params.auth] Authorization token to use
91+
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
92+
* @param {object} [params.data] Key/Value pairs of query params or a correctly formatted string
93+
* @param {object} [params.context] The invocation context, describing the tool and project
9194
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
9295
*/
9396
post({ uri, headers, data, auth, context }) {
@@ -96,11 +99,12 @@ export default class Agent {
9699

97100
/**
98101
* Make a PUT request
99-
* @param {string} uri The URI to request
100-
* @param {string} [auth] Authorization token to use
101-
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
102-
* @param {object} [data] Payload to send in the request body in JSON format
103-
* @param {object} [context] The invocation context, describing the tool and project
102+
* @param {object} params Configurations to customize the request
103+
* @param {string} params.uri The URI to request
104+
* @param {string|object} [params.auth] Authorization token to use
105+
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
106+
* @param {object} [params.data] Key/VAlue pairs of query params or a correctly formatted string
107+
* @param {object} [params.context] The invocation context, describing the tool and project
104108
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
105109
*/
106110
put({ uri, auth, headers, data, context }) {
@@ -109,11 +113,12 @@ export default class Agent {
109113

110114
/**
111115
* Make a DELETE request
112-
* @param {string} uri The URI to request
113-
* @param {string} [auth] Authorization token to use
114-
* @param {object} [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
115-
* @param {object} [data] Payload to send in the request body in JSON format
116-
* @param {object} [context] The invocation context, describing the tool and project
116+
* @param {object} params Configurations to customize the request
117+
* @param {string} params.uri The URI to request
118+
* @param {string|object} [params.auth] Authorization token to use
119+
* @param {object} [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
120+
* @param {object} [params.data] Key/Value pairs of query params or a correctly formatted string
121+
* @param {object} [params.context] The invocation context, describing the tool and project
117122
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
118123
*/
119124
delete({ uri, auth, headers, data, context }) {
@@ -122,17 +127,17 @@ export default class Agent {
122127

123128
/**
124129
*
125-
* @param {Object} config An obj with all the possible request configurations
126-
* @param {String} config.uri The URI to request
127-
* @param {String} config.method The method used to request the URI, should be in uppercase.
128-
* @param {Object} config.headers Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
129-
* @param {Object} config.data Arbitrary data to send as the body.
130-
* @param {Object} config.auth Authorization
131-
* @param {String} config.query Query parameters
132-
* @param {Object} config.form Form fields
133-
* @param {Object} config.files array of file names and file content
134-
* @param {Object} config.context the invocation context, describing the tool and project.
135-
* @param {boolean} config.isBuffer Indicate if the response should be treated as Buffer instead of JSON
130+
* @param {object} config An obj with all the possible request configurations
131+
* @param {string} config.uri The URI to request
132+
* @param {string} config.method The method used to request the URI, should be in uppercase.
133+
* @param {object} [config.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
134+
* @param {object} [config.data] Arbitrary data to send as the body.
135+
* @param {string|object} [config.auth] Authorization
136+
* @param {string|object} [config.query] Query parameters
137+
* @param {object} [config.form] Form fields
138+
* @param {object} [config.files] Array of file names and file content
139+
* @param {object} [config.context] The invocation context, describing the tool and project.
140+
* @param {boolean} [config.isBuffer=false] Indicate if the response should be treated as Buffer instead of JSON
136141
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
137142
*/
138143
request({
@@ -156,7 +161,7 @@ export default class Agent {
156161
* Promises to send the request and retreive the response.
157162
* @param {[string, object]} requestParams First argument is the URI to request, the second one are the options.
158163
* @param {boolean} isBuffer Indicate if the response body should be returned as a Buffer (Node) / ArrayBuffer (browser) instead of JSON
159-
* @param {Fetch} [makerequest] The fetch function to use. Override for testing.
164+
* @param {function} [makerequest=fetch] The fetch function to use. Override for testing.
160165
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
161166
* @private
162167
*/
@@ -213,6 +218,11 @@ export default class Agent {
213218
});
214219
}
215220

221+
/**
222+
* Generate the params in a format valid for 'fetch'
223+
* @returns {[string, object]} The uri to make the request too, and extra configs
224+
* @private
225+
*/
216226
_buildRequest({ uri, method, headers, data, auth, query, form, files, context }){
217227
let actualUri = uri;
218228
if (this.baseUrl && uri[0] === '/') {
@@ -228,6 +238,7 @@ export default class Agent {
228238
let body;
229239
let contentTypeHeader;
230240
if (files){
241+
// @ts-ignore
231242
contentTypeHeader = {}; // Needed to allow fetch create its own
232243
body = this._getFromData(files, form);
233244
} else if (form){
@@ -352,7 +363,7 @@ export default class Agent {
352363

353364
/**
354365
* Adds an authorization header.
355-
* @param {string} auth The authorization bearer token.
366+
* @param {string|object} auth The authorization bearer token.
356367
* @returns {object} The original request.
357368
*/
358369
_getAuthorizationHeader(auth){

src/Client.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import Library from './Library';
44

55
export default class Client {
66
constructor({ auth, api = new Particle() }){
7-
Object.assign(this, { auth, api });
7+
this.auth = auth;
8+
this.api = api;
89
}
910

1011
ready(){
@@ -84,8 +85,9 @@ export default class Client {
8485

8586
/**
8687
* Delete an entire published library
87-
* @param {String} $0.name Name of the library to delete
88-
* @param {String} $0.force Key to force deleting a public library
88+
* @param {object} params Specific params of the library to delete
89+
* @param {string} params.name Name of the library to delete
90+
* @param {string} params.force Key to force deleting a public library
8991
* @returns {Promise} A promise
9092
*/
9193
deleteLibrary({ name, force }){
@@ -117,8 +119,9 @@ export default class Client {
117119
}
118120

119121
/**
120-
* @param {String} $0.deviceId Device ID or Name
121-
* @param {Boolean} $0.signal Signal on or off
122+
* @param {object} params
123+
* @param {string} params.deviceId Device ID or Name
124+
* @param {boolean} params.signal Signal on or off
122125
* @returns {Promise} A promise
123126
* @deprecated Will be removed in 6.5
124127
*/
@@ -156,7 +159,7 @@ export default class Client {
156159
}, () => {});
157160
}
158161

159-
trackingIdentity({ full=false, context }={}){
162+
trackingIdentity({ full = false, context = undefined }={}){
160163
return this.api.trackingIdentity({ full, context, auth: this.auth })
161164
.then(payload => {
162165
return payload.body;

src/EventStream.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class EventStream extends EventEmitter {
3232
path: `${path}?access_token=${this.token}`,
3333
method: 'get',
3434
port: parseInt(port, 10) || (isSecure ? 443 : 80),
35+
// @ts-ignore
3536
mode: 'prefer-streaming'
3637
});
3738

@@ -70,7 +71,9 @@ class EventStream extends EventEmitter {
7071
// since we are already about to reject the promise anyway
7172
} finally {
7273
let errorDescription = `HTTP error ${statusCode} from ${this.uri}`;
74+
// @ts-ignore
7375
if (body && body.error_description) {
76+
// @ts-ignore
7477
errorDescription += ' - ' + body.error_description;
7578
}
7679
reject({ statusCode, errorDescription, body });

src/Library.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class Library {
2323
if (!this.downloadUrl) {
2424
return Promise.reject(new Error('No download URL for this library'));
2525
}
26+
// @ts-ignore
2627
return this.client.downloadFile(this.downloadUrl);
2728
}
2829

0 commit comments

Comments
 (0)