This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new api using edge runtime need to explictly enable via edge config (`edge_runtime: true`)
- Loading branch information
Showing
23 changed files
with
436 additions
and
157 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
import { get } from '@vercel/edge-config' | ||
|
||
const pattern = /^\/api\/\w+(?=\/|$)/ | ||
|
||
export async function middleware(request: NextRequest) { | ||
const original = request.nextUrl | ||
|
||
let { pathname } = original | ||
if (pathname === '/api/') { | ||
pathname = '/api/index/' | ||
} | ||
|
||
const edgeRuntimeEnabled = process.env.EDGE_CONFIG && (await get<boolean>('edge_runtime')) | ||
if (!edgeRuntimeEnabled) { | ||
pathname = pathname.replace(pattern, m => `${m}-v1`) | ||
} else { | ||
pathname = pathname.replace(pattern, m => `${m}-v2`) | ||
} | ||
const rewritten = new URL(pathname, original) | ||
original.searchParams.forEach((value, key) => { | ||
rewritten.searchParams.append(key, value) | ||
}) | ||
return NextResponse.rewrite(rewritten) | ||
} | ||
|
||
// See "Matching Paths" below to learn more | ||
export const config = { | ||
matcher: '/api/:path*', | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeRequestToWeb } from '@/utils/api/common' | ||
import handle from '@/utils/api/index' | ||
import { kv } from '@/utils/kv/ioredis' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const response = await handle(kv, NodeRequestToWeb(req)) | ||
response.toNode(res) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import handle from '@/utils/api/index' | ||
import { kv } from '@/utils/kv/edge' | ||
import { NextRequest } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default async function handler(req: NextRequest) { | ||
return (await handle(kv, req)).toWeb() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeRequestToWeb } from '@/utils/api/common' | ||
import handle from '@/utils/api/item' | ||
import { kv } from '@/utils/kv/ioredis' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const response = await handle(kv, NodeRequestToWeb(req)) | ||
response.toNode(res) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import handle from '@/utils/api/item' | ||
import { kv } from '@/utils/kv/edge' | ||
import { NextRequest } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default async function handler(req: NextRequest) { | ||
return (await handle(kv, req)).toWeb() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
import handle from '../raw-v1' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
return handle(req, res) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeRequestToWeb } from '@/utils/api/common' | ||
import handle from '@/utils/api/raw' | ||
import { kv } from '@/utils/kv/ioredis' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const response = await handle(kv, NodeRequestToWeb(req)) | ||
response.toNode(res) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import handle from '@/utils/api/raw' | ||
import { kv } from '@/utils/kv/edge' | ||
import { NextRequest } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default async function handler(req: NextRequest) { | ||
return (await handle(kv, req)).toWeb() | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeRequestToWeb } from '@/utils/api/common' | ||
import handle from '@/utils/api/search' | ||
import { kv } from '@/utils/kv/ioredis' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const response = await handle(kv, NodeRequestToWeb(req)) | ||
response.toNode(res) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import handle from '@/utils/api/search' | ||
import { kv } from '@/utils/kv/edge' | ||
import { NextRequest } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default async function handler(req: NextRequest) { | ||
return (await handle(kv, req)).toWeb() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeRequestToWeb } from '@/utils/api/common' | ||
import handle from '@/utils/api/thumbnail' | ||
import { kv } from '@/utils/kv/ioredis' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import '@/utils/api/fetch-polyfill' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const response = await handle(kv, NodeRequestToWeb(req)) | ||
response.toNode(res) | ||
} |
Oops, something went wrong.