@adobe/spacecat-shared-utils

中文文档
1.128.0发布于 2026/10/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 1.5万

Shared modules of the Spacecat Services - utils

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Functions
  4. Log Wrapper
  5. Features
  6. Usage
  7. SQS Event Adapter
  8. AWS X-Ray Integration
  9. getTraceId()
  10. Testing
  11. Sub-path Exports
  12. TypeScript
  13. Maintenance
  14. License

SpaceCat Shared Utilities

This repository contains a collection of shared utility functions used across various SpaceCat projects. These utilities provide a range of checks and validations, from basic data type validation to more complex checks like ISO date strings and URL validation.

v1.76.0: Added trace ID propagation support for distributed tracing across SpaceCat services.

Installation

To install the SpaceCat Shared Utilities, you can use npm:

npm install spacecat-shared-utils

Or, if you are using yarn:

yarn add spacecat-shared-utils

Usage

Here's how you can use the different utility functions in your project:

import { isBoolean, isValidUrl } from 'spacecat-shared-utils';

console.log(isBoolean('true')); // true
console.log(isValidUrl('https://www.example.com')); // true

Functions

The library includes the following utility functions:

  • isBoolean(value): Determines if the given value is a boolean or a string representation of a boolean.
  • isInteger(value): Checks if the given value is an integer.
  • isValidDate(obj): Checks whether the given object is a valid JavaScript Date.
  • isIsoDate(str): Validates whether the given string is a JavaScript ISO date string in Zulu (UTC) timezone.
  • isIsoTimeOffsetsDate(str): Validates whether the given string is a JavaScript ISO date string following UTC time offsets format.
  • isNumber(value): Determines if the given value is a number.
  • isObject(obj): Checks if the given parameter is an object and not an array or null.
  • isString(str): Determines if the given parameter is a string.
  • toBoolean(value): Converts a given value to a boolean. Throws an error if the value is not a boolean.
  • arrayEquals(a, b): Compares two arrays for equality.
  • isValidUrl(urlString): Validates whether the given string is a valid URL with http or https protocol.
  • hasText(str): Checks if the given string is not empty.
  • dateAfterDays(number): Calculates the date after a specified number of days from the current date.

Log Wrapper

The logWrapper enhances your Lambda function logs by automatically prepending jobId (from message) and traceId (from AWS X-Ray) to all log statements. This improves log traceability across distributed services.

Features

  • Automatically extracts AWS X-Ray trace ID
  • Includes jobId from message when available
  • Enhances context.log directly - no code changes needed
  • Works seamlessly with existing log levels (info, error, debug, warn, trace, etc.)

Usage

import { logWrapper, sqsEventAdapter } from '@adobe/spacecat-shared-utils';

async function run(message, context) {
  const { log } = context;
  
  // Use context.log as usual - trace IDs are added automatically
  log.info('Processing started'); 
  // Output: [jobId=xxx] [traceId=1-xxx-xxx] Processing started
}

export const main = wrap(run)
  .with(sqsEventAdapter)
  .with(logWrapper)  // Add this line early in the wrapper chain
  .with(dataAccess)
  .with(sqs)
  .with(secrets)
  .with(helixStatus);

Note: The logWrapper enhances context.log directly. All existing code using context.log will automatically include trace IDs and job IDs in logs without any code changes.

SQS Event Adapter

The library also includes an SQS event adapter to convert an SQS record into a function parameter. This is useful when working with AWS Lambda functions that are triggered by an SQS event. Usage:

import { sqsEventAdapter } from '@adobe/spacecat-shared-utils';

// ...

export const main = wrap(run)
  .with(dataAccess)
  .with(sqsEventAdapter) // Add this line
  .with(sqs)
  .with(secrets)
  .with(helixStatus);

AWS X-Ray Integration

getTraceId()

Extracts the current AWS X-Ray trace ID from the segment. Returns null if not in AWS Lambda or no segment is available.

import { getTraceId } from '@adobe/spacecat-shared-utils';

const traceId = getTraceId();
// Returns: '1-5e8e8e8e-5e8e8e8e5e8e8e8e5e8e8e8e' or null

This function is automatically used by logWrapper to include trace IDs in logs.

Testing

This library includes a comprehensive test suite to ensure the reliability of the utility functions. To run the tests, use the following command:

npm test

Sub-path Exports

To avoid pulling in the full dependency tree (~110MB), import from a sub-path:

ImportDependencies
@adobe/spacecat-shared-utilsAll (~110MB)
@adobe/spacecat-shared-utils/coreNone — pure JS only
@adobe/spacecat-shared-utils/aws@aws-sdk/*, aws-xray-sdk, @adobe/fetch ⚠ Side effect: initializes HTTP connection pool at import time — requires outbound internet access
@adobe/spacecat-shared-utils/localecheerio, world-countries, franc-min, iso-639-3, @adobe/fetch ⚠ Side effect: initializes HTTP connection pool at import time — requires outbound internet access
@adobe/spacecat-shared-utils/calendardate-fns
@adobe/spacecat-shared-utils/schemaszod
@adobe/spacecat-shared-utils/constantsNone — pure data

TypeScript

Sub-path exports require "moduleResolution": "node16", "nodenext", or "bundler" in tsconfig.json. Legacy "moduleResolution": "node" does not resolve sub-path exports.

Maintenance

Any new src/ file that contains top-level imperative code (anything beyond import/export statements) must be added to the sideEffects array in package.json.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

版本与发布记录

@adobe/spacecat-shared-utils 共发布 318 个版本,首次发布于 2023/11/28;过去 12 个月发布了 106 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.128.0latest2026/10/021 个月前
1.127.02026/09/301 个月前
1.126.02026/08/172 个月前
1.125.22026/08/122 个月前
1.125.12026/08/043 个月前
1.125.02026/07/173 个月前

关于 @adobe/spacecat-shared-utils 的常见问题

@adobe/spacecat-shared-utils 是什么?

Shared modules of the Spacecat Services - utils

如何安装 @adobe/spacecat-shared-utils?

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

@adobe/spacecat-shared-utils 支持 TypeScript 吗?

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

@adobe/spacecat-shared-utils 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adobe/spacecat-shared-utils 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0 <25.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@adobe/spacecat-shared-utils 可以商用吗?

@adobe/spacecat-shared-utils 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/spacecat-shared-utils 的下载量有多少?

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

@adobe/spacecat-shared-utils 还在维护吗?

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