@idfy/sdk
Version:
Node.js SDK for Idfy REST API
86 lines • 3.69 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const HttpRequestor_1 = require("../infrastructure/HttpRequestor");
const IdfyConfiguration_1 = require("../IdfyConfiguration");
const Urls_1 = require("../infrastructure/Urls");
class IdfyBaseService {
constructor(clientId, clientSecret, scopes) {
this.oauthToken = null;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.scopes = scopes;
}
get(url) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.get(url, token.access_token);
});
}
getBuffer(url) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.getBuffer(url, token.access_token);
});
}
post(url, body) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.post(url, body, token.access_token);
});
}
patch(url, body) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.patch(url, body, token.access_token);
});
}
put(url, body) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.put(url, body, token.access_token);
});
}
delete(url) {
return __awaiter(this, void 0, void 0, function* () {
const token = yield this.getToken();
return HttpRequestor_1.HttpRequestor.delete(url, token.access_token);
});
}
getToken() {
if (this.oauthToken && this.oauthToken.expiry > new Date()) {
return Promise.resolve(this.oauthToken);
}
let clientId = this.clientId;
let clientSecret = this.clientSecret;
let scopes = this.scopes;
if (!clientId) {
clientId = IdfyConfiguration_1.IdfyConfiguration.clientId;
clientSecret = IdfyConfiguration_1.IdfyConfiguration.clientSecret;
scopes = IdfyConfiguration_1.IdfyConfiguration.scopes;
}
const form = {
grant_type: 'client_credentials',
scope: scopes ? scopes.join(' ') : '',
client_id: clientId,
client_secret: clientSecret,
};
const promise = HttpRequestor_1.HttpRequestor.postForm(Urls_1.default.oauthToken, form);
promise.then((res) => {
this.oauthToken = res;
const now = new Date();
this.oauthToken.expiry = now.setSeconds(now.getSeconds() + res.expires_in);
});
return promise;
}
}
exports.default = IdfyBaseService;
//# sourceMappingURL=IdfyBaseService.js.map