diff --git a/docs/rest-api/overview.mdx b/docs/rest-api/overview.mdx index 7f4e3152e..005feee87 100644 --- a/docs/rest-api/overview.mdx +++ b/docs/rest-api/overview.mdx @@ -86,7 +86,7 @@ Each endpoint object needs to have: | **`path`** | A string for the endpoint route after the collection or globals slug | | **`method`** | The lowercase HTTP verb to use: 'get', 'head', 'post', 'put', 'delete', 'connect' or 'options' | | **`handler`** | A function or array of functions to be called with **req**, **res** and **next** arguments. [Express](https://expressjs.com/en/guide/routing.html#route-handlers) | -| **`root`** | When `true`, defines the endpoint on the root Express app, bypassing Payload handler and the `routes.api` subpath. Note: this only applies to top-level endpoints of your Payload config, endpoints defined on `collections` or `globals` cannot be root. | +| **`root`** | When `true`, defines the endpoint on the root Express app, bypassing Payload handlers and the `routes.api` subpath. Note: this only applies to top-level endpoints of your Payload config, endpoints defined on `collections` or `globals` cannot be root. | Example: diff --git a/test/endpoints/config.ts b/test/endpoints/config.ts index 23e9ca386..4d105cc7e 100644 --- a/test/endpoints/config.ts +++ b/test/endpoints/config.ts @@ -1,4 +1,4 @@ -import { Response } from 'express'; +import express, { Response } from 'express'; import { devUser } from '../credentials'; import { buildConfig } from '../buildConfig'; import { openAccess } from '../helpers/configHelpers'; @@ -91,9 +91,20 @@ const MyConfig: Config = { method: 'get', root: true, handler: (req: PayloadRequest, res: Response): void => { - res.json({ message: 'Root.' }); + res.json({ message: 'Hello, world!' }); }, }, + { + path: `/${rootEndpoint}`, + method: 'post', + root: true, + handler: [ + express.json({ type: 'application/json' }), + (req: PayloadRequest, res: Response): void => { + res.json(req.body); + } + ], + }, ], onInit: async (payload) => { await payload.create({ diff --git a/test/endpoints/int.spec.ts b/test/endpoints/int.spec.ts index 404f1dcf4..9844e852a 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 { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug } from './config'; +import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug, rootEndpoint } from './config'; require('isomorphic-fetch'); @@ -15,21 +15,21 @@ describe('Endpoints', () => { describe('Collections', () => { it('should GET a static endpoint', async () => { - const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/joe-bloggs`); + const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/joe-bloggs`); expect(status).toBe(200); expect(data.message).toStrictEqual('Hey Joey!'); }); it('should GET an endpoint with a parameter', async () => { const name = 'George'; - const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/${name}`); + const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/${name}`); expect(status).toBe(200); expect(data.message).toStrictEqual(`Hello ${name}!`); }); it('should POST an endpoint with data', async () => { const params = { name: 'George', age: 29 }; - const { status, data } = await client.endpoint(`/${collectionSlug}/whoami`, 'post', params); + const { status, data } = await client.endpoint(`/api/${collectionSlug}/whoami`, 'post', params); expect(status).toBe(200); expect(data.name).toStrictEqual(params.name); expect(data.age).toStrictEqual(params.age); @@ -39,7 +39,7 @@ describe('Endpoints', () => { describe('Globals', () => { it('should call custom endpoint', async () => { const params = { globals: 'response' }; - const { status, data } = await client.endpoint(`/globals/${globalSlug}/${globalEndpoint}`, 'post', params); + const { status, data } = await client.endpoint(`/api/globals/${globalSlug}/${globalEndpoint}`, 'post', params); expect(status).toBe(200); expect(params).toMatchObject(data); @@ -49,7 +49,17 @@ describe('Endpoints', () => { describe('API', () => { it('should call custom endpoint', async () => { const params = { app: 'response' }; - const { status, data } = await client.endpoint(`/${applicationEndpoint}`, 'post', params); + const { status, data } = await client.endpoint(`/api/${applicationEndpoint}`, 'post', params); + + expect(status).toBe(200); + expect(params).toMatchObject(data); + }); + }); + + describe('Root', () => { + it('should call custom root endpoint', async () => { + const params = { root: 'response' }; + const { status, data } = await client.endpoint(`/${rootEndpoint}`, 'post', params); expect(status).toBe(200); expect(params).toMatchObject(data); diff --git a/test/helpers/rest.ts b/test/helpers/rest.ts index 3188a4b27..59b035856 100644 --- a/test/helpers/rest.ts +++ b/test/helpers/rest.ts @@ -255,8 +255,8 @@ export class RESTClient { return { status, doc: result }; } - async endpoint(path: string, method = 'get', params = undefined): Promise<{status: number, data: T}> { - const response = await fetch(`${this.serverURL}/api${path}`, { + async endpoint(path: string, method = 'get', params = undefined): Promise<{ status: number, data: T }> { + const response = await fetch(`${this.serverURL}${path}`, { headers: { 'Content-Type': 'application/json', },