Merge branch 'feat/1.0' of github.com:payloadcms/payload into feat/1.0

This commit is contained in:
James
2022-07-17 09:51:55 -07:00
7 changed files with 111 additions and 5 deletions

View File

@@ -0,0 +1,52 @@
import { Response } from 'express';
import { CollectionConfig } from '../../../src/collections/config/types';
import { openAccess } from '../../helpers/configHelpers';
import { PayloadRequest } from '../../../src/express/types';
export const endpointsSlug = 'endpoints';
const Endpoints: CollectionConfig = {
slug: endpointsSlug,
access: openAccess,
endpoints: [
{
path: '/say-hello/joe-bloggs',
method: 'get',
handler: (req: PayloadRequest, res: Response): void => {
res.json({ message: 'Hey Joey!' });
},
},
{
path: '/say-hello/:group/:name',
method: 'get',
handler: (req: PayloadRequest, res: Response): void => {
res.json({ message: `Hello ${req.params.name} @ ${req.params.group}` });
},
},
{
path: '/say-hello/:name',
method: 'get',
handler: (req: PayloadRequest, res: Response): void => {
res.json({ message: `Hello ${req.params.name}!` });
},
},
{
path: '/whoami',
method: 'post',
handler: (req: PayloadRequest, res: Response): void => {
res.json({
name: req.body.name,
age: req.body.age,
});
},
},
],
fields: [
{
name: 'title',
type: 'text',
},
],
};
export default Endpoints;

View File

@@ -2,6 +2,7 @@ import type { CollectionConfig } from '../../src/collections/config/types';
import { devUser } from '../credentials';
import { buildConfig } from '../buildConfig';
import type { Post } from './payload-types';
import Endpoints from './Endpoints';
export interface Relation {
id: string;
@@ -89,6 +90,7 @@ export default buildConfig({
},
collectionWithName(relationSlug),
collectionWithName('dummy'),
Endpoints,
],
onInit: async (payload) => {
await payload.create({

View File

@@ -0,0 +1,47 @@
import { initPayloadTest } from '../helpers/configHelpers';
import { endpointsSlug } from './Endpoints';
require('isomorphic-fetch');
describe('Collections - Endpoints', () => {
let endpoint;
let serverURL;
beforeAll(async () => {
const config = await initPayloadTest({ __dirname, init: { local: false } });
serverURL = config.serverURL;
endpoint = async (path: string, method = 'get', params = undefined): Promise<unknown> => {
const response = await fetch(`${serverURL}/api${path}`, {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
method,
});
const { status } = response;
const data = await response.json();
return { status, data };
};
});
describe('Endpoints', () => {
it('should GET a static endpoint', async () => {
const { status, data } = await endpoint(`/${endpointsSlug}/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 endpoint(`/${endpointsSlug}/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 endpoint(`/${endpointsSlug}/whoami`, 'post', params);
expect(status).toBe(200);
expect(data.name).toStrictEqual(params.name);
expect(data.age).toStrictEqual(params.age);
});
});
});

View File

@@ -0,0 +1,7 @@
import fs from 'fs';
const removeFiles = (dir) => {
if (fs.existsSync(dir)) fs.readdirSync(dir).forEach((f) => fs.rmSync(`${dir}/${f}`));
};
export default removeFiles;

View File

@@ -1,2 +1 @@
!media/.gitkeep
media

View File

@@ -1,8 +1,8 @@
import path from 'path';
import fs from 'fs';
import { buildConfig } from '../buildConfig';
import { devUser } from '../credentials';
import getFileByPath from '../../src/uploads/getFileByPath';
import removeFiles from '../helpers/removeFiles';
export const mediaSlug = 'media';
@@ -77,9 +77,8 @@ export default buildConfig({
},
],
onInit: async (payload) => {
// delete files in /media
const mediaDir = path.resolve(__dirname, './media');
fs.readdirSync(mediaDir).forEach((f) => fs.rmSync(`${mediaDir}/${f}`));
const uploadsDir = path.resolve(__dirname, './media');
removeFiles(uploadsDir);
await payload.create({
collection: 'users',