Skip to content

Commit

Permalink
phoenix-api: refactor sendApiResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dev committed Dec 6, 2023
1 parent a5c88ca commit 859f013
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 40 deletions.
3 changes: 1 addition & 2 deletions packages/core/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { z } from 'zod';
import { PageType, type User } from './entities';
import deepClone from '@phoenix/deepclone';
import type { ApiResponse } from './api_client';

export * from './api_client';
export * from './api_routes';

export function convertToApiResponse<T>(data: T): ApiResponse<T> {
export function convertToApiResponse<T>(data: T): any {
return {
data: data,
error: null,
Expand Down
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/create_blog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uuidv7 } from "@phoenix/uuidv7";
import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, checkIsAdmin, parseAndValidateApiInput, sendApiResponse } 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 newApiResponse(blog);
return sendApiResponse(ctx, blog);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/create_page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { NotFoundError } from "@phoenix/core/errors";
import { uuidv7 } from "@phoenix/uuidv7";
import { checkIsAdmin } 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 newApiResponse(page);
return sendApiResponse(ctx, page);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/delete_blog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, checkIsAdmin, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { DeleteBlogInputValidator } from "@phoenix/core/api";

export async function deleteBlog(ctx: Context): Promise<Response> {
Expand All @@ -12,5 +12,5 @@ export async function deleteBlog(ctx: Context): Promise<Response> {
.bind(apiInput.blog_id)
.run();

return newApiResponse({ ok: true });
return sendApiResponse(ctx, { ok: true });
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/delete_page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NotFoundError } from "@phoenix/core/errors";
import { Context } from "../hono_bindings";
import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, checkIsAdmin, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { DeletePageInputValidator } from "@phoenix/core/api";
import { Page, PageValidator } from "@phoenix/core/entities";

Expand All @@ -23,5 +23,5 @@ export async function deletePage(ctx: Context): Promise<Response> {
.bind(apiInput.page_id)
.run();

return newApiResponse({ ok: true });
return sendApiResponse(ctx, { ok: true });
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/get_blog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { NotFoundError } from "@phoenix/core/errors";
import { GetBlogInputValidator } from "@phoenix/core/api";
import { parseBlogFromDB } from "./utils";
Expand All @@ -17,5 +17,5 @@ export async function getBlog(ctx: Context): Promise<Response> {
}
const blog = parseBlogFromDB(blogRes);

return newApiResponse(blog);
return sendApiResponse(ctx, blog);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/get_blogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, newApiResponse } from "../utils";
import { checkAuth, sendApiResponse } from "../utils";
import { parseBlogFromDB } from "./utils";

export async function getBlogs(ctx: Context): Promise<Response> {
Expand All @@ -8,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 newApiResponse(blogs);
return sendApiResponse(ctx, blogs);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/get_page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { NotFoundError } from "@phoenix/core/errors";
import { GetPageInputValidator } from "@phoenix/core/api";
import { Page, PageValidator } from "@phoenix/core/entities";
Expand All @@ -17,5 +17,5 @@ export async function getPage(ctx: Context): Promise<Response> {
}
const page = PageValidator.parse(pageRes);

return newApiResponse(page);
return sendApiResponse(ctx, page);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/get_pages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { GetPagesInputValidator } from "@phoenix/core/api";
import { PageValidator } from "@phoenix/core/entities";

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 newApiResponse(pages);
return sendApiResponse(ctx, 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 { Context } from "../hono_bindings";
import { NotFoundError } from "@phoenix/core/errors";
import { parseBlogFromDB } from "./utils";
import { newApiResponse } from "../utils";
import { sendApiResponse } 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 newApiResponse(blog);
return sendApiResponse(ctx, blog);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/headless_get_page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context } from "../hono_bindings";
import { NotFoundError } from "@phoenix/core/errors";
import { PageValidator } from "@phoenix/core/entities";
import { newApiResponse } from "../utils";
import { sendApiResponse } 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 newApiResponse(page);
return sendApiResponse(ctx, page);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/headless_get_posts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "../hono_bindings";
import { PageValidator } from "@phoenix/core/entities";
import { newApiResponse } from "../utils";
import { sendApiResponse } from "../utils";

export async function headlessGetPosts(ctx: Context): Promise<Response> {
const blogDomainInput = ctx.req.query('domain')?.trim() ?? '';
Expand All @@ -16,5 +16,5 @@ export async function headlessGetPosts(ctx: Context): Promise<Response> {
const pages = pagesRes.results.map((p) => PageValidator.parse(p));


return newApiResponse(pages);
return sendApiResponse(ctx, pages);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/update_blog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, checkIsAdmin, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { NotFoundError } from "@phoenix/core/errors";
import { UpdateBlogInputValidator } from "@phoenix/core/api";
import { parseBlogFromDB } from "./utils";
Expand Down Expand Up @@ -31,5 +31,5 @@ export async function updateBlog(ctx: Context): Promise<Response> {
blog.description_html, blog.id)
.run();

return newApiResponse(blog);
return sendApiResponse(ctx, blog);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/blogs/update_page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "../hono_bindings";
import { checkAuth, checkIsAdmin, newApiResponse, parseAndValidateApiInput } from "../utils";
import { checkAuth, checkIsAdmin, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { NotFoundError } from "@phoenix/core/errors";
import { UpdatePageInputValidator } from "@phoenix/core/api";
import { PageValidator } from "@phoenix/core/entities";
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 newApiResponse(page);
return sendApiResponse(ctx, page);
}
4 changes: 2 additions & 2 deletions phoenix-api/src/users/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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, newApiResponse, parseAndValidateApiInput } from "../utils";
import { base64ToBuffer, hashPassword, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { LoginInputValidator, convertUser } from "@phoenix/core/api";
import { UserValidator } from "@phoenix/core/entities";

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 newApiResponse(convertUser(user));
return sendApiResponse(ctx, convertUser(user));
}
4 changes: 2 additions & 2 deletions phoenix-api/src/users/signup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bufferToBase64, hashPassword, newApiResponse, parseAndValidateApiInput } from "../utils";
import { bufferToBase64, hashPassword, parseAndValidateApiInput, sendApiResponse } from "../utils";
import { uuidv7 } from "@phoenix/uuidv7";
import { PermissionDeniedError } from "@phoenix/core/errors";
import { User } from "@phoenix/core/entities";
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 newApiResponse(convertUser(user));
return sendApiResponse(ctx, convertUser(user));
}
16 changes: 8 additions & 8 deletions phoenix-api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { InvalidArgumentError, NotFoundError, PermissionDeniedError } from "@pho
import jwt from "@phoenix/jwt";
import { ZodSchema } from "zod";
import { UserValidator } from "@phoenix/core/entities";
import { ApiResponse } from "@phoenix/core/api_client";

export function newApiResponse<T>(data: T, status = 200): Response {
return new Response(JSON.stringify({ data, error: null }), {
status: status,
headers: {
'content-type': 'application/json'
},
});
export function sendApiResponse<T>(ctx: Context, data: T, status = 200): Response {
const body: ApiResponse<T> = {
data: data,
error: null,
};
// we convert to any because hono's json function is too restrictive (doesn't accept Date...)
return ctx.json(body as any, status);
}


/**
* Hash the given password with `PBKDF2-SHA-512` using userId as a salt
*/
Expand Down

0 comments on commit 859f013

Please sign in to comment.