Skip to content

Commit 29d1d14

Browse files
refactor: use NextResponse instead of Response (#26)
1 parent 5f21e9d commit 29d1d14

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

app/api/[area]/[code]/boundary/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextRequest } from 'next/server'
1+
import { NextRequest, NextResponse } from 'next/server'
22
import { z } from 'zod'
33
import { Params, paramSchema } from './schema'
44
import { getBoundaryData } from '@/lib/data'
@@ -11,12 +11,12 @@ export async function GET(
1111
try {
1212
validatedParams = paramSchema.parse(params)
1313
} catch (error) {
14-
return new Response(
15-
JSON.stringify({
14+
return NextResponse.json(
15+
{
1616
statusCode: 400,
1717
message: 'Bad Request',
1818
...(error instanceof z.ZodError && { error: error.errors }),
19-
}),
19+
},
2020
{ status: 400 },
2121
)
2222
}

lib/data.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { get } from 'https'
44
import { config } from './config'
55
import { Areas, GetArea, parentArea, singletonArea } from './const'
66
import { addDotSeparator } from './utils'
7+
import { NextResponse } from 'next/server'
78

89
export type Query<Area extends Areas> = {
910
limit?: number
@@ -94,7 +95,7 @@ export async function getSpecificData<Area extends Areas>(
9495
export async function getBoundaryData(area: Areas, code: string) {
9596
const url = `${config.dataSource.boundary.url}/${area}/${addDotSeparator(code.replaceAll('.', ''))}.geojson`
9697

97-
return new Promise<Response>((resolve, reject) => {
98+
return new Promise<NextResponse>((resolve, reject) => {
9899
// Create encoding to convert token (string) to Uint8Array
99100
const encoder = new TextEncoder()
100101

@@ -105,11 +106,11 @@ export async function getBoundaryData(area: Areas, code: string) {
105106
get(url, (res) => {
106107
if (res.statusCode !== 200) {
107108
resolve(
108-
new Response(
109-
JSON.stringify({
109+
NextResponse.json(
110+
{
110111
statusCode: res.statusCode,
111112
message: res.statusMessage,
112-
}),
113+
},
113114
{ status: res.statusCode },
114115
),
115116
)
@@ -121,7 +122,7 @@ export async function getBoundaryData(area: Areas, code: string) {
121122

122123
res.on('end', () => {
123124
writer.close()
124-
resolve(new Response(stream.readable, { status: res.statusCode }))
125+
resolve(new NextResponse(stream.readable, { status: res.statusCode }))
125126
})
126127

127128
res.on('error', (error) => {

0 commit comments

Comments
 (0)