6
1

TypeScript で kintone のフィールドを取り扱いやすくする

Last updated at Posted at 2023-12-01

kintone プラグインを作る場合はアプリのフィールド情報が必要になるケースがあります。

文字列1行フィールドやスペースフィールドを取得する場合は複数の API を実行しなければなりません。
また、テーブル内フィールドやグループ内フィールドの判定には一手間必要になります。

そこで、TypeScript で kintone のフィールドを取り扱いやすくするライブラリを作りました。

作ったもの

kintone-pretty-fields という名前で npm に公開しました。
https://www.npmjs.com/package/kintone-pretty-fields

できること

1. kintone のフィールドとスペースフィールドをまとめて取得

import { KintoneRestAPIClient } from "@kintone/rest-api-client";
import { kintonePrettyFields } from "kintone-pretty-fields";

const client = new KintoneRestAPIClient();
const { fields, spacers } = await kintonePrettyFields.get({ client, app: 1, lang: "en", preview: false });
console.log({ fields, spacers });

2. フィールドの並び順をソート

「1」で取得したフィールドは、フォーム設定のフィールドの並び順になっています。(画面の左上が先で、右下が後)

フィールド数が 100 を超えるアプリは少なくありません。
フィールドを検索できるようになっているのがベストですが、そうではない場合少し役に立ちます。

3. テーブル内フィールドの判定

table プロパティが存在する場合はテーブル内フィールドです。

{
  type: 'SINGLE_LINE_TEXT',
  code: '文字列__1行_',
  label: '文字列 (1行)',
  noLabel: false,
  required: false,
  minLength: '',
  maxLength: '',
  expression: '',
  hideExpression: false,
  unique: false,
  defaultValue: '',
  table: 'テーブル' // table field code
}

4. グループ内フィールド

group プロパティが存在する場合はグループ内フィールドです。

  {
    type: 'SINGLE_LINE_TEXT',
    code: '文字列__1行_',
    label: '文字列 (1行)',
    noLabel: false,
    required: false,
    minLength: '',
    maxLength: '',
    expression: '',
    hideExpression: false,
    unique: false,
    defaultValue: '',
    group: 'グループ' // group field code
  }

5. ルックアップのコピー先フィールドの判定

isLookupCopy プロパティが true の場合はルックアップのコピー先フィールドです。

{
  type: 'SINGLE_LINE_TEXT',
  code: '文字列__1行_',
  label: '文字列 (1行)',
  noLabel: false,
  required: false,
  minLength: '',
  maxLength: '',
  expression: '',
  hideExpression: false,
  unique: false,
  defaultValue: '',
  isLookupCopy: true // If true, lookup copy destination field
}

6. 選択肢のソート

sortedOptions プロパティに選択肢を index でソートした配列をセットします。

{
  type: 'CHECK_BOX',
  code: 'チェックボックス',
  label: 'チェックボックス',
  noLabel: false,
  required: false,
  options: {
    sample1: { label: 'sample1', index: '0' },
    sample4: { label: 'sample4', index: '3' },
    sample3: { label: 'sample3', index: '2' },
    sample2: { label: 'sample2', index: '1' }
  },
  defaultValue: [],
  align: 'HORIZONTAL',
  sortedOptions: [ 'sample1', 'sample2', 'sample3', 'sample4' ] // sorted options
}

インストール方法

$ npm install kintone-pretty-fields

使い方

import { KintoneRestAPIClient } from "@kintone/rest-api-client";
import { kintonePrettyFields, kintonePrettyType } from "kintone-pretty-fields";

const client = new KintoneRestAPIClient();
const { fields, spacers } = await kintonePrettyFields.get({ client, app: 1, lang: "en", preview: false });
console.log({ fields, spacers });

const checkBoxFields = fields.filter(kintonePrettyFields.isCheckBox);
console.log(checkBoxFields);

const myFunction1 = (fields: kintonePrettyType.OneOf[]) => {
  // do something
};

const myFunction2 = (field: kintonePrettyType.CheckBox) => {
  // do something
};
6
1
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
6
1