UNPKG

@wojtekmaj/jotform

Version:

Jotform API Node.js Client

1,262 lines (1,261 loc) 44.2 kB
import { serialize } from 'object-to-formdata'; const defaultOptions = { url: 'https://api.jotform.com', version: 'latest', debug: false, timeout: 10000, // 10 seconds }; const currentOptions = { ...defaultOptions }; async function sendRequest(url, method, body, customHeaders) { if (currentOptions.debug) { console.log(`Jotform: ${method.toUpperCase()} ${url}`); } const controller = new AbortController(); const baseHeaders = { accept: 'application/json', }; const options = { method, headers: customHeaders ? { ...baseHeaders, ...customHeaders, } : baseHeaders, signal: controller.signal, }; if (body) { switch (method) { case 'post': { const formData = serialize(body); options.body = formData; break; } case 'put': { options.body = JSON.stringify(body); options.headers['content-type'] = 'application/json'; break; } } } const id = setTimeout(() => controller.abort(), currentOptions.timeout); const response = await fetch(url, options); clearTimeout(id); const responseBody = await (async () => { const contentTypeHeader = response.headers.get('content-type'); const contentType = contentTypeHeader ? contentTypeHeader.split(';')[0] : null; switch (contentType) { case 'application/json': return response.json(); default: return response.text(); } })(); if (!response.ok) { const errorMessage = (typeof responseBody === 'object' ? responseBody.message : responseBody) || response.statusText; throw new Error(errorMessage); } if (responseBody.responseCode !== 200) { throw new Error(responseBody.message); } return responseBody.content; } function get(url, customHeaders) { return sendRequest(url, 'get', undefined, customHeaders); } function post(url, body, customHeaders) { return sendRequest(url, 'post', body, customHeaders); } function put(url, body, customHeaders) { return sendRequest(url, 'put', body, customHeaders); } function del(url, customHeaders) { return sendRequest(url, 'delete', undefined, customHeaders); } function getRequestUrl(endPoint, params = {}) { if (typeof currentOptions.apiKey === 'undefined') { throw new Error('API Key is undefined'); } const { orderby, direction, ...otherParams } = params; const baseUrl = currentOptions.url + (currentOptions.version === 'latest' ? '' : `/v${currentOptions.version}`); const urlSearchParams = new URLSearchParams(); for (const key in otherParams) { const value = otherParams[key]; if (value !== undefined) { urlSearchParams.append(key, typeof value === 'object' ? JSON.stringify(value) : `${value}`); } } if (orderby) { if (typeof orderby !== 'string') { throw new Error('orderby must be a string'); } if (direction && typeof direction !== 'string') { throw new Error('direction must be a string'); } const orderbyWithDirection = direction ? `${orderby},${direction}` : orderby; urlSearchParams.append('orderby', orderbyWithDirection); } urlSearchParams.append('apiKey', currentOptions.apiKey); return `${baseUrl + endPoint}?${urlSearchParams.toString()}`; } /** * Options * * @description Update client options. * @param {Partial<Options>} [newOptions] */ export function options(newOptions) { Object.assign(currentOptions, newOptions); if (currentOptions.debug) { console.log('Jotform: Updated options', currentOptions); } } /** * Get History * * @description User activity log about things like forms created/modified/deleted, account logins and other operations. * @link https://api.jotform.com/docs/#user-history * @param {GetHistoryQuery} [query] * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getHistory(query = {}, customHeaders) { const { action, date, sortBy, startDate, endDate } = query; const endPoint = '/user/history'; const requestUrl = getRequestUrl(endPoint, { action: action !== undefined ? action : 'all', date, sortBy: sortBy !== undefined ? sortBy : 'ASC', startDate, endDate, }); const promise = get(requestUrl, customHeaders); return promise; } /** * Get User Settings * * @description Get user's time zone and language. * @link https://api.jotform.com/docs/#user-settings * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getSettings(customHeaders) { const endPoint = '/user/settings'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Update User Settings * * @description Update user's settings like time zone and language. * @link https://api.jotform.com/docs/#post-user-settings * @param {unknown} settingsData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function updateSettings(settingsData, customHeaders) { if (typeof settingsData !== 'object' || settingsData === null) { return Promise.resolve(); } const endPoint = '/user/settings'; const requestUrl = getRequestUrl(endPoint); const postData = settingsData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Get Sub-User Account List * * @description Get a list of sub users for this accounts and list of forms and form folders with access privileges. * @link https://api.jotform.com/docs/#user-subusers * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getSubusers(customHeaders) { const endPoint = '/user/subusers'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Monthly User Usage * * @description Get number of form submissions received this month. Also, get number of SSL form submissions, payment form submissions and upload space used by user. * @link https://api.jotform.com/docs/#user-usage * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getUsage(customHeaders) { const endPoint = '/user/usage'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get User Information * * @description Get user account details for this Jotform user. Including user account type, avatar URL, name, email, website URL. * @link https://api.jotform.com/docs/#user * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getUser(customHeaders) { const endPoint = '/user'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get details of a plan * * @description Get limit and prices of a plan. * @param {string} planName */ export function getPlan(planName, customHeaders) { if (planName === undefined) { throw new Error('Plan name is undefined'); } const endPoint = `/system/plan/${planName}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get User Forms * * @description Get a list of forms for this account. Includes basic details such as title of the form, when it was created, number of new and total submissions. * @link https://api.jotform.com/docs/#user-forms * @param {GetFormsQuery} [query] * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getForms(query = {}, customHeaders) { const { filter, offset, limit, orderby, direction, fullText } = query; if (filter && typeof filter !== 'object') { throw new Error('filter must be an object'); } if (direction && direction !== 'ASC' && direction !== 'DESC') { throw new Error("direction must be 'ASC' or 'DESC'"); } const endPoint = '/user/forms'; const requestUrl = getRequestUrl(endPoint, { filter: filter !== undefined ? JSON.stringify(filter) : undefined, offset, limit, orderby: orderby !== undefined ? orderby : 'created_at', fullText, direction, }); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Form Details * * @description Get basic information about a form. Use /form/{id}/questions to get the list of questions. * @link https://api.jotform.com/docs/#form-id * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getForm(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Create a new form * * @description Create new form with questions, properties and email settings. * @link https://api.jotform.com/docs/#post-forms * @param {unknown} formData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createForm(formData, customHeaders) { if (typeof formData !== 'object' || formData === null) { throw new Error('formData must be an object'); } const endPoint = '/user/forms'; const requestUrl = getRequestUrl(endPoint); const postData = formData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Create new forms * * @description Create new forms with questions, properties and email settings. * @link https://api.jotform.com/docs/#put-forms * @param {unknown} formsData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createForms(formsData, customHeaders) { if (typeof formsData === 'undefined' || formsData === null) { throw new Error('formsData is required'); } const endPoint = '/user/forms'; const requestUrl = getRequestUrl(endPoint); const postData = formsData; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Delete a form * * @description Delete an existing form. * @link https://api.jotform.com/docs/#delete-form-id * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteForm(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } /** * Clone Form * * @description Clone a single form. * @link https://api.jotform.com/docs/#post-form-id-clone * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function cloneForm(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/clone`; const requestUrl = getRequestUrl(endPoint); const promise = post(requestUrl, customHeaders); return promise; } // #endregion Forms // #region Form files /** * Form files */ /** * Get Form Uploads * * @description List of files uploaded on a form. Size and file type is also included. * @link https://api.jotform.com/docs/#form-id-files * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormFiles(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/files`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } // #endregion Form files // #region Form properties /** * Form properties */ /** * Get Form Properties * * @description Get a list of all properties on a form. * @link https://api.jotform.com/docs/#form-id-properties * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormProperties(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/properties`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get a Form Property * * @description Get a specific property of the form. * @link https://api.jotform.com/docs/#form-id-properties-key * @param {string} formID * @param {string} key * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormProperty(formID, key, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/properties/${key}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Add or edit properties of a specific form * * @description Edit a form property or add a new one. * @link https://api.jotform.com/docs/#post-form-id-properties * @param {string} formID * @param {unknown} propertyData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function addFormProperty(formID, propertyData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof propertyData !== 'object' || propertyData === null) { throw new Error('propertyData must be an object'); } const endPoint = `/form/${formID}/properties`; const requestUrl = getRequestUrl(endPoint); const postData = propertyData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Add or edit properties of a specific form * * @description Edit a form property or add a new one. * @link https://api.jotform.com/docs/#put-form-id-properties * @param {string} formID * @param {unknown} propertyData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function addFormProperties(formID, propertyData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof propertyData !== 'object' || propertyData === null) { throw new Error('propertyData must be an object'); } const endPoint = `/form/${formID}/properties`; const requestUrl = getRequestUrl(endPoint); const postData = propertyData; const promise = put(requestUrl, postData, customHeaders); return promise; } // #endregion Form properties // #region Form questions /** * Form questions */ /** * Get Form Questions * * @description Get a list of all questions on a form. Type describes question field type. Order is the question order in the form. Text field is the question label. * @link https://api.jotform.com/docs/#form-id-questions * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormQuestions(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/questions`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Details About a Question * * @description Form questions might have various properties. Examples: Is it required? Are there any validations such as 'numeric only'? * @link https://api.jotform.com/docs/#form-id-question-id * @param {string} formID * @param {string} questionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormQuestion(formID, questionID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof questionID === 'undefined' || questionID === null) { throw new Error('questionID is required'); } const endPoint = `/form/${formID}/question/${questionID}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Add new question to specified form * * @description Add new question to specified form. Form questions might have various properties. Examples: Is it required? Are there any validations such as 'numeric only'? * @link https://api.jotform.com/docs/#post-form-id-questions * @param {string} formID * @param {unknown} questionData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function addFormQuestion(formID, questionData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof questionData !== 'object' || questionData === null) { throw new Error('questionData must be an object'); } const endPoint = `/form/${formID}/questions`; const requestUrl = getRequestUrl(endPoint); const postData = questionData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Add new questions to specified form * * @description Add new questions to specified form. Form questions might have various properties. Examples: Is it required? Are there any validations such as 'numeric only'? * @link https://api.jotform.com/docs/#put-form-id-questions * @param {string} formID * @param {unknown} questionData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function addFormQuestions(formID, questionData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof questionData !== 'object' || questionData === null) { throw new Error('questionData must be an object'); } const endPoint = `/form/${formID}/questions`; const requestUrl = getRequestUrl(endPoint); const postData = questionData; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Add or edit a single question properties * * @description Edit a question property or add a new one. Form questions might have various properties. Examples: Is it required? Are there any validations such as 'numeric only'? * @link https://api.jotform.com/docs/#post-form-id-question-id * @param {string} formID * @param {string} questionID * @param {unknown} questionData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function updateFormQuestion(formID, questionID, questionData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof questionID === 'undefined' || questionID === null) { throw new Error('questionID is required'); } if (typeof questionData !== 'object' || questionData === null) { throw new Error('questionData must be an object'); } const endPoint = `/form/${formID}/question/${questionID}`; const requestUrl = getRequestUrl(endPoint); const postData = questionData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Delete Form Question * * @description Delete a single form question. * @link https://api.jotform.com/docs/#delete-form-id-question-id * @param {string} formID * @param {string} questionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteFormQuestion(formID, questionID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof questionID === 'undefined' || questionID === null) { throw new Error('questionID is required'); } const endPoint = `/form/${formID}/question/${questionID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } // #endregion Form questions // #region Form reports /** * Form reports */ /** * Get form reports * * @description Get all the reports of a specific form. * @link https://api.jotform.com/docs/#form-id-reports * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormReports(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/reports`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Report Details * * @description Get more information about a data report. * @link https://api.jotform.com/docs/#report-id * @param {string} formID * @param {string} reportID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormReport(_formID, reportID, customHeaders) { return getReport(reportID, customHeaders); } /** * Create report * * @description Create new report of a form with intended fields, type and title. * @link https://api.jotform.com/docs/#post-form-id-reports * @param {string} formID * @param {unknown} reportData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createFormReport(formID, reportData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof reportData !== 'object' || reportData === null) { throw new Error('reportData must be an object'); } const endPoint = `/form/${formID}/reports`; const requestUrl = getRequestUrl(endPoint); const postData = reportData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Delete Report * * @description Delete an existing report. * @link https://api.jotform.com/docs/#delete-report-id * @param {string} formID * @param {string} reportID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteFormReport(_formID, reportID, customHeaders) { return deleteReport(reportID, customHeaders); } /** * Get Form Submissions * * @description List of form responses. answers array has the submitted data. Created_at is the date of the submission. * @link https://api.jotform.com/docs/#form-id-submissions * @param {string} formID * @param {GetFormSubmissionsQuery} [query] * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormSubmissions(formID, query = {}, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const { filter, offset, limit, orderby, direction } = query; if (filter && typeof filter !== 'object') { throw new Error('Filter must be an object'); } if (direction && direction !== 'ASC' && direction !== 'DESC') { throw new Error('Direction must be ASC or DESC'); } const endPoint = `/form/${formID}/submissions`; const requestUrl = getRequestUrl(endPoint, { filter: filter !== undefined ? JSON.stringify(filter) : undefined, offset, limit, orderby: orderby !== undefined ? orderby : 'created_at', direction, }); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Submission Data * * @description Similar to /form/{form-id}/submissions. But only get a single submission. * @link https://api.jotform.com/docs/#submission-id * @param {string} formID * @param {string} submissionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormSubmission(_formID, submissionID, customHeaders) { return getSubmission(submissionID, customHeaders); } /** * Add a Submission to the Form * * @description Submit data to this form using the API. You should get a list of question IDs from form/{id}/questions and send the submission data with qid. * @link https://api.jotform.com/docs/#post-form-id-submissions * @param {string} formID * @param {unknown} submissionData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createFormSubmission(formID, submissionData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof submissionData !== 'object' || submissionData === null) { throw new Error('submissionData must be an object'); } const endPoint = `/form/${formID}/submissions`; const requestUrl = getRequestUrl(endPoint); const postData = submissionData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Add Submissions to the Form * * @description Submit data to this form using the API. You should get a list of question IDs from form/{id}/questions and send the submission data with qid. * @link https://api.jotform.com/docs/#put-form-id-submissions * @param {string} formID * @param {unknown} submissionsData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createFormSubmissions(formID, submissionsData, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof submissionsData !== 'object' || submissionsData === null) { throw new Error('submissionsData must be an object'); } const endPoint = `/form/${formID}/submissions`; const requestUrl = getRequestUrl(endPoint); const postData = submissionsData; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Delete Submission Data * * @description Delete a single submission. * @link https://api.jotform.com/docs/#delete-submission-id * @param {string} formID * @param {string} submissionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteFormSubmission(_formID, submissionID, customHeaders) { return deleteSubmission(submissionID, customHeaders); } // #endregion Form submissions // #region Form webhooks /** * Form webhooks */ /** * List of Webhooks for a Form * * @description Webhooks can be used to send form submission data as an instant notification. Returns list of webhooks for this form. * @link https://api.jotform.com/docs/#form-id-webhooks * @param {string} formID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFormWebhooks(formID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } const endPoint = `/form/${formID}/webhooks`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Add a New Webhook * * @description Webhooks can be used to send form submission data as an instant notification. * @link https://api.jotform.com/docs/#post-form-id-webhooks * @param {string} formID * @param {string} webhookURL * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createFormWebhook(formID, webhookURL, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof webhookURL === 'undefined' || webhookURL === null) { throw new Error('webhookURL is required'); } const endPoint = `/form/${formID}/webhooks`; const requestUrl = getRequestUrl(endPoint); const postData = { webhookURL: webhookURL, }; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Delete a webhook of a specific form * * @description Delete a webhook of a specific form * @link https://api.jotform.com/docs/#delete-form-id-webhooks * @param {string} formID * @param {string} webhookID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteFormWebhook(formID, webhookID, customHeaders) { if (typeof formID === 'undefined' || formID === null) { throw new Error('formID is required'); } if (typeof webhookID === 'undefined' || webhookID === null) { throw new Error('webhookID is required'); } const endPoint = `/form/${formID}/webhooks/${webhookID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } /** * Get User Labels * * @description Get all labels in a user’s account, with optional support for including related resources via parameter. * @link https://api.jotform.com/docs/#get-user-labels * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getLabels(customHeaders) { const endPoint = '/user/labels'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Label Details * * @description Get the details of a label, including its name and color. * @link https://api.jotform.com/docs/#label-id * @param {string} labelID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getLabel(labelID, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } const endPoint = `/label/${labelID}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Create Label * * @description Create a label by defining attributes such as name and color. * @link https://api.jotform.com/docs/#post-label * @param {unknown} labelProperties * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createLabel(labelProperties, customHeaders) { if (typeof labelProperties !== 'object' || labelProperties === null) { throw new Error('labelProperties must be an object'); } const endPoint = '/label'; const requestUrl = getRequestUrl(endPoint); const postData = labelProperties; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Update Label * * @description Update an existing label with specified parameters, such as name and color. * @link https://api.jotform.com/docs/#put-label-id * @param {string} labelID * @param {unknown} labelProperties * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function updateLabel(labelID, labelProperties, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } if (typeof labelProperties !== 'object' || labelProperties === null) { throw new Error('labelProperties must be an object'); } const endPoint = `/label/${labelID}`; const requestUrl = getRequestUrl(endPoint); const postData = labelProperties; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Get Label Resources * * @description Get a list of assets in a label and their associated information. * @link https://api.jotform.com/docs/#label-id-resources * @param {string} labelID * @param {GetLabelResourcesQuery} [query] * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getLabelResources(labelID, query = {}, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } const { offset, limit, orderby, direction, status, type } = query; const endPoint = `/label/${labelID}/resources`; const requestUrl = getRequestUrl(endPoint, { offset, limit, orderby, direction, status, type, }); const promise = get(requestUrl, customHeaders); return promise; } function ensureLabelResource(resource) { if (typeof resource !== 'object' || resource === null) { throw new Error('resource must be an object'); } if (typeof resource.id !== 'string' || resource.id.length === 0) { throw new Error('resource.id must be a non-empty string'); } if (typeof resource.type !== 'string' || resource.type.length === 0) { throw new Error('resource.type must be a non-empty string'); } } /** * Add Resources to Label * * @description Add the specified asset, identified by its ID and type, into a label. * @link https://api.jotform.com/docs/#put-label-id-add-resources * @param {string} labelID * @param {LabelResource} resource * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function addResourcesToLabel(labelID, resource, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } ensureLabelResource(resource); const endPoint = `/label/${labelID}/add-resources`; const requestUrl = getRequestUrl(endPoint); const postData = { resources: [resource], }; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Remove Resources from Label * * @description Remove the specified assets, identified by their IDs and types, from a label. * @link https://api.jotform.com/docs/#put-label-id-remove-resources * @param {string} labelID * @param {LabelResource[]} resources * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function removeResourcesFromLabel(labelID, resources, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } if (!Array.isArray(resources) || resources.length === 0) { throw new Error('resources must be a non-empty array'); } for (const resource of resources) { ensureLabelResource(resource); } const endPoint = `/label/${labelID}/remove-resources`; const requestUrl = getRequestUrl(endPoint); const postData = { resources, }; const promise = put(requestUrl, postData, customHeaders); return promise; } /** * Delete Label * * @description Delete a label along with all its sublabels. * @link https://api.jotform.com/docs/#delete-label-id * @param {string} labelID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteLabel(labelID, customHeaders) { if (typeof labelID === 'undefined' || labelID === null) { throw new Error('labelID is required'); } const endPoint = `/label/${labelID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } // #endregion Labels // #region Folders /** * Folders */ const folderEndpointErrors = { list: 'This endpoint is deprecated. Please refer to the Label feature endpoints. More information is available at: https://api.jotform.com/docs/#get-user-labels', detail: 'This endpoint is deprecated. Please refer to the Label feature endpoints. More information is available at: https://api.jotform.com/docs/#get-label-id', mutate: 'This endpoint is deprecated. Please refer to the Label feature endpoints. More information is available at: https://api.jotform.com/docs/#post-label', delete: 'This endpoint is deprecated. Please refer to the Label feature endpoints. More information is available at: https://api.jotform.com/docs/#delete-label-id', }; /** * Get User Folders * * @deprecated Use `getLabels` instead * @description Get a list of form folders for this account. Returns name of the folder and owner of the folder for shared folders. * @link https://api.jotform.com/docs/#user-folders * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getFolders(_customHeaders) { return Promise.reject(new Error(folderEndpointErrors.list)); } /** * Get Folder Details * * @deprecated Use `getLabel` instead * @description Get a list of forms in a folder, and other details about the form such as folder color. * @link https://api.jotform.com/docs/#folder-id * @param {string} folderID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown> */ export function getFolder(_folderID, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.detail)); } /** * Create Folder * * @deprecated Use `createLabel` instead * @description Create a folder with specified parameters * @link https://api.jotform.com/docs/#post-folder * @param {unknown} folderProperties * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function createFolder(_folderProperties, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.mutate)); } /** * Update Folder * * @deprecated Use `updateLabel` instead * @description Update a folder with specified parameters. Also you can add forms to the folder. * @link https://api.jotform.com/docs/#put-folder-id * @param {string} folderID * @param {unknown} folderProperties * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function updateFolder(_folderID, _folderProperties, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.mutate)); } /** * @deprecated Use `addResourcesToLabel` instead */ export function addFormToFolder(_folderID, _formID, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.mutate)); } /** * @deprecated Use `addResourcesToLabel` instead */ export function addFormsToFolder(_folderID, _formIDs, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.mutate)); } /** * Delete Folder * * @deprecated Use `deleteLabel` instead * @description Delete a folder and its subfolders * @link https://api.jotform.com/docs/#delete-folder-id * @param {string} folderID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteFolder(_folderID, _customHeaders) { return Promise.reject(new Error(folderEndpointErrors.delete)); } // #endregion Folders // #region Reports /** * Reports */ /** * Get User Reports * * @description List of URLs for reports in this account. Includes reports for all of the forms. ie. Excel, CSV, printable charts, embeddable HTML tables. * @link https://api.jotform.com/docs/#user-reports * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getReports(customHeaders) { const endPoint = '/user/reports'; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Report Details * * @description Get more information about a data report. * @link https://api.jotform.com/docs/#report-id * @param {string} reportID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getReport(reportID, customHeaders) { if (typeof reportID === 'undefined' || reportID === null) { throw new Error('reportID is required'); } const endPoint = `/report/${reportID}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Delete a Report * * @description Delete an existing report. * @link https://api.jotform.com/docs/#delete-report-id * @param {string} reportID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteReport(reportID, customHeaders) { if (typeof reportID === 'undefined' || reportID === null) { throw new Error('reportID is required'); } const endPoint = `/report/${reportID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } /** * Get User Submissions * * @description Get a list of all submissions for all forms on this account. The answers array has the submission data. Created_at is the date of the submission. * @link https://api.jotform.com/docs/#user-submissions * @param {GetSubmissionsQuery} [query] * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getSubmissions(query = {}, customHeaders) { const { filter, offset, limit, orderby, direction, fullText, nocache } = query; if (filter && typeof filter !== 'object') { throw new Error('Filter must be an object'); } if (direction && direction !== 'ASC' && direction !== 'DESC') { throw new Error('Direction must be ASC or DESC'); } const endPoint = '/user/submissions'; const requestUrl = getRequestUrl(endPoint, { filter: filter !== undefined ? JSON.stringify(filter) : undefined, offset, limit, orderby: orderby !== undefined ? orderby : 'created_at', fullText, direction, nocache, }); const promise = get(requestUrl, customHeaders); return promise; } /** * Get Submission Data * * @description Similar to /form/{form-id}/submissions. But only get a single submission. * @link https://api.jotform.com/docs/#submission-id * @param {string} submissionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function getSubmission(submissionID, customHeaders) { if (typeof submissionID === 'undefined' || submissionID === null) { throw new Error('submissionID is required'); } const endPoint = `/submission/${submissionID}`; const requestUrl = getRequestUrl(endPoint); const promise = get(requestUrl, customHeaders); return promise; } /** * Edit Submission Data * * @description Edit a single submission. * @link https://api.jotform.com/docs/#post-submission-id * @param {string} submissionID * @param {unknown} submissionData * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function updateSubmission(submissionID, submissionData, customHeaders) { if (typeof submissionID === 'undefined' || submissionID === null) { throw new Error('submissionID is required'); } if (typeof submissionData !== 'object' || submissionData === null) { throw new Error('submissionData must be an object'); } const endPoint = `/submission/${submissionID}`; const requestUrl = getRequestUrl(endPoint); const postData = submissionData; const promise = post(requestUrl, postData, customHeaders); return promise; } /** * Delete Submission Data * * @description Delete a single submission. * @link https://api.jotform.com/docs/#delete-submission-id * @param {string} submissionID * @param {HeadersInit} [customHeaders] * @returns {Promise<unknown>} */ export function deleteSubmission(submissionID, customHeaders) { if (typeof submissionID === 'undefined' || submissionID === null) { throw new Error('submissionID is required'); } const endPoint = `/submission/${submissionID}`; const requestUrl = getRequestUrl(endPoint); const promise = del(requestUrl, customHeaders); return promise; } // #endregion Submissions /* For backwards compatibility */ export const getFormPropertyByKey = getFormProperty; /* For backwards compatibility */ export const editSubmission = updateSubmission;