UNPKG

@formpost/node

Version:

Official FormPost Node.js SDK – Server-side library for securely integrating FormPost forms and endpoints. Provides easy-to-use methods for submitting form data, verifying client submissions, and handling signatures with secret keys. Perfect for Next.js,

48 lines 1.59 kB
"use strict"; /** * Main FormPost SDK * - constructor({ secretKey, baseUrl }) * submit(formId, data) -> server-to-server submit using the secret key * - verifySignature(rawBody, signatureHeader, secret) -> boolean * - generateSignature(payload, timestamp, secret) -> string * - helpers: buildHeaders(), fetch wrapper */ Object.defineProperty(exports, "__esModule", { value: true }); exports.FormPost = void 0; const node_fetch_1 = require("node-fetch"); const crypto = require("crypto"); class FormPost { constructor(options) { if (!options.key) throw new Error('Secret key is required'); if (!options.endpoint) throw new Error('Endpoint is required'); this.key = options.key; this.endpoint = options.endpoint; } buildHeaders() { return { 'Content-Type': 'application/json', 'X-Api-Key': this.key }; } async submit(payload) { const res = await (0, node_fetch_1.default)(this.endpoint, { method: 'POST', headers: this.buildHeaders(), body: JSON.stringify(payload.data) }); return res.json(); } generateSignature(payload, timestamp) { return crypto.createHmac('sha256', this.key) .update(`${timestamp}.${payload}`) .digest('hex'); } verifySignature(payload, signature, timestamp) { const expected = this.generateSignature(payload, timestamp); return expected === signature; } } exports.FormPost = FormPost; //# sourceMappingURL=index.js.map