Skip to content
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

Add --api Flag to Create Headless API App with create-next-app #68130

Open
wants to merge 66 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
513d424
add: headless-api
Arindam200 Jul 24, 2024
63fb471
add: Readme Templates
Arindam200 Jul 24, 2024
632b11b
fix: --api flag
Arindam200 Jul 25, 2024
393b432
Update packages/create-next-app/create-app.ts
Arindam200 Jul 25, 2024
76057cd
Update packages/create-next-app/templates/types.ts
Arindam200 Jul 26, 2024
a0742b5
Update packages/create-next-app/templates/types.ts
Arindam200 Jul 26, 2024
d837d4d
Update packages/create-next-app/create-app.ts
Arindam200 Jul 26, 2024
f03fde4
Merge branch 'vercel:canary' into feat-headless-api
Arindam200 Jul 27, 2024
8eaecc1
rename api to app-api
Arindam200 Jul 27, 2024
5af9353
Merge branch 'vercel:canary' into feat-headless-api
Arindam200 Jul 28, 2024
e2e63a1
Improve next.config.ts and next.config.mjs for API template
Tim-Zj Jul 29, 2024
0193596
Add route.js for API routes using App Router
Tim-Zj Jul 29, 2024
7cd19b1
Merge branch 'vercel:canary' into improve-api-template
Tim-Zj Jul 29, 2024
8f3a438
Save current changes
Tim-Zj Jul 29, 2024
30d146e
added route.js and route.ts
Tim-Zj Jul 30, 2024
945454a
fix: kebab is delicious
devjiwonchoi Jul 30, 2024
e9a4949
fix kebab
devjiwonchoi Jul 30, 2024
d149150
template name from api to app-api
devjiwonchoi Jul 30, 2024
7051cae
remove unnecessary added husky
devjiwonchoi Jul 30, 2024
03767ee
Update packages/create-next-app/index.ts
devjiwonchoi Jul 30, 2024
9af9575
remove eslint
devjiwonchoi Jul 30, 2024
fa604fb
unnecessary since is pages router config
devjiwonchoi Jul 30, 2024
9160ea4
add params to ts example
ephraimduncan Jul 30, 2024
6508b38
Merge branch 'canary' into feat-headless-api
devjiwonchoi Jul 31, 2024
7e83093
Merge branch 'vercel:canary' into feat-headless-api
Arindam200 Jul 31, 2024
557c201
add: status codes
Arindam200 Aug 1, 2024
016d3e3
Merge branch 'canary' into feat-headless-api
devjiwonchoi Aug 2, 2024
fc7f524
fix: left out merges
devjiwonchoi Aug 2, 2024
149a01c
Merge branch 'canary' into feat-headless-api
Arindam200 Aug 2, 2024
c864f01
Merge branch 'canary' into feat-headless-api
devjiwonchoi Aug 2, 2024
8698458
fix: left out merges
devjiwonchoi Aug 2, 2024
a150dfc
remove unnecessary api/
devjiwonchoi Aug 2, 2024
1639ef5
fix: specification
devjiwonchoi Aug 2, 2024
03aede3
remove unnecessary check
devjiwonchoi Aug 2, 2024
6fb01e7
Add user-info API route example with NextRequest and NextResponse
Tim-Zj Aug 3, 2024
b7c3966
Merge branch 'vercel:canary' into feat-headless-api
Tim-Zj Aug 3, 2024
325f55e
Add 404 and 500 error handling examples for API routes and update README
Tim-Zj Aug 3, 2024
a445aa5
Add 404 and 500 error handling examples for API routes and update README
Tim-Zj Aug 3, 2024
31095db
Add cors.ts
Tim-Zj Aug 3, 2024
894d544
add app-api for ts
devjiwonchoi Aug 15, 2024
93ea629
readme
devjiwonchoi Aug 15, 2024
5752374
remove js for now
devjiwonchoi Aug 15, 2024
d290b26
js
devjiwonchoi Aug 15, 2024
6dfb7d5
update id to number
devjiwonchoi Aug 15, 2024
4b45991
update
devjiwonchoi Aug 15, 2024
906539c
add test
devjiwonchoi Aug 15, 2024
1db9168
Merge branch 'canary' into feat-headless-api
Arindam200 Aug 16, 2024
db6c611
remove NextRequest type
Arindam200 Aug 16, 2024
d6a48aa
add `type` NextRequest
Arindam200 Aug 16, 2024
51f1102
Merge branch 'canary' into feat-headless-api
Arindam200 Aug 16, 2024
60a1272
Merge branch 'canary' into feat-headless-api
devjiwonchoi Aug 21, 2024
e7ac0a4
test: remove git diff
devjiwonchoi Aug 21, 2024
a32bb60
cannot delete `@types/react` yet
devjiwonchoi Aug 21, 2024
ef2c545
Revert "test: remove git diff"
devjiwonchoi Aug 21, 2024
cc203c2
Merge branch 'canary' into feat-headless-api
Arindam200 Aug 29, 2024
60d1651
Merge branch 'canary' into feat-headless-api
Arindam200 Sep 7, 2024
40e2578
Merge branch 'canary' into feat-headless-api
devjiwonchoi Sep 12, 2024
2e63b3f
move to example
devjiwonchoi Sep 12, 2024
80adef5
specification
devjiwonchoi Sep 12, 2024
10b88b8
util update
devjiwonchoi Sep 12, 2024
318067b
js hello world
devjiwonchoi Sep 12, 2024
4935b2f
write tests for example app-api
devjiwonchoi Sep 12, 2024
61603de
Update examples/app-api/package.json
devjiwonchoi Sep 12, 2024
ba031ae
remove not-found
devjiwonchoi Sep 12, 2024
57fb6a7
next latest
devjiwonchoi Sep 12, 2024
f11da16
improve comments
Arindam200 Oct 26, 2024
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
10 changes: 10 additions & 0 deletions .husky/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Rename this file to `.env.local` to use environment variables locally with `next dev`
# Add .env files to your .gitignore to prevent accidentally committing sensitive information.
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
MY_HOST="example.com"
# API Configuration
API_BASE_URL="https://api.example.com"
API_VERSION="v1"
# Misc
NODE_ENV="development"
PORT="3000"
7 changes: 7 additions & 0 deletions .husky/next.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { NextConfig } from "next";
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved

