1
0

More than 3 years have passed since last update.

kintone-rest-api-clientでゲストスペースを動的に判定してみる

Posted at

はじめに

kintone-rest-api-clientはとても便利ですが、kintone.api.url()を使うような感覚で、
動的にゲストスペースかどうかの判定が今の所できません。
自社用のカスタマイズであれば問題ないですが、配布を目的としたプラグインなどの場合に少々問題があります・・・。
なので動的に判定できるようにしてみます。

実装

早速実装していきます。
今回はレコード一括取得を例に進めていきます。

    function getAllRecords(){
        return new Promise(async (resolve, reject)=>{
            const guestSpaceId = await checkGuestSpace().catch(err=>{showErrorMsg(err)})
            const prop = guestSpaceId ? {guestSpaceId} : {};
            let client = new KintoneRestAPIClient(prop)

            const params = {
              app: kintone.app.getId(),
              query: kintone.app.getQueryCondition()
            };

            client.record.getAllRecordsWithCursor(params).then(function(resp) {
              console.log(resp);
              resolve(resp)
            }).catch(function(err) {
              console.log(err);
              reject(err)
            });
        })
    }

    async function checkGuestSpace(){
        const currentUrl = kintone.api.url('/k/v1/app', true);
        if(currentUrl.indexOf('guest') > -1){
            const appId = kintone.app.getId();
            const guestSpaceId = await getGuestSpaceId(appId)
            return guestSpaceId;
        } else {
            return false;
        }
    }

    async function getGuestSpaceId(appId){
        const appInfo = await getAppInfo(appId)
        const guestSpaceId = appInfo.spaceId
        return guestSpaceId;
    }

    function getAppInfo(appId){
        return new Promise ((resolve, reject)=>{
            const body = {
              'id': appId
            };
            kintone.api(kintone.api.url('/k/v1/app', true), 'GET', body, function(resp) {
              resolve(resp)
            }, function(error) {
              reject(error)
            });
        })
    }

ゲストスペースかどうかを判定する

kintone.api.url()を使って、適当なAPIを叩いてURLを取します。
「guest」が含まれているかどうかを判定基準としています。

const currentUrl = kintone.api.url('/k/v1/app', true);
if(currentUrl.indexOf('guest') > -1){

ゲストスペースであった場合は、ゲストスペースIDを取得します
(kintone-rest-api-clientでは、ゲストスペースで使用する場合ゲストスペースIDを指定する必要があります)
アプリ情報取得APIでアプリ情報を取得し、ゲストスペースIDを取り出します。

ゲストスペースIDが取得できたらguestSpaceIdプロパティを設定、なければ何もしないといった感じです。

const prop = guestSpaceId ? {guestSpaceId} : {};

最後に

とりあえずぱぱっと書きましたが、レコード一括取得以外でも使えるように工夫が必要ですね。
コードの書き方などご指摘大歓迎ですm(_ _)m

1
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
1
0