chore: validation reuses endpoints schema

This commit is contained in:
Dan Ribbens
2022-08-17 13:06:58 -04:00
parent 040833ead8
commit 771bbaedbc
5 changed files with 35 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
import joi from 'joi';
import { componentSchema } from '../../utilities/componentSchema';
import { endpointsSchema } from '../../config/schema';
const strategyBaseSchema = joi.object().keys({
refresh: joi.boolean(),
@@ -61,14 +62,7 @@ const collectionSchema = joi.object().keys({
afterRefresh: joi.array().items(joi.func()),
afterForgotPassword: joi.array().items(joi.func()),
}),
endpoints: joi.array().items(joi.object({
path: joi.string(),
method: joi.string().valid('get', 'head', 'post', 'put', 'patch', 'delete', 'connect', 'options'),
handler: joi.alternatives().try(
joi.array().items(joi.func()),
joi.func(),
),
})),
endpoints: endpointsSchema,
auth: joi.alternatives().try(
joi.object({
tokenExpiration: joi.number(),

View File

@@ -5,6 +5,15 @@ const component = joi.alternatives().try(
joi.func(),
);
export const endpointsSchema = joi.array().items(joi.object({
path: joi.string(),
method: joi.string().valid('get', 'head', 'post', 'put', 'patch', 'delete', 'connect', 'options'),
handler: joi.alternatives().try(
joi.array().items(joi.func()),
joi.func(),
),
}));
export default joi.object({
serverURL: joi.string()
.uri()
@@ -33,6 +42,7 @@ export default joi.object({
outputFile: joi.string(),
}),
collections: joi.array(),
endpoints: endpointsSchema,
globals: joi.array(),
admin: joi.object({
user: joi.string(),

View File

@@ -1,5 +1,6 @@
import joi from 'joi';
import { componentSchema } from '../../utilities/componentSchema';
import { endpointsSchema } from '../../config/schema';
const globalSchema = joi.object().keys({
slug: joi.string().required(),
@@ -18,14 +19,7 @@ const globalSchema = joi.object().keys({
beforeRead: joi.array().items(joi.func()),
afterRead: joi.array().items(joi.func()),
}),
endpoints: joi.array().items(joi.object({
path: joi.string(),
method: joi.string().valid('get', 'head', 'post', 'put', 'patch', 'delete', 'connect', 'options'),
handler: joi.alternatives().try(
joi.array().items(joi.func()),
joi.func(),
),
})),
endpoints: endpointsSchema,
access: joi.object({
read: joi.func(),
readVersions: joi.func(),

View File

@@ -8,6 +8,7 @@ export const collectionSlug = 'endpoints';
export const globalSlug = 'global-endpoints';
export const globalEndpoint = 'global';
export const applicationEndpoint = 'path';
export default buildConfig({
collections: [
@@ -68,6 +69,15 @@ export default buildConfig({
fields: [],
},
],
endpoints: [
{
path: applicationEndpoint,
method: 'post',
handler: (req: PayloadRequest, res: Response): void => {
res.json(req.body);
},
},
],
onInit: async (payload) => {
await payload.create({
collection: 'users',

View File

@@ -1,6 +1,6 @@
import { initPayloadTest } from '../helpers/configHelpers';
import { RESTClient } from '../helpers/rest';
import { collectionSlug, globalEndpoint, globalSlug } from './config';
import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug } from './config';
require('isomorphic-fetch');
@@ -45,4 +45,14 @@ describe('Endpoints', () => {
expect(params).toMatchObject(data);
});
});
describe('Application', () => {
it('should call custom endpoint', async () => {
const params = { app: 'response' };
const { status, data } = await client.endpoint(`/${applicationEndpoint}`, 'post', params);
expect(status).toBe(200);
expect(params).toMatchObject(data);
});
});
});