Created
January 18, 2024 04:32
-
-
Save vanduc1102/0e1d9f933ab1bef6bf786d39102b2473 to your computer and use it in GitHub Desktop.
NextJS@13 and KoaJS@2 integration to use koajs to handle nextjs api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Koa, { Context } from "koa"; | |
import bodyParser from "@koa/bodyparser"; | |
import registerRoutes from "./routers"; | |
import Jwt from "koa-jwt"; | |
const { THIRD_PARTY_APP_JWT_SECRET = "" } = process.env; | |
const koaApp = new Koa(); | |
koaApp.use(bodyParser()); | |
koaApp.use(async (ctx: Context, next) => { | |
try { | |
await next(); | |
} catch (err: any) { | |
ctx.status = err.statusCode || err.status || 500; | |
ctx.body = { | |
message: err.message, | |
}; | |
} | |
}); | |
koaApp.use( | |
Jwt({ secret: THIRD_PARTY_APP_JWT_SECRET }).unless({ | |
path: [ | |
"/api/third-party/open-api", | |
"/api/third-party/docs", | |
"/api/third-party/authentication/get-tokens", | |
"/api/third-party/authentication/refresh-tokens", | |
], | |
}) | |
); | |
registerRoutes(koaApp); | |
export default koaApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/api/third-party/[...all].js | |
import type { NextApiRequest, NextApiResponse } from "next"; | |
import { createServer } from "http"; | |
import koaApp from "@/backend/third-party/app"; | |
const server = createServer(koaApp.callback()); | |
export default async function handler( | |
req: NextApiRequest, | |
res: NextApiResponse | |
) { | |
server.emit("request", req, res); | |
} | |
export const config = { | |
api: { | |
bodyParser: false, | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Router from "@koa/router"; | |
import * as productService from "@/backend/third-party/services/productService"; | |
import { ProductListResponseBody } from "../types"; | |
const router = new Router({ | |
prefix: "/api/third-party", | |
}); | |
router.get("/products", async (ctx, next) => { | |
const { take, skip } = ctx.request.query; | |
const { products, total } = await productService.findMany({ | |
take: Number(take), | |
skip: Number(skip), | |
}); | |
const response: ProductListResponseBody = { | |
items: products as any[], | |
total, | |
}; | |
ctx.body = response; | |
}); | |
router.get("/products/:productId", async (ctx, next) => { | |
const { productId } = ctx.params; | |
const product = await productService.findUnique({ id: Number(productId) }); | |
ctx.body = product; | |
}); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment