refactor: enhance configuration handling and error checking for template fields in the Word output plugin

This commit is contained in:
2025-10-02 13:26:13 +09:00
parent b1540e8374
commit 99251b4748
9 changed files with 193 additions and 70 deletions

View File

@@ -0,0 +1,92 @@
import { KintoneRestAPIClient } from '@kintone/rest-api-client';
import i18n from 'i18next';
import {
type PluginConfig,
type PluginConfigApp,
type PluginConfigSelf,
PluginConfigType,
type PluginConfigUrl,
} from '../common/config';
import { DOCX_CONTENT_TYPE } from '../common/constants';
const checkConfigSelf = (config: PluginConfigSelf, record: kintone.types.SavedFields): string => {
const { template } = config;
if (template === '') {
throw new Error(i18n.t('errors.template-field-is-required'));
}
const field = record[template];
if (!field) {
throw new Error(i18n.t('errors.template-field-is-not-available'));
}
if (field.type !== 'FILE') {
throw new Error(i18n.t('errors.template-field-must-be-an-attachment-field'));
}
const files = Array.isArray(field.value) ? (field.value as kintone.fieldTypes.File['value']) : [];
if (files.length === 0) {
throw new Error(i18n.t('errors.template-field-does-not-contain-any-files'));
}
if (files.length > 1) {
throw new Error(i18n.t('errors.template-field-contains-multiple-files'));
}
if (files[0].contentType !== DOCX_CONTENT_TYPE) {
throw new Error(i18n.t('errors.template-file-must-be-a-docx', { contentType: files[0].contentType }));
}
return files[0].fileKey;
};
const checkConfigApp = async (config: PluginConfigApp): Promise<string> => {
const { appId, template, recordId } = config;
if (appId === '') {
throw new Error(i18n.t('errors.app-id-is-required'));
}
if (recordId === '') {
throw new Error(i18n.t('errors.record-id-is-required'));
}
if (template === '') {
throw new Error(i18n.t('errors.template-field-is-required'));
}
const client = new KintoneRestAPIClient();
const { record } = await client.record.getRecord({ app: appId, id: recordId }).catch((_error) => {
throw new Error(i18n.t('errors.record-is-not-available'));
});
const field = record[template];
if (!field) {
throw new Error(i18n.t('errors.template-field-is-not-available'));
}
if (field.type !== 'FILE') {
throw new Error(i18n.t('errors.template-field-must-be-an-attachment-field'));
}
const files = Array.isArray(field.value) ? (field.value as kintone.fieldTypes.File['value']) : [];
if (files.length === 0) {
throw new Error(i18n.t('errors.template-field-does-not-contain-any-files'));
}
if (files.length > 1) {
throw new Error(i18n.t('errors.template-field-contains-multiple-files'));
}
if (files[0].contentType !== DOCX_CONTENT_TYPE) {
throw new Error(i18n.t('errors.template-file-must-be-a-docx', { contentType: files[0].contentType }));
}
return files[0].fileKey;
};
const checkConfigUrl = (config: PluginConfigUrl): string => {
const { template } = config;
if (template === '') {
throw new Error(i18n.t('errors.template-field-is-required'));
}
if (!template.startsWith('http://') && !template.startsWith('https://')) {
throw new Error(i18n.t('errors.template-field-must-be-a-valid-url'));
}
return template;
};
export const checkConfig = async (config: PluginConfig, record: kintone.types.SavedFields): Promise<string> => {
if (config.type === PluginConfigType.SELF) {
return checkConfigSelf(config, record);
} else if (config.type === PluginConfigType.APP) {
return checkConfigApp(config);
} else if (config.type === PluginConfigType.URL) {
return checkConfigUrl(config);
}
throw new Error(i18n.t('errors.unexpected-error-occurred'));
};