弹性路径商业 Cloud JavaScript SDK
[大
此寄存器已经从 @ moltin/ sdk (英语)如果您是 Moltin SDK 的前用户,请阅读我们 指南 学习如何将您的执行迁移到这个代码库
一个简单的使用API接口来帮助您快速高效地从地上脱身,与您的"弹性路径"商业Cloud JavaScript应用程序相通.
* 安装
安装软件包从 无 并导入您的项目.
npm install --save @elasticpath/js-sdk
使用量
要启动, 请用您的商店证书即时处理一个新的 Elastic Path 客户端 .
// JavaScript
import { gateway as ElasticPathGateway } from '@elasticpath/js-sdk'
const ElasticPath = ElasticPathGateway({
client_id: 'XXX'
})
// Node.js
const ElasticPathGateway = require('@elasticpath/js-sdk').gateway
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
client_secret: 'XXX'
})
或者您可以包括 UMD 通过捆绑 联科办 像这样:
<script src="https://unpkg.com/@elasticpath/js-sdk"></script>
<script>
const ElasticPath = elasticpath.gateway({
client_id: 'XXX'
});
</script>
说明: 如果你在用的话 网络软件包,您需要在您的工程配置文件中添加以下内容 .
node: {
fs: 'empty'
}
您现在可以通过 ElasticPath 服务认证
ElasticPath.Authenticate().then(response => {
console.log('authenticated', response)
})
看看这个 API 参考 学习更多关于认证和可用终点的知识.
自定义主机
如果您是拥有自己基础设施的企业客户,您需要指定您的 API URL 当即时:
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
host: 'api.yourdomain.com'
})
自定义存储
默认情况下, Elastic Path Commerce Cloud SDK 将数据持续到 window.localStorage 在浏览器和 node-localstorage 在节点。 如果这不适合您的需要, 您可以用一个 MemoryStorageFactory 将持续使用 JavaScript VM 的生命周期数据:
import { gateway as ElasticPathGateway, MemoryStorageFactory } from '@elasticpath/js-sdk'
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
storage: new MemoryStorageFactory()
})
或者,通过通过一个执行下列接口的对象来创建自己的存储厂:
interface StorageFactory {
set(key: string, value: string): void;
get(key: string): string | null;
delete(key: string): void;
}
多个网关
您可以支持多个网关 name 属性在初始化网关时.
name 应当独有,以避免与同名的其他网关共享存储密钥.
import { gateway as EPCCGateway } from "@elasticpath/js-sdk"
const gatewayOne = EPCCGateway({
name: "my-first-gateway",
client_id: 'XXX'
})
const gatewayTwo = EPCCGateway({
name: "my-second-gateway",
client_id: 'XXX'
})
用于存储解决方案的存储密钥被前缀所提供名称并用相关特性等结尾.
my-first-gateway_ep_cart, (中文(简体) ), my-first-gateway_ep_credentials 财务报告和审定财务报表 my-first-gateway_ep_currency。 。 。 .
如果不向EPCCGateway函数提供名称属性,则保留遗留的命名:
mcart, (中文(简体) ), epCredentials 财务报告和审定财务报表 mcurrency
包含标题
目前有几种可选标题可以传递到配置中,其中包括: application, (中文(简体) ), language 财务报告和审定财务报表 currency。 。 。 .
你可以把它们传到网关使用的配置中:
// JavaScript
import { gateway as ElasticPathGateway } from '@elasticpath/js-sdk'
// const ElasticPathGateway = require('@elasticpath/js-sdk').gateway -> for Node
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
client_secret: 'XXX'
currency: 'YEN',
language: 'en',
application: 'my-app'
})
检索
如果服务器响应状态为429 - "太多请求" SDK会等待一段时间并重试相同的API请求,最多可以重试一次. 您可以通过下列配置参数来调整这个逻辑 :
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
client_secret: 'XXX',
retryDelay: 1000,
retryJitter: 500,
fetchMaxAttempts: 4
})
如果有429个响应 SDK会等待 retryDelay 毫秒( 默认为 1000 秒) , 然后再尝试发出相同的呼叫 .
如果服务器再次响应429,它将等待2* retryDelay ms,然后3 * retryDelay 毫发等.
上面是零和零之间的随机值 retryJitter 每等一等,将增加(默认500).
这将重复到 fetchMaxAttempts (默认 4) 乘.
调压( 限制时间)
SDK 通过使用支持节流 throttled-queue 图书馆.
和流行图书馆的节流器不同, throttled-queue 不会阻止任何处决.
相反,每一次执行都被排入队列,排出队将按所期望的速率极限排出.
您可以通过下列参数控制节流 :
const ElasticPath = ElasticPathGateway({
client_id: 'XXX',
client_secret: 'XXX',
throttleEnabled: true,
throttleLimit: 3,
throttleInterval: 125
})
此功能默认已禁用, 并允许您需要设置 throttleEnabled 真实的.
启用后可以使用 throttleLimit (违约3)和 throttleInterval (default 125),用于定义每个间隔的最大呼叫次数.
例如设置 throttleLimit = 5, throttleInterval = 1000 表示每秒最多5通电话.
处理文件上传
文件可上传到 EPCC 文件服务 ElasticPath.Files.Create 方法。 你应该通过一个 FormData 对象,如 文档。 。 。 .
在节点环境中,您可能正在使用替代品 FormData 中,您可以包含第二个参数来表示 Content-Type 请求头。 这一定是 multipart/form-data 并且必须包括 boundary。例如,使用 form-data 软件包数字 :
const FormData = require('form-data')
const formData = new FormData()
formData.append('file', buffer)
const contentType = formData.getHeaders()['content-type']
ElasticPath.Files.Create(formData, contentType)
引用存储在其他地方的文件
如果您想要简单创建文件 参考资料 保存到其它地方的文件, 您可以使用此帮助方法 :
ElasticPath.Files.Link('https://cdn.external-host.com/files/filename.png')
将 URL 传给 Link 方法和创造会为你处理.
类型脚本支持
弹性路径通商云JavaScript SDK在Typescript中得到充分支持.
被导入的模块将包含消费后端服务所需的所有接口. 即:
import * as elasticpath from '@elasticpath/js-sdk';
const product: elasticpath.ProductBase = {...}
如果您不想使用命名空间,您可以扩展接口并自己定义它们,比如:
// You can name the interface anything you like
interface Product extends product.ProductBase {
}
const product: Product = {...}
以下是简单产品创造的例子:
import { ElasticPath, gateway, ProductBase, Resource } from '@elasticpath/js-sdk';
async function main() {
const g: ElasticPath = gateway({client_id, client_secret});
const auth = await g.Authenticate();
const newProduct: ProductBase = {
type: "product",
name: "My Product",
slug: "my-prod",
sku: "my-prod",
manage_stock: false,
description: "Some description",
status: "draft",
commodity_type: "physical",
price: [
{
amount: 5499,
currency: "USD",
includes_tax: true
}
]
};
const nP: Resource<Product> = await g.Products.Create(newProduct);
}
您也可以扩展任何与流量相容的基础接口,以创建您可能通过重新声明而使用的自定义接口 @elasticpath/js-sdk 模块。 以下示例增加了几个属性 ProductsBase 对应于后端所添加的流量的界面.
在您的工程中添加定义文件( 有 .d.ts 扩展),代码如下:
import * as elasticpath from '@elasticpath/js-sdk';
declare module '@elasticpath/js-sdk' {
interface Weight {
g: number;
kg: number;
lb: number;
oz: number;
}
interface ProductBase {
background_color: string;
background_colour: string | null;
bulb: string;
bulb_qty: string;
finish: string;
material: string;
max_watt: string;
new: string | null;
on_sale: string | null;
weight: Weight;
}
}
这将影响基础接口,在创建、更新、取取产品时,所有继承基础接口的所有其他产品接口都将存在.
捐款
我们爱社区的贡献。 这里有一个快速的向导,如果你想提交拉动请求:
- 叉子存储器
- 添加您更改的测试( 它会失败 )
- 考试通过
- 提交更改(见下文注)
- 提交您的 PR 简略描述解释您的更改
说明: 承诺应遵守 角承诺公约。 。 。 .
确保你有 高级 为您的编辑器安装, 启用 ESLint 集成.
发展
如果您想要滚动自己的捆绑, 或修改其中的任何模块 src,然后需要安装软件包的依赖性,并在观察变化时运行滚动.
npm install
npm start
运行测试
npm test
您可以更多地了解 Rollup API 和配置 这里。 。 。 .
术语 条件
- 这一项目的任何改动都必须由存储器所有人审查和批准.
- 关于执照的更多信息,请参见: MIT 许可证。 。 。 .