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 4 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
15 changes: 13 additions & 2 deletions 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,
api,
}: {
appPath: string
packageManager: PackageManager
Expand All @@ -51,11 +52,20 @@ export async function createApp({
skipInstall: boolean
empty: boolean
turbo: boolean
api: boolean
}): 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 = api
? 'api'
: `${appRouter ? 'app' : 'default'}${tailwind ? '-tw' : ''}${empty ? '-empty' : ''}`;
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved


if (api && template !== 'api') {
console.error(`Invalid template for api flag: ${red(template)}`)
process.exit(1)
}
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved

if (example) {
let repoUrl: URL | undefined

Expand Down Expand Up @@ -188,7 +198,7 @@ export async function createApp({
isErrorLike(reason) ? reason.message : reason + ''
)
}
// Copy `.gitignore` if the application did not provide one
// Copy `.gitignore` if the application did not provide one
Arindam200 marked this conversation as resolved.
Show resolved Hide resolved
const ignorePath = join(root, '.gitignore')
if (!existsSync(ignorePath)) {
copyFileSync(
Expand Down Expand Up @@ -232,6 +242,7 @@ export async function createApp({
importAlias,
skipInstall,
turbo,
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 @@ -97,6 +97,12 @@ const program = new Command(packageJson.name)
`

Specify import alias to use (default "@/*").
`
)
.option(
'--api',
`
Initialize a Headless API.
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
`
)
.option(
Expand Down Expand Up @@ -484,6 +490,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.
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
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": {
"@/*": ["./*"]
}
}
}
4 changes: 4 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,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
3 changes: 3 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,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"
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" });
};
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.
7 changes: 7 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,7 @@
import type { NextConfig } from "next";

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

export default nextConfig;
27 changes: 27 additions & 0 deletions packages/create-next-app/templates/api/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
1 change: 1 addition & 0 deletions packages/create-next-app/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const installTemplate = async ({
importAlias,
skipInstall,
turbo,
api,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));

Expand Down
3 changes: 3 additions & 0 deletions packages/create-next-app/templates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PackageManager } from "../helpers/get-pkg-manager";

export type TemplateType =
| "app"
| "api"
| "app-empty"
| "app-tw"
| "app-tw-empty"
Expand All @@ -19,6 +20,7 @@ export interface GetTemplateFileArgs {

export interface InstallTemplateArgs {
appName: string;
api:boolean;
Arindam200 marked this conversation as resolved.
Show resolved Hide resolved
root: string;
packageManager: PackageManager;
isOnline: boolean;
Expand All @@ -31,3 +33,4 @@ export interface InstallTemplateArgs {
skipInstall: boolean;
turbo: boolean;
}
j
Arindam200 marked this conversation as resolved.
Show resolved Hide resolved
Loading