feat: custom api endpoints

This commit is contained in:
Dan Ribbens
2022-08-17 13:28:50 -04:00
parent 771bbaedbc
commit 11d8fc71e8
5 changed files with 7 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ export const defaults: Config = {
maxDepth: 10, maxDepth: 10,
collections: [], collections: [],
globals: [], globals: [],
endpoints: [],
cookiePrefix: 'payload', cookiePrefix: 'payload',
csrf: [], csrf: [],
cors: [], cors: [],

View File

@@ -200,6 +200,7 @@ export type Config = {
export type SanitizedConfig = Omit<DeepRequired<Config>, 'collections' | 'globals'> & { export type SanitizedConfig = Omit<DeepRequired<Config>, 'collections' | 'globals'> & {
collections: SanitizedCollectionConfig[] collections: SanitizedCollectionConfig[]
globals: SanitizedGlobalConfig[] globals: SanitizedGlobalConfig[]
endpoints: SanitizedGlobalConfig[]
paths: { [key: string]: string }; paths: { [key: string]: string };
} }

View File

@@ -31,6 +31,7 @@ import { Payload } from '.';
import loadConfig from './config/load'; import loadConfig from './config/load';
import Logger from './utilities/logger'; import Logger from './utilities/logger';
import { getDataLoader } from './collections/dataloader'; import { getDataLoader } from './collections/dataloader';
import mountEndpoints from './express/mountEndpoints';
export const init = (payload: Payload, options: InitOptions): void => { export const init = (payload: Payload, options: InitOptions): void => {
payload.logger.info('Starting Payload...'); payload.logger.info('Starting Payload...');
@@ -105,6 +106,8 @@ export const init = (payload: Payload, options: InitOptions): void => {
initGraphQLPlayground(payload); initGraphQLPlayground(payload);
} }
mountEndpoints(payload.router, payload.config.endpoints);
// Bind router to API // Bind router to API
payload.express.use(payload.config.routes.api, payload.router); payload.express.use(payload.config.routes.api, payload.router);

View File

@@ -71,7 +71,7 @@ export default buildConfig({
], ],
endpoints: [ endpoints: [
{ {
path: applicationEndpoint, path: `/${applicationEndpoint}`,
method: 'post', method: 'post',
handler: (req: PayloadRequest, res: Response): void => { handler: (req: PayloadRequest, res: Response): void => {
res.json(req.body); res.json(req.body);

View File

@@ -46,7 +46,7 @@ describe('Endpoints', () => {
}); });
}); });
describe('Application', () => { describe('API', () => {
it('should call custom endpoint', async () => { it('should call custom endpoint', async () => {
const params = { app: 'response' }; const params = { app: 'response' };
const { status, data } = await client.endpoint(`/${applicationEndpoint}`, 'post', params); const { status, data } = await client.endpoint(`/${applicationEndpoint}`, 'post', params);