From 771bbaedbcc69d368864008f554d6dc5bcd839d4 Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Wed, 17 Aug 2022 13:06:58 -0400 Subject: [PATCH] chore: validation reuses endpoints schema --- src/collections/config/schema.ts | 10 ++-------- src/config/schema.ts | 10 ++++++++++ src/globals/config/schema.ts | 10 ++-------- test/endpoints/config.ts | 10 ++++++++++ test/endpoints/int.spec.ts | 12 +++++++++++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/collections/config/schema.ts b/src/collections/config/schema.ts index 5439a86293..dd5adcb3d3 100644 --- a/src/collections/config/schema.ts +++ b/src/collections/config/schema.ts @@ -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(), diff --git a/src/config/schema.ts b/src/config/schema.ts index 76d3a6a5c9..5bd744b4d5 100644 --- a/src/config/schema.ts +++ b/src/config/schema.ts @@ -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(), diff --git a/src/globals/config/schema.ts b/src/globals/config/schema.ts index 77f3a75394..72987857e0 100644 --- a/src/globals/config/schema.ts +++ b/src/globals/config/schema.ts @@ -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(), diff --git a/test/endpoints/config.ts b/test/endpoints/config.ts index ea94d8b413..af2631af57 100644 --- a/test/endpoints/config.ts +++ b/test/endpoints/config.ts @@ -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', diff --git a/test/endpoints/int.spec.ts b/test/endpoints/int.spec.ts index bf52ba0e9a..5cb6f0c55f 100644 --- a/test/endpoints/int.spec.ts +++ b/test/endpoints/int.spec.ts @@ -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); + }); + }); });