1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Switch to the new API

This commit is contained in:
Lukas Schulte Pelkum 2021-07-23 17:10:32 +02:00
parent b19c8a077e
commit b377acf6b0
No known key found for this signature in database
GPG Key ID: 408DA7CA81DB885C
2 changed files with 17 additions and 20 deletions

View File

@ -1,5 +1,5 @@
// apiBase defines the base URL of the API // apiBase defines the base URL of the API
const apiBase = location.protocol + "//" + location.host + "/api/v1"; const apiBase = location.protocol + "//" + location.host + "/api/v2";
// getAPIInformation returns the API information // getAPIInformation returns the API information
export async function getAPIInformation() { export async function getAPIInformation() {
@ -25,14 +25,12 @@ export async function createPaste(content) {
} }
// deletePaste deletes a paste // deletePaste deletes a paste
export async function deletePaste(id, deletionToken) { export async function deletePaste(id, modificationToken) {
return await fetch(apiBase + "/pastes/" + id, { return await fetch(apiBase + "/pastes/" + id, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}, 'Authorization': 'Bearer ' + modificationToken,
body: JSON.stringify({ }
deletionToken
})
}); });
} }

View File

@ -12,20 +12,20 @@ export function setupKeybinds() {
// Define the DOM element of the pressed button // Define the DOM element of the pressed button
let element = null; let element = null;
switch (event.keyCode) { switch (event.code) {
case 81: { case "KeyQ": {
element = document.getElementById("btn_new"); element = document.getElementById("btn_new");
break; break;
} }
case 83: { case "KeyS": {
element = document.getElementById("btn_save"); element = document.getElementById("btn_save");
break; break;
} }
case 88: { case "KeyX": {
element = document.getElementById("btn_delete"); element = document.getElementById("btn_delete");
break; break;
} }
case 66: { case "KeyB": {
element = document.getElementById("btn_copy"); element = document.getElementById("btn_copy");
break; break;
} }
@ -62,14 +62,13 @@ export function setupButtons() {
} }
const data = await response.json(); const data = await response.json();
// Give the user the chance to copy the deletion token // Give the user the chance to copy the modification token
if (data.deletionToken) { if (data.modificationToken) {
prompt("The deletion token for your paste is:", data.deletionToken); prompt("The modification token for your paste is:", data.modificationToken);
} }
// Redirect the user to the paste page // Redirect the user to the paste page
let address = location.protocol + "//" + location.host + "/" + data.id; let address = location.protocol + "//" + location.host + "/" + data.id;
if (data.suggestedSyntaxType) address += "." + data.suggestedSyntaxType;
location.replace(address); location.replace(address);
}); });
}); });
@ -77,12 +76,12 @@ export function setupButtons() {
// Define the behavior of the 'delete' button // Define the behavior of the 'delete' button
document.getElementById("btn_delete").addEventListener("click", function () { document.getElementById("btn_delete").addEventListener("click", function () {
spinner.surround(async function () { spinner.surround(async function () {
// Ask the user for the deletion token // Ask the user for the modification token
const deletionToken = prompt("Deletion Token:"); const modificationToken = prompt("Modification Token:");
if (!deletionToken) return; if (!modificationToken) return;
// Delete the paste // Delete the paste
const response = await api.deletePaste(autoload.PASTE_ID, deletionToken); const response = await api.deletePaste(autoload.PASTE_ID, modificationToken);
const data = await response.text(); const data = await response.text();
if (!response.ok) { if (!response.ok) {
notifications.error("Failed deleting the paste: <b>" + data + "</b>"); notifications.error("Failed deleting the paste: <b>" + data + "</b>");