diff options
Diffstat (limited to 'static/api')
| -rw-r--r-- | static/api/auth.js | 12 | ||||
| -rw-r--r-- | static/api/images.js | 20 | ||||
| -rw-r--r-- | static/api/index.js | 13 | ||||
| -rw-r--r-- | static/api/rest.js | 22 |
4 files changed, 56 insertions, 11 deletions
diff --git a/static/api/auth.js b/static/api/auth.js new file mode 100644 index 0000000..e663d88 --- /dev/null +++ b/static/api/auth.js @@ -0,0 +1,12 @@ +import * as rest from './rest.js'; + + +export async function login(user, password) { + const response = await rest.post('/api/auth/login', { user, password }); + return response?.success ?? false; +} + +export async function is_first_login(user) { + const response = await rest.post('/api/auth/first-login', { user }); + return response?.is_first ?? false; +} diff --git a/static/api/images.js b/static/api/images.js new file mode 100644 index 0000000..915aae6 --- /dev/null +++ b/static/api/images.js @@ -0,0 +1,20 @@ +import * as sfw from 'sfw'; +const { Input } = sfw.element.native; + +export async function upload_to_timeline() { + const input = Input.new({ + type: 'file', + multiple: true, + accept: 'image/jpeg', + }) + input.click(); +} + +export async function upload_to_profile() { + const input = Input.new({ + type: 'file', + multiple: false, + accept: 'image/jpeg', + }) + input.click(); +} diff --git a/static/api/index.js b/static/api/index.js index 50b9b26..07b484d 100644 --- a/static/api/index.js +++ b/static/api/index.js @@ -1,11 +1,2 @@ -import * as sfw from 'sfw'; -const { Input } = sfw.element.native; - -export async function upload_images() { - const input = Input.new({ - type: 'file', - multiple: true, - accept: 'image/jpeg', - }) - input.click(); -} +export * as images from './images.js'; +export * as auth from './auth.js'; diff --git a/static/api/rest.js b/static/api/rest.js new file mode 100644 index 0000000..b314615 --- /dev/null +++ b/static/api/rest.js @@ -0,0 +1,22 @@ + +async function handle_fetch(promise) { + const result = await promise; + const json = await result.json(); + + if (!result.ok) { + throw json; + } + + return json; +} + +export function get(url) { + return handle_fetch(fetch(url)); +} + +export async function post(url, body) { + return handle_fetch(fetch(url, { + method: 'POST', + body: JSON.stringify(body), + })); +} |