@gatekeeper_technology/report-utils

中文文档
1.3.3发布于 2026/07/10MITTypeScriptCommonJS近 30 天下载 1.3万

Gatekeeper's pdf/email Utils - shared in NPM

原始使用文档
文档目录41 节
  1. Install
  2. Common Examples
  3. Format Dates
  4. Prepare SendGrid Recipients
  5. Generate HTML And Save A PDF Attachment
  6. Use Photos And Signatures In Reports
  7. Display JourneyApps Fields And Sort Data
  8. Exported Helpers
  9. Date And Time
  10. Email
  11. Attachments And PDFs
  12. General
  13. Development
  14. Changelog
  15. [1.3.3]
  16. [1.3.2]
  17. [1.3.1]
  18. [1.3.0]
  19. [1.2.3]
  20. [1.2.2]
  21. [1.2.1]
  22. [1.2.0]
  23. [1.1.10]
  24. [1.1.9]
  25. [1.1.8]
  26. [1.1.7]
  27. [1.1.6]
  28. [1.1.5]
  29. [1.1.4]
  30. [1.1.3]
  31. [1.1.2]
  32. [1.1.1]
  33. [1.1.0]
  34. [1.00.07]
  35. [1.00.06]
  36. [1.00.05]
  37. [1.00.04]
  38. [1.00.03]
  39. [1.00.02]
  40. [1.00.01]
  41. [1.00.00]

Gatekeeper Report Utils

Shared PDF, email, date, attachment, and display helpers for Gatekeeper JourneyApps reports.

Install

Add the package to a JourneyApps CloudCode task:

{
  "dependencies": {
    "@gatekeeper_technology/report-utils": "1.3.3"
  }
}

Create a .npmrc file in the CloudCode task with the read-only npm token from 1Password:

//registry.npmjs.org/:_authToken=<read-only-npm-token>

Then import the helpers:

const reportUtils = require("@gatekeeper_technology/report-utils");

Common Examples

Format Dates

const {
  displayDate,
  displayDateTime,
  displayTime,
  displayTimeWithSeconds,
  getOffsetDate,
  getStartOfWeek,
  getEndOfWeek,
  getWeekNumber,
  isSameDay,
  compareDates,
} = require("@gatekeeper_technology/report-utils");

const date = new Date(2026, 6, 10, 8, 30, 0);

displayDate(date);                  // "10 Jul 2026"
displayDate(date, "long");          // "10 July 2026"
displayTime(date);                  // "08:30"
displayTimeWithSeconds(date);       // "08:30:00"
displayDateTime(date);              // "10 Jul 2026, 08:30"

getOffsetDate(date, 7);             // date plus 7 days
getStartOfWeek(date);               // Monday start, ISO mode by default
getEndOfWeek(date);                 // Sunday end, ISO mode by default
getWeekNumber(date);                // calendar week number

isSameDay(date, new Date());        // true/false
compareDates(date, new Date());     // -1, 0, or 1

Non-Date inputs generally return null for date comparison helpers and "-" for display helpers.

Prepare SendGrid Recipients

const {
  getToEmailsArray,
  setCheckWhitelistEnvironments,
} = require("@gatekeeper_technology/report-utils");

const recipients = getToEmailsArray(
  [" [email protected] ", "[email protected]; [email protected]"],
  ["[email protected]", "[email protected]"],
  "staging",
  ["[email protected]", "[email protected]"]
);

// recipients:
// {
//   to: [{ email: "[email protected]" }, { email: "[email protected]" }],
//   cc: []
// }

getToEmailsArray trims, lowercases, de-duplicates, splits comma/semicolon separated strings, removes CC addresses that are already in TO, and filters by whitelist in staging and testing.

To change which environments use the whitelist:

setCheckWhitelistEnvironments(["testing", "staging", "production"]);

Generate HTML And Save A PDF Attachment

const {
  generateHTML,
  postToJourneyBackend,
} = require("@gatekeeper_technology/report-utils");

async function createReport(request, data) {
  const html = generateHTML(data, __dirname, "templates/report.html");

  const saved = await postToJourneyBackend(
    request,
    "report_pdf",
    html,
    `report-${request.id}.pdf`,
    process.env.JOURNEY_PDF_REPORTS_TOKEN
  );

  if (!saved) {
    throw new Error("Report PDF could not be generated or saved");
  }
}

generateHTML reads and compiles a Handlebars template. postToJourneyBackend generates a PDF with @journeyapps/pdf-reports, creates a JourneyApps Attachment, assigns it to the requested field, and saves the object.

Use Photos And Signatures In Reports

const {
  getImageContentForPDF,
  isAttachmentsUploaded,
  loadAttachments,
  loadPhotoOrSignature,
} = require("@gatekeeper_technology/report-utils");

