wip
This commit is contained in:
@@ -1,22 +1,57 @@
|
||||
import { defineAsyncComponent } from 'vue';
|
||||
import Stream from '@/scripts/stream';
|
||||
import { store } from './store';
|
||||
import { store } from '@/store';
|
||||
import { apiUrl } from '@/config';
|
||||
|
||||
export const stream = new Stream();
|
||||
|
||||
export const dialogCallbacks = {};
|
||||
|
||||
export function api(endpoint: string, data: Record<string, any> = {}, token?: string | null | undefined) {
|
||||
return store.dispatch('api', { endpoint, data, token });
|
||||
store.commit('beginApiRequest');
|
||||
|
||||
const onFinally = () => {
|
||||
store.commit('endApiRequest');
|
||||
};
|
||||
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
// Append a credential
|
||||
if (store.getters.isSignedIn) (data as any).i = store.state.i.token;
|
||||
if (token !== undefined) (data as any).i = token;
|
||||
|
||||
// Send request
|
||||
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
credentials: 'omit',
|
||||
cache: 'no-cache'
|
||||
}).then(async (res) => {
|
||||
const body = res.status === 204 ? null : await res.json();
|
||||
|
||||
if (res.status === 200) {
|
||||
resolve(body);
|
||||
} else if (res.status === 204) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(body.error);
|
||||
}
|
||||
}).catch(reject);
|
||||
});
|
||||
|
||||
promise.then(onFinally, onFinally);
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
export function dialog(props: Record<string, any>) {
|
||||
return store.dispatch('showDialog', {
|
||||
return store.dispatch('popup', {
|
||||
component: defineAsyncComponent(() => import('@/components/dialog.vue')),
|
||||
props
|
||||
});
|
||||
}
|
||||
|
||||
export function menu(props: Record<string, any>) {
|
||||
return store.dispatch('showDialog', {
|
||||
return store.dispatch('popup', {
|
||||
component: defineAsyncComponent(() => import('@/components/menu.vue')),
|
||||
props
|
||||
});
|
||||
|
Reference in New Issue
Block a user