@adobe/spacecat-shared-http-utils

中文文档
1.38.0发布于 2026/10/07Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2548

Shared modules of the Spacecat Services - HTTP Utils

原始使用文档
文档目录15 节
  1. Table of Contents
  2. Introduction
  3. Installation
  4. Usage
  5. API
  6. ok(body?: string): Response
  7. noContent(headers?: Headers): Response
  8. badRequest(message: string, headers?: Headers): Response
  9. notFound(message: string, headers?: Headers): Response
  10. internalServerError(message: string, headers?: Headers): Re…
  11. Authentication
  12. FACS / ReBAC group subjects
  13. ScopedApiKeyHandler
  14. Contributing
  15. License

Spacecat Shared - HTTP Utilities

A set of TypeScript functions for creating HTTP responses with standardized formats, and classes for dealing with authenticating HTTP requests.

v1.19.0: Added trace ID extraction from HTTP headers for distributed tracing support.

Table of Contents

Introduction

This library provides a collection of functions related to http to be used in Spacecat development. The functions are designed to create responses with standardized formats, making it easier to maintain a consistent structure across different parts of your application.

Installation

Install the package using npm or yarn:

npm install @adobe/spacecat-shared-http-utils

or

yarn add @adobe/spacecat-shared-http-utils

Usage

Import the functions in your TypeScript file and use them to generate HTTP responses. Here's an example:

import {
  ok,
  noContent,
  badRequest,
  notFound,
  internalServerError,
} from '@adobe/spacecat-shared-http-utils';

// Example usage
const successResponse: Response = ok('Request was successful');

const emptyResponse: Response = noContent();

const errorResponse: Response = badRequest('Invalid input');

const notFoundResponse: Response = notFound('Resource not found');

const serverErrorResponse: Response = internalServerError('Something went wrong');

API

ok(body?: string): Response

Creates a successful response with an optional body.

noContent(headers?: Headers): Response

Creates a response with no content and optional headers.

badRequest(message: string, headers?: Headers): Response

Creates a response for a bad request with an error message and optional headers.

notFound(message: string, headers?: Headers): Response

Creates a response for a not found scenario with an error message and optional headers.

internalServerError(message: string, headers?: Headers): Response

Creates a response for an internal server error with an error message and optional headers.

Authentication

This package includes classes for dealing with authenticating HTTP requests.

FACS / ReBAC group subjects

For FACS-enabled JWT sessions, AuthInfo#getFacsGroups() reads the facs_grps claim and returns up to 100 sanitized IMS group idents. The facsWrapper computes effective access as the union of JWT facs_permissions, user state mappings, org state mappings, and group state mappings from facs_access_mappings (subject_type = 'group'). Grants are additive only.

ScopedApiKeyHandler

Scoped API keys are defined in the datalayer and can be used to authenticate requests to the Spacecat API. They employ "scopes" to enable fine-grained access to resources. An example API key entity looks like this (id omitted):

{
  "name": "Example API Key",
  "hashedApiKey": "4c806362b613f7496abf284146efd31da90e4b16169fe001841ca17290f427c4",
  "createdAt": "2024-08-21T19:00:00.000Z",
  "expiresAt": "2024-12-21T19:00:00.000Z",
  "scopes": [
    { "name": "imports.write" },
    { "name": "imports.read" }
  ]
}

Key points on the above:

  • hashedApiKey is the SHA-256 hash of the actual API key ("test-api-key" above)
  • scopes are the permissions granted to the API key
  • Each scope object can contain additional data, but the name field is required

The ScopedApiKeyHandler class is used to authenticate requests using scoped API keys. To support the existing Legacy API keys, it should be ordered after the LegacyApiKeyHandler in the authHandlers array. This enables requests with the existing API keys to be authenticated quickly without requiring a database lookup.

Checking for scope access

To enable a new scope, first refer to the scopeNames array in the ApiKey model (/packages/spacecat-shared-data-access/src/models/api-key.js). If the scope you need is not listed here, please add it. Note the convention for scope names is resource.action, e.g. imports.write or sites.read_all. The _all action suffix indicates access beyond resources created (or jobs initiated by) the current API key.

Next, you will want to check that the API used to make the request has access to the required scope(s) from your controller. The authWrapper adds an auth helper to the context which makes this easy. Here's an example of how to check for scope access from a controller:

// This route requires the 'imports.write' scope
function protectedRoute(context) {
  const { auth } = context;
  
  try {
    auth.checkScopes(['imports.write']);
  } catch (error) {
    throw new ErrorWithStatusCode('Missing required scopes', 401);
  }
  
  return ok('You have access to this resource');
}

Need additional details from the API key entity object? The authWrapper places the authenticated authInfo object into the context at context.attributes.authInfo, with the API key entity available in its profile property.

Creating a new API key

This is currently a manual process, and involves duplicating an existing API key entity in the datalayer and updating its properties. For the table to update, refer to the TABLE_NAME_API_KEYS constant (which will be overridden on prod).

In the future we are planning to support a way for clients to request their own API key, given a valid IMS token.

Contributing

Feel free to contribute by opening issues or creating pull requests. Please follow the existing coding style and include tests when adding new features.

License

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

版本与发布记录

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

版本发布日期距今
1.38.0latest2026/10/071 个月前
1.37.02026/09/081 个月前
1.36.02026/08/272 个月前
1.35.22026/08/252 个月前
1.35.12026/08/242 个月前
1.35.02026/08/122 个月前

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

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

Shared modules of the Spacecat Services - HTTP Utils

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

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

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

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

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

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

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

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

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

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

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

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

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

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