async function buildReportData(inspection) {
  try {
    isAttachmentsUploaded(inspection);
  } catch (error) {
    if (error.cause === "attachments-uploading") {
      // Reschedule the CloudCode task using the app's normal retry pattern.
      throw error;
    }
    throw error;
  }

  return loadAttachments({}, inspection);
}

async function addSignature(inspection) {
  return loadPhotoOrSignature(inspection, "customer_signature");
}

async function addImage(inspection) {
  return {
    image_url: await getImageContentForPDF(inspection.photo, "url"),
    image_base64: await getImageContentForPDF(inspection.photo, "base64"),
  };
}

Attachment helpers throw with cause: "attachments-uploading" when a JourneyApps attachment has not finished uploading. Catch that error and reschedule the task from the calling app.

When type is "base64", getImageContentForPDF detects the image MIME type from the base64 payload (PNG, JPEG, GIF, WebP, SVG), falling back to the attachment URL extension, then to PNG.

Display JourneyApps Fields And Sort Data

const {
  formatText,
  getDisplayLabel,
  getDisplayValue,
  sortArrayByField,
} = require("@gatekeeper_technology/report-utils");

const label = getDisplayLabel(store, "store_type");
const value = getDisplayValue(store, "store_type");

formatText(" Store Visit / Audit ");      // "store_visit_audit"
formatText(" Store Visit / Audit ", "higher"); // "STORE_VISIT_AUDIT"

const stores = [
  { name: "Cape Town", score: 88 },
  { name: "Durban", score: 72 },
];

sortArrayByField(stores, "score", -1);    // sorts highest score first

sortArrayByField uses JavaScript's native Array.sort, so it mutates the original array.

Exported Helpers

Date And Time

  • displayDate(date, monthLength)
  • displayDateTime(date, monthLength)
  • displayTime(date)
  • displayTimeWithSeconds(date)
  • getOffsetDate(originalDate, offsetDays)
  • getStartOfWeek(date, iso)
  • getEndOfWeek(date, iso)
  • getWeekNumber(date, iso)
  • isSameDay(date1, date2)
  • compareDates(date1, date2)

Email

  • getToEmailsArray(toArray, ccArray, env, whitelist)
  • setCheckWhitelistEnvironments(environments)
  • storeValidEmail(emailObject, email)
  • validateEmail(email)

Attachments And PDFs

  • getImageContentForPDF(image, type)
  • loadImageBase64(path)
  • loadAttachments(target, objectToCheck)
  • loadPhotoOrSignature(object, fieldName)
  • isAttachmentsUploaded(objectToCheck)
  • isAttachmentUploaded(object, fieldName)
  • generateHTML(data, dirName, fileName)
  • postToJourneyBackend(object, fieldName, pdfHtml, fileName, token)

General

  • getDisplayValue(object, field)
  • getDisplayLabel(object, field)
  • formatText(text, type)
  • sortArrayByField(objectArray, fieldName, sortDirection)

Development

Install with the publish npm token from 1Password:

//registry.npmjs.org/:_authToken=<publish-npm-token>

