⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
import { HTTPException } from "hono/http-exception";
import { assertValidDefinition } from "./assert-valid-definition";
import {
convertChoiceValue,
isChoiceType,
normalizeOptions,
readHiddenOptions,
readOptions,
} from "./choice-options";
export type CurrentCustomField = {
name: string;
type: string;
required: boolean;
defaultValue: string | null;
options: unknown;
hiddenOptions: unknown;
};
export type CustomFieldUpdateInput = {
name?: string;
type?: string;
required?: boolean;
defaultValue?: string | null;
options?: string[];
hiddenOptions?: string[];
};
export type ResolvedFieldUpdate = {
name: string;
type: string;
required: boolean;
defaultValue: string | null;
options: unknown;
hiddenOptions: string[];
removedOptions: string[];
};
function badRequest(message: string): never {
throw new HTTPException(400, { message });
}
function resolveType(field: CurrentCustomField, requested?: string) {
const type = requested ?? field.type;
if (type === field.type) return type;
if (isChoiceType(type) && isChoiceType(field.type)) return type;
badRequest(
"The type of a custom field can't change. Only dropdown and multiselect fields can switch between each other.",
);
}
function resolveDefault(
field: CurrentCustomField,
type: string,
requested: string | null | undefined,
) {
if (requested !== undefined) {
if (requested === null || requested.trim() === "") return null;
return type === "date" ? requested.trim() : requested;
}
if (
field.defaultValue === null ||
type === field.type ||
!isChoiceType(type) ||
!isChoiceType(field.type)
) {
return field.defaultValue;
}
const converted = convertChoiceValue(field.defaultValue, field.type, type);
if (converted === undefined) {
badRequest(
"The default has more than one option picked. Send a default with one option.",
);
}
return converted || null;
}
export function resolveFieldUpdate(
field: CurrentCustomField,
input: CustomFieldUpdateInput,
): ResolvedFieldUpdate {
const type = resolveType(field, input.type);
const name = input.name === undefined ? field.name : input.name.trim();
if (!name) badRequest("Name cannot be empty");
if (
(input.options !== undefined || input.hiddenOptions !== undefined) &&
!isChoiceType(type)
) {
badRequest("Only dropdown and multiselect fields have options");
}
const previousOptions = readOptions(field.options);
const options = isChoiceType(type)
? input.options === undefined
? previousOptions
: normalizeOptions(input.options)
: null;
// Without new hidden options, keep the old ones that are still options.
const hiddenOptions = !options
? []
: input.hiddenOptions === undefined
? readHiddenOptions(field.hiddenOptions, options)
: normalizeOptions(input.hiddenOptions);
const required = input.required ?? field.required;
const defaultValue = resolveDefault(field, type, input.defaultValue);
assertValidDefinition({
name,
type,
required,
defaultValue,
options,
hiddenOptions,
});
return {
name,
type,
required,
defaultValue,
options: options ?? field.options,
hiddenOptions,
removedOptions: options
? previousOptions.filter((option) => !options.includes(option))
: [],
};
}