Skip to content

Commit

Permalink
phoenix-api: move to D1
Browse files Browse the repository at this point in the history
  • Loading branch information
dev committed Dec 6, 2023
1 parent 162da17 commit f5a2b5f
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 48 deletions.
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/create_blog.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -28,5 +28,5 @@ export async function createBlog(ctx: Context): Promise<Response> {
JSON.stringify(blog.navigation), blog.description_html)
.run();

return ctx.json(convertToApiResponse(blog));
return newApiResponse(blog);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/create_page.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -44,5 +44,5 @@ export async function createPage(ctx: Context): Promise<Response> {
.bind(now.toISOString(), blog.id)
.run();

return ctx.json(convertToApiResponse(page));
return newApiResponse(page);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/delete_blog.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
const userId = await checkAuth(ctx);
Expand All @@ -12,5 +12,5 @@ export async function deleteBlog(ctx: Context): Promise<Response> {
.bind(apiInput.blog_id)
.run();

return ctx.json(convertToApiResponse({ ok: true }));
return newApiResponse({ ok: true });
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/delete_page.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
Expand All @@ -23,5 +23,5 @@ export async function deletePage(ctx: Context): Promise<Response> {
.bind(apiInput.page_id)
.run();

return ctx.json(convertToApiResponse({ ok: true }));
return newApiResponse({ ok: true });
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/get_blog.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
Expand All @@ -17,5 +17,5 @@ export async function getBlog(ctx: Context): Promise<Response> {
}
const blog = parseBlogFromDB(blogRes);

return ctx.json(convertToApiResponse(blog));
return newApiResponse(blog);
}
5 changes: 2 additions & 3 deletions phoenix-api/src/blogs/get_blogs.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
Expand All @@ -9,5 +8,5 @@ export async function getBlogs(ctx: Context): Promise<Response> {
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);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/get_page.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
Expand All @@ -17,5 +17,5 @@ export async function getPage(ctx: Context): Promise<Response> {
}
const page = PageValidator.parse(pageRes);

return ctx.json(convertToApiResponse(page));
return newApiResponse(page);
}
8 changes: 4 additions & 4 deletions phoenix-api/src/blogs/get_pages.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
await checkAuth(ctx);
Expand All @@ -13,5 +13,5 @@ export async function getPages(ctx: Context): Promise<Response> {
.all();
const pages = pagesRes.results.map((p) => PageValidator.parse(p));

return ctx.json(convertToApiResponse(pages));
return newApiResponse(pages);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/headless_get_blog.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
const blogDomainInput = ctx.req.query('domain')?.trim() ?? '';
Expand All @@ -15,5 +15,5 @@ export async function headlessGetBlog(ctx: Context): Promise<Response> {
}
const blog = parseBlogFromDB(blogRes);

return ctx.json(convertToApiResponse(blog));
return newApiResponse(blog);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/headless_get_page.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
const pageSlug = ctx.req.query('slug')?.trim() ?? '';
Expand All @@ -19,5 +19,5 @@ export async function headlessGetPage(ctx: Context): Promise<Response> {
}
const page = PageValidator.parse(pageRes);

return ctx.json(convertToApiResponse(page));
return newApiResponse(page);
}
8 changes: 4 additions & 4 deletions phoenix-api/src/blogs/headless_get_posts.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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
Expand All @@ -16,5 +16,5 @@ export async function headlessGetPosts(ctx: Context): Promise<Response> {
const pages = pagesRes.results.map((p) => PageValidator.parse(p));


return ctx.json(convertToApiResponse(pages));
return newApiResponse(pages);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/blogs/update_blog.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
Expand Down Expand Up @@ -31,5 +31,5 @@ export async function updateBlog(ctx: Context): Promise<Response> {
blog.description_html, blog.id)
.run();

return ctx.json(convertToApiResponse(blog));
return newApiResponse(blog);
}
8 changes: 4 additions & 4 deletions phoenix-api/src/blogs/update_page.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
const userId = await checkAuth(ctx);
Expand Down Expand Up @@ -30,5 +30,5 @@ export async function updatePage(ctx: Context): Promise<Response> {
.bind(page.updated_at.toISOString(), page.slug, page.title, page.content_html, page.id)
.run();

return ctx.json(convertToApiResponse(page));
return newApiResponse(page);
}
6 changes: 3 additions & 3 deletions phoenix-api/src/users/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
Expand Down Expand Up @@ -37,5 +37,5 @@ export async function login(ctx: Context): Promise<Response> {
{ httpOnly: false, expires: expiresAt, sameSite: 'Lax', secure: true, path: '/' },
);

return ctx.json(convertToApiResponse(convertUser(user)));
return newApiResponse(convertUser(user));
}
8 changes: 4 additions & 4 deletions phoenix-api/src/users/signup.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -60,5 +60,5 @@ export async function signup(ctx: Context): Promise<Response> {
{ httpOnly: false, expires: expiresAt, sameSite: 'Lax', secure: true, path: '/' },
);

return ctx.json(convertToApiResponse(convertUser(user)));
return newApiResponse(convertUser(user));
}
10 changes: 10 additions & 0 deletions phoenix-api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import jwt from "@phoenix/jwt";
import { ZodSchema } from "zod";
import { UserValidator } from "@phoenix/core/entities";

export function newApiResponse<T>(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
*/
Expand Down

0 comments on commit f5a2b5f

Please sign in to comment.