Release checklist:

  1. Make code changes in src/*.ts and export new helpers from index.ts.
  2. Add or update Jest tests when behavior changes.
  3. Update the version in package.json.
  4. Add a changelog entry below.
  5. Run npm test.
  6. Publish with npm publish.
  7. Commit and push the release.

Changelog

[1.3.3]

Fixed

  • getImageContentForPDF now detects the correct MIME type for base64 data URIs (PNG, JPEG, GIF, WebP, SVG) instead of treating non-SVG attachments as PNG.
  • Date helpers now treat Invalid Date values as invalid and return null / "-" as documented.

Changed

  • Rewrote the README with install steps, usage examples, and an exported helpers reference.
  • Replaced outdated photo attachment tests with coverage for MIME detection, upload checks, and local image loading.
  • Aligned general and date tests with current behavior, and added email utility coverage.
  • Added Jest + ts-jest so TypeScript sources can be tested directly.

[1.3.2]

Changed

  • Bumped @journeyapps/db from ^8.0.10 to ^8.1.1.

[1.3.1]

Changed

  • Bumped @gatekeeper_technology/rollbar-utils to ^1.2.4.

[1.3.0]

Changed

  • Bumped the package version to 1.3.0.
  • Bumped @gatekeeper_technology/rollbar-utils to ^1.2.3.
  • Bumped @journeyapps/db to ^8.0.10.
  • Removed the unused @journeyapps/cloudcode dependency.

[1.2.3]

Changed

  • Bumped @gatekeeper_technology/rollbar-utils from 1.1.1 to ^1.2.2.

[1.2.2]

Fixed

  • Fixed postToJourneyBackend so it throws when the target field is missing, instead of throwing when the field exists.

Changed

  • Added parameter documentation for generateHTML.

[1.2.1]

Removed

  • dist ignore file in .npmignore.

[1.2.0]

Added

  • Added postToJourneyBackend function
  • Added journeyApps npm packages as dependencies to assist with the reportUtil package.
  • Added sortArrayByField function
  • Added generateHTML function
  • Added formatText function
  • Added getImageContentForPDF function

Changed

  • Migrated to TypeScript
  • All date functions should now be able to accept Day variables.
  • .gitignore file now ignores compiled files.
  • isAttachment(s)Uploaded functions no longer reschedules. It now throws an error with the cause = attachments-uploading

[1.1.10]

Removed

  • Removed loadBase64 function in a race against time.

[1.1.9]

Added

  • Jest Test cases
  • formatText, sortArrayByField and generateHTML functions
  • .d.ts files.

[1.1.8]

Changed

  • Added more validation on general utils

Added

  • Unit Tests on General Utils.

Removed

  • console.error() on date Utils.

[1.1.7]

Changed

  • Fixed funky way of throwing errors
  • Added a new utils folder with small internal utils

[1.1.6]

Changed

  • When retrying a task, we don't need to specify the CloudCode task name as we can access it programmatically

[1.1.5]

Added

  • isSameDay: Checks if the given two dates falls on the same day (second date value defaults to now).
  • compareDates: Checks if the given two dates falls on the same date and time (return 0), date 1 is before date 2 (returns -1), otherwise returns 1 (second date value defaults to now).

Removed

  • isToday: Can be used via the isSameDay() function
  • isPastDate and isFutureDate: Can be user via the compareDates() function

Changed

  • Improved Test file (WIP)

[1.1.4]

Added

  • areEqualDates
  • areEqualDateTimes
  • isToday
  • getStartOfWeek
  • getEndOfWeek
  • getWeekNumber
  • isFutureDate
  • isPastDate

[1.1.3]

Fixed

  • A bug where you cannot import the report-utils package in the /mobile side of an App.

[1.1.2]

Changed

  • [TESTING] Moved the loadImageBase64 function to index.js to see if the required("fs") problem is fixed.

[1.1.1]

Changed

  • [TESTING] Moved the require("fs") out of the function.

[1.1.0]

Changed

  • Refactored the file structure to separate the concerns.
  • Replaced displayPhoto with journeyPhotoToBase64, it now has a more descriptive name
  • Replaced loadPhotoOrSignature with getPhotoOrSignatureData, it's now up to the developer to load the data into a data hash.
  • Replaced loadSignatureBase64 with encodeBase64ImagePNG, renamed function and changed the input parameter to the actual image data.

[1.00.07]

Added

  • index.js: Added the ability to specify in which environments to enable email whitelisting.

[1.00.06]

Changed

  • index.js: Emails are first trimmed and toLowerCase'd before checking if it is valid.

[1.00.05]

Changed

  • index.js: Changed the way loadAttachments function receives retry_count value.

[1.00.04]

Fixed

  • index.js: a Bug where the loadPhotoOrSignature function was out of date.

[1.00.03]

Changed

  • index.js: Emails check now convert all emails to lowercase before checking for duplicates.

[1.00.02]

Added

  • index.js: Added function displayTimeWithSeconds.

Changed

  • index.js: improved javadocs commenting .

[1.00.01]

Changed

  • Readme.md: explanation improved.
  • index.js: Added functions displayDateTime and displayTime.

[1.00.00]

Added

  • package.json: Added.
  • index.js: Added and populated with Gatekeeper's handlebarUtil functions.
  • .npmignore: Added.
  • .gitignore: Added.
  • README.md: Added with description, set-up and change-log.

版本与发布记录

@gatekeeper_technology/report-utils 共发布 28 个版本,首次发布于 2022/02/18;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.3.3latest2026/07/104 个月前
1.3.22025/06/261.4 年前
1.3.12025/01/291.8 年前
1.3.02025/01/161.8 年前
1.2.32024/08/062 年前
1.2.22023/11/073 年前

关于 @gatekeeper_technology/report-utils 的常见问题

@gatekeeper_technology/report-utils 是什么?

Gatekeeper's pdf/email Utils - shared in NPM

如何安装 @gatekeeper_technology/report-utils?

在项目目录运行 npm install @gatekeeper_technology/report-utils,或使用 pnpm add / yarn add / bun add。

@gatekeeper_technology/report-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gatekeeper_technology/report-utils 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gatekeeper_technology/report-utils 可以商用吗?

@gatekeeper_technology/report-utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gatekeeper_technology/report-utils 的下载量有多少?

根据 npm 官方下载统计,@gatekeeper_technology/report-utils 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,280 次,日均约 443 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gatekeeper_technology/report-utils 还在维护吗?

最新版本 1.3.3 发布于 2026/07/10(4 个月前);至今共发布 28 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/02/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。