Skip to content

Commit

Permalink
Added CORS headers
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Feb 10, 2024
1 parent 87ce995 commit 11fa273
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions functions/account/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { createAccount, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkBody, error, parseError, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { deleteAccount, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, error, getAccountFromTokenOrID, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StatusCodes } from 'http-status-codes';
import { response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export function onRequest(): Response {
return response(StatusCodes.OK, {});
}
2 changes: 2 additions & 0 deletions functions/account/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, checkParams, error, response } from '../../src/backend/utils';
import { Access } from '../../src/generic';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getAccount, hash, login, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkBody, error, parseError, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { logout, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, error, getAccountFromTokenOrID, parseError, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env, request }: RequestContext): Promise<Response> {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { getAccountNum, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { error, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

export async function onRequest({ env }: RequestContext) {
try {
setDB(env.DB);
Expand Down
2 changes: 2 additions & 0 deletions functions/account/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { setAccountAttribute, setDB } from '../../src/backend/api';
import type { RequestContext } from '../../src/backend/context';
import { checkAuth, checkBody, error, getAccountFromTokenOrID, parseError, response } from '../../src/backend/utils';

export { onRequestOptions } from '../../src/backend/utils';

const requiredTypeForChange: { [K in keyof FullAccount]: AccountType } = {
...(Object.fromEntries(accountAttributes.map(k => [k, AccountType.MOD])) as { [K in keyof FullAccount]: AccountType.MOD }),
username: AccountType.DEV,
Expand Down
14 changes: 13 additions & 1 deletion src/backend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Request as CFRequest } from '@cloudflare/workers-types';
import type { Request as CFRequest, PagesFunction } from '@cloudflare/workers-types';

Check warning on line 1 in src/backend/utils.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'PagesFunction' is defined but never used

Check warning on line 1 in src/backend/utils.ts

View workflow job for this annotation

GitHub Actions / macos-latest

'PagesFunction' is defined but never used

Check warning on line 1 in src/backend/utils.ts

View workflow job for this annotation

GitHub Actions / windows-latest

'PagesFunction' is defined but never used
import { ReasonPhrases, StatusCodes } from 'http-status-codes';
import { AccountType, type Account } from '../accounts';
import { Access, type Response as APIResponse } from '../generic';
Expand Down Expand Up @@ -138,3 +138,15 @@ export async function getAccountFromTokenOrID<const B extends { id?: string; tok

return targetUser;
}

export async function onRequestOptions() {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Max-Age': '86400',
},
});
}

0 comments on commit 11fa273

Please sign in to comment.