Skip to content

Commit

Permalink
Added metadata frontend function
Browse files Browse the repository at this point in the history
Added Metadata interface
  • Loading branch information
james-pre committed Feb 2, 2024
1 parent 1a9d134 commit c73fb25
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
16 changes: 7 additions & 9 deletions functions/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { StatusCodes } from 'http-status-codes';
import { version } from '../package.json';
import { setDB } from '../src/backend/api';
import type { RequestContext } from '../src/backend/context';
import { error, response } from '../src/backend/utils';
import { version } from '../package.json';
import type { Metadata } from '../src/generic';

export async function onRequest({ env }: RequestContext) {
try {
setDB(env.DB);
return response(
StatusCodes.OK,
{
version,
debug: !!env.DEBUG,
},
false
);
const metadata: Metadata = {
version,
debug: !!env.DEBUG,
};
return response(StatusCodes.OK, metadata, false);
} catch (e) {
console.error(e);
return error(StatusCodes.INTERNAL_SERVER_ERROR, env.DEBUG && e?.message);
Expand Down
11 changes: 7 additions & 4 deletions src/frontend/account.ts → src/frontend/endpoints/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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';
/**
* Account-related endpoints
*/
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';

/**
* Parses the account result of a response
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { request, type Metadata } from '../index.js';

export * from './account.js';

/**
* Get metadata about the API
*/
export function metadata(): Promise<Metadata> {
return request('GET', '/metadata');
}
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 '../accounts.js';
export * from '../generic.js';
export * from './account.js';
export * from './endpoints';
export { auth } from './auth.js';
export * from './request.js';
12 changes: 12 additions & 0 deletions src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ export interface Response<Result> {
*/
result: Result;
}

export interface Metadata {
/**
* Current API version
*/
version: string;

/**
* Whether the API has debug features enabled
*/
debug: boolean;
}

0 comments on commit c73fb25

Please sign in to comment.