開発者向けライブラリ

ソース操作

コード上からプロジェクト内のソースを管理できます。

前提条件

インストールのドキュメントの内容が完了しているものとします。

import { createHishoAdmin } from "hisho-admin";

const hisho = createHishoAdmin({
  apiKey: process.env.HISHO_ADMIN_API_KEY!,
});

ソースを作成

テキストソース

const created = await hisho.project(projectId).sources.createText({
  originalName: "refund-policy.txt",
  text: "返金ポリシー本文...",
});

URLソース

const created = await hisho.project(projectId).sources.createUrl({
  originalName: "pricing",
  url: "https://example.com/pricing",
});

ファイルソース

import { readFile } from "node:fs/promises";

const data = await readFile("./manual.pdf");

const source = await hisho.project(projectId).sources.createFile({
  originalName: "manual.pdf",
  contentType: "application/pdf",
  data,
});

ソース一覧を取得

全件取得

const sources = await hisho.project(projectId).sources.listAll();

ページング取得

const { sources, next_cursor, total_count } = await hisho.project(projectId).sources.list({
  limit: 20,
  source_type: "text",
  cursor: nextCursor,
});

ソースを取得

const sourceId = "550e8400-e29b-41d4-a716-446655440001";

const source = await hisho.project(projectId).sources.get(sourceId);

ソースを更新

const sourceId = "550e8400-e29b-41d4-a716-446655440001";

const source = await hisho.project(projectId).sources.patch(sourceId, {
  original_name: "refund-policy-updated.txt",
  raw_text: "更新後の返金ポリシー本文...",
});

ソースを削除

const sourceId = "550e8400-e29b-41d4-a716-446655440001";

await hisho.project(projectId).sources.delete(sourceId);