node-flipr-client
Version:
Node client for the Flipr Developper API
63 lines • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const url_1 = require("url");
class FliprClient {
constructor() {
this.debug = (0, debug_1.default)('node-flipr');
this.origin = 'https://apis.goflipr.com';
}
async authenticate(username, password) {
const res = await this.post('/OAuth2/token', new url_1.URLSearchParams({
grant_type: 'password',
username: username,
password: password,
}));
const json = await res.json();
if (!res.ok) {
throw `${res.status} ${res.statusText}: ${json.error} ${json.error_description}`;
}
this.access_token = json.access_token;
}
async request(method, path, body) {
let headers = {};
if (method === 'GET') {
headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.access_token}`,
};
}
if (method === 'POST') {
headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
}
return await (0, node_fetch_1.default)(`${this.origin}/${path}`, {
method,
headers,
body,
});
}
async get(path) {
return await this.request('GET', path);
}
async post(path, body) {
return await this.request('POST', path, body);
}
async modules() {
const fliprModulesResponse = await this.get('/modules');
if (!fliprModulesResponse.ok) {
this.debug('Could not fetch Flipr Modules', fliprModulesResponse);
return [];
}
return await fliprModulesResponse.json();
}
async lastSurvey(serial) {
const res = await this.get(`/modules/${serial}/survey/last`);
return await res.json();
}
}
exports.default = FliprClient;
//# sourceMappingURL=index.js.map