1.6.3 • Published 2 months ago

hacocms-js-sdk v1.6.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

1.6.3

2 months ago

1.6.2

2 months ago

1.6.1

3 months ago

1.6.0

6 months ago

1.5.0

7 months ago

1.4.0

1 year ago

1.2.0

2 years ago

1.1.0

2 years ago

1.2.0-1

2 years ago

1.2.0-0

2 years ago

1.2.0-3

2 years ago

1.2.0-2

2 years ago

1.2.0-5

2 years ago

1.2.0-4

2 years ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.2.0-6

2 years ago

1.0.0

2 years ago

0.2.0

2 years ago

0.2.0-0

2 years ago

0.1.0

2 years ago