import server from "../../../services/server.js"; import dialogService from "../../../services/dialog.js"; import toastService from "../../../services/toast.js"; import OptionsTab from "./options_tab.js"; const TPL = `

ETAPI

ETAPI is a REST API used to access Trilium instance programmatically, without UI.
See more details on wiki and ETAPI OpenAPI spec.

Existing tokens
There are no tokens yet. Click on the button above to create one.
Token name Created Actions
`; export default class EtapiOptions extends OptionsTab { get tabTitle() { return "ETAPI" } lazyRender() { this.$widget = $(TPL); this.$widget.find("#create-etapi-token").on("click", async () => { const tokenName = await dialogService.prompt({ title: "New ETAPI token", message: "Please enter new token's name", defaultValue: "new token" }); if (!tokenName.trim()) { toastService.showError("Token name can't be empty"); return; } const {authToken} = await server.post('etapi-tokens', {tokenName}); await dialogService.prompt({ title: "ETAPI token created", message: 'Copy the created token into clipboard. Trilium stores the token hashed and this is the last time you see it.', defaultValue: authToken }); this.refreshTokens(); }); this.refreshTokens(); } async refreshTokens() { const $noTokensYet = this.$widget.find("#no-tokens-yet"); const $tokensTable = this.$widget.find("#tokens-table"); const tokens = await server.get('etapi-tokens'); $noTokensYet.toggle(tokens.length === 0); $tokensTable.toggle(tokens.length > 0); const $tokensTableBody = $tokensTable.find("tbody"); $tokensTableBody.empty(); for (const token of tokens) { $tokensTableBody.append( $("") .append($("").text(token.name)) .append($("").text(token.utcDateCreated)) .append($("").append( $('') .on("click", () => this.renameToken(token.etapiTokenId, token.name)), $('') .on("click", () => this.deleteToken(token.etapiTokenId, token.name)) )) ); } } async renameToken(etapiTokenId, oldName) { const tokenName = await dialogService.prompt({ title: "Rename token", message: "Please enter new token's name", defaultValue: oldName }); await server.patch(`etapi-tokens/${etapiTokenId}`, {name: tokenName}); this.refreshTokens(); } async deleteToken(etapiTokenId, name) { if (!confirm(`Are you sure you want to delete ETAPI token "${name}"?`)) { return; } await server.remove(`etapi-tokens/${etapiTokenId}`); this.refreshTokens(); } }