import { customAlphabet } from '@nanoid'; import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http'; import config from '~config'; import dbClient from 'db/client.ts'; // Using custom alphabet to exclude - and _ const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const nanoid = customAlphabet(alphabet, 20); const genericResponse = (status: StatusCode, customText = '') => new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status] }); Deno.serve({ port: config.api.port }, async (req) => { const urlPath = req.url.split('?')[0] ?? ''; const path = (urlPath.split('api')[1] ?? '').toLowerCase().trim(); console.log(urlPath, path); if (req.method === 'GET') { // handle all gets } else if (req.method === 'POST' && path === '/enroll') { const body = await req.json(); let readFailure = false; const userNameMatches = await dbClient.query('SELECT name FROM users WHERE name = ?', [body.name]).catch(() => { readFailure = true; }); if (readFailure) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB."); if (userNameMatches.length === 0) { if (body.name.length < 4 || body.name.length > 20) 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.email.length > 20) return genericResponse(STATUS_CODE.BadRequest, `Email too long.`); const userId = nanoid(); let writeFailure = false; await dbClient.execute('INSERT INTO users(id,name,pin,email) values(?,?,?,?)', [userId, body.name, body.pin, body.email]).catch(() => { writeFailure = true; }); if (writeFailure) { return genericResponse(STATUS_CODE.InternalServerError, "Couldn't write DB."); } else { return genericResponse(STATUS_CODE.OK, JSON.stringify({ userId })); } } else { return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.'); } } else { // handle auth then all other shiz } return genericResponse(STATUS_CODE.NotImplemented); });