0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GROWI TypeScript/JavaScript SDKの紹介(その3:ユーザーグループの操作)

Posted at

オープンソースの社内WikiであるGROWIではREST APIが公開されています。このAPIをより簡単に使えるよう、TypeScript/JavaScript SDKを開発しています。まだ途中ですが、徐々に機能追加していきます。

今回は権限管理などに利用する、ユーザーグループの操作方法を解説します。

注意点

こちらはコミュニティSDKになります。公式への問い合わせはご遠慮ください。

ソースコード

GROWI TypeScript/JavaScript SDKのソースコードはGitHubにて公開しています。ライセンスはMITです。

goofmint/growi-sdk-alpha

インストール

インストールはnpm/yarn経由で行います。

$ npm install @goofmint/growi-js
# または
$ yarn add @goofmint/growi-js

使い方

まずはSDKをインポートします。

const { GROWI, UserGroup } = require('@goofmint/growi-js');
// または
import { GROWI, UserGroup } from '@goofmint/growi-js';

そして、初期化します。この時、GROWIのAPIトークンを指定します。

const growi = new GROWI({ apiToken: 'your-api-token' });

この他、以下のパラメーターを用意しています。

  • url : GROWIのURLを指定します。デフォルトはhttp://localhost:3000です。
  • path : GROWIのページパスを指定します。デフォルトは `` です。サブディレクトリにインストールしている際に指定します。

ユーザーグループの作成

ユーザーグループの作成時には、名前と説明を加えて save メソッドを実行します。

const group = new UserGroup();
group.name = 'Test';
group.description = 'グループの説明';
const bol = await group.save();

ユーザーグループの取得

1件ごとの取得の場合は、 get メソッドを実行します。 id が必須です。

group.id = '999999';
await group.get();
group.name; // Test

グループの一覧取得

グループの一覧取得は、 groups メソッドを実行します。

const { groups } = await growi.groups();

子グループの取得

あるグループ以下にある子グループを取得する場合は、 children メソッドを実行します。

const children = await group.children();

さらに子グループを取得する

子グループの下にあるグループを取得する場合は、引数に true を指定します。その後、 grandChildren プロパティで取得できます。

const children = await group.children(true);
const { grandChildren } = group;

親グループの取得

あるグループの親グループを取得する場合は、 ancestors メソッドを実行します。

const ancestors = await group.ancestors();

子グループの作成

子グループを作成する際には、 parent プロパティに親グループを指定してください。

const childGroup = new UserGroup;
childGroup.name = 'Child Group';
childGroup.description = '子グループの説明';
childGroup.parent = group;
await childGroup.save();

グループの更新

グループの更新は、 save メソッドを実行します。

group.name = 'Test2';
group.description = 'グループの説明2';
const bol = await group.save();

グループの削除

グループの削除は、 delete メソッドを実行します。

const bol = await group.delete();

まとめ

GROWIは社内Wikiとして、社内システムとのデータ連係ニーズが出やすいでしょう。ぜひ、Node.js SDKを使って、GROWIをシステムから操作してください。

OSS開発wikiツールのGROWI | 快適な情報共有を、全ての人へ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?