stub out get endpoints, built list endpoint

This commit is contained in:
Ean Milligan
2026-04-10 01:20:53 -04:00
parent b8bd784807
commit 6444b84af4
7 changed files with 49 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
meta { meta {
name: create plan name: create plan
type: http type: http
seq: 1 seq: 2
} }
post { post {

View File

@@ -1,7 +1,7 @@
meta { meta {
name: delete plan name: delete plan
type: http type: http
seq: 5 seq: 7
} }
delete { delete {

View File

@@ -0,0 +1,16 @@
meta {
name: list plans
type: http
seq: 1
}
get {
url: http://localhost:14014/api/list/[userId]
body: none
auth: inherit
}
settings {
encodeUrl: true
timeout: 0
}

View File

@@ -1,7 +1,7 @@
meta { meta {
name: move plan name: move plan
type: http type: http
seq: 4 seq: 5
} }
put { put {

View File

@@ -1,7 +1,7 @@
meta { meta {
name: rename plan name: rename plan
type: http type: http
seq: 3 seq: 4
} }
put { put {

View File

@@ -1,7 +1,7 @@
meta { meta {
name: update plan name: update plan
type: http type: http
seq: 2 seq: 3
} }
put { put {

32
mod.ts
View File

@@ -16,8 +16,8 @@ discordHeaders.append('Accept', 'application/json');
const zohoHeaders = new Headers(discordHeaders); const zohoHeaders = new Headers(discordHeaders);
let zohoAuthExpireDT = new Date().getTime(); let zohoAuthExpireDT = new Date().getTime();
const genericResponse = (status: StatusCode, customText = '') => const genericResponse = (status: StatusCode, customText = '', customHeaders = new Headers()) =>
new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status] }); new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status], headers: customHeaders });
Deno.serve({ port: config.api.port }, async (req) => { Deno.serve({ port: config.api.port }, async (req) => {
try { try {
@@ -29,7 +29,31 @@ Deno.serve({ port: config.api.port }, async (req) => {
let failed = false; let failed = false;
if (req.method === 'GET') { if (req.method === 'GET') {
// handle all gets if (path === '/home') {
// SSR "login page"
return genericResponse(STATUS_CODE.Unauthorized, 'Please sign in.');
} else if (path.startsWith('/home/')) {
// SSR "home page"
return genericResponse(STATUS_CODE.NotImplemented, 'WIP');
} else if (path.startsWith('/read/')) {
return genericResponse(STATUS_CODE.NotImplemented, 'WIP');
} else if (path.startsWith('/list/')) {
const userId = path.replace('/list/', '');
const userMatch = await dbClient.query('SELECT id FROM users WHERE id = ?', [userId]).catch(() => {
failed = true;
});
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
if (!userMatch.length) return genericResponse(STATUS_CODE.NotFound, 'User ID does not exist.');
const plans = await dbClient.query('SELECT id, name, folder FROM plans WHERE ownerId = ?', [userId]).catch(() => {
failed = true;
});
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
return genericResponse(STATUS_CODE.OK, JSON.stringify(plans));
} else if (path.startsWith('/export/')) {
return genericResponse(STATUS_CODE.NotImplemented, 'WIP');
}
} else if (req.method === 'POST' && path === '/enroll') { } else if (req.method === 'POST' && path === '/enroll') {
const body = await req.json(); const body = await req.json();
@@ -42,7 +66,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
if (body.name.length < 4 || body.name.length > 20) if (body.name.length < 4 || body.name.length > 20)
return genericResponse(STATUS_CODE.BadRequest, `Name too ${body.name.length < 4 ? 'short' : 'long'}.`); return genericResponse(STATUS_CODE.BadRequest, `Name too ${body.name.length < 4 ? 'short' : 'long'}.`);
if (body.pin.length < 4 || body.pin.length > 20) return genericResponse(STATUS_CODE.BadRequest, `PIN too ${body.pin.length < 4 ? 'short' : 'long'}.`); if (body.pin.length < 4 || body.pin.length > 20) return genericResponse(STATUS_CODE.BadRequest, `PIN too ${body.pin.length < 4 ? 'short' : 'long'}.`);
if (body.email.length > 255) return genericResponse(STATUS_CODE.BadRequest, `Email too long.`); if (body.email.length > 255) return genericResponse(STATUS_CODE.BadRequest, 'Email too long.');
const id = nanoid(); const id = nanoid();