Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: builds plugin infrastructure #149

Merged
merged 1 commit into from
Apr 22, 2021
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
3 changes: 2 additions & 1 deletion src/collections/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export type PayloadCollectionConfig = {
timestamps?: boolean
};

export interface CollectionConfig extends Omit<DeepRequired<PayloadCollectionConfig>, 'auth' | 'upload'> {
export interface CollectionConfig extends Omit<DeepRequired<PayloadCollectionConfig>, 'auth' | 'upload' | 'fields'> {
auth: Auth;
upload: Upload;
fields: Field[];
}

export type Collection = {
Expand Down
4 changes: 4 additions & 0 deletions src/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ import sanitize from './sanitize';
export function buildConfig(config: PayloadConfig): Config {
const sanitized = sanitize(config);

if (Array.isArray(config.plugins)) {
return sanitized.plugins.reduce((configWithPlugins, plugin) => plugin(configWithPlugins), sanitized);
}

return sanitized;
}
5 changes: 4 additions & 1 deletion src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default joi.object({
.keys({
json: joi.object(),
compression: joi.object(),
middleware: joi.array().items(joi.object()),
middleware: joi.array().items(joi.func()),
}),
local: joi.boolean(),
upload: joi.object()
Expand Down Expand Up @@ -116,4 +116,7 @@ export default joi.object({
hooks: joi.object().keys({
afterError: joi.func(),
}),
plugins: joi.array().items(
joi.func(),
),
});
13 changes: 8 additions & 5 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { Payload } from '..';
import { AfterErrorHook, PayloadCollectionConfig, CollectionConfig } from '../collections/config/types';
import { PayloadGlobalConfig, GlobalConfig } from '../globals/config/types';
import { PayloadRequest } from '../express/types';
import InitializeGraphQL from '../graphql';
import { Where } from '../types';

type Email = {
fromName: string;
fromAddress: string;
}

// eslint-disable-next-line no-use-before-define
type Plugin = (config: Config) => Config;

export type EmailTransport = Email & {
transport: Transporter;
transportOptions?: SMTPConnection.Options;
Expand Down Expand Up @@ -99,7 +101,7 @@ export type PayloadConfig = {
};
debug?: boolean
express?: {
json: {
json?: {
limit?: number
},
compression?: {
Expand All @@ -126,8 +128,8 @@ export type PayloadConfig = {
fallback?: boolean
};
graphQL?: {
mutations?: ((graphQL: typeof GraphQL, payload: InitializeGraphQL) => any),
queries?: ((graphQL: typeof GraphQL, payload: InitializeGraphQL) => any),
mutations?: ((graphQL: typeof GraphQL, payload: Payload) => Record<string, unknown>),
queries?: ((graphQL: typeof GraphQL, payload: Payload) => Record<string, unknown>),
maxComplexity?: number;
disablePlaygroundInProduction?: boolean;
disable?: boolean;
Expand All @@ -136,9 +138,10 @@ export type PayloadConfig = {
hooks?: {
afterError?: AfterErrorHook;
};
plugins?: Plugin[]
};

export type Config = Omit<DeepRequired<PayloadConfig>, 'collections'> & {
export type Config = Omit<DeepRequired<PayloadConfig>, 'collections' | 'globals'> & {
collections: CollectionConfig[]
globals: GlobalConfig[]
paths: { [key: string]: string };
Expand Down
4 changes: 3 additions & 1 deletion src/globals/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export type PayloadGlobalConfig = {
}
}

export type GlobalConfig = DeepRequired<PayloadGlobalConfig>
export interface GlobalConfig extends Omit<DeepRequired<PayloadGlobalConfig>, 'fields'> {
fields: Field[]
}

export type Globals = {
Model: GlobalModel
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class InitializeGraphQL {
};

if (typeof this.config.graphQL.queries === 'function') {
const customQueries = this.config.graphQL.queries(GraphQL, this);
const customQueries = this.config.graphQL.queries(GraphQL, init);
this.Query = {
...this.Query,
fields: {
Expand All @@ -110,7 +110,7 @@ class InitializeGraphQL {
}

if (typeof this.config.graphQL.mutations === 'function') {
const customMutations = this.config.graphQL.mutations(GraphQL, this);
const customMutations = this.config.graphQL.mutations(GraphQL, init);
this.Mutation = {
...this.Mutation,
fields: {
Expand Down