FDK Javascript
Javascript 的 FDK 客户端
开始
启动 Fynd 平台的 Javascript 开发 SDK
使用量
节点
npm install @gofynd/fdk-client-javascript
用这种方法,你可以 require fdk - 客户端 - javascript 像这样:
const {
ApplicationClient,
} = require("fdk-client-javascript");
浏览器
您可以从 CDN 装入 fdk-client-javascript 的应用程序浏览器捆绑; ApplicationClient 财务报告和审定财务报表 ApplicationModels 将附加在浏览器上 window 对象.
<script src="https://cdn.jsdelivr.net/gh/gofynd/fdk-client-javascript@<version>/dist/application.js"></script>
安装特定版本
<script src="https://cdn.jsdelivr.net/gh/gofynd/[email protected]/dist/application.js"></script>
const { ApplicationClient } = window;
日志
对于日志支持用户可以通过 logLevel 内 ApplicationClient 或者说 PlatformClient 在宣布的时候.
Available logging levels: TRACE, DEBUG, INFO, WARN, ERROR.
Default log level: ERROR
样本使用 - 应用程序客户端
const applicationClient = new ApplicationClient({
applicationID: "YOUR_APPLICATION_ID",
applicationToken: "YOUR_APPLICATION_TOKEN",
locationDetails: "LOCATION_DETAILS_OBJECT"
});
applicationClient.setLocationDetails({
pincode:"385001",
country: "India",
city: "Ahmedabad",
location: {longitude: "72.585022", latitude: "23.033863"}
});
async function getProductDetails() {
try {
const product = await applicationClient.catalog.getProductDetailBySlug({
slug: "product-slug"
});
console.log(product.name);
} catch (err) {
console.log(err);
}
}
getProductDetails();
样本使用量 - 平台客户端
const { PlatformClient } = require("fdk-client-javascript");
async function getData() {
try {
const client = new PlatformClient({
companyId: "COMPANY_ID",
apiKey: "API_KEY",
apiSecret: "API_SECRET",
domain: "DOMAIN",
useAutoRenewTimer: true // Setting `true` will use timer based logic to refresh the access token. With `false` will issue refresh token just before any api call when it is expired.
});
const token = await platformClient.getAccesstokenObj({
grant_type: 'client_credentials'
})
platformClient.setToken(token);
// API's without application_id
const tickets = await client.lead.getTickets();
console.log("tickets", tickets);
// API's with application_id
const customers = await client
.application("APPLICATION_ID")
.user.getCustomers();
console.log("customers", customers);
} catch (err) {
console.log(err);
}
}
getData();
样本使用 - 请求功能.
请求功能允许您轻松地自定义 API 请求 。 两者都有 platform 财务报告和审定财务报表 application 客户端.
let auditLog = await client.request({
url: `/service/platform/audit-trail/v1.0/company/1/logs`,
method: 'GET',
});
这个 request 函数接受一个带有以下可能按键的对象:
- 方法 (字符串):使用HTTP方法(如"GET","POST","PUT","DELETE").
- url 密码 (字符串):请求的URL端点.
- 查询 (对象):在请求的URL中包含查询参数.
- 机构 (对象):请求发送的有效载荷.
- 页眉 (对象):请求中包含的额外头.
饼干
代码片段会代表你读写曲奇
以后的请求中会附上饼干.
const { FdkAxios } = require('@gofynd/fdk-client-javascript');
const { wrapper } = require("axios-cookiejar-support");
const { CookieJar } = require("tough-cookie");
wrapper(FdkAxios);
const cookieJar = new CookieJar();
FdkAxios.defaults.jar = cookieJar;
module.exports = cookieJar
从 CookieJar 实例中获得存储的饼干
const cookieJar = require('path/to/cookieJar') // replace with actual path
let cookies = await cookieJar.getCookies("https://api.fynd.com");
页眉
添加请求信头
要在调用方法时包含请求头, 请添加 requestHeaders 第一个参数中的对象.
const requestHeaders = {
"x-api-version": "1.0"
}
const options = {
responseHeaders: true
}
const [response, headers] = await platformClient.application("<APPLICATION_ID>").theme.getAllPages({theme_id: "<THEME_ID>", requestHeaders: requestHeaders}, options);
获取回复信头
要获取响应头,只需设置 responseHeaders 选项到 true 当唤作理想方法时. 以下是说明如何实现这一目标的例子:
const options = {
responseHeaders: true
}
const [response, headers] = await platformClient.application("<APPLICATION_ID>").theme.getAllPages({theme_id: "<THEME_ID>"}, options);
console.log("[API RESPONSE]: ", response);
console.log("[RESPONSE HEADERS]: ", headers);
日志曲线
在控制台上打印所有网络呼叫的卷曲命令 applicationClient 或者说 platformClient,设置日志级别以调试.
const {
ApplicationClient,
} = require("fdk-client-javascript");
let applicationClient = new ApplicationClient({
applicationID: "YOUR_APPLICATION_ID",
applicationToken: "YOUR_APPLICATION_TOKEN",
logLevel: "debug"
});
let response = await applicationClient.theme.getAppliedTheme();
console.log("Active Theme: ", response.information.name);
上述代码将登录控制台的卷轴命令
curl --request GET "https://api.fynd.com/service/application/theme/v1.0/applied-theme" --header 'authorization: Bearer <authorization-token>' --header 'x-fp-sdk-version: 3.36.0' --header 'x-fp-date: 20230222T115108Z' --header 'x-fp-signature: v1.1:1e3ab3b02b5bc626e3c32a37ee844266ade02bbcbaafc28fc7a0e46a76a7a1a8'
Active Theme: Emerge
类型脚本
fdk-client-javascript包括了Typescript定义.
import { ApplicationClient } from "fdk-client-javascript";