# Breaking Changes
### New file import locations
Exports from the `payload` package have been _significantly_ cleaned up.
Now, just about everything is able to be imported from `payload`
directly, rather than an assortment of subpath exports. This means that
things like `import { buildConfig } from 'payload/config'` are now just
imported via `import { buildConfig } from 'payload'`. The mental model
is significantly simpler for developers, but you might need to update
some of your imports.
Payload now exposes only three exports:
1. `payload` - all types and server-only Payload code
2. `payload/shared` - utilities that can be used in either the browser
or in Node environments
3. `payload/node` - heavy utilities that should only be imported in Node
scripts and never be imported into bundled code like Next.js
### UI library pre-bundling
With this release, we've dramatically sped up the compile time for
Payload by pre-bundling our entire UI package for use inside of the
Payload admin itself. There are new exports that should be used within
Payload custom components:
1. `@payloadcms/ui/client` - all client components
2. `@payloadcms/ui/server` - all server components
For all of your custom Payload admin UI components, you should be
importing from one of these two pre-compiled barrel files rather than
importing from the more deeply nested exports directly. That will keep
compile times nice and speedy, and will also make sure that the bundled
JS for your admin UI is kept small.
For example, whereas before, if you imported the Payload `Button`, you
would have imported it like this:
```ts
import { Button } from '@payloadcms/ui/elements/Button'
```
Now, you would import it like this:
```ts
import { Button } from '@payloadcms/ui/client'
```
This is a significant DX / performance optimization that we're pretty
pumped about.
However, if you are importing or re-using Payload UI components
_outside_ of the Payload admin UI, for example in your own frontend
apps, you can import from the individual component exports which will
make sure that the bundled JS is kept to a minimum in your frontend
apps. So in your own frontend, you can continue to import directly to
the components that you want to consume rather than importing from the
pre-compiled barrel files.
Individual component exports will now come with their corresponding CSS
and everything will work perfectly as-expected.
### Specific exports have changed
- `'@payloadcms/ui/templates/Default'` and
`'@payloadcms/ui/templates/Minimal`' are now exported from
`'@payloadcms/next/templates'`
- Old: `import { LogOut } from '@payloadcms/ui/icons/LogOut'` new:
`import { LogOutIcon } from '@payloadcms/ui/icons/LogOut'`
## Background info
In effort to make local dev as fast as possible, we need to import as
few files as possible so that the compiler has less to process. One way
we've achieved this in the Admin Panel was to _remove_ all .scss imports
from all components in the `@payloadcms/ui` module using a build
process. This stripped all `import './index.scss'` statements out of
each component before injecting them into `dist`. Instead, it bundles
all of the CSS into a single `main.css` file, and we import _that_ at
the root of the app.
While this concept is _still_ the right solution to the problem, this
particular approach is not viable when using these components outside
the Admin Panel, where not only does this root stylesheet not exist, but
where it would also bloat your app with unused styles. Instead, we need
to _keep_ these .scss imports in place so they are imported directly
alongside your components, as expected. Then, we need create a _new_
build step that _separately_ compiles the components _without_ their
stylesheets—this way your app can consume either as needed from the new
`client` and `server` barrel files within `@payloadcms/ui`, i.e. from
within `@payloadcms/next` and all other admin-specific packages and
plugins.
This way, all other applications will simply import using the direct
file paths, just as they did before. Except now they come with
stylesheets.
And we've gotten a pretty awesome initial compilation performance boost.
---------
Co-authored-by: James <james@trbl.design>
Co-authored-by: Alessio Gravili <alessio@gravili.de>
408 lines
9.5 KiB
TypeScript
408 lines
9.5 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import type { Where } from 'payload'
|
|
import type { Config } from 'payload'
|
|
import type { PaginatedDocs } from 'payload'
|
|
|
|
import qs from 'qs'
|
|
|
|
import { devUser } from '../credentials.js'
|
|
|
|
type Args = {
|
|
defaultSlug: string
|
|
serverURL: string
|
|
}
|
|
|
|
type LoginArgs = {
|
|
collection: string
|
|
email: string
|
|
password: string
|
|
}
|
|
|
|
type LogoutArgs = {
|
|
collection: string
|
|
}
|
|
|
|
type CreateArgs<T = any> = {
|
|
auth?: boolean
|
|
data: T
|
|
file?: boolean
|
|
slug?: string
|
|
}
|
|
|
|
type FindArgs = {
|
|
auth?: boolean
|
|
depth?: number
|
|
limit?: number
|
|
page?: number
|
|
query?: Where
|
|
slug?: string
|
|
sort?: string
|
|
}
|
|
|
|
type FindByIDArgs = {
|
|
auth?: boolean
|
|
id: number | string
|
|
options?: {
|
|
depth?: number
|
|
limit?: number
|
|
page?: number
|
|
}
|
|
query?: Where
|
|
slug?: string
|
|
}
|
|
|
|
type UpdateArgs<T = any> = {
|
|
auth?: boolean
|
|
data: Partial<T>
|
|
id: string
|
|
query?: any
|
|
slug?: string
|
|
}
|
|
|
|
type UpdateManyArgs<T = any> = {
|
|
auth?: boolean
|
|
data: Partial<T>
|
|
slug?: string
|
|
where: any
|
|
}
|
|
|
|
type DeleteArgs = {
|
|
auth?: boolean
|
|
id: string
|
|
slug?: string
|
|
}
|
|
|
|
type DeleteManyArgs = {
|
|
auth?: boolean
|
|
slug?: string
|
|
where: any
|
|
}
|
|
|
|
type FindGlobalArgs<T = any> = {
|
|
auth?: boolean
|
|
slug?: string
|
|
}
|
|
|
|
type UpdateGlobalArgs<T = any> = {
|
|
auth?: boolean
|
|
data: Partial<T>
|
|
slug?: string
|
|
}
|
|
|
|
type DocResponse<T> = {
|
|
doc: T
|
|
errors?: { data: any; message: string; name: string }[]
|
|
status: number
|
|
}
|
|
|
|
type DocsResponse<T> = {
|
|
docs: T[]
|
|
errors?: { data: any; id: number | string; message: string; name: string }[]
|
|
status: number
|
|
}
|
|
|
|
const headers = {
|
|
Authorization: '',
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
type QueryResponse<T> = {
|
|
result: PaginatedDocs<T>
|
|
status: number
|
|
}
|
|
|
|
export class RESTClient {
|
|
private readonly config: Config
|
|
|
|
private defaultSlug: string
|
|
|
|
private token: string
|
|
|
|
serverURL: string
|
|
|
|
constructor(config: Config, args: Args) {
|
|
this.config = config
|
|
this.serverURL = args.serverURL
|
|
this.defaultSlug = args.defaultSlug
|
|
}
|
|
|
|
async create<T = any>(args: CreateArgs): Promise<DocResponse<T>> {
|
|
const options = {
|
|
body: args.file ? args.data : JSON.stringify(args.data),
|
|
headers: {
|
|
...(args.file ? [] : headers),
|
|
Authorization: '',
|
|
},
|
|
method: 'POST',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const slug = args.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}`, options)
|
|
const { status } = response
|
|
const { doc } = await response.json()
|
|
return { doc, status }
|
|
}
|
|
|
|
async delete<T = any>(id: string, args?: DeleteArgs): Promise<DocResponse<T>> {
|
|
const options = {
|
|
headers: { ...headers },
|
|
method: 'DELETE',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}/${id}`, options)
|
|
const { status } = response
|
|
const doc = await response.json()
|
|
return { doc, status }
|
|
}
|
|
|
|
async deleteMany<T = any>(args: DeleteManyArgs): Promise<DocsResponse<T>> {
|
|
const { where } = args
|
|
const options = {
|
|
headers: { ...headers },
|
|
method: 'DELETE',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const formattedQs = qs.stringify(
|
|
{
|
|
...(where ? { where } : {}),
|
|
},
|
|
{
|
|
addQueryPrefix: true,
|
|
},
|
|
)
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}${formattedQs}`, options)
|
|
const { status } = response
|
|
const json = await response.json()
|
|
return { docs: json.docs, errors: json.errors, status }
|
|
}
|
|
|
|
async endpoint<T = any>(
|
|
path: string,
|
|
method = 'GET',
|
|
params: any = undefined,
|
|
): Promise<{ data: T; status: number }> {
|
|
const options = {
|
|
body: JSON.stringify(params),
|
|
headers: { ...headers },
|
|
method,
|
|
}
|
|
|
|
const response = await fetch(`${this.serverURL}${path}`, options)
|
|
const { status } = response
|
|
const data = await response.json()
|
|
return { data, status }
|
|
}
|
|
|
|
async endpointWithAuth<T = any>(
|
|
path: string,
|
|
method = 'GET',
|
|
params: any = undefined,
|
|
): Promise<{ data: T; status: number }> {
|
|
const options = {
|
|
body: JSON.stringify(params),
|
|
headers: { ...headers },
|
|
method,
|
|
}
|
|
|
|
if (this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const response = await fetch(`${this.serverURL}${path}`, options)
|
|
const { status } = response
|
|
const data = await response.json()
|
|
return { data, status }
|
|
}
|
|
|
|
async find<T = any>(args?: FindArgs): Promise<QueryResponse<T>> {
|
|
const options = {
|
|
headers: { ...headers },
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const whereQuery = qs.stringify(
|
|
{
|
|
...(args?.query ? { where: args.query } : {}),
|
|
limit: args?.limit,
|
|
page: args?.page,
|
|
sort: args?.sort,
|
|
},
|
|
{
|
|
addQueryPrefix: true,
|
|
},
|
|
)
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}${whereQuery}`, options)
|
|
const { status } = response
|
|
const result = await response.json()
|
|
if (result.errors) throw new Error(result.errors[0].message)
|
|
return { result, status }
|
|
}
|
|
|
|
async findByID<T = any>(args: FindByIDArgs): Promise<DocResponse<T>> {
|
|
const options = {
|
|
headers: { ...headers },
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const formattedOpts = qs.stringify(args?.options || {}, { addQueryPrefix: true })
|
|
const response = await fetch(
|
|
`${this.serverURL}/api/${slug}/${args.id}${formattedOpts}`,
|
|
options,
|
|
)
|
|
const { status } = response
|
|
const doc = await response.json()
|
|
return { doc, status }
|
|
}
|
|
|
|
async findGlobal<T = any>(args?: FindGlobalArgs): Promise<DocResponse<T>> {
|
|
const options = {
|
|
headers: { ...headers },
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/globals/${slug}`, options)
|
|
const { status } = response
|
|
const doc = await response.json()
|
|
return { doc, status }
|
|
}
|
|
|
|
async login(incomingArgs?: LoginArgs): Promise<string> {
|
|
const args = incomingArgs ?? {
|
|
collection: 'users',
|
|
email: devUser.email,
|
|
password: devUser.password,
|
|
}
|
|
|
|
const response = await fetch(`${this.serverURL}/api/${args.collection}/login`, {
|
|
body: JSON.stringify({
|
|
email: args.email,
|
|
password: args.password,
|
|
}),
|
|
headers,
|
|
method: 'POST',
|
|
})
|
|
|
|
let { token } = await response.json()
|
|
|
|
// If the token is not in the response body, then we can extract it from the cookies
|
|
if (!token) {
|
|
const setCookie = response.headers.get('Set-Cookie')
|
|
const tokenMatchResult = setCookie?.match(/payload-token=(?<token>.+?);/)
|
|
token = tokenMatchResult?.groups?.token
|
|
}
|
|
|
|
this.token = token
|
|
|
|
return token
|
|
}
|
|
|
|
async logout(incomingArgs?: LogoutArgs): Promise<void> {
|
|
const args = incomingArgs ?? {
|
|
collection: 'users',
|
|
}
|
|
|
|
await fetch(`${this.serverURL}/api/${args.collection}/logout`, {
|
|
headers,
|
|
method: 'POST',
|
|
})
|
|
|
|
this.token = ''
|
|
}
|
|
|
|
async update<T = any>(args: UpdateArgs<T>): Promise<DocResponse<T>> {
|
|
const { id, data, query } = args
|
|
|
|
const options = {
|
|
body: JSON.stringify(data),
|
|
headers: { ...headers },
|
|
method: 'PATCH',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const formattedQs = qs.stringify(query)
|
|
const slug = args.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}/${id}${formattedQs}`, options)
|
|
const { status } = response
|
|
const json = await response.json()
|
|
return { doc: json.doc, errors: json.errors, status }
|
|
}
|
|
|
|
async updateGlobal<T = any>(args: UpdateGlobalArgs): Promise<DocResponse<T>> {
|
|
const { data } = args
|
|
const options = {
|
|
body: JSON.stringify(data),
|
|
headers: { ...headers },
|
|
method: 'POST',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/globals/${slug}`, options)
|
|
const { status } = response
|
|
const { result } = await response.json()
|
|
return { doc: result, status }
|
|
}
|
|
|
|
async updateMany<T = any>(args: UpdateManyArgs<T>): Promise<DocsResponse<T>> {
|
|
const { data, where } = args
|
|
const options = {
|
|
body: JSON.stringify(data),
|
|
headers: { ...headers },
|
|
method: 'PATCH',
|
|
}
|
|
|
|
if (args?.auth !== false && this.token) {
|
|
options.headers.Authorization = `JWT ${this.token}`
|
|
}
|
|
|
|
const formattedQs = qs.stringify(
|
|
{
|
|
...(where ? { where } : {}),
|
|
},
|
|
{
|
|
addQueryPrefix: true,
|
|
},
|
|
)
|
|
|
|
const slug = args?.slug || this.defaultSlug
|
|
const response = await fetch(`${this.serverURL}/api/${slug}${formattedQs}`, options)
|
|
const { status } = response
|
|
const json = await response.json()
|
|
return { docs: json.docs, errors: json.errors, status }
|
|
}
|
|
}
|