chore: adds root endpoint test
This commit is contained in:
@@ -86,7 +86,7 @@ Each endpoint object needs to have:
|
|||||||
| **`path`** | A string for the endpoint route after the collection or globals slug |
|
| **`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' |
|
| **`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) |
|
| **`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:
|
Example:
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Response } from 'express';
|
import express, { Response } from 'express';
|
||||||
import { devUser } from '../credentials';
|
import { devUser } from '../credentials';
|
||||||
import { buildConfig } from '../buildConfig';
|
import { buildConfig } from '../buildConfig';
|
||||||
import { openAccess } from '../helpers/configHelpers';
|
import { openAccess } from '../helpers/configHelpers';
|
||||||
@@ -91,9 +91,20 @@ const MyConfig: Config = {
|
|||||||
method: 'get',
|
method: 'get',
|
||||||
root: true,
|
root: true,
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
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) => {
|
onInit: async (payload) => {
|
||||||
await payload.create({
|
await payload.create({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { initPayloadTest } from '../helpers/configHelpers';
|
import { initPayloadTest } from '../helpers/configHelpers';
|
||||||
import { RESTClient } from '../helpers/rest';
|
import { RESTClient } from '../helpers/rest';
|
||||||
import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug } from './config';
|
import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug, rootEndpoint } from './config';
|
||||||
|
|
||||||
require('isomorphic-fetch');
|
require('isomorphic-fetch');
|
||||||
|
|
||||||
@@ -15,21 +15,21 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
describe('Collections', () => {
|
describe('Collections', () => {
|
||||||
it('should GET a static endpoint', async () => {
|
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(status).toBe(200);
|
||||||
expect(data.message).toStrictEqual('Hey Joey!');
|
expect(data.message).toStrictEqual('Hey Joey!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should GET an endpoint with a parameter', async () => {
|
it('should GET an endpoint with a parameter', async () => {
|
||||||
const name = 'George';
|
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(status).toBe(200);
|
||||||
expect(data.message).toStrictEqual(`Hello ${name}!`);
|
expect(data.message).toStrictEqual(`Hello ${name}!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should POST an endpoint with data', async () => {
|
it('should POST an endpoint with data', async () => {
|
||||||
const params = { name: 'George', age: 29 };
|
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(status).toBe(200);
|
||||||
expect(data.name).toStrictEqual(params.name);
|
expect(data.name).toStrictEqual(params.name);
|
||||||
expect(data.age).toStrictEqual(params.age);
|
expect(data.age).toStrictEqual(params.age);
|
||||||
@@ -39,7 +39,7 @@ describe('Endpoints', () => {
|
|||||||
describe('Globals', () => {
|
describe('Globals', () => {
|
||||||
it('should call custom endpoint', async () => {
|
it('should call custom endpoint', async () => {
|
||||||
const params = { globals: 'response' };
|
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(status).toBe(200);
|
||||||
expect(params).toMatchObject(data);
|
expect(params).toMatchObject(data);
|
||||||
@@ -49,7 +49,17 @@ describe('Endpoints', () => {
|
|||||||
describe('API', () => {
|
describe('API', () => {
|
||||||
it('should call custom endpoint', async () => {
|
it('should call custom endpoint', async () => {
|
||||||
const params = { app: 'response' };
|
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(status).toBe(200);
|
||||||
expect(params).toMatchObject(data);
|
expect(params).toMatchObject(data);
|
||||||
|
|||||||
@@ -255,8 +255,8 @@ export class RESTClient {
|
|||||||
return { status, doc: result };
|
return { status, doc: result };
|
||||||
}
|
}
|
||||||
|
|
||||||
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{status: number, data: T}> {
|
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{ status: number, data: T }> {
|
||||||
const response = await fetch(`${this.serverURL}/api${path}`, {
|
const response = await fetch(`${this.serverURL}${path}`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user