Merge pull request #1161 from jacobsfletch/master

feat: supports root endpoints
This commit is contained in:
James Mikrut
2022-09-23 09:46:30 -07:00
committed by GitHub
14 changed files with 180 additions and 26 deletions

View File

@@ -16,14 +16,19 @@ export interface Global {
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "posts".
* via the `definition` "group-globals-one".
*/
export interface Post {
export interface GroupGlobalsOne {
id: string;
title?: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "group-globals-two".
*/
export interface GroupGlobalsTwo {
id: string;
title?: string;
description?: string;
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
@@ -39,3 +44,55 @@ export interface User {
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "posts".
*/
export interface Post {
id: string;
title?: string;
description?: string;
number?: number;
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "group-one-collection-ones".
*/
export interface GroupOneCollectionOne {
id: string;
title?: string;
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "group-one-collection-twos".
*/
export interface GroupOneCollectionTwo {
id: string;
title?: string;
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "group-two-collection-ones".
*/
export interface GroupTwoCollectionOne {
id: string;
title?: string;
createdAt: string;
updatedAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "group-two-collection-twos".
*/
export interface GroupTwoCollectionTwo {
id: string;
title?: string;
createdAt: string;
updatedAt: string;
}

View File

@@ -1,16 +1,18 @@
import { Response } from 'express';
import express, { Response } from 'express';
import { devUser } from '../credentials';
import { buildConfig } from '../buildConfig';
import { openAccess } from '../helpers/configHelpers';
import { PayloadRequest } from '../../src/express/types';
import { Config } from '../../src/config/types';
export const collectionSlug = 'endpoints';
export const globalSlug = 'global-endpoints';
export const globalEndpoint = 'global';
export const applicationEndpoint = 'path';
export const rootEndpoint = 'root';
export default buildConfig({
const MyConfig: Config = {
collections: [
{
slug: collectionSlug,
@@ -77,6 +79,32 @@ export default buildConfig({
res.json(req.body);
},
},
{
path: `/${applicationEndpoint}`,
method: 'get',
handler: (req: PayloadRequest, res: Response): void => {
res.json({ message: 'Hello, world!' });
},
},
{
path: `/${rootEndpoint}`,
method: 'get',
root: true,
handler: (req: PayloadRequest, res: Response): void => {
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({
@@ -87,4 +115,6 @@ export default buildConfig({
},
});
},
});
}
export default buildConfig(MyConfig);

View File

@@ -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);

View File

@@ -16,6 +16,10 @@ export interface ArrayField {
text: string;
id?: string;
}[];
collapsedArray: {
text: string;
id?: string;
}[];
localized: {
text: string;
id?: string;
@@ -80,6 +84,49 @@ export interface BlockField {
blockType: 'tabs';
}
)[];
collapsedByDefaultBlocks: (
| {
text: string;
richText?: {
[k: string]: unknown;
}[];
id?: string;
blockName?: string;
blockType: 'text';
}
| {
number: number;
id?: string;
blockName?: string;
blockType: 'number';
}
| {
subBlocks: (
| {
text: string;
id?: string;
blockName?: string;
blockType: 'text';
}
| {
number: number;
id?: string;
blockName?: string;
blockType: 'number';
}
)[];
id?: string;
blockName?: string;
blockType: 'subBlocks';
}
| {
textInCollapsible?: string;
textInRow?: string;
id?: string;
blockName?: string;
blockType: 'tabs';
}
)[];
localizedBlocks: (
| {
text: string;
@@ -153,6 +200,7 @@ export interface CollapsibleField {
textWithinSubGroup?: string;
};
};
someText?: string;
createdAt: string;
updatedAt: string;
}

View File

@@ -255,8 +255,8 @@ export class RESTClient {
return { status, doc: result };
}
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{status: number, data: T}> {
const response = await fetch(`${this.serverURL}/api${path}`, {
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{ status: number, data: T }> {
const response = await fetch(`${this.serverURL}${path}`, {
headers: {
'Content-Type': 'application/json',
},