Skip to content

Feature/add next 13 beta support #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/example/app/new/api/simple/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BadRequestException, handler } from 'next-api-handler/beta';

import { getDataById } from '@/server/service';

export const GET = handler(async (req) => {
const searchParams = new URL(req.url).searchParams;
const id = searchParams.get('id');

if (typeof id !== 'string')
// can throw status code related errors
throw new BadRequestException('Id is required');

// automatically handle errors
return getDataById(id);
});
1 change: 1 addition & 0 deletions apps/example/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
10 changes: 8 additions & 2 deletions apps/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "tsconfig/nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"compilerOptions": {
"rootDir": ".",
"baseUrl": ".",
Expand All @@ -9,7 +9,13 @@
// Fixes TypesCript infeered type error for pnpm morenorpos
// Refernce: https://github.com/microsoft/TypeScript/issues/42873
"next": ["node_modules/next"]
}
},
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": true
},
"exclude": ["node_modules"]
}
21 changes: 18 additions & 3 deletions packages/next-api-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@
"serverless"
],
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"module": "dist/index.js",
"typesVersions": {
"*": {
".": [
"dist/index.d.ts"
],
"beta": [
"dist/beta/index.d.ts"
]
}
},
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./beta": {
"import": "./dist/beta/index.js",
"require": "./dist/beta/index.cjs"
}
},
"type": "module",
Expand All @@ -44,6 +58,7 @@
"license": "MIT",
"scripts": {
"clean": "rm -rf .turbo node_modules build dist coverage",
"dev": "pnpm watch:build",
"build": "tsup",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write --list-different",
Expand Down Expand Up @@ -92,9 +107,9 @@
"typescript": "^5.2.2"
},
"peerDependencies": {
"next": ">= 9.0.0"
"next": ">=13.0.0"
},
"engines": {
"node": ">=8.10"
"node": ">=18"
}
}
38 changes: 38 additions & 0 deletions packages/next-api-handler/src/lib/beta/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NextRequest } from 'next/server';

import { HttpException } from '../http-exceptions';

export type MaybePromise<T> = T | Promise<T>;

export type HandlerParams<T> = (req: NextRequest) => MaybePromise<T>;

export const handler =
<T>(params: HandlerParams<T>) =>
async (req: NextRequest): Promise<Response> => {
try {
const data = await params(req);
return Response.json({ success: true, data }, { status: 200 });
} catch (error) {
if (error instanceof HttpException) {
return Response.json(
{
success: false,
message:
process.env.NODE_ENV === 'production'
? error.defaultMessage
: error.message,
},
{ status: error.status }
);
}

console.error('Unexpected Error', error);
return Response.json(
{
success: false,
message: 'Internal Server Error',
},
{ status: 500 }
);
}
};
3 changes: 3 additions & 0 deletions packages/next-api-handler/src/lib/beta/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './handler';
export * from '../http-exceptions';
export * from '../type';
6 changes: 3 additions & 3 deletions packages/next-api-handler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"target": "es2017",
"target": "ES2017",
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"lib": ["es2017"]
"lib": ["ES2017", "DOM"]
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "build", "node_modules"],
"exclude": ["dist", "build"],
"compileOnSave": false
}
2 changes: 1 addition & 1 deletion packages/next-api-handler/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig((options) => ({
entry: ['src/lib/*.ts'],
entry: ['src/lib/*.ts', 'src/lib/beta/index.ts'],
format: ['cjs', 'esm'],
splitting: true,
treeshake: true,
Expand Down