From f5a2b5f27f8fb7de702d5240c0c3502b48e0db58 Mon Sep 17 00:00:00 2001 From: dev <> Date: Wed, 6 Dec 2023 07:24:45 +0000 Subject: [PATCH] phoenix-api: move to D1 --- phoenix-api/src/blogs/create_blog.ts | 6 +++--- phoenix-api/src/blogs/create_page.ts | 6 +++--- phoenix-api/src/blogs/delete_blog.ts | 6 +++--- phoenix-api/src/blogs/delete_page.ts | 6 +++--- phoenix-api/src/blogs/get_blog.ts | 6 +++--- phoenix-api/src/blogs/get_blogs.ts | 5 ++--- phoenix-api/src/blogs/get_page.ts | 6 +++--- phoenix-api/src/blogs/get_pages.ts | 8 ++++---- phoenix-api/src/blogs/headless_get_blog.ts | 4 ++-- phoenix-api/src/blogs/headless_get_page.ts | 6 +++--- phoenix-api/src/blogs/headless_get_posts.ts | 8 ++++---- phoenix-api/src/blogs/update_blog.ts | 6 +++--- phoenix-api/src/blogs/update_page.ts | 8 ++++---- phoenix-api/src/users/login.ts | 6 +++--- phoenix-api/src/users/signup.ts | 8 ++++---- phoenix-api/src/utils.ts | 10 ++++++++++ 16 files changed, 57 insertions(+), 48 deletions(-) diff --git a/phoenix-api/src/blogs/create_blog.ts b/phoenix-api/src/blogs/create_blog.ts index 64bd5ae..20f7d59 100644 --- a/phoenix-api/src/blogs/create_blog.ts +++ b/phoenix-api/src/blogs/create_blog.ts @@ -1,6 +1,6 @@ import { uuidv7 } from "@phoenix/uuidv7"; -import { checkAuth, checkIsAdmin, parseAndValidateApiInput } from "../utils"; -import { CreateBlogInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils"; +import { CreateBlogInputValidator } from "@phoenix/core/api"; import { Blog } from "@phoenix/core/entities"; import { Context } from "../hono_bindings"; @@ -28,5 +28,5 @@ export async function createBlog(ctx: Context): Promise { JSON.stringify(blog.navigation), blog.description_html) .run(); - return ctx.json(convertToApiResponse(blog)); + return newApiResponse(blog); } diff --git a/phoenix-api/src/blogs/create_page.ts b/phoenix-api/src/blogs/create_page.ts index 62dcca6..dbb18e5 100644 --- a/phoenix-api/src/blogs/create_page.ts +++ b/phoenix-api/src/blogs/create_page.ts @@ -1,8 +1,8 @@ -import { checkAuth, parseAndValidateApiInput } from "../utils"; +import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils"; import { NotFoundError } from "@phoenix/core/errors"; import { uuidv7 } from "@phoenix/uuidv7"; import { checkIsAdmin } from "../utils"; -import { CreatePageInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { CreatePageInputValidator } from "@phoenix/core/api"; import { Blog, Page } from "@phoenix/core/entities"; import { Context } from "../hono_bindings"; import { parseBlogFromDB } from "./utils"; @@ -44,5 +44,5 @@ export async function createPage(ctx: Context): Promise { .bind(now.toISOString(), blog.id) .run(); - return ctx.json(convertToApiResponse(page)); + return newApiResponse(page); } diff --git a/phoenix-api/src/blogs/delete_blog.ts b/phoenix-api/src/blogs/delete_blog.ts index 73ff361..5b408e1 100644 --- a/phoenix-api/src/blogs/delete_blog.ts +++ b/phoenix-api/src/blogs/delete_blog.ts @@ -1,6 +1,6 @@ import { Context } from "../hono_bindings"; -import { checkAuth, checkIsAdmin, parseAndValidateApiInput } from "../utils"; -import { DeleteBlogInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils"; +import { DeleteBlogInputValidator } from "@phoenix/core/api"; export async function deleteBlog(ctx: Context): Promise { const userId = await checkAuth(ctx); @@ -12,5 +12,5 @@ export async function deleteBlog(ctx: Context): Promise { .bind(apiInput.blog_id) .run(); - return ctx.json(convertToApiResponse({ ok: true })); + return newApiResponse({ ok: true }); } diff --git a/phoenix-api/src/blogs/delete_page.ts b/phoenix-api/src/blogs/delete_page.ts index b8ca7a2..bd90689 100644 --- a/phoenix-api/src/blogs/delete_page.ts +++ b/phoenix-api/src/blogs/delete_page.ts @@ -1,7 +1,7 @@ import { NotFoundError } from "@phoenix/core/errors"; import { Context } from "../hono_bindings"; -import { checkAuth, checkIsAdmin, parseAndValidateApiInput } from "../utils"; -import { DeletePageInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils"; +import { DeletePageInputValidator } from "@phoenix/core/api"; import { Page, PageValidator } from "@phoenix/core/entities"; export async function deletePage(ctx: Context): Promise { @@ -23,5 +23,5 @@ export async function deletePage(ctx: Context): Promise { .bind(apiInput.page_id) .run(); - return ctx.json(convertToApiResponse({ ok: true })); + return newApiResponse({ ok: true }); } diff --git a/phoenix-api/src/blogs/get_blog.ts b/phoenix-api/src/blogs/get_blog.ts index a07c10e..2a954ad 100644 --- a/phoenix-api/src/blogs/get_blog.ts +++ b/phoenix-api/src/blogs/get_blog.ts @@ -1,7 +1,7 @@ import { Context } from "../hono_bindings"; -import { checkAuth, parseAndValidateApiInput } from "../utils"; +import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils"; import { NotFoundError } from "@phoenix/core/errors"; -import { GetBlogInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { GetBlogInputValidator } from "@phoenix/core/api"; import { parseBlogFromDB } from "./utils"; export async function getBlog(ctx: Context): Promise { @@ -17,5 +17,5 @@ export async function getBlog(ctx: Context): Promise { } const blog = parseBlogFromDB(blogRes); - return ctx.json(convertToApiResponse(blog)); + return newApiResponse(blog); } diff --git a/phoenix-api/src/blogs/get_blogs.ts b/phoenix-api/src/blogs/get_blogs.ts index edba253..791c785 100644 --- a/phoenix-api/src/blogs/get_blogs.ts +++ b/phoenix-api/src/blogs/get_blogs.ts @@ -1,6 +1,5 @@ -import { convertToApiResponse } from "@phoenix/core/api"; import { Context } from "../hono_bindings"; -import { checkAuth } from "../utils"; +import { checkAuth, newApiResponse } from "../utils"; import { parseBlogFromDB } from "./utils"; export async function getBlogs(ctx: Context): Promise { @@ -9,5 +8,5 @@ export async function getBlogs(ctx: Context): Promise { const blogsRes = (await ctx.env.DB.prepare('SELECT * FROM blogs ORDER BY id DESC').all()).results; const blogs = blogsRes.map(parseBlogFromDB); - return ctx.json(convertToApiResponse(blogs)); + return newApiResponse(blogs); } diff --git a/phoenix-api/src/blogs/get_page.ts b/phoenix-api/src/blogs/get_page.ts index 62fea21..65b3538 100644 --- a/phoenix-api/src/blogs/get_page.ts +++ b/phoenix-api/src/blogs/get_page.ts @@ -1,7 +1,7 @@ import { Context } from "../hono_bindings"; -import { checkAuth, parseAndValidateApiInput } from "../utils"; +import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils"; import { NotFoundError } from "@phoenix/core/errors"; -import { GetPageInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { GetPageInputValidator } from "@phoenix/core/api"; import { Page, PageValidator } from "@phoenix/core/entities"; export async function getPage(ctx: Context): Promise { @@ -17,5 +17,5 @@ export async function getPage(ctx: Context): Promise { } const page = PageValidator.parse(pageRes); - return ctx.json(convertToApiResponse(page)); + return newApiResponse(page); } diff --git a/phoenix-api/src/blogs/get_pages.ts b/phoenix-api/src/blogs/get_pages.ts index 1963f21..6a98561 100644 --- a/phoenix-api/src/blogs/get_pages.ts +++ b/phoenix-api/src/blogs/get_pages.ts @@ -1,7 +1,7 @@ import { Context } from "../hono_bindings"; -import { checkAuth, parseAndValidateApiInput } from "../utils"; -import { GetPagesInputValidator, convertToApiResponse } from "@phoenix/core/api"; -import { Page, PageValidator } from "@phoenix/core/entities"; +import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils"; +import { GetPagesInputValidator } from "@phoenix/core/api"; +import { PageValidator } from "@phoenix/core/entities"; export async function getPages(ctx: Context): Promise { await checkAuth(ctx); @@ -13,5 +13,5 @@ export async function getPages(ctx: Context): Promise { .all(); const pages = pagesRes.results.map((p) => PageValidator.parse(p)); - return ctx.json(convertToApiResponse(pages)); + return newApiResponse(pages); } diff --git a/phoenix-api/src/blogs/headless_get_blog.ts b/phoenix-api/src/blogs/headless_get_blog.ts index dd779d3..d72d396 100644 --- a/phoenix-api/src/blogs/headless_get_blog.ts +++ b/phoenix-api/src/blogs/headless_get_blog.ts @@ -1,7 +1,7 @@ -import { convertToApiResponse } from "@phoenix/core/api"; import { Context } from "../hono_bindings"; import { NotFoundError } from "@phoenix/core/errors"; import { parseBlogFromDB } from "./utils"; +import { newApiResponse } from "../utils"; export async function headlessGetBlog(ctx: Context): Promise { const blogDomainInput = ctx.req.query('domain')?.trim() ?? ''; @@ -15,5 +15,5 @@ export async function headlessGetBlog(ctx: Context): Promise { } const blog = parseBlogFromDB(blogRes); - return ctx.json(convertToApiResponse(blog)); + return newApiResponse(blog); } diff --git a/phoenix-api/src/blogs/headless_get_page.ts b/phoenix-api/src/blogs/headless_get_page.ts index 654fa54..b831a1b 100644 --- a/phoenix-api/src/blogs/headless_get_page.ts +++ b/phoenix-api/src/blogs/headless_get_page.ts @@ -1,7 +1,7 @@ -import { convertToApiResponse } from "@phoenix/core/api"; import { Context } from "../hono_bindings"; import { NotFoundError } from "@phoenix/core/errors"; -import { Page, PageValidator } from "@phoenix/core/entities"; +import { PageValidator } from "@phoenix/core/entities"; +import { newApiResponse } from "../utils"; export async function headlessGetPage(ctx: Context): Promise { const pageSlug = ctx.req.query('slug')?.trim() ?? ''; @@ -19,5 +19,5 @@ export async function headlessGetPage(ctx: Context): Promise { } const page = PageValidator.parse(pageRes); - return ctx.json(convertToApiResponse(page)); + return newApiResponse(page); } diff --git a/phoenix-api/src/blogs/headless_get_posts.ts b/phoenix-api/src/blogs/headless_get_posts.ts index f54247e..352b94b 100644 --- a/phoenix-api/src/blogs/headless_get_posts.ts +++ b/phoenix-api/src/blogs/headless_get_posts.ts @@ -1,10 +1,10 @@ -import { convertToApiResponse } from "@phoenix/core/api"; import { Context } from "../hono_bindings"; -import { Page, PageValidator } from "@phoenix/core/entities"; +import { PageValidator } from "@phoenix/core/entities"; +import { newApiResponse } from "../utils"; export async function headlessGetPosts(ctx: Context): Promise { const blogDomainInput = ctx.req.query('domain')?.trim() ?? ''; - const blogSlug = blogDomainInput.replace(`.?{ctx.env.BLOGS_ROOT_DOMAIN}`, ''); + const blogSlug = blogDomainInput.replace(`.${ctx.env.BLOGS_ROOT_DOMAIN}`, ''); const pagesRes = await ctx.env.DB.prepare(`SELECT pages.* FROM pages INNER JOIN blogs ON pages.blog_id = blogs.id @@ -16,5 +16,5 @@ export async function headlessGetPosts(ctx: Context): Promise { const pages = pagesRes.results.map((p) => PageValidator.parse(p)); - return ctx.json(convertToApiResponse(pages)); + return newApiResponse(pages); } diff --git a/phoenix-api/src/blogs/update_blog.ts b/phoenix-api/src/blogs/update_blog.ts index 32cb7f1..b2a9949 100644 --- a/phoenix-api/src/blogs/update_blog.ts +++ b/phoenix-api/src/blogs/update_blog.ts @@ -1,7 +1,7 @@ import { Context } from "../hono_bindings"; -import { checkAuth, checkIsAdmin, parseAndValidateApiInput } from "../utils"; +import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils"; import { NotFoundError } from "@phoenix/core/errors"; -import { UpdateBlogInputValidator, convertToApiResponse } from "@phoenix/core/api"; +import { UpdateBlogInputValidator } from "@phoenix/core/api"; import { parseBlogFromDB } from "./utils"; export async function updateBlog(ctx: Context): Promise { @@ -31,5 +31,5 @@ export async function updateBlog(ctx: Context): Promise { blog.description_html, blog.id) .run(); - return ctx.json(convertToApiResponse(blog)); + return newApiResponse(blog); } diff --git a/phoenix-api/src/blogs/update_page.ts b/phoenix-api/src/blogs/update_page.ts index 98338c9..d42c66c 100644 --- a/phoenix-api/src/blogs/update_page.ts +++ b/phoenix-api/src/blogs/update_page.ts @@ -1,8 +1,8 @@ import { Context } from "../hono_bindings"; -import { checkAuth, checkIsAdmin, parseAndValidateApiInput } from "../utils"; +import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils"; import { NotFoundError } from "@phoenix/core/errors"; -import { UpdatePageInputValidator, convertToApiResponse } from "@phoenix/core/api"; -import { Page, PageValidator } from "@phoenix/core/entities"; +import { UpdatePageInputValidator } from "@phoenix/core/api"; +import { PageValidator } from "@phoenix/core/entities"; export async function updatePage(ctx: Context): Promise { const userId = await checkAuth(ctx); @@ -30,5 +30,5 @@ export async function updatePage(ctx: Context): Promise { .bind(page.updated_at.toISOString(), page.slug, page.title, page.content_html, page.id) .run(); - return ctx.json(convertToApiResponse(page)); + return newApiResponse(page); } diff --git a/phoenix-api/src/users/login.ts b/phoenix-api/src/users/login.ts index fff90bd..40bc152 100644 --- a/phoenix-api/src/users/login.ts +++ b/phoenix-api/src/users/login.ts @@ -2,8 +2,8 @@ import { setCookie } from "hono/cookie"; import { Context } from "../hono_bindings"; import jwt from "@phoenix/jwt"; import { NotFoundError, PermissionDeniedError } from "@phoenix/core/errors"; -import { base64ToBuffer, hashPassword, parseAndValidateApiInput } from "../utils"; -import { LoginInputValidator, convertToApiResponse, convertUser } from "@phoenix/core/api"; +import { base64ToBuffer, hashPassword, newApiResponse, parseAndValidateApiInput } from "../utils"; +import { LoginInputValidator, convertUser } from "@phoenix/core/api"; import { UserValidator } from "@phoenix/core/entities"; export async function login(ctx: Context): Promise { @@ -37,5 +37,5 @@ export async function login(ctx: Context): Promise { { httpOnly: false, expires: expiresAt, sameSite: 'Lax', secure: true, path: '/' }, ); - return ctx.json(convertToApiResponse(convertUser(user))); + return newApiResponse(convertUser(user)); } diff --git a/phoenix-api/src/users/signup.ts b/phoenix-api/src/users/signup.ts index f2e1b7d..97ef091 100644 --- a/phoenix-api/src/users/signup.ts +++ b/phoenix-api/src/users/signup.ts @@ -1,8 +1,8 @@ -import { bufferToBase64, hashPassword, parseAndValidateApiInput } from "../utils"; +import { bufferToBase64, hashPassword, newApiResponse, parseAndValidateApiInput } from "../utils"; import { uuidv7 } from "@phoenix/uuidv7"; -import { InternalServerError, PermissionDeniedError } from "@phoenix/core/errors"; +import { PermissionDeniedError } from "@phoenix/core/errors"; import { User } from "@phoenix/core/entities"; -import { SignupInputValidator, convertToApiResponse, convertUser } from "@phoenix/core/api"; +import { SignupInputValidator, convertUser } from "@phoenix/core/api"; import { Context } from "../hono_bindings"; import jwt from "@phoenix/jwt"; import { setCookie } from "hono/cookie"; @@ -60,5 +60,5 @@ export async function signup(ctx: Context): Promise { { httpOnly: false, expires: expiresAt, sameSite: 'Lax', secure: true, path: '/' }, ); - return ctx.json(convertToApiResponse(convertUser(user))); + return newApiResponse(convertUser(user)); } diff --git a/phoenix-api/src/utils.ts b/phoenix-api/src/utils.ts index 600e90d..eaf6b41 100644 --- a/phoenix-api/src/utils.ts +++ b/phoenix-api/src/utils.ts @@ -5,6 +5,16 @@ import jwt from "@phoenix/jwt"; import { ZodSchema } from "zod"; import { UserValidator } from "@phoenix/core/entities"; +export function newApiResponse(data: T, status = 200): Response { + return new Response(JSON.stringify({ data, error: null }), { + status: status, + headers: { + 'content-type': 'application/json' + }, + }); +} + + /** * Hash the given password with `PBKDF2-SHA-512` using userId as a salt */