exported function fetch with catch
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { msgs, layout, allowed_chars } from './Store_chat';
|
||||
import { change_room, create_room } from './Request_rooms';
|
||||
import { onMount } from 'svelte';
|
||||
import type { FetchResponse } from './Interface_chat';
|
||||
import type { FetchResponse } from './Types_chat';
|
||||
import Button from './Element_button.svelte';
|
||||
import Warning from './Element_warning.svelte';
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { layout, current_room_name, current_room_type } from './Store_chat';
|
||||
import { change_room, send_password } from './Request_rooms';
|
||||
import type { FetchResponse } from './Interface_chat';
|
||||
import type { FetchResponse } from './Types_chat';
|
||||
import Button from './Element_button.svelte';
|
||||
import Warning from './Element_warning.svelte';
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat';
|
||||
import type { Room, FetchResponse } from './Interface_chat';
|
||||
import { set_client_name_on_room, fill_fetch_response } from './Request_utils';
|
||||
import type { Room, FetchMethod, FetchResponse } from './Types_chat';
|
||||
import { fetch_request, set_client_name_on_room, fill_fetch_response } from './Request_utils';
|
||||
|
||||
export async function get_room_messages()
|
||||
{
|
||||
console.log("in get_room_messages");
|
||||
const response = await fetch('/api/v2/chat/messages');
|
||||
const data = await response.json();
|
||||
const messages = data.messages;
|
||||
|
||||
let response: FetchResponse = await fetch_request('messages', FetchMethod.GET);
|
||||
|
||||
const messages = response.messages;
|
||||
|
||||
if (messages === null)
|
||||
return;
|
||||
@@ -25,29 +26,7 @@ export async function create_room(room: Room)
|
||||
{
|
||||
console.log("in create_room");
|
||||
|
||||
let response: FetchResponse = { status: 0 };
|
||||
|
||||
// send the new room
|
||||
try {
|
||||
const resp = await fetch('/api/v2/chat/create', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(room),
|
||||
});
|
||||
console.log("resp.status:");
|
||||
console.log(resp.status);
|
||||
response.status = resp.status;
|
||||
if (!resp.ok)
|
||||
throw new Error(resp.statusText);
|
||||
|
||||
// get response message
|
||||
let data = await resp.json();
|
||||
fill_fetch_response(response, data);
|
||||
}
|
||||
catch (error)
|
||||
{
|
||||
console.error('Error', error);
|
||||
}
|
||||
let response: FetchResponse = await fetch_request('create', FetchMethod.POST, room);
|
||||
|
||||
return response;
|
||||
}
|
||||
@@ -56,26 +35,16 @@ export async function join_room(room: Room)
|
||||
{
|
||||
console.log("in join_room");
|
||||
|
||||
const response = await fetch('/api/v2/chat/join', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(room),
|
||||
});
|
||||
let data = await response.json();
|
||||
let response: FetchResponse = await fetch_request('join', FetchMethod.POST, room);
|
||||
|
||||
return data.room;
|
||||
return response.room;
|
||||
}
|
||||
|
||||
export async function change_room(room: Room)
|
||||
{
|
||||
console.log("in change_room");
|
||||
|
||||
const response = await fetch('/api/v2/chat/change', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(room),
|
||||
});
|
||||
let data = await response.json();
|
||||
await fetch_request('change', FetchMethod.POST, room);
|
||||
|
||||
await get_room_messages();
|
||||
|
||||
@@ -86,33 +55,12 @@ export async function change_room(room: Room)
|
||||
layout.set("room");
|
||||
}
|
||||
|
||||
|
||||
export async function send_password(room: Room)
|
||||
{
|
||||
console.log("in create_room");
|
||||
console.log("in send_password");
|
||||
|
||||
let response: FetchResponse = { status: 0 };
|
||||
|
||||
// send the new room
|
||||
try {
|
||||
const resp = await fetch('/api/v2/chat/create', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(room),
|
||||
});
|
||||
console.log("resp.status:");
|
||||
console.log(resp.status);
|
||||
response.status = resp.status;
|
||||
if (!resp.ok)
|
||||
throw new Error(resp.statusText);
|
||||
|
||||
// get response message
|
||||
let data = await resp.json();
|
||||
fill_fetch_response(response, data);
|
||||
}
|
||||
catch (error)
|
||||
{
|
||||
console.error('Error', error);
|
||||
}
|
||||
let response: FetchResponse = await fetch_request('password', FetchMethod.POST, room);
|
||||
|
||||
return response;
|
||||
}
|
||||
@@ -121,12 +69,7 @@ export async function invite_user(user_name: string)
|
||||
{
|
||||
console.log("in invite_user");
|
||||
|
||||
const response = await fetch('/api/v2/chat/invite', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({username: user_name}),
|
||||
});
|
||||
let data = await response.json();
|
||||
let response: FetchResponse = await fetch_request('invite', FetchMethod.POST, {username: user_name});
|
||||
|
||||
await get_room_messages();
|
||||
}
|
||||
@@ -135,13 +78,9 @@ export async function get_my_rooms()
|
||||
{
|
||||
console.log("in get_my_rooms");
|
||||
|
||||
const response = await fetch('/api/v2/chat/myrooms');
|
||||
console.log("response.status", response.status);
|
||||
const data = await response.json();
|
||||
console.log("data:", data);
|
||||
let response: FetchResponse = await fetch_request('myrooms', FetchMethod.GET);
|
||||
|
||||
let rooms = data.rooms.map(room => set_client_name_on_room(room));
|
||||
console.log("rooms:", rooms);
|
||||
let rooms = response.rooms.map(room => set_client_name_on_room(room));
|
||||
|
||||
return rooms;
|
||||
}
|
||||
@@ -150,41 +89,33 @@ export async function get_all_rooms()
|
||||
{
|
||||
console.log("in get_all_rooms");
|
||||
|
||||
const response = await fetch('/api/v2/chat/allrooms');
|
||||
const data = await response.json();
|
||||
let response: FetchResponse = await fetch_chat_request('allrooms', FetchMethod.GET);
|
||||
|
||||
return data.rooms;
|
||||
return response.rooms;
|
||||
}
|
||||
|
||||
export async function get_room_users()
|
||||
{
|
||||
console.log("in get_room_users");
|
||||
|
||||
const response = await fetch('/api/v2/chat/roomusers');
|
||||
const data = await response.json();
|
||||
console.log("users:", data.users);
|
||||
let response: FetchResponse = await fetch_chat_request('roomusers', FetchMethod.GET);
|
||||
|
||||
return data.users;
|
||||
return response.users;
|
||||
}
|
||||
|
||||
export async function get_all_users()
|
||||
{
|
||||
console.log("in get_all_users");
|
||||
|
||||
const response = await fetch('/api/v2/chat/users');
|
||||
const data = await response.json();
|
||||
console.log("users:", data.users);
|
||||
let response: FetchResponse = await fetch_chat_request('users', FetchMethod.GET);
|
||||
|
||||
return data.users;
|
||||
return response.users;
|
||||
}
|
||||
|
||||
export async function leave_room()
|
||||
{
|
||||
console.log("in leave_room");
|
||||
|
||||
const response = await fetch('/api/v2/chat/leave', {
|
||||
method: 'DELETE',
|
||||
});
|
||||
let response: FetchResponse = await fetch_chat_request('leave', FetchMethod.DELETE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,33 @@
|
||||
import { user } from './Store_chat';
|
||||
import type { Room, FetchResponse } from './Interface_chat';
|
||||
import type { Room, FetchResponse, FetchInit, FetchMethod } from './Types_chat';
|
||||
|
||||
export async function fetch_request(route: string, fetchMethod: FetchMethod, param?: any)
|
||||
{
|
||||
let response: FetchResponse = { status: 0 };
|
||||
|
||||
let fetch_params: FetchInit = {
|
||||
method: fetchMethod,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
if (param)
|
||||
fetch_params.body = JSON.stringify(param);
|
||||
|
||||
try {
|
||||
const resp = await fetch(`/api/v2/chat/${route}`, fetch_params);
|
||||
response.status = resp.status;
|
||||
if (!resp.ok)
|
||||
throw new Error(resp.statusText);
|
||||
|
||||
let data = await resp.json();
|
||||
fill_fetch_response(response, data);
|
||||
}
|
||||
catch (error)
|
||||
{
|
||||
console.error('Error', error);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function set_client_name_on_room(room: Room)
|
||||
{
|
||||
|
||||
@@ -13,5 +13,20 @@ export interface FetchResponse
|
||||
code?: string;
|
||||
display?: boolean;
|
||||
message?: string;
|
||||
room?: any;
|
||||
room?: Room;
|
||||
}
|
||||
|
||||
export interface FetchInit
|
||||
{
|
||||
method: string;
|
||||
headers: any;
|
||||
body?: string;
|
||||
}
|
||||
|
||||
export enum FetchMethod
|
||||
{
|
||||
POST = 'POST',
|
||||
GET = 'GET',
|
||||
LEAVE = 'LEAVE',
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user