generated from cloud-cli/app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cb679a
commit d2d7b5d
Showing
3 changed files
with
172 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const authDomain = "https://__API_URL__"; | ||
const fetchOptions = { credentials: "include", mode: "cors" }; | ||
|
||
async function toJson(r) { | ||
if (r.ok) { | ||
return await r.json(); | ||
} | ||
|
||
throw new Error(r.status + ": " + r.statusText); | ||
} | ||
|
||
function toBoolean(r) { | ||
if (r.ok) { | ||
return true; | ||
} | ||
|
||
throw new Error(r.status + ": " + r.statusText); | ||
} | ||
|
||
async function getProfile() { | ||
const r = await fetch(authDomain, fetchOptions); | ||
return toJson(r); | ||
} | ||
|
||
async function isAuthenticated() { | ||
const r = await fetch(authDomain, { ...fetchOptions, method: "HEAD" }); | ||
return Boolean(r.ok && r.status < 300); | ||
} | ||
|
||
async function signOut() { | ||
const r = await fetch(authDomain, { | ||
...fetchOptions, | ||
method: "DELETE", | ||
}); | ||
|
||
const ok = await toBoolean(r); | ||
events.dispatchEvent(new CustomEvent("signout", { detail: ok })); | ||
return ok; | ||
} | ||
|
||
async function getProperties() { | ||
const r = await fetch(new URL("/properties", authDomain), fetchOptions); | ||
return await toJson(r); | ||
} | ||
|
||
async function getProperty(property) { | ||
const r = await fetch( | ||
new URL("/properties/" + property, authDomain), | ||
fetchOptions | ||
); | ||
return r.ok ? (await toJson(r)).value : ""; | ||
} | ||
|
||
async function setProperty(property, value) { | ||
const r = await fetch(new URL("/properties", authDomain), { | ||
...fetchOptions, | ||
method: "PUT", | ||
headers: { "content-type": "application/json" }, | ||
body: JSON.stringify({ key: property, value }), | ||
}); | ||
|
||
return toBoolean(r); | ||
} | ||
|
||
async function deleteProperty(key) { | ||
const r = await fetch(new URL("/properties/" + key, authDomain), { | ||
...fetchOptions, | ||
method: "DELETE", | ||
}); | ||
|
||
return toBoolean(r); | ||
} | ||
|
||
export const commands = { | ||
getProfile, | ||
isAuthenticated, | ||
signOut, | ||
getProperties, | ||
getProperty, | ||
setProperty, | ||
deleteProperty, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters