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

✨ next-response #158

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Changes from 1 commit
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
64 changes: 32 additions & 32 deletions docs/02-app/02-api-reference/04-functions/next-response.mdx
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
---
title: NextResponse
description: API Reference for NextResponse.
description: NextResponse에 대한 API 참조입니다.
---

NextResponse extends the [Web Response API](https://developer.mozilla.org/en-US/docs/Web/API/Response) with additional convenience methods.
NextResponse는 편리한 메서드를 추가하여 [Web Response API](https://developer.mozilla.org/en-US/docs/Web/API/Response)를 확장합니다.

## `cookies`

Read or mutate the [`Set-Cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header of the response.
response의 [`Set-Cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) 헤더를 읽거나 변경할 수 있습니다.
chaejunlee marked this conversation as resolved.
Show resolved Hide resolved

### `set(name, value)`
### `set(이름, 값)`
chaejunlee marked this conversation as resolved.
Show resolved Hide resolved

Given a name, set a cookie with the given value on the response.
쿠키 **이름**이 주어지면 response에서 주어진 **값**으로 쿠키를 설정합니다.

```ts
// Given incoming request /home
// /home이라는 수신 request가 주어질 때
let response = NextResponse.next()
// Set a cookie to hide the banner
// 배너를 숨기도록 쿠키를 설정
response.cookies.set('show-banner', 'false')
// Response will have a `Set-Cookie:show-banner=false;path=/home` header
// Response에는 `Set-Cookie:show-banner=false;path=/home` header가 존재
return response
```

### `get(name)`
### `get(이름)`

Given a cookie name, return the value of the cookie. If the cookie is not found, `undefined` is returned. If multiple cookies are found, the first one is returned.
쿠키 **이름**이 주어질 때 쿠키 **이름**에 맞는 쿠키 값을 반환합니다. 쿠키를 찾을 수 없다면 `undefined`를 반환합니다. 여러 개의 쿠키를 찾았다면 그 중 첫 번째로 찾은 쿠키를 반환합니다.

```ts
// Given incoming request /home
// /home이라는 수신 request가 주어질 때
chaejunlee marked this conversation as resolved.
Show resolved Hide resolved
let response = NextResponse.next()
// { name: 'show-banner', value: 'false', Path: '/home' }
response.cookies.get('show-banner')
```

### `getAll()`

Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the response.
쿠키 이름이 주어질 때 쿠키 이름에 맞는 모든 쿠키 값을 반환합니다. 쿠키 이름이 주어지지 않으면 response에 포함된 모든 쿠키를 전부 반환합니다.
Copy link
Contributor

@JoChaeWoo JoChaeWoo Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~할 때 ~하면을 혼용하는 것에 대해서 개인적으로 통일하는 것이 좋다고 생각하는데 번역 기여자님을 포함한 다른 분들의 의견을 묻고 싶습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 문구 통일에 동의합니다!

저는 개인적으로 하면으로의 통일이 좋아 보입니다. 다른 의견이 없다면 통일하는 것으로 수정하겠습니다.

혹시 문구의 통일은 본 문서에만 해당되는 사항인지 전체적으로 적용되어야 할 사항으로 제시하는 것인지 궁금합니다. 할 때가 의미론적으로 맞을 경우에도 통일성을 위해 하면으로 번역해야 하는 상황이 생길 가능성 때문입니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문서를 보면서 한 생각이라 전체적으로 통일하는 것은 생각 못했네요
할 때가 문맥이 맞는 경우라면 할 때로 번역하는 것이 좋다고 생각합니다. 글을 읽는 흐름을 해치면서 까지 표현을 통일할 필요는 없는 것 같습니다.
어디까지 저의 의견이며 이에 대해서 필요하다면 어려 번역 기여자님들의 의견을 모을 필요가 있다고 생각합니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 위 문장에선 하면으로의 번역이 좀 더 자연스러운 것 같네요 :)

글을 읽는 흐름을 해치지 않는 선에서는 우선 번역을 진행하고 후에 폴더 단위로 검수하는 과정에서 조금씩 맞춰가는 건 어떨까요? 🤔
@chaejunlee @JoChaeWoo


```ts
// Given incoming request /home
// /home이라는 수신 request가 주어질 때
let response = NextResponse.next()
// [
// { name: 'experiments', value: 'new-pricing-page', Path: '/home' },
// { name: 'experiments', value: 'winter-launch', Path: '/home' },
// ]
response.cookies.getAll('experiments')
// Alternatively, get all cookies for the response
// 다른 방법으로 request의 모든 쿠키 가져오기
response.cookies.getAll()
```

### `delete(name)`
### `delete(이름)`

Given a cookie name, delete the cookie from the response.
쿠키 **이름**이 주어질 때 response에서 쿠키를 지웁니다.

```ts
// Given incoming request /home
// /home이라는 수신 request가 주어질 때
let response = NextResponse.next()
// Returns true for deleted, false is nothing is deleted
// 삭제되었다면 true를 반환하고 삭제되지 않았다면 false를 반환
response.cookies.delete('experiments')
```

## `json()`

Produce a response with the given JSON body.
주어진 JSON body로 response를 만듭니다.

```ts filename="app/api/route.ts" switcher
import { NextResponse } from 'next/server'
Expand All @@ -82,59 +82,59 @@ export async function GET(request) {

## `redirect()`

Produce a response that redirects to a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL).
어떤 [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)로 리디렉션하는 response를 생성합니다.

```ts
import { NextResponse } from 'next/server'

return NextResponse.redirect(new URL('/new', request.url))
```

The [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) can be created and modified before being used in the `NextResponse.redirect()` method. For example, you can use the `request.nextUrl` property to get the current URL, and then modify it to redirect to a different URL.
`NextResponse.redirect()` 메서드에서 사용하기 전에 [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)을 생성하고 수정할 수 있습니다. 예를 들어, `request.nextUrl` 속성을 사용해서 현재 URL을 가져온 다음, 현재 URL을 수정해 다른 URL로 리디렉션할 수 있습니다.

```ts
import { NextResponse } from 'next/server'

// Given an incoming request...
// 수신 요청이 주어질 때...
const loginUrl = new URL('/login', request.url)
// Add ?from=/incoming-url to the /login URL
// /login URL에 ?from=/incoming-url을 추가
loginUrl.searchParams.set('from', request.nextUrl.pathname)
// And redirect to the new URL
// 그리고 새 URL로 리디렉션
return NextResponse.redirect(loginUrl)
```

## `rewrite()`

Produce a response that rewrites (proxies) the given [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) while preserving showing the original URL.
원래 URL을 표시(유지)하면서 주어진 [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)을 재작성(프록시)하는 응답을 생성합니다.

```ts
import { NextResponse } from 'next/server'

// Incoming request: /about, browser shows /about
// Rewritten request: /proxy, browser shows /about
// 수신 request: /about, browser shows /about
// 재작성한 request: /proxy, browser shows /about
return NextResponse.rewrite(new URL('/proxy', request.url))
```

## `next()`

The `next()` method is useful for Middleware, as it allows you to return early and continue routing.
`next()` 메서드는 조기 반환(early return)과 연속된 라우팅을 가능하게 해주기 때문에 미들웨어에 유용합니다.

```ts
import { NextResponse } from 'next/server'

return NextResponse.next()
```

You can also forward `headers` when producing the response:
response를 생성할 때 `headers`를 전달할 수 있습니다.

```ts
import { NextResponse } from 'next/server'

// Given an incoming request...
// 수신 request가 주어질 때...
const newHeaders = new Headers(request.headers)
// Add a new header
// 새로운 헤더를 추가
newHeaders.set('x-version', '123')
// And produce a response with the new headers
// 새로운 헤더를 가진 response를 생성
return NextResponse.next({
request: {
// New request headers
Expand Down