Skip to content

Commit ac137e5

Browse files
authored
Merge pull request #86 from devforth/fix-showin-type
fix: typescript type for showIn
2 parents 5369d72 + 5b56377 commit ac137e5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

adminforth/modules/configValidator.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
AllowedActions,
1212
ShowIn,
1313
ShowInInput,
14+
ShowInLegacyInput,
15+
ShowInModernInput,
1416
} from "../types/Back.js";
1517

1618
import fs from 'fs';
@@ -299,31 +301,33 @@ export default class ConfigValidator implements IConfigValidator {
299301
return;
300302
}
301303

302-
let showIn = column.showIn || { all: true };
304+
let showIn: ShowInInput = column.showIn || { all: true };
303305

304306
if (column.showIn && Array.isArray(column.showIn)) {
305307
showIn = Object.values(AdminForthResourcePages).reduce((acc, key) => {
306308
return {
307309
...acc,
308-
[key]: column.showIn.includes(key),
310+
[key]: (column.showIn as ShowInLegacyInput).includes(key),
309311
}
310312
}, {} as ShowInInput);
311313
if (warnings.filter((w) => w.includes('showIn should be an object, array is deprecated')).length === 0) {
312314
warnings.push(`Resource "${resInput.resourceId || resInput.table}" column "${column.name}" showIn should be an object, array is deprecated`);
313315
}
314316
}
315317

318+
const showInTransformedToObject: ShowInModernInput = showIn as ShowInModernInput;
319+
316320
// by default copy from 'all' key if present or show on all pages
317321
for (const key of Object.keys(AdminForthResourcePages)) {
318-
if (!Object.keys(showIn).includes(key)) {
319-
showIn[key] = showIn.all !== undefined ? showIn.all : true;
322+
if (!Object.keys(showInTransformedToObject).includes(key)) {
323+
showInTransformedToObject[key] = showInTransformedToObject.all !== undefined ? showInTransformedToObject.all : true;
320324
}
321325
}
322-
if (showIn.all !== undefined) {
323-
delete showIn.all;
326+
if (showInTransformedToObject.all !== undefined) {
327+
delete showInTransformedToObject.all;
324328
}
325329

326-
return showIn as ShowIn;
330+
return showInTransformedToObject as ShowIn;
327331
}
328332

329333
validateAndNormalizeResources(errors: string[], warnings: string[]): AdminForthResource[] {

adminforth/types/Back.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,14 +1324,18 @@ export interface AdminForthForeignResource extends AdminForthForeignResourceComm
13241324
},
13251325
}
13261326

1327-
/**
1328-
* Object which describes on what pages should column be displayed on.
1329-
*/
1330-
export type ShowInInput = {
1327+
export type ShowInModernInput = {
13311328
[key in AdminForthResourcePages]?: AllowedActionValue
13321329
} & {
13331330
all?: AllowedActionValue;
1334-
} & Array<AdminForthResourcePages | keyof typeof AdminForthResourcePages>;
1331+
}
1332+
1333+
export type ShowInLegacyInput = Array<AdminForthResourcePages | keyof typeof AdminForthResourcePages>;
1334+
1335+
/**
1336+
* Object which describes on what pages should column be displayed on.
1337+
*/
1338+
export type ShowInInput = ShowInModernInput | ShowInLegacyInput;
13351339

13361340
export type ShowIn = {
13371341
[key in AdminForthResourcePages]: AllowedActionValue

0 commit comments

Comments
 (0)