# 공유 링크

> Nodaro SDK의 client.shares로 보기 전용 공유 링크를 켜고 끄며, 토큰이 없는 클라이언트에서 링크에 보이는 내용을 페이지 단위로 읽는 방법입니다.

Source: https://nodaro.ai/ko/docs/developers/sdk/share-links

**`client.shares`**는 내 결과 하나(작업), 생성 세션 또는 프로젝트로 가는 보기 전용 공유 링크를 관리합니다. 링크에는 결과, 작성한 그대로의 프롬프트, 모델, 설정이 보입니다. `allowCopy`를 켜면 결과를 만드는 데 쓴 레퍼런스 파일도 보입니다. 이 메서드들은 [공유 링크 REST API](https://nodaro.ai/docs/developers/api/share-links)를 호출합니다.

항목마다 링크는 하나입니다. 끄면 링크는 `404`로 응답합니다. 다시 켜면 같은 토큰이 돌아옵니다.

## 메서드
| 메서드 | 하는 일 |
| --- | --- |
| [`get(params)`](#getparams) | 내 항목 하나의 링크를 읽습니다 |
| [`set(input)`](#setinput) | 내 항목 하나의 링크를 켜거나 끕니다 |
| [`getPublic(token)`](#getpublictoken) | 로그인 여부와 관계없이 링크에 보이는 내용을 읽습니다 |
| [`publicItems(token, params?)`](#publicitemstoken-params) | 로그인 여부와 관계없이 공유된 세션이나 프로젝트의 결과를 나열합니다 |

## client.shares
### 결과를 링크로 공유
```ts
import { createClient, CallbackAuth } from "@nodaro/sdk"

// locale: the interface language the item is shared in. The link's preview card is written in it.
const share = await client.shares.set({ kind: "job", targetId: jobId, shared: true, allowCopy: false, locale: "de" })

// A page opened by someone who is not signed in reads the link with a client that has no token.
const guest = createClient({ baseUrl: "https://app.nodaro.ai", auth: new CallbackAuth(() => null) })
const { job } = await guest.shares.getPublic(share.token)
console.log(job?.outputUrl, job?.prompt, job?.model?.name, job?.settings)

// Off: the link answers 404. On again later: the same token works again.
await client.shares.set({ kind: "job", targetId: jobId, shared: false })
```

### get(params)
내 항목 하나의 링크를 읽습니다(`GET /v1/shares`). 항목을 한 번도 공유하지 않았으면 `null`을 반환합니다. 항목이 내 것이 아니면 `404` `NodaroError`를 던집니다.

```ts
get(params: GetShareParams): Promise<Share | null>
```

| 속성 | 타입 | 설명 |
| --- | --- | --- |
| `kind` (필수) | `"job" \| "session" \| "project"` | 항목의 종류입니다. |
| `targetId` (필수) | `string` | 내 작업, 세션 또는 프로젝트의 ID입니다. |

### set(input)
내 항목 하나의 링크를 켜거나 끕니다(`PUT /v1/shares`). 첫 호출이 링크를 만듭니다. 이후 호출은 그 토큰을 유지합니다.

```ts
set(input: SetShareInput): Promise<Share>
```

| 속성 | 타입 | 설명 |
| --- | --- | --- |
| `kind` (필수) | `"job" \| "session" \| "project"` | 항목의 종류입니다. |
| `targetId` (필수) | `string` | 내 작업, 세션 또는 프로젝트의 ID입니다. |
| `shared` (필수) | `boolean` | true는 링크를 켜고, false는 끕니다. |
| `allowCopy` | `boolean` | true이면 레퍼런스 파일도 보입니다. 생략하면 링크의 값이 유지되며, 새 링크에서는 false입니다. |
| `locale` | `string` | 항목을 공유하는 인터페이스 언어입니다. 예를 들어 "de", "pt-BR", "he"입니다. 채팅과 소셜 네트워크에 보이는 링크의 미리보기 카드가 이 언어로 작성됩니다. 생략하면 링크의 언어가 유지되며, 새 링크에서는 영어입니다. 알 수 없는 ID는 무시됩니다. |

지금 공유할 수 없는 항목을 켜면 코드가 `share_not_allowed`이고 `reason`이 담긴 `422` `NodaroError`를 던집니다. `reason`은 `not_completed`, `no_media`, `private_output`, `content_filtered`, `restricted`, `workspace`, `sharing_disabled` 중 하나입니다. 각 이유는 [링크에 절대 보이지 않는 것](https://nodaro.ai/docs/developers/api/share-links#never-shown)에서 설명합니다. 링크 끄기는 휴지통에 있는 작업에서도 항상 작동합니다.

OAuth 앱에는 `assets:write`와 `jobs:read` 범위가 필요합니다.

### getPublic(token)
링크에 보이는 내용을 읽습니다(`GET /v1/public/shares/:token`). 로그인하지 않아도 작동합니다.

```ts
getPublic(token: string): Promise<PublicShare>
```

응답은 `{ kind, allowCopy, job? | session? | project? }`입니다. 알 수 없거나, 꺼졌거나, 항목이 없어졌거나 더 이상 보일 수 없는 링크에는 `404` `NodaroError`를 던집니다.

### publicItems(token, params?)
공유된 세션의 결과를 최신순으로 나열합니다(`GET /v1/public/shares/:token/items`). 로그인하지 않아도 작동합니다.

```ts
publicItems(token: string, params?: PublicShareItemsParams): Promise<PublicShareItemsPage>
```

| 속성 | 타입 | 기본값 | 설명 |
| --- | --- | --- | --- |
| `sessionId` | `string` |  | 공유된 프로젝트에서는 필수입니다. getPublic이 나열하는 세션 중 하나입니다. |
| `limit` | `number` | `50` | 페이지 크기이며, 1~100입니다. |
| `cursor` | `string` |  | 이전 페이지의 nextCursor입니다. |

응답은 `{ data: SharedJob[], nextCursor }`입니다. 한 페이지에 `limit`보다 적은 결과가 들어 있을 수 있습니다. `nextCursor`가 `null`이 될 때까지 따라가세요.

```ts
const shown = await client.shares.getPublic(share.token)
if (shown.kind === "session") {
  const { data: results, nextCursor } = await client.shares.publicItems(share.token)
}
```

## 타입
```ts
type ShareKind = "job" | "session" | "project"

interface Share { token: string; kind: ShareKind; targetId: string; shared: boolean; allowCopy: boolean; createdAt: string; updatedAt: string; locale?: string | null }

interface SharedJob {
  id: string; type: string; mediaKind: "image" | "video" | "audio"
  outputUrl: string; outputUrls?: string[]; width?: number; height?: number; durationSec?: number
  title: string | null; createdAt: string
  songTitle?: string                 // a song's own title, when it has one
  toolName?: string | null           // what a person calls the tool: "Song Mashup", "Text to Speech"
  coverUrl?: string | null           // the result's cover picture, see client.jobs.update
  prompt: string | null              // as written; null for a trend run
  displayPrompt?: string | null      // as a person reads it: @maya:1 → @Maya, {video:1:clip} → @clip
  negativePrompt?: string | null
  model: { id: string; name: string } | null   // the public model name
  settings: Record<string, SharedSettingValue> // never an address or an id
  references?: Array<{ url: string; kind: "image" | "video" | "audio" }> // only with allowCopy
  trend?: { slug: string; name: string }       // a trend run: no prompt or settings
}

interface SharedSession { title: string | null; tool: string | null; createdAt: string; itemCount: number }
interface SharedProject { name: string; sessions: Array<{ id: string; title: string | null; tool: string | null; itemCount: number; coverUrl: string | null }> }
```

## 링크에 보이는 것
링크에는 플랫폼이 누구에게나 보여 줄 수 있는, 소유자의 완료된 결과만 보입니다. 휴지통에 있거나, 세션에서 제거되었거나, 제한되었거나, 비공개로 유지되거나, 콘텐츠 필터에 걸리는 결과는 절대 보이지 않으며, 워크스페이스의 것은 아무것도 보이지 않습니다. 이는 읽을 때마다 다시 판단되므로, 공유한 뒤 휴지통으로 옮긴 결과는 즉시 보이지 않게 됩니다.

## 공유할 주소
소셜 네트워크와 채팅의 미리 보기에는 공유 카드인 `https://nodaro.ai/s/<token>`을 나눠 주세요. 이 주소의 HTML에는 Open Graph와 Twitter 태그가 들어 있으며, 방문자를 링크 내용을 보여 주는 페이지인 `https://nodaro.ai/shared/<token>`으로 보냅니다. [링크 미리 보기용 공유 카드](https://nodaro.ai/docs/developers/api/share-links#share-card)를 참고하세요.

## 자주 묻는 질문

### Nodaro SDK로 결과를 링크로 공유하려면 어떻게 하나요?

kind를 job으로, 작업 ID를 targetId로, shared를 true로 해서 client.shares.set을 호출하세요. 반환된 Share에 토큰이 들어 있습니다. 그 토큰으로 공유 주소를 만드세요.

### 로그인하지 않은 방문자도 공유 링크를 읽을 수 있나요?

네. client.shares.getPublic과 client.shares.publicItems는 로그인하지 않아도 작동합니다. 인증이 토큰을 돌려주지 않는 클라이언트를 만들고(예: new CallbackAuth(() => null)) 토큰으로 호출하세요.

### client.shares.set이 share_not_allowed를 던지는 이유는 무엇인가요?

그 항목을 지금은 공유할 수 없기 때문입니다. 오류는 not_completed, no_media, workspace 같은 reason이 담긴 422 NodaroError입니다. 링크 끄기는 항상 작동합니다.

### 링크를 껐다가 다시 켜면 토큰이 바뀌나요?

아니요. 항목마다 링크는 하나입니다. 끄면 링크는 404로 응답합니다. 다시 켜면 같은 토큰이 다시 작동합니다.
