时间轴:
设置( 需要)
要设置您需要告诉租户的Sdk和认证方法,需要租户. 你必须运行这个 先打个电话 如果你不安排房客,SDK会失败的.
import { DrozSdk } from '@droz-js/sdk';
DrozSdk.forTenant('dev').withAuthorization('Basic', 'username', 'password');
也可以分成两步
import { DrozSdk } from '@droz-js/sdk';
// first step (required)
DrozSdk.forTenant('dev');
// (optional) later when you have the authentication token you can set it and it will automatically be used in the new requests
DrozSdk.withAuthorization('Basic', 'username', 'password');
全局错误处理
Sdk 会给每个失败的请求投出一个错误, 但你也可以通过使用 on
方法.
import { DrozSdk } from '@droz-js/sdk';
DrozSdk.on('error', (error) => {
console.log(error);
});
发送的错误都是类型 SdkError 并包含下列属性:
message: 服务器发送的原始错误消息;statusCode:与http状态代码相类似并显示出错的类别,400个坏请求,403被禁, 404没有找到..;errorCode: 错误的唯一标识符,这对i18n友好消息有用;
网页
对于我们使用的 Websocket graphql-ws 协议与 AsyncIterator 处理订阅。 SDK 将自动
处理您的连接和重接,
见 https://the-guild.dev/graphql/ws/recipes#客户端-usage- with-asynciterator (英语)
关于如何使用 AsyncIterator。 。 。 .
示例 AsyncIterator
import { DrozSdk } from '@droz-js/sdk';
import { DrozChat } from '@droz-js/sdk/drozchat';
DrozSdk.forTenant('dev');
const chat = new DrozChat();
const iterable = chat.onTicketInQueue();
for await (const result of iterable) {
console.log(result);
}
反应开发者
import { DrozSdk } from '@droz-js/sdk';
import { DrozChat } from '@droz-js/sdk/drozchat';
DrozSdk.forTenant('dev');
function useOnTicketInQueue() {
const chat = new DrozChat();
const [tickets, setTickets] = useState<Ticket[]>([]);
useEffect(() => {
const subscription = chat.onTicketInQueue();
// YES I KNOW, REACT SUCKS
(async () => {
// attention! this is just an example what you do with the result is up to you
for await (const each of subscription) setTickets(tickets => [...tickets, each]);
})();
// this is required to close the subscription when the component unmounts
return () => subscription.return();
}, []);
}
可用磁盘
德罗斯·内克索
import { DrozNexo } from '@droz-js/sdk/droznexo';
德罗兹聊天
import { DrozChat } from '@droz-js/sdk/drozchat';