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,
collections: [],
globals: [],
endpoints: [],
cookiePrefix: 'payload',
csrf: [],
cors: [],

View File

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

View File

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

View File

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

View File

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