diff --git a/CHANGELOG.md b/CHANGELOG.md index 658b54b7f..c50b7bde4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## [1.1.4](https://github.com/payloadcms/payload/compare/v1.1.3...v1.1.4) (2022-09-24) + + +### Bug Fixes + +* field level access for nested fields ([22ea98c](https://github.com/payloadcms/payload/commit/22ea98ca33770a0ec6652f814726454abb6da24e)) +* refine type generation for relationships ([ef83bdb](https://github.com/payloadcms/payload/commit/ef83bdb709ebde008b90930a6875b24f042a41b0)) + + +### Features + +* supports root endpoints ([52cd3b4](https://github.com/payloadcms/payload/commit/52cd3b4a7ed9bc85e93d753a3aaf190489ca98cd)) + ## [1.1.3](https://github.com/payloadcms/payload/compare/v1.1.2...v1.1.3) (2022-09-16) diff --git a/contributing.md b/contributing.md index 263f49057..6fd444c10 100644 --- a/contributing.md +++ b/contributing.md @@ -49,7 +49,9 @@ The directory split up in this way specifically to reduce friction when creating The following command will start Payload with your config: `yarn dev my-test-dir`. This command will start up Payload using your config and refresh a test database on every restart. -NOTE: It is recommended to add the test credentials to your autofill for `localhost:3000/admin` as this will be required on every nodemon restart. +When switching between test directories, you will want to remove your `node_modules/.cache ` manually or by running `yarn clean:cache`. + +NOTE: It is recommended to add the test credentials (located in `test/credentials.ts`) to your autofill for `localhost:3000/admin` as this will be required on every nodemon restart. ## Pull Requests diff --git a/docs/admin/components.mdx b/docs/admin/components.mdx index 6e82397e8..9950df5e4 100644 --- a/docs/admin/components.mdx +++ b/docs/admin/components.mdx @@ -97,11 +97,51 @@ All Payload fields support the ability to swap in your own React components. So, **Fields support the following custom components:** -| Component | Description | -| --------------- | -------------| -| **`Filter`** | Override the text input that is presented in the `List` view when a user is filtering documents by the customized field. | -| **`Cell`** | Used in the `List` view's table to represent a table-based preview of the data stored in the field. | -| **`Field`** | Swap out the field itself within all `Edit` views. | +| Component | Description | +| --------------- |------------------------------------------------------------------------------------------------------------------------------| +| **`Filter`** | Override the text input that is presented in the `List` view when a user is filtering documents by the customized field. | +| **`Cell`** | Used in the `List` view's table to represent a table-based preview of the data stored in the field. [More](#cell-component) | +| **`Field`** | Swap out the field itself within all `Edit` views. [More](#field-component) | + +## Cell Component + +These are the props that will be passed to your custom Cell to use in your own components. + +| Property | Description | +|--------------|-------------------------------------------------------------------| +| **`field`** | An object that includes the field configuration. | +| **`colIndex`** | A unique number for the column in the list. | +| **`collection`** | An object with the config of the collection that the field is in. | +| **`cellData`** | The data for the field that the cell represents. | +| **`rowData`** | An object with all the field values for the row. | + +#### Example + +```tsx +import React from 'react'; +import './index.scss'; +const baseClass = 'custom-cell'; + +const CustomCell: React.FC = (props) => { + const { + field, + colIndex, + collection, + cellData, + rowData, + } = props; + + return ( + + { cellData } + + ); +}; +``` + +## Field Component + +When writing your own custom components you can make use of a number of hooks to set data, get reactive changes to other fields, get the id of the document or interact with a context from a custom provider. ### Sending and receiving values from the form diff --git a/docs/admin/webpack.mdx b/docs/admin/webpack.mdx index b48060b87..c696a678a 100644 --- a/docs/admin/webpack.mdx +++ b/docs/admin/webpack.mdx @@ -155,6 +155,11 @@ export default {}; Now, when Webpack sees that you're attempting to import your `createStripeSubscriptionPath` file, it'll disregard that actual file and load your mock file instead. Not only will your Admin panel now bundle successfully, you will have optimized its filesize by removing unnecessary code! And you might have learned something about Webpack, too. + + Tip:
+ If changes to your Webpack aliases are not surfacing, they might be [cached](https://webpack.js.org/configuration/cache/) in `node_modules/.cache/webpack`. Try deleting that folder and restarting your server. +
+ ## Admin environment vars diff --git a/docs/fields/rich-text.mdx b/docs/fields/rich-text.mdx index bfee4e2d2..96373a1d1 100644 --- a/docs/fields/rich-text.mdx +++ b/docs/fields/rich-text.mdx @@ -174,6 +174,20 @@ const ExampleCollection: CollectionConfig = { ] } ], + link: { + // Inject your own fields into the Link element + fields: [ + { + name: 'rel', + label: 'Rel Attribute', + type: 'select', + hasMany: true, + options: [ + 'noopener', 'noreferrer', 'nofollow', + ], + }, + ], + }, upload: { collections: { media: { diff --git a/docs/fields/ui.mdx b/docs/fields/ui.mdx index 89737a40f..657f3466e 100644 --- a/docs/fields/ui.mdx +++ b/docs/fields/ui.mdx @@ -23,12 +23,12 @@ With this field, you can also inject custom `Cell` components that appear as add ### Config -| Option | Description | -| ---------------------------- | ----------- | -| **`name`** * | A unique identifier for this field. | -| **`label`** | Human-readable label for this UI field. | -| **`admin.components.Field`** | React component to be rendered for this field within the Edit view. | -| **`admin.components.Cell`** | React component to be rendered as a Cell within collection List views. | +| Option | Description | +| ---------------------------- |-------------------------------------------------------------------------------------------------------------------| +| **`name`** * | A unique identifier for this field. | +| **`label`** | Human-readable label for this UI field. | +| **`admin.components.Field`** | React component to be rendered for this field within the Edit view. [More](/admin/components/#field-component) | +| **`admin.components.Cell`** | React component to be rendered as a Cell within collection List views. [More](/admin/components/#field-component) | *\* An asterisk denotes that a property is required.* diff --git a/docs/plugins/overview.mdx b/docs/plugins/overview.mdx index 180d19f75..82578a21e 100644 --- a/docs/plugins/overview.mdx +++ b/docs/plugins/overview.mdx @@ -27,7 +27,7 @@ Writing plugins is no more complex than writing regular JavaScript. If you know ### How to install plugins -The base Payload config allows for a `plugins` property which takes an `array` of [`Plugin`s](https://github.com/payloadcms/payload/blob/master/src/config/types.ts#L21). +The base Payload config allows for a `plugins` property which takes an `array` of [`Plugins`](https://github.com/payloadcms/payload/blob/master/src/config/types.ts#L21). ```js import { buildConfig } from 'payload/config'; @@ -134,7 +134,7 @@ const addLastModified: Plugin = (incomingConfig: Config): Config => { export default addLastModified; ``` -#### Available Plugins +### Available Plugins You can discover existing plugins by browsing the `payload-plugin` topic on [GitHub](https://github.com/topics/payload-plugin). diff --git a/docs/rest-api/overview.mdx b/docs/rest-api/overview.mdx index 57d8dad1d..005feee87 100644 --- a/docs/rest-api/overview.mdx +++ b/docs/rest-api/overview.mdx @@ -86,6 +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 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/package.json b/package.json index fbab8546e..90a655c02 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "payload", - "version": "1.1.3", + "version": "1.1.4", "description": "Node, React and MongoDB Headless CMS and Application Framework", "license": "MIT", "author": { @@ -41,6 +41,7 @@ "test:e2e:headed": "cross-env DISABLE_LOGGING=true playwright test --headed", "test:e2e:debug": "cross-env PWDEBUG=1 DISABLE_LOGGING=true playwright test", "test:components": "cross-env jest --config=jest.components.config.js", + "clean:cache": "rimraf node_modules/.cache", "clean": "rimraf dist", "release": "release-it", "release:patch": "release-it patch", @@ -169,7 +170,7 @@ "react-sortable-hoc": "^2.0.0", "react-toastify": "^8.2.0", "sanitize-filename": "^1.6.3", - "sass": "^1.52.1", + "sass": "^1.55.0", "sass-loader": "^12.6.0", "sharp": "^0.29.3", "slate": "^0.72.8", diff --git a/src/admin/components/forms/RenderFields/types.ts b/src/admin/components/forms/RenderFields/types.ts index b8b2df008..3ee4d6b29 100644 --- a/src/admin/components/forms/RenderFields/types.ts +++ b/src/admin/components/forms/RenderFields/types.ts @@ -6,7 +6,7 @@ export type Props = { className?: string readOnly?: boolean forceRender?: boolean - permissions?: { + permissions?: FieldPermissions | { [field: string]: FieldPermissions } filter?: (field: Field) => boolean diff --git a/src/admin/components/forms/field-types/Collapsible/index.tsx b/src/admin/components/forms/field-types/Collapsible/index.tsx index a48adaee5..23d9ac538 100644 --- a/src/admin/components/forms/field-types/Collapsible/index.tsx +++ b/src/admin/components/forms/field-types/Collapsible/index.tsx @@ -75,7 +75,7 @@ const CollapsibleField: React.FC = (props) => { ({ ...field, diff --git a/src/admin/components/forms/field-types/Row/index.tsx b/src/admin/components/forms/field-types/Row/index.tsx index c5878dd54..88a1542c8 100644 --- a/src/admin/components/forms/field-types/Row/index.tsx +++ b/src/admin/components/forms/field-types/Row/index.tsx @@ -28,7 +28,7 @@ const Row: React.FC = (props) => { ({ ...field, diff --git a/src/admin/components/forms/field-types/Tabs/index.tsx b/src/admin/components/forms/field-types/Tabs/index.tsx index b6646eced..84bafbddf 100644 --- a/src/admin/components/forms/field-types/Tabs/index.tsx +++ b/src/admin/components/forms/field-types/Tabs/index.tsx @@ -71,7 +71,7 @@ const TabsField: React.FC = (props) => { key={String(activeTab.label)} forceRender readOnly={readOnly} - permissions={permissions?.fields} + permissions={tabHasName(activeTab) ? permissions[activeTab.name].fields : permissions} fieldTypes={fieldTypes} fieldSchema={activeTab.fields.map((field) => ({ ...field, diff --git a/src/admin/scss/vars.scss b/src/admin/scss/vars.scss index 3e8a8f17a..1a521a3ba 100644 --- a/src/admin/scss/vars.scss +++ b/src/admin/scss/vars.scss @@ -18,7 +18,7 @@ $baseline-body-size : 13px !default; $baseline : math.div($baseline-px, $baseline-body-size)+rem; @function base($multiplier) { - @return math.div($baseline-px, $baseline-body-size) * $multiplier +rem; + @return (math.div($baseline-px, $baseline-body-size) * $multiplier)+rem; } ////////////////////////////// diff --git a/src/auth/operations/access.ts b/src/auth/operations/access.ts index a9dfbc6a4..40cd096f4 100644 --- a/src/auth/operations/access.ts +++ b/src/auth/operations/access.ts @@ -1,6 +1,7 @@ import { PayloadRequest } from '../../express/types'; import { Permissions } from '../types'; import { adminInit as adminInitTelemetry } from '../../utilities/telemetry/events/adminInit'; +import { tabHasName } from '../../fields/config/types'; const allOperations = ['create', 'read', 'update', 'delete']; @@ -66,7 +67,12 @@ async function accessOperation(args: Arguments): Promise { executeFieldPolicies(updatedObj, field.fields, operation); } else if (field.type === 'tabs') { field.tabs.forEach((tab) => { - executeFieldPolicies(updatedObj, tab.fields, operation); + if (tabHasName(tab)) { + if (!updatedObj[tab.name]) updatedObj[tab.name] = { fields: {} }; + executeFieldPolicies(updatedObj[tab.name].fields, tab.fields, operation); + } else { + executeFieldPolicies(updatedObj, tab.fields, operation); + } }); } }); diff --git a/src/bin/generateTypes.ts b/src/bin/generateTypes.ts index 99ed3cdf5..bc6ead518 100644 --- a/src/bin/generateTypes.ts +++ b/src/bin/generateTypes.ts @@ -131,33 +131,50 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { if (Array.isArray(field.relationTo)) { if (field.hasMany) { fieldSchema = { - type: 'array', - items: { - oneOf: field.relationTo.map((relation) => { - const idFieldType = getCollectionIDType(config.collections, relation); + oneOf: [ + { + type: 'array', + items: { + oneOf: field.relationTo.map((relation) => { + const idFieldType = getCollectionIDType(config.collections, relation); - return { - type: 'object', - additionalProperties: false, - properties: { - value: { - oneOf: [ - { + return { + type: 'object', + additionalProperties: false, + properties: { + value: { type: idFieldType, }, - { + relationTo: { + const: relation, + }, + }, + required: ['value', 'relationTo'], + }; + }), + }, + }, + { + type: 'array', + items: { + oneOf: field.relationTo.map((relation) => { + return { + type: 'object', + additionalProperties: false, + properties: { + value: { $ref: `#/definitions/${relation}`, }, - ], - }, - relationTo: { - const: relation, - }, - }, - required: ['value', 'relationTo'], - }; - }), - }, + relationTo: { + const: relation, + }, + }, + required: ['value', 'relationTo'], + }; + }), + }, + }, + ], }; } else { fieldSchema = { @@ -192,17 +209,20 @@ function generateFieldTypes(config: SanitizedConfig, fields: Field[]): { if (field.hasMany) { fieldSchema = { - type: 'array', - items: { - oneOf: [ - { + oneOf: [ + { + type: 'array', + items: { type: idFieldType, }, - { + }, + { + type: 'array', + items: { $ref: `#/definitions/${field.relationTo}`, }, - ], - }, + }, + ], }; } else { fieldSchema = { diff --git a/src/collections/config/types.ts b/src/collections/config/types.ts index c8d4ec15b..f9aa0c41b 100644 --- a/src/collections/config/types.ts +++ b/src/collections/config/types.ts @@ -226,7 +226,7 @@ export type CollectionConfig = { /** * Custom rest api endpoints */ - endpoints?: Endpoint[] + endpoints?: Omit[] /** * Access control */ diff --git a/src/collections/init.ts b/src/collections/init.ts index fb94bdde9..736e42590 100644 --- a/src/collections/init.ts +++ b/src/collections/init.ts @@ -113,7 +113,7 @@ export default function registerCollections(ctx: Payload): void { } const endpoints = buildEndpoints(collection); - mountEndpoints(router, endpoints); + mountEndpoints(ctx.express, router, endpoints); ctx.router.use(`/${slug}`, router); } diff --git a/src/config/schema.ts b/src/config/schema.ts index 14a815e87..83b8c6f47 100644 --- a/src/config/schema.ts +++ b/src/config/schema.ts @@ -8,6 +8,7 @@ const component = joi.alternatives().try( export const endpointsSchema = joi.array().items(joi.object({ path: joi.string(), method: joi.string().valid('get', 'head', 'post', 'put', 'patch', 'delete', 'connect', 'options'), + root: joi.bool(), handler: joi.alternatives().try( joi.array().items(joi.func()), joi.func(), diff --git a/src/config/types.ts b/src/config/types.ts index 80291c03b..e8517a6ac 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -79,16 +79,19 @@ export type AccessResult = boolean | Where; */ export type Access = (args?: any) => AccessResult | Promise; -export interface PayloadHandler {( +export interface PayloadHandler { + ( req: PayloadRequest, res: Response, next: NextFunction, - ): void } + ): void +} export type Endpoint = { path: string method: 'get' | 'head' | 'post' | 'put' | 'patch' | 'delete' | 'connect' | 'options' | string handler: PayloadHandler | PayloadHandler[] + root?: boolean } export type AdminView = React.ComponentType<{ user: User, canAccessAdmin: boolean }> diff --git a/src/express/mountEndpoints.ts b/src/express/mountEndpoints.ts index eb0b547e5..8ac026d46 100644 --- a/src/express/mountEndpoints.ts +++ b/src/express/mountEndpoints.ts @@ -1,9 +1,13 @@ -import { Router } from 'express'; +import { Express, Router } from 'express'; import { Endpoint } from '../config/types'; -function mountEndpoints(router: Router, endpoints: Endpoint[]): void { +function mountEndpoints(express: Express, router: Router, endpoints: Endpoint[]): void { endpoints.forEach((endpoint) => { - router[endpoint.method](endpoint.path, endpoint.handler); + if (!endpoint.root) { + router[endpoint.method](endpoint.path, endpoint.handler); + } else { + express[endpoint.method](endpoint.path, endpoint.handler); + } }); } diff --git a/src/globals/config/types.ts b/src/globals/config/types.ts index c46d8de9a..14a14a055 100644 --- a/src/globals/config/types.ts +++ b/src/globals/config/types.ts @@ -56,7 +56,7 @@ export type GlobalConfig = { beforeRead?: BeforeReadHook[] afterRead?: AfterReadHook[] } - endpoints?: Endpoint[], + endpoints?: Omit[], access?: { read?: Access; readDrafts?: Access; diff --git a/src/globals/init.ts b/src/globals/init.ts index e8732291b..a82d946d9 100644 --- a/src/globals/init.ts +++ b/src/globals/init.ts @@ -48,7 +48,7 @@ export default function initGlobals(ctx: Payload): void { const { slug } = global; const endpoints = buildEndpoints(global); - mountEndpoints(router, endpoints); + mountEndpoints(ctx.express, router, endpoints); ctx.router.use(`/globals/${slug}`, router); }); diff --git a/src/init.ts b/src/init.ts index 1f5d7daeb..2a3dd916f 100644 --- a/src/init.ts +++ b/src/init.ts @@ -106,7 +106,7 @@ export const init = (payload: Payload, options: InitOptions): void => { initGraphQLPlayground(payload); } - mountEndpoints(payload.router, payload.config.endpoints); + mountEndpoints(options.express, payload.router, payload.config.endpoints); // Bind router to API payload.express.use(payload.config.routes.api, payload.router); diff --git a/test/access-control/config.ts b/test/access-control/config.ts index 3cc59faf8..8eed5be93 100644 --- a/test/access-control/config.ts +++ b/test/access-control/config.ts @@ -25,7 +25,7 @@ const PublicReadabilityAccess: FieldAccess = ({ req: { user }, siblingData }) => return false; }; -export const requestHeaders = {authorization: 'Bearer testBearerToken'}; +export const requestHeaders = { authorization: 'Bearer testBearerToken' }; const UseRequestHeadersAccess: FieldAccess = ({ req: { headers } }) => { return !!headers && headers.authorization === requestHeaders.authorization; }; @@ -47,6 +47,50 @@ export default buildConfig({ update: () => false, }, }, + { + type: 'group', + name: 'group', + fields: [ + { + name: 'restrictedGroupText', + type: 'text', + access: { + read: () => false, + update: () => false, + create: () => false, + }, + }, + ], + }, + { + type: 'row', + fields: [ + { + name: 'restrictedRowText', + type: 'text', + access: { + read: () => false, + update: () => false, + create: () => false, + }, + }, + ], + }, + { + type: 'collapsible', + label: 'Access', + fields: [ + { + name: 'restrictedCollapsibleText', + type: 'text', + access: { + read: () => false, + update: () => false, + create: () => false, + }, + }, + ], + }, ], }, { diff --git a/test/access-control/e2e.spec.ts b/test/access-control/e2e.spec.ts index f248966fa..afbdbe64f 100644 --- a/test/access-control/e2e.spec.ts +++ b/test/access-control/e2e.spec.ts @@ -44,7 +44,31 @@ describe('access control', () => { await page.goto(url.edit(id)); - await expect(page.locator('input[name="restrictedField"]')).toHaveCount(0); + await expect(page.locator('#field-restrictedField')).toHaveCount(0); + }); + + test('field without read access inside a group should not show', async () => { + const { id } = await createDoc({ restrictedField: 'restricted' }); + + await page.goto(url.edit(id)); + + await expect(page.locator('#field-group__restrictedGroupText')).toHaveCount(0); + }); + + test('field without read access inside a collapsible should not show', async () => { + const { id } = await createDoc({ restrictedField: 'restricted' }); + + await page.goto(url.edit(id)); + + await expect(page.locator('#field-restrictedRowText')).toHaveCount(0); + }); + + test('field without read access inside a row should not show', async () => { + const { id } = await createDoc({ restrictedField: 'restricted' }); + + await page.goto(url.edit(id)); + + await expect(page.locator('#field-restrictedCollapsibleText')).toHaveCount(0); }); describe('restricted collection', () => { diff --git a/test/admin/payload-types.ts b/test/admin/payload-types.ts index b1e2c80c4..fa4d33168 100644 --- a/test/admin/payload-types.ts +++ b/test/admin/payload-types.ts @@ -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; +} diff --git a/test/endpoints/config.ts b/test/endpoints/config.ts index 171a3690c..4d105cc7e 100644 --- a/test/endpoints/config.ts +++ b/test/endpoints/config.ts @@ -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); 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/fields/payload-types.ts b/test/fields/payload-types.ts index a10e766fe..8bba44203 100644 --- a/test/fields/payload-types.ts +++ b/test/fields/payload-types.ts @@ -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; } 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', }, diff --git a/yarn.lock b/yarn.lock index 421e3dc6a..8c04dfb09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11115,10 +11115,10 @@ sass-loader@^12.6.0: klona "^2.0.4" neo-async "^2.6.2" -sass@^1.52.1: - version "1.54.0" - resolved "https://registry.npmjs.org/sass/-/sass-1.54.0.tgz#24873673265e2a4fe3d3a997f714971db2fba1f4" - integrity sha512-C4zp79GCXZfK0yoHZg+GxF818/aclhp9F48XBu/+bm9vXEVAYov9iU3FBVRMq3Hx3OA4jfKL+p2K9180mEh0xQ== +sass@^1.55.0: + version "1.55.0" + resolved "https://registry.npmjs.org/sass/-/sass-1.55.0.tgz#0c4d3c293cfe8f8a2e8d3b666e1cf1bff8065d1c" + integrity sha512-Pk+PMy7OGLs9WaxZGJMn7S96dvlyVBwwtToX895WmCpAOr5YiJYEUJfiJidMuKb613z2xNWcXCHEuOvjZbqC6A== dependencies: chokidar ">=3.0.0 <4.0.0" immutable "^4.0.0"