Skip to content

Commit

Permalink
feat(per-page): add pagination to admin config
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Oct 11, 2021
1 parent 3715e01 commit c132f2f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/config/generateTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* eslint-disable no-nested-ternary */
/* eslint-disable no-use-before-define */
import { compile } from 'json-schema-to-typescript';
// import fse from 'fs-extra';
import { CollectionConfig, SanitizedCollectionConfig } from '../collections/config/types';
import { SanitizedConfig } from './types';

export function generateTypes(sanitizedConfig: SanitizedConfig): void {
console.log('compiling ts types');
const jsonSchema = configToJsonSchema(sanitizedConfig.collections);

compile(jsonSchema, 'Config', {
bannerComment: '// auto-generated by payload',
unreachableDefinitions: true,
}).then((compiled) => {
// fse.writeFileSync('generated-types.ts', compiled);
console.log('compiled', compiled);
});
}

function collectionToJsonSchema(
collection: CollectionConfig,
slugToLabel: Record<string, string>,
): any {
return {
title: collection.labels.singular,
type: 'object',
properties: Object.fromEntries(
collection.fields.map((field) => {
const type = field.type === 'number'
? { type: 'integer' }
: field.type === 'relationship'
? {
$ref: `#/definitions/${slugToLabel[field.relationTo as string]}`,
}
: { type: 'string' };
const enum_ = field.type === 'select' ? { enum: field.options } : {};
const default_ = field.defaultValue ? { default: field.defaultValue } : {};

return [
field.name,
{
...type,
...enum_,
...default_,
},
];
}),
),
required: collection.fields
.filter((field) => field.required === true)
.map((field) => field.name),
additionalProperties: false,
};
}

function configToJsonSchema(collections: SanitizedCollectionConfig[]): any {
const slugToLabel = Object.fromEntries(
collections.map((collection) => [
collection.slug,
collection.labels.singular,
]),
);
return {
definitions: Object.fromEntries(
collections.map((collection) => [
collection.labels.singular,
collectionToJsonSchema(collection, slugToLabel),
]),
),
additionalProperties: false,
};
}

// const result = await compile(jsonSchema, 'Config', {
// bannerComment: '// auto-generated by payload',
// unreachableDefinitions: true,
// });

// await fse.writeFile('generated-types.ts', result);
5 changes: 5 additions & 0 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export default joi.object({
Logo: component,
}),
}),
pagination: joi.object()
.keys({
default: joi.number(),
options: joi.array().items(joi.number()),
}),
webpack: joi.func(),
}),
defaultDepth: joi.number()
Expand Down
4 changes: 4 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export type Config = {
Dashboard?: React.ComponentType
}
}
pagination?: {
default?: number;
options?: number[];
}
webpack?: (config: Configuration) => Configuration;
};
collections?: CollectionConfig[];
Expand Down

0 comments on commit c132f2f

Please sign in to comment.