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;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,41 @@
|
|||
export const CANNED_RESPONSES: Record<string, () => Response> = {
|
||||
permission_denied: (): Response => {
|
||||
console.trace('denied');
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'Permission denied.',
|
||||
cause: 'permission_denied'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
export function not_found(): Response {
|
||||
if (Deno.env.get('TRACE_ERROR_RESPONSES')) {
|
||||
console.trace('not_found');
|
||||
}
|
||||
};
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'Not found.',
|
||||
cause: 'not_found'
|
||||
}
|
||||
}, {
|
||||
status: 404
|
||||
});
|
||||
}
|
||||
|
||||
export function permission_denied(): Response {
|
||||
if (Deno.env.get('TRACE_ERROR_RESPONSES')) {
|
||||
console.trace('permission_denied');
|
||||
}
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'Permission denied.',
|
||||
cause: 'permission_denied'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
export function append_only_events(): Response {
|
||||
if (Deno.env.get('TRACE_ERROR_RESPONSES')) {
|
||||
console.trace('append_only_events');
|
||||
}
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'This server does not allow modifying events.',
|
||||
cause: 'append_only_events'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,8 +2,7 @@ import { getCookies } from 'jsr:@std/http/cookie';
|
|||
import { SESSIONS } from '../models/session.ts';
|
||||
import { verifyTotp } from 'jsr:@stdext/crypto/totp';
|
||||
import { USERS } from '../models/user.ts';
|
||||
import { PERMISSIONS_STORE } from '../models/user_permissions.ts';
|
||||
import { CANNED_RESPONSES } from './canned_responses.ts';
|
||||
import * as CANNED_RESPONSES from './canned_responses.ts';
|
||||
|
||||
export type PRECHECK = (req: Request, meta: Record<string, any>) => Promise<Response | undefined> | Response | undefined;
|
||||
export type PRECHECK_TABLE = Record<string, PRECHECK[]>;
|
||||
|
@ -30,7 +29,6 @@ export async function get_user(request: Request, meta: Record<string, any>): Pro
|
|||
meta.cookies = meta.cookies ?? getCookies(request.headers);
|
||||
|
||||
meta.user = meta.valid_totp && meta.session ? await USERS.get(meta.session.user_id) : null;
|
||||
meta.user_permissions = meta.valid_totp && meta.session ? await PERMISSIONS_STORE.get(meta.session.user_id) : null;
|
||||
}
|
||||
|
||||
export function require_user(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue