A simple and easy to use SDK for SiYuan Note.
Getting Started
Installation
Using npm:
$ npm install @siyuan-community/siyuan-sdk
Using pnpm:
$ pnpm i @siyuan-community/siyuan-sdk
Using yarn:
$ yarn add @siyuan-community/siyuan-sdk
Examples
Initialize the client
Default configuration
import { Client } from "@siyuan-community/siyuan-sdk";
const client = new Client({
baseURL: "http://localhost:6806/",
token: "0123456789abcdef",
});
Configure as a XHR client
import { Client } from "@siyuan-community/siyuan-sdk";
const client = new Client(
{
baseURL: "http://localhost:6806/",
token: "0123456789abcdef",
},
"xhr",
);
Configure as a Fetch client
import { Client } from "@siyuan-community/siyuan-sdk";
const client = new Client(
{
baseURL: "http://localhost:6806/",
token: "0123456789abcdef",
},
"fetch",
);
Update the model of HTTP client
client._setClientType("fetch");
client._setClientType("xhr");
Update client global configuration
client._updateOptions({
token: "abcdef0123456789",
});
client._updateOptions(
{
timeout: 10_000,
},
"xhr",
);
client._updateOptions(
{
retry: 3,
},
"fetch",
);
Call Kernel API (async)
import { KernelError, HTTPError } from "@siyuan-community/siyuan-sdk";
async function func() {
try {
const response = await client.pushMsg({
msg: "This is a notification message",
timeout: 7_000,
});
console.log(response);
}
catch (error) {
if (error instanceof KernelError) {
console.error(error.body);
}
else if (error instanceof HTTPError) {
console.error(error.status);
}
else {
throw error;
}
}
finally {
}
}
Call Kernel API (Promise)
import { KernelError, HTTPError } from "@siyuan-community/siyuan-sdk";
function func() {
client
.pushErrMsg({
msg: "This is an error message",
timeout: 7_000,
})
.then((response) => {
console.log(response);
})
.catch((error) => {
if (error instanceof KernelError) {
console.error(error.body);
}
else if (error instanceof HTTPError) {
console.error(error.status);
}
else {
throw error;
}
})
.finally(() => {
});
}
Use type definitions
import { types } from "@siyuan-community/siyuan-sdk";
const payload: types.kernel.api.notification.pushMsg.IPayload = {
msg: "This is a notification message",
timeout: 7_000,
};
import pushMsg from "@siyuan-community/siyuan-sdk/dist/types/kernel/api/notification/pushMsg";
const payload: pushMsg.IPayload = {
msg: "This is a notification message",
timeout: 7_000,
};
References
API References
Changelog
CHANGELOG.md