feat: passing collections-rest int suite

This commit is contained in:
Jarrod Flesch
2024-02-16 13:41:48 -05:00
parent 2175562b9a
commit 5927bf8149
4 changed files with 543 additions and 412 deletions

View File

@@ -261,7 +261,7 @@ export type Endpoint<U = User> = {
* Please add "root" routes under the /api folder in the Payload Project.
* https://nextjs.org/docs/app/api-reference/file-conventions/route
*/
root: never
root?: never
}
export type AdminViewConfig = {

View File

@@ -19,17 +19,16 @@ describe('collections-graphql', () => {
payload = await getPayload({ config })
restClient = new NextRESTClient(payload.config)
// TODO: reenable when we migrate back to mongoose v6
// Wait for indexes to be created,
// as we need them to query by point
// if (payload.db.name === 'mongoose') {
// await new Promise((resolve, reject) => {
// payload.db?.collections?.point?.ensureIndexes(function (err) {
// if (err) reject(err)
// resolve(true)
// })
// })
// }
if (payload.db.name === 'mongoose') {
await new Promise((resolve, reject) => {
payload.db?.collections?.point?.ensureIndexes(function (err) {
if (err) reject(err)
resolve(true)
})
})
}
})
afterAll(async () => {

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,7 @@
import type { Where } from 'payload/types'
import QueryString from 'qs'
import type { SanitizedConfig } from '../../packages/payload/src/config/types'
import { GRAPHQL_POST as createGraphqlPOST } from '../../packages/next/src/routes/graphql'
@@ -9,6 +13,28 @@ import {
} from '../../packages/next/src/routes/rest'
type ValidPath = `/${string}`
type RequestQuery = {
query?: {
limit?: number
page?: number
sort?: string
where?: Where
}
}
function generateQueryString(query: RequestQuery['query']): string {
return QueryString.stringify(
{
...(query?.where ? { where: query.where } : {}),
limit: query?.limit,
page: query?.page,
sort: query?.sort,
},
{
addQueryPrefix: true,
},
)
}
export class NextRESTClient {
private _DELETE: (request: Request, args: { params: { slug: string[] } }) => Promise<Response>
@@ -48,15 +74,38 @@ export class NextRESTClient {
}
}
async DELETE(path: ValidPath, options: RequestInit): Promise<Response> {
async DELETE(path: ValidPath, options: RequestInit & RequestQuery = {}): Promise<Response> {
const { url, slug } = this.generateRequestParts(path)
const request = new Request(url, { ...options, method: 'DELETE' })
const { query, ...rest } = options || {}
const whereQuery = generateQueryString(query)
const request = new Request(`${url}${whereQuery}`, {
...rest,
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
...(options?.headers || {}),
},
})
return this._DELETE(request, { params: { slug } })
}
async GET(path: ValidPath, options?: Omit<RequestInit, 'body'>): Promise<Response> {
async GET(
path: ValidPath,
options: Omit<RequestInit, 'body'> & RequestQuery = {},
): Promise<Response> {
const { url, slug } = this.generateRequestParts(path)
const request = new Request(url, { ...options, method: 'GET' })
const { query, ...rest } = options || {}
const whereQuery = generateQueryString(query)
const request = new Request(`${url}${whereQuery}`, {
...rest,
method: 'GET',
headers: new Headers({
'Content-Type': 'application/json',
...(options?.headers || {}),
}),
})
return this._GET(request, { params: { slug } })
}
@@ -75,13 +124,23 @@ export class NextRESTClient {
return this._GRAPHQL_POST(request)
}
async PATCH(path: ValidPath, options: RequestInit): Promise<Response> {
async PATCH(path: ValidPath, options: RequestInit & RequestQuery): Promise<Response> {
const { url, slug } = this.generateRequestParts(path)
const request = new Request(url, { ...options, method: 'PATCH' })
const { query, ...rest } = options
const whereQuery = generateQueryString(query)
const request = new Request(`${url}${whereQuery}`, {
...rest,
method: 'PATCH',
headers: new Headers({
'Content-Type': 'application/json',
...(options?.headers || {}),
}),
})
return this._PATCH(request, { params: { slug } })
}
async POST(path: ValidPath, options?: RequestInit): Promise<Response> {
async POST(path: ValidPath, options: RequestInit = {}): Promise<Response> {
const { url, slug } = this.generateRequestParts(path)
const request = new Request(url, {