# 共有リンク

> Nodaro SDK の client.shares で閲覧専用の共有リンクをオンまたはオフにし、トークンのないクライアントから、リンクが表示する内容をページごとに読み取ります。

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

**`client.shares`** は、自分の結果（ジョブ）1 つ、生成セッション、プロジェクトへの閲覧専用の共有リンクを管理します。リンクには、結果、書かれたとおりのプロンプト、モデル、設定が表示されます。`allowCopy` を指定すると、結果の作成に使ったリファレンスのファイルも表示されます。各メソッドは[共有リンクの REST API](https://nodaro.ai/docs/developers/api/share-links)を呼び出します。

各アイテムのリンクは 1 つです。オフにすると、リンクは `404` を返します。もう一度オンにすると、同じトークンが戻ります。

## メソッド
| メソッド | 役割 |
| --- | --- |
| [`get(params)`](#getparams) | 自分のアイテム 1 つのリンクを読み取ります |
| [`set(input)`](#setinput) | 自分のアイテム 1 つのリンクをオンまたはオフにします |
| [`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)
自分のアイテム 1 つのリンクを読み取ります（`GET /v1/shares`）。アイテムが一度も共有されていない場合は `null` を返します。アイテムが自分のものでない場合は `404` の `NodaroError` を投げます。

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

| プロパティ | 型 | 説明 |
| --- | --- | --- |
| `kind` (必須) | `"job" \| "session" \| "project"` | アイテムの種類です。 |
| `targetId` (必須) | `string` | 自分のジョブ、セッション、プロジェクトの id です。 |

### set(input)
自分のアイテム 1 つのリンクをオンまたはオフにします（`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" です。チャットや SNS に表示されるリンクのプレビューカードは、この言語で書かれます。省略すると、リンクは現在の言語を保ちます。新しいリンクでは英語です。不明な 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 が一覧にするセッションの 1 つです。 |
| `limit` | `number` | `50` | ページのサイズで、1〜100 です。 |
| `cursor` | `string` |  | 前のページの nextCursor です。 |

応答は `{ data: SharedJob[], nextCursor }` です。1 ページの結果が `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 }> }
```

## リンクが表示するもの
リンクが表示するのは、所有者の完成した結果のうち、プラットフォームが誰にでも見せてよいとするものだけです。ゴミ箱にある結果、セッションから外した結果、制限された結果、非公開に保たれている結果、コンテンツフィルターにかかる結果は決して表示されず、ワークスペースのものは何も表示されません。これは読み取りのたびに判定し直されるため、共有したあとにゴミ箱へ移した結果は、すぐに表示されなくなります。

## 共有するアドレス
SNS やチャットでのプレビューには、共有カード `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、targetId にジョブの id、shared に true を指定して client.shares.set を呼びます。返される Share にトークンが含まれます。そのトークンから共有用のアドレスを組み立ててください。

### サインインしていない訪問者も、共有リンクを読み取れますか？

はい。client.shares.getPublic と client.shares.publicItems は、サインアウトした状態でも使えます。auth がトークンを返さないクライアント（たとえば new CallbackAuth(() => null)）を作成し、トークンを指定して呼んでください。

### client.shares.set が share_not_allowed を投げるのは、なぜですか？

そのアイテムは今は共有できません。エラーは reason を持つ 422 の NodaroError で、reason は not_completed、no_media、workspace などです。リンクをオフにする操作は常に成功します。

### リンクをオフにしてもう一度オンにすると、トークンは変わりますか？

いいえ。各アイテムのリンクは 1 つです。オフの間、リンクは 404 を返します。もう一度オンにすると、同じトークンがまた使えます。
