Skip to content

Commit

Permalink
Moved Reponse outside frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Feb 2, 2024
1 parent 6b25b58 commit 1a9d134
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion functions/account/info.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StatusCodes } from 'http-status-codes';
import { Access } from '../../src/access';
import { AccountType, stripAccountInfo } from '../../src/accounts';
import { getAccount, getAccounts, getAllAccounts, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, checkParams, error, response } from '../../src/backend/utils';
import { Access } from '../../src/generic';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
Expand Down
5 changes: 0 additions & 5 deletions src/access.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Access } from './access.js';
import { Access } from './generic.js';
import type { KeyValue } from './utils.js';

export const uniqueAccountAttributes = ['id', 'username', 'email', 'token', 'session'];
Expand Down
5 changes: 3 additions & 2 deletions src/backend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request as CFRequest } from '@cloudflare/workers-types';
import { ReasonPhrases, StatusCodes } from 'http-status-codes';
import { Access } from '../access';
import { AccountType, type Account } from '../accounts';
import { Access, type Response as APIResponse } from '../generic';
import { getAccount } from './api';

export async function parseBody<V extends Record<string, FormDataEntryValue>>(request: Request): Promise<V> {
Expand All @@ -22,7 +22,8 @@ export function response<R>(status: StatusCodes = StatusCodes.OK, result?: R, er
const headers = {
'Access-Control-Allow-Origin': '*',
};
return new Response(JSON.stringify({ status, statusText, result, error }), { status, statusText, headers });
const body: APIResponse<R> = { status, statusText, result, error };
return new Response(JSON.stringify(body), { status, statusText, headers });
}

export function error(status: StatusCodes = StatusCodes.INTERNAL_SERVER_ERROR, message?: string): Response {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Access } from '../access.js';
import type { Account, AccountResult, FullAccount, UniqueAccountKey } from '../accounts.js';
import { accountAttributes, checkAccountAttribute } from '../accounts.js';
import { Access } from '../generic.js';
import { request } from './request.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from '../access.js';
export * from '../accounts.js';
export * from '../generic.js';
export * from './account.js';
export { auth } from './auth.js';
export * from './request.js';
29 changes: 1 addition & 28 deletions src/frontend/request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Response } from '../generic.js';
import { authToken } from './auth.js';

export const config = {
Expand All @@ -12,34 +13,6 @@ export const config = {
throw_errors: true,
};

/**
* A response to an API request
*/
export interface Response<Result> {
/**
* The HTTP status of the response
*/
status: number;

/**
* The HTTP status' text
*/
statusText: string;

/**
* Whether the request failed (true) or not (false)
*/
error: boolean;

/**
* The result of the request.
*
* @remarks
* If the request fails, result will contain the error message
*/
result: Result;
}

/**
* Makes a request to the API
* @param method Which HTTP method to use with the request
Expand Down
40 changes: 40 additions & 0 deletions src/generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Miscellaneous code
*/

/**
* Access level for information
*/
export enum Access {
PRIVATE = 0,
PROTECTED = 1,
PUBLIC = 2,
}

/**
* A response to an API request
*/
export interface Response<Result> {
/**
* The HTTP status of the response
*/
status: number;

/**
* The HTTP status' text
*/
statusText: string;

/**
* Whether the request failed (true) or not (false)
*/
error: boolean;

/**
* The result of the request.
*
* @remarks
* If the request fails, result will contain the error message
*/
result: Result;
}

0 comments on commit 1a9d134

Please sign in to comment.