UNPKG

@complycube/api

Version:

ComplyCube's Node.js library for the AML/KYC API

566 lines (538 loc) 17.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var axios = _interopDefault(require('axios')); class ComplyCubeError extends Error { constructor(message) { super(message); this.name = "ComplyCubeError"; } } class ComplyCubeApiError extends ComplyCubeError { constructor(message, responseBody, statusCode, type, param) { super(message); this.name = "ComplyCubeApiError"; this.responseBody = responseBody; this.statusCode = statusCode; this.type = type; this.param = param; } static fromResponse(responseBody, statusCode) { const innerErrorData = responseBody instanceof Object ? responseBody : {}; const innerError = innerErrorData instanceof Object ? innerErrorData : {}; const type = `${innerError.type || "unknown"}`; const message = `${innerError.message || responseBody}`; const param = innerError.param; const fullMessage = `${message} (status code ${statusCode})`; return new ComplyCubeApiError(fullMessage, responseBody, statusCode, type, param); } isClientError() { return this.statusCode < 500; } } var Method; (function (Method) { Method["GET"] = "get"; Method["POST"] = "post"; Method["DELETE"] = "delete"; })(Method || (Method = {})); const convertAxiosErrorToComplyCubeError = async (error) => { if (!error.response) { return new ComplyCubeError(error.message || "An unknown error occurred making the request"); } // Received a 4XX or 5XX response. const response = error.response; const data = response.data; return ComplyCubeApiError.fromResponse(data, response.status); }; const handleResponse = async (request) => { try { const response = await request; const data = response.data; return data; } catch (error) { throw await convertAxiosErrorToComplyCubeError(error); } }; class Resource { constructor(name, axiosInstance) { this.name = name; this.axiosInstance = axiosInstance; } async request({ method, path = "", body, query }) { const request = this.axiosInstance({ method, url: `${this.name}/${path}`, data: body, params: query }); return handleResponse(request); } async upload(path, body, documentSide) { const finalPath = documentSide ? `${path}/upload/${documentSide}` : path; const request = this.axiosInstance({ method: Method.POST, url: `${this.name}/${finalPath}`, data: body }); return handleResponse(request); } async download(path, documentSide) { const finalPath = documentSide ? `${path}/download/${documentSide}` : path; const request = this.axiosInstance({ method: Method.GET, url: `${this.name}/${finalPath}` }); return handleResponse(request); } async validateCheck(path, validation) { const request = this.axiosInstance({ method: Method.POST, url: `${this.name}/${path}`, data: validation }); return handleResponse(request); } async completeSession(path) { const request = this.axiosInstance({ method: Method.POST, url: `${this.name}/${path}` }); return handleResponse(request); } } class AccountInfo extends Resource { constructor(axiosInstance) { super("accountInfo", axiosInstance); } async get() { return this.request({ method: Method.GET }); } } class Addresses extends Resource { constructor(axiosInstance) { super("addresses", axiosInstance); } async create(clientId, addressRequest) { return this.request({ method: Method.POST, body: Object.assign({ clientId }, addressRequest) }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async update(id, addressRequest) { return this.request({ method: Method.POST, path: id, body: addressRequest }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list(clientId, { page, pageSize } = {}) { const { items: addresses } = await this.request({ method: Method.GET, query: { clientId, page, pageSize } }); return addresses; } } class AuditLogs extends Resource { constructor(axiosInstance) { super("auditLogs", axiosInstance); } async get(id) { return this.request({ method: Method.GET, path: id }); } async list({ page, pageSize } = {}) { const { items: auditLogs } = await this.request({ method: Method.GET, query: { page, pageSize } }); return auditLogs; } } class Autofill extends Resource { constructor(axiosInstance) { super("autofill", axiosInstance); } async performAutofill(performAutofillRequest) { return this.request({ method: Method.POST, body: performAutofillRequest }); } } class Checks extends Resource { constructor(axiosInstance) { super("checks", axiosInstance); } async create(clientId, checkRequest) { return this.request({ method: Method.POST, body: Object.assign({ clientId }, checkRequest) }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async update(id, checkRequest) { return this.request({ method: Method.POST, path: id, body: checkRequest }); } async validate(id, validateRequest) { const path = `${id}/validate`; return super.validateCheck(path, validateRequest); } async list(clientId, { page, pageSize } = {}) { const { items: checks } = await this.request({ method: Method.GET, query: { clientId, page, pageSize } }); return checks; } } class Clients extends Resource { constructor(axiosInstance) { super("clients", axiosInstance); } async create(clientRequest) { return this.request({ method: Method.POST, body: clientRequest }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async update(id, clientRequest) { return this.request({ method: Method.POST, path: id, body: clientRequest }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list({ page, pageSize } = {}) { const { items: clients } = await this.request({ method: Method.GET, query: { page, pageSize } }); return clients; } } class Documents extends Resource { constructor(axiosInstance) { super("documents", axiosInstance); } async create(clientId, documentRequest) { return this.request({ method: Method.POST, body: Object.assign({ clientId }, documentRequest) }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async update(id, documentRequest) { return this.request({ method: Method.POST, path: id, body: documentRequest }); } async upload(id, imageUploadRequest, documentSide) { return super.upload(id, imageUploadRequest, documentSide); } async download(id, documentSide) { return super.download(id, documentSide); } async deleteImage(id, documentSide) { const path = `${id}/${documentSide}`; await this.request({ method: Method.DELETE, path }); } async redact(id, redactionRequest) { return this.request({ method: Method.POST, path: `${id}/redact`, body: redactionRequest }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list(clientId, { page, pageSize } = {}) { const { items: documents } = await this.request({ method: Method.GET, query: { clientId, page, pageSize } }); return documents; } } class Flow extends Resource { constructor(axiosInstance) { super("flow/sessions", axiosInstance); } async createSession(clientId, flowRequest) { return this.request({ method: Method.POST, body: Object.assign({ clientId }, flowRequest) }); } } class LivePhotos extends Resource { constructor(axiosInstance) { super("livePhotos", axiosInstance); } async upload(clientId, livePhotoRequest) { return this.request({ method: Method.POST, body: Object.assign({ clientId }, livePhotoRequest) }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async download(id) { return super.download(`${id}/download`); } async redact(id) { return this.request({ method: Method.POST, path: `${id}/redact` }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list(clientId, { page, pageSize } = {}) { const { items: livePhotos } = await this.request({ method: Method.GET, query: { clientId, page, pageSize } }); return livePhotos; } } class LiveVideos extends Resource { constructor(axiosInstance) { super("liveVideos", axiosInstance); } async get(id) { return this.request({ method: Method.GET, path: id }); } async redact(id) { return this.request({ method: Method.POST, path: `${id}/redact` }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list(clientId, { page, pageSize } = {}) { const { items: liveVideos } = await this.request({ method: Method.GET, query: { clientId, page, pageSize } }); return liveVideos; } } class Lookups extends Resource { constructor(axiosInstance) { super("lookup", axiosInstance); } async searchCompany(searchCompanyRequest) { const path = `companies`; return this.request({ method: Method.POST, body: Object.assign({}, searchCompanyRequest), path }); } async getCompany(id) { const path = `companies/${id}`; return this.request({ method: Method.GET, path }); } async searchAddress(searchAddressRequest) { const path = `addresses`; return this.request({ method: Method.POST, body: Object.assign({}, searchAddressRequest), path }); } } class Reports extends Resource { constructor(axiosInstance) { super("reports", axiosInstance); } async generate(reportRequest) { return await this.request({ method: Method.GET, query: Object.assign({}, reportRequest) }); } } class RiskProfiles extends Resource { constructor(axiosInstance) { super("clients", axiosInstance); } async get(clientId) { return this.request({ method: Method.GET, path: `${clientId}/riskProfile` }); } } class Static extends Resource { constructor(axiosInstance) { super("static", axiosInstance); } async listScreeningLists() { const path = `screeningLists`; return this.request({ method: Method.GET, path }); } async listSupportedDocuments() { const path = `supportedDocuments`; return this.request({ method: Method.GET, path }); } } class TeamMembers extends Resource { constructor(axiosInstance) { super("teamMembers", axiosInstance); } async get(id) { return this.request({ method: Method.GET, path: id }); } async list({ page, pageSize } = {}) { const { items: teamMembers } = await this.request({ method: Method.GET, query: { page, pageSize } }); return teamMembers; } } class Tokens extends Resource { constructor(axiosInstance) { super("tokens", axiosInstance); } async generate(clientId, tokenRequest) { const { token } = await this.request({ method: Method.POST, body: Object.assign({ clientId }, tokenRequest) }); return token; } } class Webhooks extends Resource { constructor(axiosInstance) { super("webhooks", axiosInstance); } async create(webhookRequest) { return this.request({ method: Method.POST, body: webhookRequest }); } async get(id) { return this.request({ method: Method.GET, path: id }); } async update(id, webhookRequest) { return this.request({ method: Method.POST, path: id, body: webhookRequest }); } async delete(id) { await this.request({ method: Method.DELETE, path: id }); } async list({ page, pageSize } = {}) { const { items: webhooks } = await this.request({ method: Method.GET, query: { page, pageSize } }); return webhooks; } } class WorkflowSessions extends Resource { constructor(axiosInstance) { super("workflowSessions", axiosInstance); } async get(id) { return this.request({ method: Method.GET, path: id }); } async complete(id) { const path = `${id}/complete`; return super.completeSession(path); } async list(clientId, workflowTemplateId, { page, pageSize } = {}) { const { items: workflowSessions } = await this.request({ method: Method.GET, query: { clientId, workflowTemplateId, page, pageSize } }); return workflowSessions; } } const baseURL = "https://api.complycube.com/v1"; class ComplyCube { constructor({ apiKey }) { if (!apiKey) { throw new Error("No apiKey provided"); } this.axiosInstance = axios.create({ baseURL, headers: { Authorization: apiKey, "Content-Type": "application/json", "User-Agent": "complycube-nodejs/1.1.13" } }); this.client = new Clients(this.axiosInstance); this.document = new Documents(this.axiosInstance); this.livePhoto = new LivePhotos(this.axiosInstance); this.liveVideo = new LiveVideos(this.axiosInstance); this.check = new Checks(this.axiosInstance); this.workflowSession = new WorkflowSessions(this.axiosInstance); this.report = new Reports(this.axiosInstance); this.address = new Addresses(this.axiosInstance); this.riskProfile = new RiskProfiles(this.axiosInstance); this.webhook = new Webhooks(this.axiosInstance); this.auditLog = new AuditLogs(this.axiosInstance); this.token = new Tokens(this.axiosInstance); this.teamMember = new TeamMembers(this.axiosInstance); this.flow = new Flow(this.axiosInstance); this.static = new Static(this.axiosInstance); this.lookup = new Lookups(this.axiosInstance); this.autofill = new Autofill(this.axiosInstance); this.accountInfo = new AccountInfo(this.axiosInstance); } } let crypto; try { // tslint:disable-next-line: no-var-requires crypto = require("crypto"); // tslint:disable-next-line:no-empty } catch (_a) { } class EventVerifier { constructor(secret) { this.secret = secret; } constructEvent(requestBody, signature) { if (!crypto) { throw new Error("Crypto support required to verify events"); } const givenSignature = Buffer.from(signature, "hex"); const hmac = crypto.createHmac("sha256", this.secret); hmac.update(requestBody); const eventSignature = hmac.digest(); if (!crypto.timingSafeEqual(givenSignature, eventSignature)) { throw new ComplyCubeError("Invalid signature for event"); } const result = JSON.parse(requestBody.toString()); return result; } } exports.AccountInfo = AccountInfo; exports.Autofill = Autofill; exports.ComplyCube = ComplyCube; exports.ComplyCubeApiError = ComplyCubeApiError; exports.ComplyCubeError = ComplyCubeError; exports.EventVerifier = EventVerifier; exports.Flow = Flow; exports.Lookups = Lookups; exports.Static = Static; //# sourceMappingURL=index.js.map