const nextConfig: NextConfig = {
/* config options here */
};

export default nextConfig;
13 changes: 12 additions & 1 deletion packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function createApp({
skipInstall,
empty,
turbo,
app-api,
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
}: {
appPath: string
packageManager: PackageManager
Expand All @@ -51,11 +52,20 @@ export async function createApp({
skipInstall: boolean
empty: boolean
turbo: boolean
app-api: boolean
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
const template: TemplateType = `${appRouter ? 'app' : 'default'}${tailwind ? '-tw' : ''}${empty ? '-empty' : ''}`
const template: TemplateType = app-api
? 'app-api'
: `${appRouter ? 'app' : 'default'}${tailwind ? '-tw' : ''}${empty ? '-empty' : ''}`;


if (api && template !== 'app-api') {
console.error(`Invalid template for api flag: ${red(template)}`)
process.exit(1)
}

if (example) {
let repoUrl: URL | undefined

Expand Down Expand Up @@ -232,6 +242,7 @@ export async function createApp({
importAlias,
skipInstall,
turbo,
app-api,
})
}

Expand Down
7 changes: 7 additions & 0 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ const program = new Command(packageJson.name)
`

Specify import alias to use (default "@/*").
`
)
.option(
'--api',
`
Initialize a Headless API using app router.
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
`
)
.option(
Expand Down Expand Up @@ -501,6 +507,7 @@ async function run(): Promise<void> {
skipInstall: program.skipInstall,
empty: program.empty,
turbo: program.turbo,
api: program.api,
})
} catch (reason) {
if (!(reason instanceof DownloadError)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/create-next-app/templates/api/js/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rename this file to `.env.local` to use environment variables locally with `next dev`
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
MY_HOST="example.com"
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions packages/create-next-app/templates/api/js/README-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000/api](http://localhost:3000/api) with your browser to see the result.

You can start editing the page by modifying `app/index.js`. The page auto-updates as you edit the file.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
11 changes: 11 additions & 0 deletions packages/create-next-app/templates/api/js/api/[param]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
export async function GET(request, context) {
const { params } = context;
const { param } = params; // dynamic router

return new Response(JSON.stringify({ message: `Hello, ${param}` }), {
headers: { 'Content-Type': 'application/json' },
});
}

3 changes: 3 additions & 0 deletions packages/create-next-app/templates/api/js/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
return res.json({ message: "Hello World" });
};
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions packages/create-next-app/templates/api/js/api/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export async function GET(request) {
return new Response(JSON.stringify({ message: "Hello World" }), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function POST(request) {
const data = await request.json();
return new Response(JSON.stringify({ message: "Data received", data }), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function PUT(request) {
const data = await request.json();
return new Response(JSON.stringify({ message: "Data updated", data }), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function DELETE(request) {
return new Response(JSON.stringify({ message: "Data deleted" }), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function OPTIONS() {
return new Response(null, {
headers: {
'Allow': 'GET, POST, PUT, DELETE, OPTIONS',
'Content-Length': '0',
},
});
}

3 changes: 3 additions & 0 deletions packages/create-next-app/templates/api/js/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
"extends": "next/core-web-vitals"
}
40 changes: 40 additions & 0 deletions packages/create-next-app/templates/api/js/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env files (can opt-in for commiting if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
7 changes: 7 additions & 0 deletions packages/create-next-app/templates/api/js/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./*"]
}
}
}
16 changes: 16 additions & 0 deletions packages/create-next-app/templates/api/js/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
// API routes configuration
api: {

bodyParser: {
sizeLimit: '1mb',
},
cors: {
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
},
},
};

export default nextConfig;
4 changes: 4 additions & 0 deletions packages/create-next-app/templates/api/ts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Rename this file to `.env.local` to use environment variables locally with `next dev`
# Add .env files to your .gitignore to prevent accidentally committing sensitive information.
# https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
MY_HOST="example.com"
34 changes: 34 additions & 0 deletions packages/create-next-app/templates/api/ts/README-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000/api](http://localhost:3000/api) with your browser to see the result.

You can start editing the page by modifying `app/index.ts`. The page auto-updates as you edit the file.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
5 changes: 5 additions & 0 deletions packages/create-next-app/templates/api/ts/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { VercelRequest, VercelResponse } from "@vercel/node";

export default (req: VercelRequest, res: VercelResponse) => {
return res.json({ message: "Hello World" });
};
36 changes: 36 additions & 0 deletions packages/create-next-app/templates/api/ts/api/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextRequest, NextResponse } from 'next/server';

export async function GET(request: NextRequest) {
return NextResponse.json({ message: "Hello World" }, {
headers: { 'Content-Type': 'application/json' },
});
}

export async function POST(request: NextRequest) {
const data = await request.json();
return NextResponse.json({ message: "Data received", data }, {
headers: { 'Content-Type': 'application/json' },
});
}

export async function PUT(request: NextRequest) {
const data = await request.json();
return NextResponse.json({ message: "Data updated", data }, {
headers: { 'Content-Type': 'application/json' },
});
}

export async function DELETE(request: NextRequest) {
return NextResponse.json({ message: "Data deleted" }, {
headers: { 'Content-Type': 'application/json' },
});
}

export async function OPTIONS(request: NextRequest) {
return new Response(null, {
headers: {
'Allow': 'GET, POST, PUT, DELETE, OPTIONS',
'Content-Length': '0',
},
});
}
3 changes: 3 additions & 0 deletions packages/create-next-app/templates/api/ts/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["next/core-web-vitals", "next/typescript"]
}
40 changes: 40 additions & 0 deletions packages/create-next-app/templates/api/ts/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env files (can opt-in for commiting if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
5 changes: 5 additions & 0 deletions packages/create-next-app/templates/api/ts/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
17 changes: 17 additions & 0 deletions packages/create-next-app/templates/api/ts/next.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
/* config options here */
// API routes configuration
api: {
bodyParser: {
sizeLimit: '1mb',
},
cors: {
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
},
},
};

export default nextConfig;
Loading
Loading