UNPKG

@usedigisign/node-sdk

Version:

Node.js library for the DigiSign API

443 lines (426 loc) 13.2 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // lib/index.ts var lib_exports = {}; __export(lib_exports, { RequestMessage: () => RequestMessage, SessionEnvironment: () => SessionEnvironment, TemplateRecipient: () => TemplateRecipient, WebhookEventType: () => WebhookEventType, WebhookStatus: () => WebhookStatus, createSession: () => createSession }); module.exports = __toCommonJS(lib_exports); // lib/templates/types/index.ts var RequestMessage = class { }; var TemplateRecipient = class { }; // lib/webhooks/types/index.ts var WebhookStatus = /* @__PURE__ */ ((WebhookStatus2) => { WebhookStatus2["active"] = "active"; WebhookStatus2["inactive"] = "inactive"; return WebhookStatus2; })(WebhookStatus || {}); var WebhookEventType = /* @__PURE__ */ ((WebhookEventType2) => { WebhookEventType2["DOCUMENT_VIEWED"] = "document.viewed"; WebhookEventType2["DOCUMENT_SIGNED"] = "document.signed"; WebhookEventType2["DOCUMENT_REJECTED"] = "document.rejected"; WebhookEventType2["DOCUMENT_COMPLETED"] = "document.completed"; return WebhookEventType2; })(WebhookEventType || {}); // lib/types/index.ts var SessionEnvironment = /* @__PURE__ */ ((SessionEnvironment2) => { SessionEnvironment2["SANDBOX"] = "sandbox"; SessionEnvironment2["PRODUCTION"] = "production"; return SessionEnvironment2; })(SessionEnvironment || {}); // package.json var version = "0.1.1"; // lib/factory/index.ts var import_axios5 = require("axios"); var import_lodash = require("lodash"); // lib/utils/axios/index.ts var import_axios = __toESM(require("axios")); var defaultOptions = { baseURL: process.env.DIGISIGN_BASE_URL || "https://api.usedigisign.com" }; var instance = import_axios.default.create(defaultOptions); var createAPIRequest = (config) => instance(config); // lib/utils/fn/index.ts var getURI = (env) => { if (env === "sandbox" /* SANDBOX */) return "https://sandbox.usedigisign.dev"; return "https://api.usedigisign.com"; }; async function createRequest(config) { return await createAPIRequest(config); } // lib/utils/definitions/index.ts var APIDefinition = class { }; // lib/keys/index.ts var Keys = class extends APIDefinition { constructor(DSFactory2) { super(); this.DSFactory = DSFactory2; this.extendConfig = (config) => { return Object.assign({}, this.baseConfig, config); }; this.headers = DSFactory2.headers; this.baseConfig = { baseURL: getURI(DSFactory2.env) }; } async list(options) { const config = this.extendConfig({ method: "GET", url: "/v1/keys", headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []), per_page: options?.limit ?? 10, page: options?.page ?? 1, sort: options?.sort } }); const response = await createRequest(config); return response.data; } async delete(key) { const config = this.extendConfig({ method: "DELETE", url: `/v1/keys/${key}`, headers: this.headers }); const response = await createRequest(config); return response.data; } async get(key, options) { const config = this.extendConfig({ method: "GET", url: `/v1/keys/${key}`, headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []) } }); const response = await createRequest(config); return response.data; } }; // lib/workspaces/index.ts var Workspaces = class { constructor(DSFactory2) { this.DSFactory = DSFactory2; this.extendConfig = (config) => { return Object.assign({}, this.baseConfig, config); }; this.headers = DSFactory2.headers; this.baseConfig = { baseURL: getURI(DSFactory2.env) }; } async list(options) { const config = this.extendConfig({ method: "GET", url: "/v1/workspaces", headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []), per_page: options?.limit ?? 10, page: options?.page ?? 1, sort: options?.sort } }); const response = await createRequest(config); return response.data; } async get(id) { const config = this.extendConfig({ method: "GET", url: `/v1/workspaces/${id}`, headers: this.headers }); const response = await createRequest(config); return response.data; } }; // lib/webhooks/index.ts var import_axios3 = require("axios"); var Webhooks = class _Webhooks { constructor(DSFactory2) { this.DSFactory = DSFactory2; this.extendConfig = (config) => { return Object.assign({}, this.baseConfig, config); }; this.headers = DSFactory2.headers; this.baseConfig = { baseURL: getURI(DSFactory2.env) }; } workspace(workspaceId) { const headers = this.addWSIdentifier(workspaceId); const webhooks = new _Webhooks(this.DSFactory); webhooks.headers = headers; return webhooks; } addWSIdentifier(wsIdentifier) { const headers = new import_axios3.AxiosHeaders(this.headers.toJSON()); headers.set("X-WS-Identifier", wsIdentifier); return headers; } async list(options) { const config = this.extendConfig({ method: "GET", url: "/v1/webhooks", headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []), per_page: options?.limit ?? 10, page: options?.page ?? 1, sort: options?.sort } }); const response = await createRequest(config); return response.data; } async delete(id) { const config = this.extendConfig({ method: "DELETE", url: `/v1/webhooks/${id}`, headers: this.headers }); const response = await createRequest(config); return response.data; } async get(id, options) { const config = this.extendConfig({ method: "GET", url: `/v1/webhooks/${id}`, headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []) } }); const response = await createRequest(config); return response.data; } async rotateKey(id) { const config = this.extendConfig({ method: "PUT", url: `/v1/webhooks/${id}/rotate-key`, headers: this.headers }); const response = await createRequest(config); return response.data; } async update(id, payload, params = {}) { const config = this.extendConfig({ method: "PUT", url: `/v1/webhooks/${id}`, headers: this.headers, params, data: payload }); const response = await createRequest(config); return response.data; } async create(payload, params = {}) { const config = this.extendConfig({ method: "POST", url: `/v1/webhooks`, headers: this.headers, params, data: payload }); const response = await createRequest(config); return response.data; } }; // lib/templates/index.ts var import_axios4 = require("axios"); var import_class_validator = require("class-validator"); // lib/error/index.ts var DigiError = class extends Error { constructor(code, message) { super(message); this.code = code; this.name = this.constructor.name; } toString() { return JSON.stringify( { message: this.message, name: this.name }, null, 2 ); } }; // lib/templates/index.ts var Templates = class _Templates { constructor(DSFactory2) { this.DSFactory = DSFactory2; this.extendConfig = (config) => { return Object.assign({}, this.baseConfig, config); }; this.headers = DSFactory2.headers; this.baseConfig = { baseURL: getURI(DSFactory2.env) }; } workspace(workspaceId) { const headers = this.addWSIdentifier(workspaceId); const ws = new _Templates(this.DSFactory); ws.headers = headers; return ws; } addWSIdentifier(wsIdentifier) { const headers = new import_axios4.AxiosHeaders(this.headers.toJSON()); headers.set("X-WS-Identifier", wsIdentifier); return headers; } async list(options) { const config = this.extendConfig({ method: "GET", url: "/v1/templates", headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []), per_page: options?.limit ?? 10, page: options?.page ?? 1, sort: options?.sort } }); const response = await createRequest(config); return response.data; } async get(id, options) { const config = this.extendConfig({ method: "GET", url: `/v1/templates/${id}`, headers: this.headers, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []) } }); const response = await createRequest(config); return response.data; } async transform(id, payload, options) { this.validateTransform(payload); const config = this.extendConfig({ method: "PUT", url: `/v1/templates/${id}/transform`, headers: this.headers, data: payload, params: { population: JSON.stringify(options?.population ?? []), filter: JSON.stringify(options?.filter ?? []) } }); const response = await createRequest(config); return response.data; } validateTransform(payload) { if (payload.expiration && !(0, import_class_validator.isDateString)(payload.expiration)) { throw new DigiError("404", "Expiration requires a date string."); } } }; // lib/factory/index.ts var USER_AGENT = process.env.DIGISIGN_USER_AGENT || `digisign-node:${version}`; var DSFactory = class { constructor(apiKey, token, organisationId, env) { this.apiKey = apiKey; this.token = token; this.organisationId = organisationId; this.env = env; this.headers = initHeaders(); this.headers.set("Authorization", `Bearer ${this.token}`); this.headers.set("X-O10N-Identifier", this.organisationId); this.keys = new Keys(this); this.workspaces = new Workspaces(this); this.webhooks = new Webhooks(this); this.templates = new Templates(this); } }; function initHeaders() { const value = { "User-Agent": USER_AGENT, "Content-Type": "application/json" }; return new import_axios5.AxiosHeaders(value); } async function createSession(options) { const headers = initHeaders(); const xAPIKey = options?.key ?? process.env.DIGISIGN_API_KEY; if (!xAPIKey) { throw new Error( 'Missing API key. Pass it to the constructor `createSession("ds_123")`' ); } headers.set("X-API-KEY", xAPIKey); const env = options?.environment ?? "production" /* PRODUCTION */; const config = { method: "post", maxBodyLength: Number.POSITIVE_INFINITY, url: "/v1/keys/session", headers, baseURL: getURI(env) }; try { const response = await createRequest(config); const token = (0, import_lodash.get)(response, ["data", "meta", "access_token"]); const organisationId = (0, import_lodash.get)(response, ["data", "data", "organisation_id"]); return new DSFactory(xAPIKey, token, organisationId, env); } catch (err) { throw new DigiError( (0, import_lodash.get)(err, ["response", "data", "statusCode"]), (0, import_lodash.get)(err, ["response", "data", "message"]) ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RequestMessage, SessionEnvironment, TemplateRecipient, WebhookEventType, WebhookStatus, createSession });