Skip to content
Merged
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
62 changes: 62 additions & 0 deletions examples/remind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Adds a suggestion to remind you to read a link.
//
// Usage:
// export RANGE_ACCESS_KEY=....
// node remind.js https://blog.remote.com/why-you-should-be-doing-async-work/

const https = require('https');
const Range = require('../lib/range');

const htmlURL = process.argv[2];

console.log('Fetching', htmlURL);
const req = https.get(htmlURL, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', data => (body += data));
res.on('end', () => {
if (res.statusCode !== 200) {
fail(`status ${res.statusCode}`);
return;
}
const payload = parse(body);
new Range()
.addSuggestion(payload)
.then(data => {
console.log('Suggestion made');
})
.catch(fail);
});
});
req.on('error', fail);
req.end();

function parse(body) {
let title;
const matches = body.match(/<title[^>]*>(.*)<\/title>/);
if (matches && matches[1]) {
title = matches[1];
} else {
title = htmlURL;
}

return {
snippet_type: 1,
dedupe_strategy: 'UPSERT_PENDING',
reason: 'ASSIGNED',
is_future: true,
attachment: {
source_id: htmlURL.replace(/[^a-zA-Z0-9]/g, ''),
type: 'LINK',
provider: 'readinglist',
provider_name: 'Reading List',
name: title,
html_url: htmlURL,
},
};
}

function fail(err) {
console.error('Error:', err);
process.exit(1);
}
16 changes: 14 additions & 2 deletions lib/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class Range {
return this._get('users/auth-user', null, params);
}

addSuggestion(args, params = {}) {
return this._post('suggestions', args, params);
}

_get(path, query = {}, params = {}) {
return this._request(path, {
...params,
Expand Down Expand Up @@ -118,7 +122,13 @@ class Range {
try {
payload = JSON.parse(body);
} catch (e) {
reject(new NetworkError(`server error: failed to parse response body`, res.statusCode));
// Non JSON response means an error before the response handler.
reject(
new NetworkError(
`server error: failed to parse response body: "${body.trim()}"`,
res.statusCode
)
);
return;
}
if (res.statusCode !== 200) {
Expand All @@ -129,7 +139,9 @@ class Range {
}
});
});
req.on('error', e => reject(new RangeError(`connection error: ${e.message}`)));
req.on('error', e => {
reject(new RangeError(`connection error: ${e.message}`));
});
if (params.body) {
req.write(params.body);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "range-sdk",
"description": "Range SDK for node",
"version": "1.0.0",
"version": "1.1.0",
"scripts": {
"lint": "./node_modules/.bin/eslint **/*.js",
"fmt": "./node_modules/.bin/prettier --write **/*.js"
Expand Down