@forgerock/crest-js

中文文档
2.1.11发布于 2022/04/12MITESM + CJS近 30 天下载 8399

Lightweight Library for Communicating With ForgeRock CREST APIs.

原始使用文档
文档目录18 节
  1. Prerequisites
  2. Supported Versions
  3. Installation
  4. via Yarn
  5. via NPM
  6. Usage
  7. Basic
  8. Supported Methods
  9. Options
  10. Middleware
  11. Development
  12. Project Structure
  13. Building
  14. Docs
  15. Testing
  16. Test Coverage
  17. Contributing
  18. Further Reading

ForgeRock CREST.js

Lightweight Library for Communicating With ForgeRock CREST APIs.

Prerequisites

ForgeRock CREST.js requires the fetch and Promise APIs to be available in the global environment.

Supported Versions

ForgeRock CREST.js supports CREST versions 2.0 and 2.1.

import { CRESTv2, CRESTv2_1 } from "@forgerock/crest-js";

Installation

via Yarn

yarn add @forgerock/crest-js

via NPM

npm install @forgerock/crest-js

Usage

Basic

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.get("id").then(json => {
  // Success! `json` is an Object
}, error => {
  if (error instanceof CRESTError) {
    // CREST error
  } else if (error instanceof RequestError) {
    // Request could not be completed e.g. network failure
  } else if (error instanceof ParseError) {
    // Response couldn't be parsed as JSON
  }
});

All the functions upon a CREST resource return Promises.

Promises allow for easily building on top of the core functionality, for example, with common handlers that deal with rejections consistently.

Supported Methods

import { CRESTv2_1 } from "@forgerock/crest-js";

const resource = new CRESTv2_1("http://www.domain.com/crest/api");
const body = { data: "value" };

// #action
resource.action("action");
resource.action("action", { body }); // Action with body

// #create
resource.create(body); // Server provided ID
resource.create(body, { id: "id" }); // Client provided ID

// #delete
resource.delete("id");

// #get
resource.get("id");

// #queryFilter
resource.queryFilter(); // Only supports `_queryFilter=true`

// #update
resource.update("id", body);

Pagination is currently only supported via the additional query strings option queryString.

See the API Documentation for all possible options.

Options

queryString

For adding additional query strings to the any request.

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.get("id", {
  queryString: {
      query: "value"
  }
})
// => http://www.domain.com/crest/api/id?query=value

Query strings applied by ForgeRock CREST.js cannot be overridden.

import { CRESTv2 } from "@forgerock/crest-js";

const resource = new CRESTv2("http://www.domain.com/crest/api");

resource.action("action1", {
  queryString: {
      _action: "action2"
  }
})
// => http://www.domain.com/crest/api?_action=action1

Middleware

One or many middleware can be applied to an CREST resource.

Each middleware is a function that takes a single Promise parameter, and returns a Promise to pass to the next middleware.

The first middleware in the chain is guaranteed to receive a Promise which is either resolved to a parsed JSON payload, or rejected with one of the defined error types.

import { CRESTv2_1 } from "@forgerock/crest-js";

const customMiddleware = (promise) => {
  return promise.then((json) => {
    // Success! `json` is an Object
    const jsonToReturn = {
      ...json,
      myAttribute: true
    };

    // Return value will be passed the next middleware
    return json;
  }, (error) => {
    // Capture, rethrow or modify errors
    throw new CustomError(error.message);
  });
};

const resource = new CRESTv2_1("http://www.domain.com/crest/api", {
  middleware: [customMiddleware]
});

Development

Project Structure

dist/ # Output of "yarn run build".
config/ # Tooling configuration files.
docs/ # API documentation And output of "yarn run docs".
src/ # Source code.
├── .babelrc # Babel configuration for source when executing "yarn run test" or "yarn run test:coverage".
└── ...

Building

Builds production CommonJS, ES and UMD versions into /dist.

yarn run build

Docs

Generates API JSDoc into /docs.

yarn run docs

Testing

Runs tests against the source.

yarn run test

The yarn command will pass arguments to the underlying command. For examples, use yarn run test --verbose --watch for continuous testing with extra output.

Test Coverage

Generates test coverage report.

yarn run test:coverage

Contributing

Contribute to ForgeRock CREST.js by opening a Pull Request.

Further Reading

版本与发布记录

@forgerock/crest-js 共发布 17 个版本,首次发布于 2018/11/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.11latest2022/04/124 年前
2.1.102022/03/104 年前
2.1.92022/03/084 年前
2.1.82022/03/014 年前
2.1.72021/08/105 年前
2.1.62021/07/145 年前

关于 @forgerock/crest-js 的常见问题

@forgerock/crest-js 是什么?

Lightweight Library for Communicating With ForgeRock CREST APIs.

如何安装 @forgerock/crest-js?

在项目目录运行 npm install @forgerock/crest-js,或使用 pnpm add / yarn add / bun add。

@forgerock/crest-js 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/forgerock__crest-js 是否存在。

@forgerock/crest-js 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@forgerock/crest-js 可以商用吗?

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

@forgerock/crest-js 的下载量有多少?

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

@forgerock/crest-js 还在维护吗?

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