feat: custom api endpoints
This commit is contained in:
@@ -7,6 +7,7 @@ export const defaults: Config = {
|
||||
maxDepth: 10,
|
||||
collections: [],
|
||||
globals: [],
|
||||
endpoints: [],
|
||||
cookiePrefix: 'payload',
|
||||
csrf: [],
|
||||
cors: [],
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ export default buildConfig({
|
||||
],
|
||||
endpoints: [
|
||||
{
|
||||
path: applicationEndpoint,
|
||||
path: `/${applicationEndpoint}`,
|
||||
method: 'post',
|
||||
handler: (req: PayloadRequest, res: Response): void => {
|
||||
res.json(req.body);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user