@onfido/api
Version:
Node.js library for the Onfido API
123 lines (122 loc) • 5.82 kB
JavaScript
;
/* tslint:disable */
/* eslint-disable */
/**
* Onfido Public API v3.6
* The Onfido Public API (v3.6)
*
* The version of the OpenAPI document: v3.6
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
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 });
exports.Configuration = exports.Region = void 0;
const base_1 = require("./base");
const axios_1 = require("axios");
var Region;
(function (Region) {
Region[Region["EU"] = 0] = "EU";
Region[Region["US"] = 1] = "US";
Region[Region["CA"] = 2] = "CA";
})(Region = exports.Region || (exports.Region = {}));
class Configuration {
constructor(param = {}) {
var _a;
const hasApiToken = !!param.apiToken;
const hasPartialOAuth = !!(param.oauthClientId || param.oauthClientSecret);
const hasOAuth = !!(param.oauthClientId && param.oauthClientSecret);
if (hasPartialOAuth && !hasOAuth) {
throw new Error("Both oauthClientId and oauthClientSecret must be provided together");
}
if (!hasApiToken && !hasOAuth) {
throw new Error("No apiToken or OAuth credentials (oauthClientId + oauthClientSecret) provided");
}
if (hasApiToken && hasOAuth) {
throw new Error("Provide either apiToken or OAuth credentials (oauthClientId + oauthClientSecret), not both");
}
if (param.region && !Object.values(Region).includes(param.region)) {
throw new Error(`Unknown or missing region '${param.region}'`);
}
const regionStr = Region[param.region || Region.EU].toLowerCase();
this.basePath = param.basePath || base_1.BASE_PATH.replace('.eu.', `.${regionStr}.`);
if (hasApiToken) {
this.apiKey = 'Token token=' + param.apiToken;
}
else {
this._oauthClientId = param.oauthClientId;
this._oauthClientSecret = param.oauthClientSecret;
this._oauthTokenUrl = this.basePath + '/oauth/token';
this.accessToken = () => __awaiter(this, void 0, void 0, function* () { return this._getOAuthAccessToken(); });
}
this.baseOptions = Object.assign(Object.assign({ timeout: 30000 }, param.baseOptions), { headers: Object.assign(Object.assign({}, (_a = param.baseOptions) === null || _a === void 0 ? void 0 : _a.headers), { 'User-Agent': "onfido-node/6.2.0" }) });
this.formDataCtor = param.formDataCtor || require('form-data'); // Injiect form data constructor (if needed)
}
/**
* Fetches and caches an OAuth2 access token using the client credentials flow.
* Automatically refreshes the token when it expires (with a 30-second buffer).
*/
_getOAuthAccessToken() {
return __awaiter(this, void 0, void 0, function* () {
const now = Date.now();
if (this._cachedAccessToken && this._tokenExpiresAt && now < this._tokenExpiresAt) {
return this._cachedAccessToken;
}
if (this._tokenRefreshPromise) {
return this._tokenRefreshPromise;
}
this._tokenRefreshPromise = this._fetchOAuthToken();
try {
return yield this._tokenRefreshPromise;
}
finally {
this._tokenRefreshPromise = undefined;
}
});
}
/**
* Performs the actual token exchange via POST to the OAuth2 token endpoint.
*/
_fetchOAuthToken() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const response = yield axios_1.default.post(this._oauthTokenUrl, new URLSearchParams({
client_id: this._oauthClientId,
client_secret: this._oauthClientSecret,
}).toString(), Object.assign(Object.assign({}, this.baseOptions), { headers: Object.assign(Object.assign({}, (_a = this.baseOptions) === null || _a === void 0 ? void 0 : _a.headers), { 'Content-Type': 'application/x-www-form-urlencoded' }) }));
const { access_token, expires_in } = response.data;
if (!access_token) {
throw new Error('OAuth2 token response did not contain an access_token');
}
this._cachedAccessToken = access_token;
this._tokenExpiresAt = Date.now() + ((expires_in) - 30) * 1000;
return access_token;
});
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
isJsonMime(mime) {
const jsonMime = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
}
exports.Configuration = Configuration;