先决条件
ForgeRock CREST.js 要求将 fetch 财务报告和审定财务报表 Promise 可在全球环境中获取的API.
支持的版本
ForgeRock CREST.js支持CREST版本2.0和2.1.
import { CRESTv2, CRESTv2_1 } from "@forgerock/crest-js";
安装
通过 Yarn
yarn add @forgerock/crest-js
通过国家预防机制
npm install @forgerock/crest-js
使用量
基础
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
}
});
CREST 资源返回时的所有功能.
承诺允许在核心功能之上容易地建立起来,例如,与一贯处理拒绝问题的普通处理者打交道.
支助方法
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);
目前只通过额外的查询字符串选项支持 Pgination queryString。 。 。 .
见 API 文档 所有可能的选择.
选项
queryString
在任何请求中添加额外查询字符串 .
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
ForgeRock CREST.js 应用的查询字符串不能被推翻 .
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
中间软件
一个或许多中间软件可以被应用于 CREST 资源.
每个中件是一个函数,它需要一个单一的"承诺"参数,并返回一个"承诺"以传递给下一个中件.
链中的第一个中间器件可以保证得到一个承诺,它要么解析出JSON有效载荷,要么用定义的错误类型之一拒绝.
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]
});
发展
项目结构
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".
└── ...
大楼
将生产通用JS、ES和UMD版本制成 /dist。 。 。 .
yarn run build
文档
生成 API JSDoc 到 /docs。 。 。 .
yarn run docs
测试
运行测试对源.
yarn run test
这个 yarn 命令会将参数传递给基础命令。 示例,使用 yarn run test --verbose --watch 用于带有额外输出的连续测试.
测试范围
生成测试覆盖报告.
yarn run test:coverage
捐款
向ForgeRock CREST.js 投稿,通过打开拉出请求.