// NIHO ホストスクリプトの型定義(host-script-sdk-v0.4)。NIHO が生成します。直接編集しないでください。 // // 使い方(scripts//index.js の先頭): // // @ts-check // /// // /** @param {Niho.Env} env */ // export default async function (env) { ... } declare namespace Niho { /** script.json の permissions に宣言できる権限。公開する版ごとに宣言し、公開前に人が確認します。 */ type Permission = "devices.execute" | "fetch" | "storage" | "drive" | "bookings.read" | "gcal.format"; /** JSON として保存・往復できる値。 */ type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; /** * 設定画面で入力された値(script.json の configSchema で宣言した項目)。全体で 8KiB まで。 * 宣言した既定値(default)は、設定画面で保存されない限り入りません。未設定の項目は undefined として扱ってください。 */ interface Config { readonly [key: string]: string | number | boolean | undefined; } interface StorageListOptions { /** この文字列で始まる key だけ。 */ prefix?: string; /** 前回の結果の nextCursor をそのまま渡すと、その続きから返します。 */ cursor?: string; /** 1〜100。省略は 100。 */ limit?: number; } interface StorageListResult { /** key の文字コード順。 */ keys: { key: string; updatedAt: string }[]; /** 続きがあるときの cursor。無ければ null。 */ nextCursor: string | null; } /** * 拠点×スクリプトに閉じた小さな永続保存領域(GAS の PropertiesService に近いもの)。 * 処理位置・対応表・少量の設定を置きます。会員台帳や予約など、正本が NIHO にあるデータの置き場にはしません。 * key は 1〜200 文字(制御文字不可)、値は JSON で 8KiB まで、全体で 500 key まで。 * permissions に "storage" を宣言します。宣言を必須にする運用では、無いと失敗します。 */ interface Storage { /** 保存した値。無ければ null。 */ get(key: string): Promise; /** JSON 値を上書き保存します。undefined は保存できません(削除は delete)。 */ set(key: string, value: JsonValue): Promise<{ key: string; saved: true; updatedAt: string }>; delete(key: string): Promise<{ key: string; deleted: boolean }>; list(options?: StorageListOptions): Promise; } type BookingSource = "NIHOAPP" | "EXTERNAL_GCAL"; /** * 拠点の予定 1 件。利用者の連絡先・決済・参加者などは含みません。 * 注意: title / description には、Google カレンダーから取り込んだ外部利用者の名前が入り得ます。 */ interface Booking { id: string; revision: number; source: BookingSource; externalProvider: "INSTABASE" | "SPACEMARKET" | null; externalCalendarId: string | null; externalEventId: string | null; bookingType: "EVENT" | "PERSONAL"; spaceScope: "ENTIRE_FACILITY" | "DESIGNATED_AREA"; reservationStatus: "PROVISIONAL" | "CONFIRMED" | "REJECTED"; title: string | null; description: string | null; /** ISO 8601(UTC・ミリ秒つき)。 */ startAt: string; endAt: string; useZone: string[]; headCount: number; externalHeadCount: number | null; seriesId: string | null; createdAt: string; updatedAt: string; /** 取り消された予定か(includeDeleted: true のときだけ返ります)。 */ deleted: boolean; deletedAt: string | null; } interface BookingsListOptions { /** 期間の開始。タイムゾーン付きの ISO 8601(例: 2026-11-15T00:00:00+09:00)。 */ from: string; /** 期間の終了(from より後)。from から 93 日以内。 */ to: string; source?: BookingSource; /** 取り消された予定も返す。取消への追随に使います。 */ includeDeleted?: boolean; /** この時刻より後に変更された予定だけ。前回処理位置からの差分読みに使います。 */ updatedSince?: string; /** 前回の結果の nextCursor をそのまま渡すと、その続きから返します。 */ cursor?: string; /** 1〜100。省略は 100。 */ limit?: number; } interface BookingsListResult { /** from〜to と重なる予定(期間の前から続く予定も含む)。開始時刻・id の順。 */ bookings: Booking[]; nextCursor: string | null; } /** * 拠点の予定を読む。permissions に "bookings.read" の宣言が必要です(無いと失敗します)。 * 題名・説明に外部利用者の名前が入り得るため、読んだ内容をログ・storage・戻り値へそのまま書かないでください。 */ interface Bookings { list(options: BookingsListOptions): Promise; /** 取消済みも返します。無ければ null。id は最大 64 文字。 */ get(bookingId: string): Promise; } type DeviceDomain = "MUSIC" | "FACILITY" | "LIGHT" | "PLUG" | "SCENE" | "ACCESS"; interface DeviceExecuteInput { domain: DeviceDomain; /** 機器の操作名(前後の空白は除かれ、大文字にそろえます)。最大 80 文字。 */ actionKey: string; /** actionKey が AC_CONTROL(domain は FACILITY)のときだけ必須。それ以外では指定できません。 */ acControl?: { /** 16〜30 */ temperature: number; mode: "auto" | "cool" | "dry" | "fan" | "heat"; fanSpeed: "auto" | "low" | "medium" | "high"; }; actionParams?: Record; } interface DeviceExecuteResult { ok: true; domain: DeviceDomain; actionKey: string; provider: string; targetDeviceId: string | null; targetDeviceName: string | null; remoteType: string | null; hubDeviceId: string | null; command: string | null; commandType: string | null; commandId: string | null; [key: string]: unknown; } /** * 拠点の機器を操作する。鍵(domain: "ACCESS")は使えません。 * permissions に "devices.execute" を宣言します。宣言を必須にする運用では、無いと失敗します。 * 機器ごとの安全・クォータ・監査は本番側で適用されます(試験器は再現しません)。 */ interface Devices { execute(input: DeviceExecuteInput): Promise; } type DriveCell = string | number | boolean | null; /** 書き込みの結果の共通部分。同じ operationKey・同じ内容の再呼び出しは replayed: true で前回の結果を返します。 */ interface DriveWriteReplay { replayed: boolean; truncated?: true; } interface DriveReadResult { operation: "read"; range: string; /** 文字列にそろえた値(空は "")。最大 200 行 × 50 列。 */ values: string[][]; truncated: boolean; } interface DriveAppendResult extends DriveWriteReplay { operation: "append"; updatedRange: string | null; updatedRows: number; updatedCells: number; } interface DriveUpdateResult extends DriveWriteReplay { operation: "update"; updatedRange: string | null; updatedRows: number; updatedCells: number; } interface DriveClearResult extends DriveWriteReplay { operation: "clear"; clearedRange: string; } interface DriveReadDocumentResult { operation: "readDocument"; /** 読んだ版。insertText / deleteText の requiredRevisionId に渡します。 */ revisionId: string | null; /** 整形した本文(行の前後の空白と空行を除く)。位置(index)は元の文書の位置で、この text の位置とは一致しません。 */ text: string; truncated: boolean; } interface DriveDocumentWriteResult extends DriveWriteReplay { operation: "appendText" | "insertText" | "replaceAllText" | "deleteText"; /** replaceAllText で置き換えた件数。ほかの操作では null。 */ occurrencesChanged: number | null; } /** * Studio の契機(STUDIO_EVENT)で連携した Google スプレッドシート・ドキュメントを読み書きする。 * permissions に "drive" を宣言します。宣言を必須にする運用では、無いと失敗します。 * 定期実行(SCHEDULE)では使えません。ファイルが連携されていない fixture でも失敗します。 * 書き込みの operationKey(1〜120 文字)は 1 回の実行の中で一意にします。range は A1:C100 のような有限の範囲(append だけ A:C 可)。 * 読む 200 行 × 50 列、書く 100 行 × 50 列、1 セル 2000 文字、文書の書き込み 12000 文字まで。 */ interface Drive { read(range: string): Promise; append(operationKey: string, range: string, values: DriveCell[][]): Promise; update(operationKey: string, range: string, values: DriveCell[][]): Promise; clear(operationKey: string, range: string): Promise; readDocument(): Promise; appendText(operationKey: string, text: string, requiredRevisionId?: string): Promise; insertText(operationKey: string, index: number, text: string, requiredRevisionId: string): Promise; replaceAllText(operationKey: string, searchText: string, replaceText: string, matchCase?: boolean, requiredRevisionId?: string): Promise; deleteText(operationKey: string, startIndex: number, endIndex: number, requiredRevisionId: string): Promise; } /** NIHO が管理する外部通信の結果。応答の本文は返りません。 */ interface FetchResult { ok: boolean; status: number; } /** ユーザー関数へ渡る env(SCHEDULE と STUDIO_EVENT)。GCAL_FORMAT には渡りません。 */ interface Env { readonly config: Config; readonly storage: Storage; readonly bookings: Bookings; readonly devices: Devices; readonly drive: Drive; /** * 外部への POST(本文は文字列だけ・32KiB まで)。HTTPS の公開アドレスだけ。応答の本文・ヘッダは返りません。 * permissions に "fetch" を宣言します。宣言を必須にする運用では、無いと失敗します。 * グローバルの fetch は使えません(ソース検査が止めます)。 */ fetch(url: string, init?: { body?: string }): Promise; } interface StudioEventMember { spotMemberId: string; userId: string; email: string | null; fullName: string | null; displayName: string | null; } /** Studio の契機の入力(STUDIO_EVENT)。全体で 48KiB まで。 */ interface StudioEventInput { version: 1; event: { /** 例: "membership.member.created" */ type: string; occurredAt: string; subjectType: string; /** 契機ごとの内容。会員が対象の契機では member(氏名・メールを含む)が入ります。 */ data: { member?: StudioEventMember; [key: string]: unknown }; }; drive: { fileHandle: "driveFile"; kind: "SPREADSHEET" | "DOCUMENT" }; } interface GcalFormatJstDateTime { /** 例: 2026-11-18 */ date: string; /** 例: 16:30 */ time: string; /** 例: 水 */ weekday: string; } interface GcalFormatOption { label: string; quantity: number; } interface GcalFormatAnswer { label: string; value: string; } /** * Google 予定の出力(GCAL_FORMAT)へ渡る入力(version 1)。permissions に "gcal.format" の宣言が必要です。 * 氏名・連絡先・申込の回答を含みます。出力に何を書くかは拠点の責任です。全体で 48KiB まで。 * 長い文字列は 2000 文字、氏名などは 200 文字、回答 50 件・参加者 200 件で切り詰め、切り詰めた場所を truncated に記します。 */ interface GcalFormatInput { version: 1; spot: { id: string; name: string; slug: string | null; timeZone: "Asia/Tokyo" }; booking: { id: string; revision: number; bookingType: "EVENT" | "PERSONAL"; spaceScope: "ENTIRE_FACILITY" | "DESIGNATED_AREA"; zones: { key: string; label: string }[]; placeLabel: string; startAt: string; endAt: string; start: GcalFormatJstDateTime; end: GcalFormatJstDateTime; headCount: number; title: string | null; memo: string | null; staffMemo: string | null; reservationStatus: "PROVISIONAL" | "CONFIRMED" | "REJECTED"; isProvisional: boolean; /** 外部の申込に返事をする前(申込が承認待ち)。 */ awaitingReply: boolean; provisionalExpiresAt: string | null; organizer: { type: "SPOT" | "MEMBER" | "EXTERNAL"; name: string }; creatorName: string; options: GcalFormatOption[]; payment: { status: string | null; billingLabel: string | null; totalAmount: number | null; manualPaymentMethod: string | null; }; createdAt: string; updatedAt: string; hostUrl: string | null; }; /** 予約した人。無いときは null。 */ applicant: { kind: "DROPIN" | "RENTAL" | "VISITOR" | "EXTERNAL_ORGANIZER" | "MEMBER"; fullName: string | null; fullNameKana: string | null; organization: string | null; email: string | null; phone: string | null; } | null; /** 申込。無いときは null。 */ request: { kind: "DROPIN" | "RENTAL" | "MEMBER_RENTAL"; isTrialVisit: boolean; purpose: string | null; note: string | null; headCount: number | null; options: GcalFormatOption[]; answers: GcalFormatAnswer[]; totalAmount: number | null; status: string; } | null; /** 会員の予約のとき。無いときは null。 */ member: { userId: string; displayName: string | null; fullName: string | null; email: string | null; phone: string | null; spotContact: { fullName: string | null; email: string | null; phone: string | null }; } | null; participants: { name: string; kind: "MEMBER" | "GUEST"; status: "CONFIRMED" | "CANCELLED"; fullName: string | null; email: string | null; phone: string | null; }[]; /** 今の設定で NIHO が書く標準の題名・説明・色。手を加えるだけの書き方ができます。 */ defaults: { summary: string; description: string; colorId: string | null }; /** 切り詰めた項目の場所(例: booking.memo)。無ければ空。 */ truncated: string[]; } /** * Google 予定の出力。未知のキー・型の違い・上限の超過は失敗します(本番と同じ)。 * summary は必須(空不可・1024 文字まで)。 */ interface GcalFormatOutput { summary: string; /** 8192 文字まで。省略は空。 */ description?: string; /** 1024 文字まで。省略は空。 */ location?: string; /** 省略は標準の色。 */ colorId?: "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "11"; } /** trigger.kind が SCHEDULE: (env) で呼ばれます。戻り値は実行ログへ残ります(16KiB まで)。 */ type ScheduleScript = (env: Env) => unknown | Promise; /** * trigger.kind が STUDIO_EVENT: (input, env) で呼ばれます。 * script.json に outputSchema を宣言したら、戻り値はその形・選択肢に一致する必要があります(8KiB まで)。 */ type StudioEventScript = (input: StudioEventInput, env: Env) => unknown | Promise; /** trigger.kind が GCAL_FORMAT: (input) で呼ばれます。env は渡りません。10 秒以内に返してください。 */ type GcalFormatScript = (input: GcalFormatInput) => GcalFormatOutput | Promise; }