@flipdish/org 管理层
这个软件包为Flipdish的组织管理API提供了TypeScript/JavaScript客户端,覆盖了HTTP.
它的产生与 嘿,API(英语: 并使用本地语言 fetch API,所以它没有运行时间依赖性. 它在Node.js 18+, Bun, Deno和浏览器中工作,并同时运送ES模块和CommonJS.
示例代码
import { createFlipdishClient, SDK_INFO } from "@flipdish/orgmanagement";
import { describe, expect, test } from "@jest/globals";
const orgManagement = createFlipdishClient({
baseUrl: "https://api-prod-staging.flipdishdev.com",
// to get the API key, you should follow these docs:
// https://developers.flipdish.com/docs/getting-started
accessToken: process.env.FLIPDISH_BEARER_TOKEN,
// optional: identifies your app in the X-Flipdish-Client header,
// and in User-Agent outside browsers. Defaults to env vars such as DD_SERVICE.
clientName: "sdk-test-app",
});
describe("Org Management Tests", () => {
describe("Orgs", () => {
// test("Get org by id", async () => {
// const { data, error } = await orgManagement.getOrgById({
// path: { orgId: "org12345" },
// });
// expect(error).toBeUndefined();
// expect(data?.data.orgId).toBe("org12345");
// });
test("client is created", () => {
expect(orgManagement.getAllOrgs).toBeInstanceOf(Function);
});
});
describe("Client identity", () => {
test("sends User-Agent and X-Flipdish-Client in Node", async () => {
let sent: Request | undefined;
const sdk = createFlipdishClient({
clientName: "sdk-test-app",
clientVersion: "1.2.3",
fetch: async (input) => {
sent = input as Request;
return new Response(JSON.stringify({ data: [] }), {
headers: { "content-type": "application/json" },
});
},
});
await sdk.getAllOrgs();
const expected = `Flipdish-orgmanagement-ts/${SDK_INFO.version} sdk-test-app/1.2.3`;
expect(sent?.headers.get("user-agent")).toMatch(
new RegExp(`^${expected.replace(/[.+]/g, "\\$&")} `),
);
expect(sent?.headers.get("x-flipdish-client")).toBe(
sent?.headers.get("user-agent"),
);
});
});
});
客户身份
每个请求都包含 X-Flipdish-Client 识别SDK和应用程序的头条 外部浏览器同样值也被发送为 User-Agent,例如:
Flipdish-orgmanagement-ts/2.0.1 menu-service/2.3.1 env/production node/22.18.0
每个部分都可以被设定在代码中,或者使用环境变量. 第一个被设定的获胜者, 请先选中选项 :
| 编 | 选项 | 环境变量 |
|---|---|---|
| 应用名称 | clientName | FLIPDISH_CLIENT_NAME, (中文(简体) ), DD_SERVICE, (中文(简体) ), AWS_LAMBDA_FUNCTION_NAME, (中文(简体) ), npm_package_name |
| App 版本 | clientVersion | FLIPDISH_CLIENT_VERSION, (中文(简体) ), DD_VERSION, (中文(简体) ), npm_package_version |
| 环境 | clientEnvironment | FLIPDISH_CLIENT_ENVIRONMENT, (中文(简体) ), DD_ENV, (中文(简体) ), NODE_ENV |
| 运行时间 | runtime | FLIPDISH_CLIENT_RUNTIME,然后检测出节点.js、 Bun 或 Deno 版本 |
| 后缀 | userAgentSuffix | FLIPDISH_CLIENT_USER_AGENT_SUFFIX |
将选项传递到 createFlipdishClient,或打电话 configureClientIdentity 设置默认值 client数字 :
import { configureClientIdentity } from "@flipdish/orgmanagement";
configureClientIdentity({ clientName: "menu-service", clientVersion: "2.3.1", clientEnvironment: "production" });
浏览器没有环境变量, 因此至少设定 clientName 在代码那里。 任何传入的东西 headers 覆盖这些值.