hacocms-js-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.6.3 • Public • Published

hacoCMS JavaScript/TypeScript SDK

build, test, lint npm version License: MIT

hacoCMS の JavaScript/TypeScript 用 API クライアントライブラリです。

インストール

npm install hacocms-js-sdk

# or

yarn add hacocms-js-sdk

使い方

import {
  HacoCmsClient, // API クライアント
  ApiContent, // API スキーマの基底クラス
  SortQuery, // ソートクエリビルダ
} from 'hacocms-js-sdk'

const client = new HacoCmsClient(
  'https://{プロジェクトのサブドメイン}.hacocms.com', // API のベース URL
  'ACCESS_TOKEN', // プロジェクトの Access-Token
  'PROJECT_DRAFT_TOKEN', // オプション:プロジェクトの Project-Draft-Token
)

// API スキーマの定義(例)
class ExampleContent extends ApiContent {
  // API レスポンスの JSON からコンテンツオブジェクトを生成するコンストラクタ
  constructor(json) {
    super(json) // API 共通のフィールド(id, createdAt など)が初期化されます。

    this.title = json.title
    this.body = json.body
  }
}

// コンテンツ一覧を取得
const res = await client.getList(ExampleContent, '/example', {
  s: SortQuery.build(['createdAt', 'desc']), // 作成日時の降順
})
for (const content of res.data) {
  console.log(content.title)
}

// 特定のコンテンツを取得
const contentId = 'CONTENT_ID' // コンテンツ ID
const content = await client.getContent(ExampleContent, '/example', contentId)
console.log(content.title)

// シングル形式の API コンテンツを取得
const single = await client.getSingle(ExampleContent, '/single')
console.log(single.title)

// 下書きを含めたコンテンツ一覧を取得
// ※ HacoCmsClient のコンストラクタに Project-Draft-Token を渡しておく必要があります。
const all = await client.getListIncludingDraft(ExampleContent, '/example', {
  s: SortQuery.build('-updatedAt'), // 更新日時の降順
})
for (const content of all.data) {
  console.log(content.title)
}

// 下書きのコンテンツを取得
const draftToken = 'DRAFT_TOKEN' // 取得するコンテンツの Draft-Token
const draft = await client.getContent(ExampleContent, '/example', contentId, draftToken)
console.log(draft.title)

ライセンス

MIT License

Readme

Keywords

none

Package Sidebar

Install

npm i hacocms-js-sdk

Weekly Downloads

551

Version

1.6.3

License

MIT

Unpacked Size

41.2 kB

Total Files

27

Last publish

Collaborators

  • j-hayashi_seesaa