Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions adminforth/modules/configValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
AllowedActions,
ShowIn,
ShowInInput,
ShowInLegacyInput,
ShowInModernInput,
} from "../types/Back.js";

import fs from 'fs';
Expand Down Expand Up @@ -299,31 +301,33 @@ export default class ConfigValidator implements IConfigValidator {
return;
}

let showIn = column.showIn || { all: true };
let showIn: ShowInInput = column.showIn || { all: true };

if (column.showIn && Array.isArray(column.showIn)) {
showIn = Object.values(AdminForthResourcePages).reduce((acc, key) => {
return {
...acc,
[key]: column.showIn.includes(key),
[key]: (column.showIn as ShowInLegacyInput).includes(key),
}
}, {} as ShowInInput);
if (warnings.filter((w) => w.includes('showIn should be an object, array is deprecated')).length === 0) {
warnings.push(`Resource "${resInput.resourceId || resInput.table}" column "${column.name}" showIn should be an object, array is deprecated`);
}
}

const showInTransformedToObject: ShowInModernInput = showIn as ShowInModernInput;

// by default copy from 'all' key if present or show on all pages
for (const key of Object.keys(AdminForthResourcePages)) {
if (!Object.keys(showIn).includes(key)) {
showIn[key] = showIn.all !== undefined ? showIn.all : true;
if (!Object.keys(showInTransformedToObject).includes(key)) {
showInTransformedToObject[key] = showInTransformedToObject.all !== undefined ? showInTransformedToObject.all : true;
}
}
if (showIn.all !== undefined) {
delete showIn.all;
if (showInTransformedToObject.all !== undefined) {
delete showInTransformedToObject.all;
}

return showIn as ShowIn;
return showInTransformedToObject as ShowIn;
}

validateAndNormalizeResources(errors: string[], warnings: string[]): AdminForthResource[] {
Expand Down
14 changes: 9 additions & 5 deletions adminforth/types/Back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1324,14 +1324,18 @@ export interface AdminForthForeignResource extends AdminForthForeignResourceComm
},
}

/**
* Object which describes on what pages should column be displayed on.
*/
export type ShowInInput = {
export type ShowInModernInput = {
[key in AdminForthResourcePages]?: AllowedActionValue
} & {
all?: AllowedActionValue;
} & Array<AdminForthResourcePages | keyof typeof AdminForthResourcePages>;
}

export type ShowInLegacyInput = Array<AdminForthResourcePages | keyof typeof AdminForthResourcePages>;

/**
* Object which describes on what pages should column be displayed on.
*/
export type ShowInInput = ShowInModernInput | ShowInLegacyInput;

export type ShowIn = {
[key in AdminForthResourcePages]: AllowedActionValue
Expand Down