UNPKG

@usedigisign/node-sdk

Version:

Node.js library for the DigiSign API

402 lines (387 loc) 11.2 kB
// 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 import { AxiosHeaders as AxiosHeaders3 } from "axios"; import { get } from "lodash"; // lib/utils/axios/index.ts import axios from "axios"; var defaultOptions = { baseURL: process.env.DIGISIGN_BASE_URL || "https://api.usedigisign.com" }; var instance = axios.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 import { AxiosHeaders } from "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 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 import { AxiosHeaders as AxiosHeaders2 } from "axios"; import { isDateString } from "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 AxiosHeaders2(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 && !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 AxiosHeaders3(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 = get(response, ["data", "meta", "access_token"]); const organisationId = get(response, ["data", "data", "organisation_id"]); return new DSFactory(xAPIKey, token, organisationId, env); } catch (err) { throw new DigiError( get(err, ["response", "data", "statusCode"]), get(err, ["response", "data", "message"]) ); } } export { RequestMessage, SessionEnvironment, TemplateRecipient, WebhookEventType, WebhookStatus, createSession };