feature: rooms and events implemented on the backend
This commit is contained in:
parent
df00324e24
commit
85024c6e62
29 changed files with 1659 additions and 115 deletions
19
utils/api.ts
19
utils/api.ts
|
@ -2,7 +2,7 @@ import { getSetCookies } from '@std/http/cookie';
|
|||
import { generateTotp } from '@stdext/crypto/totp';
|
||||
|
||||
export interface API_CLIENT {
|
||||
fetch: (url: string, options?: FETCH_OPTIONS, transform?: (obj: any) => any) => Promise<object>;
|
||||
fetch: (url: string, options?: FETCH_OPTIONS, transform?: (obj: any) => any) => Promise<any>;
|
||||
}
|
||||
|
||||
export type API_CONFIG = {
|
||||
|
@ -43,8 +43,8 @@ export function api(api_config?: Record<string, any>): API_CLIENT {
|
|||
...(api_config ?? {})
|
||||
};
|
||||
|
||||
return {
|
||||
fetch: async (url: string, options?: FETCH_OPTIONS, transform?: (obj: any) => any) => {
|
||||
const client: API_CLIENT = {
|
||||
fetch: async (url: string, options?: FETCH_OPTIONS, transform?: (obj: any) => any): Promise<any> => {
|
||||
const prefix: string = `${config.protocol}//${config.hostname}:${config.port}${config.prefix}`;
|
||||
const retry: RETRY_OPTIONS = options?.retry ?? {
|
||||
limit: 0,
|
||||
|
@ -100,11 +100,14 @@ export function api(api_config?: Record<string, any>): API_CLIENT {
|
|||
continue;
|
||||
}
|
||||
|
||||
if (response.status > 400) {
|
||||
if (response.status >= 400) {
|
||||
const error_response = await response.json();
|
||||
throw new Error('Bad Request', {
|
||||
cause: error_response?.cause ?? JSON.stringify(error_response)
|
||||
});
|
||||
throw new Error(
|
||||
error_response.error?.message ?? 'Bad Reqeest',
|
||||
error_response.error ?? {
|
||||
cause: error_response?.cause ?? JSON.stringify(error_response)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
@ -118,4 +121,6 @@ export function api(api_config?: Record<string, any>): API_CLIENT {
|
|||
} while (retries < retry.limit);
|
||||
}
|
||||
};
|
||||
|
||||
return client;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue