Skip to content

Instantly share code, notes, and snippets.

@AlexandroMtzG
Created January 29, 2023 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexandroMtzG/c934727cbd3d214c7ac8991b2ae5c409 to your computer and use it in GitHub Desktop.
Save AlexandroMtzG/c934727cbd3d214c7ac8991b2ae5c409 to your computer and use it in GitHub Desktop.
Implementation of Dropbox Sign (formerly HelloSign) Node.js SDK
import fs from "fs";
var hellosign = require("hellosign-sdk")({ key: process.env.DROPBOX_SIGN_API_KEY });
export type DropboxSignatureRequestDto = {
signature_request_id: string;
test_mode: boolean;
title: string;
original_title: string;
subject: string;
message: string;
metadata: any;
created_at: number;
expires_at: number | null;
is_complete: boolean;
is_declined: boolean;
has_error: boolean;
custom_fields: any[];
response_data: any[];
signing_url: string | null;
signing_redirect_url: string | null;
final_copy_uri: string | null;
files_url: string;
details_url: string;
requester_email_address: string;
signatures: [
{
signature_id: string;
has_pin: boolean;
has_sms_auth: boolean;
has_sms_delivery: boolean;
sms_phone_number: string | null;
signer_email_address: string;
signer_name: string | null;
signer_role: string | null;
order: number | null;
status_code: string;
signed_at: number | null;
last_viewed_at: null;
last_reminded_at: null;
error: null;
}
];
cc_email_addresses: any[];
template_ids: any[] | null;
client_id: string;
};
async function list(options?: { page: number; page_size: number }): Promise<{
list_info: { page: number; num_pages: number; num_results: number; page_size: number };
signature_requests: DropboxSignatureRequestDto[];
}> {
return new Promise(async (resolve, reject) => {
hellosign.signatureRequest
.list(options)
.then((res: any) => {
resolve(res);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.error(err);
reject(err);
});
});
}
async function get(id: string): Promise<DropboxSignatureRequestDto> {
return new Promise(async (resolve, reject) => {
hellosign.signatureRequest
.get(id)
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.get]", { res });
resolve(res.signature_request);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.error(err);
reject(err);
});
});
}
async function create({
embedded,
subject,
message,
signers,
files,
}: {
embedded: boolean;
subject: string;
message: string;
signers: { email_address: string; name: string }[];
files: string[];
}): Promise<DropboxSignatureRequestDto> {
return new Promise(async (resolve, reject) => {
const opts = {
test_mode: 1,
clientId: process.env.DROPBOX_SIGN_APP_ID,
subject,
message,
signers,
files,
};
if (embedded) {
await hellosign.signatureRequest
.createEmbedded(opts)
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.create]", { res });
resolve(res.signature_request);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
} else {
await hellosign.signatureRequest
.send(opts)
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.create]", { res });
resolve(res);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
}
});
}
async function remind(id: string, { email_address }: { email_address: string }) {
return new Promise(async (resolve, reject) => {
await hellosign.signatureRequest
.remind(id, {
email_address,
})
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.remind]", { res });
resolve(res);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
});
}
async function getSignUrl(id: string): Promise<string> {
return new Promise(async (resolve, reject) => {
await hellosign.embedded
.getSignUrl(id)
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.getSignUrl]", { res });
resolve(res.embedded.sign_url);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
});
}
async function download(id: string): Promise<{ base64: string; name: string; type: string }> {
return new Promise(async (resolve, reject) => {
await hellosign.signatureRequest
.download(id)
.then((res: any) => {
const path = "/tmp/pdfs/signatures";
const dir = path.replace(/\/\w+\.\w+$/, "");
if (dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
const fileName = res.headers["content-disposition"].split("filename=")[1].replace(/"/g, "");
const filePath = `${path}/${fileName}`;
var write = fs.createWriteStream(filePath);
res.pipe(write);
write.on("finish", () => {
write.close();
const base64 = fs.readFileSync(filePath, { encoding: "base64" });
fs.unlinkSync(filePath);
resolve({
base64,
name: fileName,
type: res.headers["content-type"],
});
});
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
});
}
async function cancel(id: string) {
return new Promise(async (resolve, reject) => {
await hellosign.signatureRequest
.cancel(id)
.then((res: any) => {
// eslint-disable-next-line no-console
console.log("[DropboxSignService.cancel]", { res });
resolve(res);
})
.catch((err: any) => {
// eslint-disable-next-line no-console
console.log({ err });
reject(err);
});
});
}
export default {
list,
get,
create,
remind,
getSignUrl,
download,
cancel,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment