fetch error consolidate
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
import { msgs, layout, allowed_chars } from './Store_chat';
|
import { msgs, layout, allowed_chars } from './Store_chat';
|
||||||
import { change_room, create_room } from './Request_rooms';
|
import { change_room, create_room } from './Request_rooms';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import type { FetchResponse } from './Types_chat';
|
import { FetchResponse } from './Types_chat';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Warning from './Element_warning.svelte';
|
import Warning from './Element_warning.svelte';
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { layout, current_room_name, current_room_type } from './Store_chat';
|
import { layout, current_room_name, current_room_type } from './Store_chat';
|
||||||
import { change_room, send_password } from './Request_rooms';
|
import { change_room, send_password } from './Request_rooms';
|
||||||
import type { FetchResponse } from './Types_chat';
|
import { FetchResponse } from './Types_chat';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Warning from './Element_warning.svelte';
|
import Warning from './Element_warning.svelte';
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat';
|
import { msgs, user, layout, socket, current_room_name, current_room_type } from './Store_chat';
|
||||||
import type { Room, FetchMethod, FetchResponse } from './Types_chat';
|
import { Room, FetchResponse, FetchMethod } from './Types_chat';
|
||||||
import { fetch_request, set_client_name_on_room, fill_fetch_response } from './Request_utils';
|
import { fetch_chat_request, set_client_name_on_room, fill_fetch_response } from './Request_utils';
|
||||||
|
|
||||||
export async function get_room_messages()
|
export async function get_room_messages()
|
||||||
{
|
{
|
||||||
console.log("in get_room_messages");
|
console.log("in get_room_messages");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('messages', FetchMethod.GET);
|
let response: FetchResponse = await fetch_chat_request('messages', FetchMethod.GET);
|
||||||
|
|
||||||
const messages = response.messages;
|
const messages = response.messages;
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ export async function create_room(room: Room)
|
|||||||
{
|
{
|
||||||
console.log("in create_room");
|
console.log("in create_room");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('create', FetchMethod.POST, room);
|
let response: FetchResponse = await fetch_chat_request('create', FetchMethod.POST, room);
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ export async function join_room(room: Room)
|
|||||||
{
|
{
|
||||||
console.log("in join_room");
|
console.log("in join_room");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('join', FetchMethod.POST, room);
|
let response: FetchResponse = await fetch_chat_request('join', FetchMethod.POST, room);
|
||||||
|
|
||||||
return response.room;
|
return response.room;
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ export async function change_room(room: Room)
|
|||||||
{
|
{
|
||||||
console.log("in change_room");
|
console.log("in change_room");
|
||||||
|
|
||||||
await fetch_request('change', FetchMethod.POST, room);
|
await fetch_chat_request('change', FetchMethod.POST, room);
|
||||||
|
|
||||||
await get_room_messages();
|
await get_room_messages();
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ export async function send_password(room: Room)
|
|||||||
{
|
{
|
||||||
console.log("in send_password");
|
console.log("in send_password");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('password', FetchMethod.POST, room);
|
let response: FetchResponse = await fetch_chat_request('password', FetchMethod.POST, room);
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ export async function invite_user(user_name: string)
|
|||||||
{
|
{
|
||||||
console.log("in invite_user");
|
console.log("in invite_user");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('invite', FetchMethod.POST, {username: user_name});
|
let response: FetchResponse = await fetch_chat_request('invite', FetchMethod.POST, {username: user_name});
|
||||||
|
|
||||||
await get_room_messages();
|
await get_room_messages();
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ export async function get_my_rooms()
|
|||||||
{
|
{
|
||||||
console.log("in get_my_rooms");
|
console.log("in get_my_rooms");
|
||||||
|
|
||||||
let response: FetchResponse = await fetch_request('myrooms', FetchMethod.GET);
|
let response: FetchResponse = await fetch_chat_request('myrooms', FetchMethod.GET);
|
||||||
|
|
||||||
let rooms = response.rooms.map(room => set_client_name_on_room(room));
|
let rooms = response.rooms.map(room => set_client_name_on_room(room));
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { user } from './Store_chat';
|
import { user } from './Store_chat';
|
||||||
import type { Room, FetchResponse, FetchInit, FetchMethod } from './Types_chat';
|
import { Room, FetchResponse, FetchInit, FetchMethod } from './Types_chat';
|
||||||
|
|
||||||
export async function fetch_request(route: string, fetchMethod: FetchMethod, param?: any)
|
export async function fetch_chat_request(route: string, fetchMethod: FetchMethod, param?: any)
|
||||||
{
|
{
|
||||||
let response: FetchResponse = { status: 0 };
|
let response: FetchResponse = { status: 0 };
|
||||||
|
|
||||||
@@ -26,6 +26,7 @@ export async function fetch_request(route: string, fetchMethod: FetchMethod, par
|
|||||||
console.error('Error', error);
|
console.error('Error', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("response:", response);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,12 +47,11 @@ export function set_client_name_on_room(room: Room)
|
|||||||
|
|
||||||
export function fill_fetch_response(response: FetchResponse, data: any)
|
export function fill_fetch_response(response: FetchResponse, data: any)
|
||||||
{
|
{
|
||||||
if (data.display)
|
console.log("data:", data);
|
||||||
response.display = data.display;
|
Object.keys(data).forEach(key =>
|
||||||
if (data.code)
|
{
|
||||||
response.code = data.code;
|
console.log(key)
|
||||||
if (data.message)
|
response[key] = data[key];
|
||||||
response.message = data.message;
|
});
|
||||||
if (data.room)
|
console.log(response);
|
||||||
response.room = data.room;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user