Compare commits

...

2 Commits

Author SHA1 Message Date
Jacob Fletcher
e740cef308 fix slug 2025-01-16 11:39:55 -05:00
Jacob Fletcher
75041afe90 test: ensures api errors are hidden from rest responses when not using config.debug 2025-01-16 11:13:17 -05:00
3 changed files with 165 additions and 118 deletions

View File

@@ -73,7 +73,7 @@ export const handleEndpoints = async ({
// This can be used against GET request search params size limit.
// Instead you can do POST request with a text body as search params.
// We use this interally for relationships querying on the frontend
// We use this internally for relationships querying on the frontend
// packages/ui/src/fields/Relationship/index.tsx
if (
request.method.toLowerCase() === 'post' &&
@@ -217,17 +217,20 @@ export const handleEndpoints = async ({
}
const response = await handler(req)
return new Response(response.body, {
headers: mergeHeaders(req.responseHeaders ?? new Headers(), response.headers),
status: response.status,
statusText: response.statusText,
})
} catch (err) {
return routeError({
const result = await routeError({
collection,
config: incomingConfig,
err,
req,
})
return result
}
}

View File

@@ -34,7 +34,7 @@ const collectionWithName = (collectionSlug: string): CollectionConfig => {
}
}
export const slug = 'posts'
export const postsSlug = 'posts'
export const relationSlug = 'relation'
export const pointSlug = 'point'
export const customIdSlug = 'custom-id'
@@ -51,7 +51,7 @@ export default buildConfigWithDefaults({
},
collections: [
{
slug,
slug: postsSlug,
access: openAccess,
fields: [
{
@@ -346,14 +346,14 @@ export default buildConfigWithDefaults({
// Relation - hasMany
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationHasManyField: rel1.id,
title: 'rel to hasMany',
},
})
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationHasManyField: rel2.id,
title: 'rel to hasMany 2',
@@ -362,7 +362,7 @@ export default buildConfigWithDefaults({
// Relation - relationTo multi
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationMultiRelationTo: {
relationTo: relationSlug,
@@ -374,7 +374,7 @@ export default buildConfigWithDefaults({
// Relation - relationTo multi hasMany
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationMultiRelationToHasMany: [
{

View File

@@ -18,7 +18,7 @@ import {
methods,
pointSlug,
relationSlug,
slug,
postsSlug,
} from './config.js'
const filename = fileURLToPath(import.meta.url)
@@ -47,15 +47,15 @@ describe('collections-rest', () => {
const data = {
title: 'title',
}
const doc = await createPost(data)
const { doc } = await createPost(data)
expect(doc).toMatchObject(data)
})
it('should find', async () => {
const post1 = await createPost()
const post2 = await createPost()
const response = await restClient.GET(`/${slug}`)
const { doc: post1 } = await createPost()
const { doc: post2 } = await createPost()
const response = await restClient.GET(`/${postsSlug}`)
const result = await response.json()
expect(response.status).toEqual(200)
@@ -68,7 +68,7 @@ describe('collections-rest', () => {
it('should count', async () => {
await createPost()
await createPost()
const response = await restClient.GET(`/${slug}/count`)
const response = await restClient.GET(`/${postsSlug}/count`)
const result = await response.json()
expect(response.status).toEqual(200)
@@ -76,9 +76,9 @@ describe('collections-rest', () => {
})
it('should find where id', async () => {
const post1 = await createPost()
const { doc: post1 } = await createPost()
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { id: { equals: post1.id } },
},
@@ -91,11 +91,11 @@ describe('collections-rest', () => {
})
it('should find with pagination false', async () => {
const post1 = await createPost()
const post2 = await createPost()
const { doc: post1 } = await createPost()
const { doc: post2 } = await createPost()
const { docs, totalDocs } = await payload.find({
collection: slug,
collection: postsSlug,
overrideAccess: false,
pagination: false,
})
@@ -108,10 +108,12 @@ describe('collections-rest', () => {
})
it('should update existing', async () => {
const { id, description } = await createPost({ description: 'desc' })
const {
doc: { id, description },
} = await createPost({ description: 'desc' })
const updatedTitle = 'updated-title'
const response = await restClient.PATCH(`/${slug}/${id}`, {
const response = await restClient.PATCH(`/${postsSlug}/${id}`, {
body: JSON.stringify({ title: updatedTitle }),
})
const { doc } = await response.json()
@@ -128,7 +130,7 @@ describe('collections-rest', () => {
}
const description = 'updated'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -146,12 +148,12 @@ describe('collections-rest', () => {
it('should bulk update with limit', async () => {
const ids = []
for (let i = 0; i < 3; i++) {
const post = await createPost({ description: `to-update` })
const { doc: post } = await createPost({ description: `to-update` })
ids.push(post.id)
}
const description = 'updated-description'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -167,7 +169,7 @@ describe('collections-rest', () => {
const { docs: resDocs } = await payload.find({
limit: 10,
collection: slug,
collection: postsSlug,
where: { id: { in: ids } },
})
expect(resDocs.at(-1).description).toEqual('to-update')
@@ -180,7 +182,7 @@ describe('collections-rest', () => {
const description = 'updated'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -193,7 +195,7 @@ describe('collections-rest', () => {
expect(errors).toHaveLength(1)
const { docs } = await payload.find({
collection: slug,
collection: postsSlug,
})
expect(docs[0].description).not.toEqual(description)
@@ -206,7 +208,7 @@ describe('collections-rest', () => {
}
const description = 'updated'
const relationFieldResponse = await restClient.PATCH(`/${slug}`, {
const relationFieldResponse = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -214,7 +216,7 @@ describe('collections-rest', () => {
})
expect(relationFieldResponse.status).toEqual(400)
const relationMultiRelationToResponse = await restClient.PATCH(`/${slug}`, {
const relationMultiRelationToResponse = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -223,7 +225,7 @@ describe('collections-rest', () => {
expect(relationMultiRelationToResponse.status).toEqual(400)
const { docs } = await payload.find({
collection: slug,
collection: postsSlug,
})
expect(docs[0].description).not.toEqual(description)
@@ -232,14 +234,14 @@ describe('collections-rest', () => {
it('should not bulk update with a read restricted field query', async () => {
const { id } = await payload.create({
collection: slug,
collection: postsSlug,
data: {
restrictedField: 'restricted',
},
})
const description = 'description'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -249,7 +251,7 @@ describe('collections-rest', () => {
const doc = await payload.findByID({
id,
collection: slug,
collection: postsSlug,
})
expect(response.status).toEqual(400)
@@ -301,7 +303,7 @@ describe('collections-rest', () => {
await createPost({ description: `desc ${i}` })
}
const response = await restClient.DELETE(`/${slug}`, {
const response = await restClient.DELETE(`/${postsSlug}`, {
query: { where: { title: { equals: 'title' } } },
})
const { docs } = await response.json()
@@ -479,9 +481,11 @@ describe('collections-rest', () => {
})
it('should delete', async () => {
const { id } = await createPost()
const {
doc: { id },
} = await createPost()
const response = await restClient.DELETE(`/${slug}/${id}`)
const response = await restClient.DELETE(`/${postsSlug}/${id}`)
const { doc } = await response.json()
expect(response.status).toEqual(200)
@@ -491,7 +495,7 @@ describe('collections-rest', () => {
it('should include metadata', async () => {
await createPosts(11)
const result = await restClient.GET(`/${slug}`).then((res) => res.json())
const result = await restClient.GET(`/${postsSlug}`).then((res) => res.json())
expect(result.totalDocs).toBeGreaterThan(0)
expect(result.limit).toBe(10)
@@ -525,16 +529,18 @@ describe('collections-rest', () => {
})
.then((res) => res.json()))
post = await createPost({
const res = await createPost({
relationField: relation.id,
})
doc = res.doc
await createPost() // Extra post to allow asserting totalDoc count
})
describe('regular relationship', () => {
it('query by property value', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -547,7 +553,7 @@ describe('collections-rest', () => {
})
it('should count query by property value', async () => {
const response = await restClient.GET(`/${slug}/count`, {
const response = await restClient.GET(`/${postsSlug}/count`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -559,7 +565,7 @@ describe('collections-rest', () => {
})
it('query by id', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -573,13 +579,13 @@ describe('collections-rest', () => {
it('should query LIKE by ID', async () => {
const post = await payload.create({
collection: slug,
collection: postsSlug,
data: {
title: 'find me buddy',
},
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
id: {
@@ -596,11 +602,11 @@ describe('collections-rest', () => {
})
it('should query nested relationship - hasMany', async () => {
const post1 = await createPost({
const { doc: post1 } = await createPost({
relationHasManyField: [relation.id, relation2.id],
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationHasManyField.name': { equals: relation.name } },
},
@@ -612,7 +618,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// Query second relationship
const response2 = await restClient.GET(`/${slug}`, {
const response2 = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationHasManyField.name': { equals: relation2.name } },
},
@@ -626,12 +632,13 @@ describe('collections-rest', () => {
describe('relationTo multi', () => {
it('nested by id', async () => {
const post1 = await createPost({
const { doc: post1 } = await createPost({
relationMultiRelationTo: { relationTo: relationSlug, value: relation.id },
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationTo.value': { equals: relation.id } },
},
@@ -645,12 +652,13 @@ describe('collections-rest', () => {
})
it('should query relationships by not_equals', async () => {
const ogPost = await createPost({
const { doc: ogPost } = await createPost({
relationMultiRelationTo: { relationTo: relationSlug, value: relation.id },
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
and: [
@@ -670,15 +678,16 @@ describe('collections-rest', () => {
describe('relationTo multi hasMany', () => {
it('nested by id', async () => {
const post1 = await createPost({
const { doc: post1 } = await createPost({
relationMultiRelationToHasMany: [
{ relationTo: relationSlug, value: relation.id },
{ relationTo: relationSlug, value: relation2.id },
],
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationToHasMany.value': { equals: relation.id } },
},
@@ -690,7 +699,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// Query second relation
const response2 = await restClient.GET(`/${slug}`, {
const response2 = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationToHasMany.value': { equals: relation.id } },
},
@@ -711,7 +720,7 @@ describe('collections-rest', () => {
const test = 'test'
await createPost({ fakeLocalization: test })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { fakeLocalization: { equals: test } },
},
@@ -723,7 +732,7 @@ describe('collections-rest', () => {
})
it('should not error when attempting to sort on a field that does not exist', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
sort: 'fake',
},
@@ -736,9 +745,11 @@ describe('collections-rest', () => {
describe('Operators', () => {
it('equals', async () => {
const valueToQuery = 'valueToQuery'
const post1 = await createPost({ title: valueToQuery })
const { doc: post1 } = await createPost({ title: valueToQuery })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { equals: valueToQuery } },
},
@@ -751,14 +762,16 @@ describe('collections-rest', () => {
})
it('not_equals', async () => {
const post1 = await createPost({ title: 'not-equals' })
const post2 = await createPost()
const post3 = await createPost({ title: undefined })
const response = await restClient.GET(`/${slug}`, {
const { doc: post1 } = await createPost({ title: 'not-equals' })
const { doc: post2 } = await createPost()
const { doc: post3 } = await createPost({ title: undefined })
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { not_equals: post1.title } },
},
})
const result = await response.json()
expect(response.status).toEqual(200)
@@ -767,9 +780,11 @@ describe('collections-rest', () => {
})
it('in', async () => {
const post1 = await createPost({ title: 'my-title' })
const { doc: post1 } = await createPost({ title: 'my-title' })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { in: [post1.title] } },
},
@@ -782,9 +797,10 @@ describe('collections-rest', () => {
})
it('not_in', async () => {
const post1 = await createPost({ title: 'not-me' })
const post2 = await createPost()
const response = await restClient.GET(`/${slug}`, {
const { doc: post1 } = await createPost({ title: 'not-me' })
const { doc: post2 } = await createPost()
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { not_in: [post1.title] } },
},
@@ -804,8 +820,8 @@ describe('collections-rest', () => {
await createPost({ relationField: relationship.id, title: 'not-me' })
// await createPost({ relationMultiRelationTo: relationship.id, title: 'not-me' })
const post2 = await createPost({ title: 'me' })
const response = await restClient.GET(`/${slug}`, {
const { doc: post2 } = await createPost({ title: 'me' })
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { not_in: [relationship.id] } },
},
@@ -817,7 +833,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// do not want to error for empty arrays
const emptyNotInResponse = await restClient.GET(`/${slug}`, {
const emptyNotInResponse = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { not_in: [] } },
},
@@ -835,7 +851,7 @@ describe('collections-rest', () => {
const post1 = await createPost({ relationField: relationship.id, title: 'me' })
// await createPost({ relationMultiRelationTo: relationship.id, title: 'not-me' })
await createPost({ title: 'not-me' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { in: [relationship.id] } },
},
@@ -847,7 +863,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// do not want to error for empty arrays
const emptyNotInResponse = await restClient.GET(`/${slug}`, {
const emptyNotInResponse = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { in: [] } },
},
@@ -857,13 +873,14 @@ describe('collections-rest', () => {
})
it('like', async () => {
const post1 = await createPost({ title: 'prefix-value' })
const { doc: post1 } = await createPost({ title: 'prefix-value' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { like: 'prefix' } },
},
})
const result = await response.json()
expect(response.status).toEqual(200)
@@ -877,11 +894,11 @@ describe('collections-rest', () => {
it.each(specialCharacters.split(''))(
'like - special characters - %s',
async (character) => {
const post1 = await createPost({
const { doc: post1 } = await createPost({
title: specialCharacters,
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -900,9 +917,9 @@ describe('collections-rest', () => {
})
it('like - cyrillic characters', async () => {
const post1 = await createPost({ title: 'Тест' })
const { doc: post1 } = await createPost({ title: 'Тест' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -919,9 +936,9 @@ describe('collections-rest', () => {
})
it('like - cyrillic characters in multiple words', async () => {
const post1 = await createPost({ title: 'привет, это тест полезной нагрузки' })
const { doc: post1 } = await createPost({ title: 'привет, это тест полезной нагрузки' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -938,8 +955,9 @@ describe('collections-rest', () => {
})
it('like - partial word match', async () => {
const post = await createPost({ title: 'separate words should partially match' })
const response = await restClient.GET(`/${slug}`, {
const { doc: post } = await createPost({ title: 'separate words should partially match' })
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -956,9 +974,9 @@ describe('collections-rest', () => {
})
it('like - id should not crash', async () => {
const post = await createPost({ title: 'post' })
await createPost({ title: 'post' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
id: {
@@ -972,9 +990,9 @@ describe('collections-rest', () => {
})
it('exists - true', async () => {
const postWithDesc = await createPost({ description: 'exists' })
const { doc: postWithDesc } = await createPost({ description: 'exists' })
await createPost({ description: undefined })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
description: {
@@ -991,9 +1009,9 @@ describe('collections-rest', () => {
})
it('exists - false', async () => {
const postWithoutDesc = await createPost({ description: undefined })
const { doc: postWithoutDesc } = await createPost({ description: undefined })
await createPost({ description: 'exists' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
description: {
@@ -1013,12 +1031,12 @@ describe('collections-rest', () => {
let post1: Post
let post2: Post
beforeEach(async () => {
post1 = await createPost({ number: 1 })
post2 = await createPost({ number: 2 })
post1 = await createPost({ number: 1 })?.then((res) => res.doc)
post2 = await createPost({ number: 2 })?.then((res) => res.doc)
})
it('greater_than', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1035,7 +1053,7 @@ describe('collections-rest', () => {
})
it('greater_than_equal', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1054,7 +1072,7 @@ describe('collections-rest', () => {
})
it('less_than', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1071,7 +1089,7 @@ describe('collections-rest', () => {
})
it('less_than_equal', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1308,11 +1326,11 @@ describe('collections-rest', () => {
})
it('or', async () => {
const post1 = await createPost({ title: 'post1' })
const post2 = await createPost({ title: 'post2' })
const post1 = await createPost({ title: 'post1' })?.then((res) => res.doc)
const post2 = await createPost({ title: 'post2' })?.then((res) => res.doc)
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
or: [
@@ -1339,10 +1357,10 @@ describe('collections-rest', () => {
})
it('or - 1 result', async () => {
const post1 = await createPost({ title: 'post1' })
const { doc: post1 } = await createPost({ title: 'post1' })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
or: [
@@ -1370,11 +1388,11 @@ describe('collections-rest', () => {
it('and', async () => {
const description = 'description'
const post1 = await createPost({ description, title: 'post1' })
const { doc: post1 } = await createPost({ description, title: 'post1' })
await createPost({ description, title: 'post2' }) // Diff title, same desc
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
and: [
@@ -1427,13 +1445,13 @@ describe('collections-rest', () => {
},
},
}
let response = await restClient.GET(`/${slug}`, { query })
let response = await restClient.GET(`/${postsSlug}`, { query })
const page1 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 2 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 2 } })
const page2 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 3 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 3 } })
const page3 = await response.json()
expect(page1.hasNextPage).toStrictEqual(true)
@@ -1476,13 +1494,13 @@ describe('collections-rest', () => {
},
},
}
let response = await restClient.GET(`/${slug}`, { query })
let response = await restClient.GET(`/${postsSlug}`, { query })
const page1 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 2 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 2 } })
const page2 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 3 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 3 } })
const page3 = await response.json()
expect(page1.hasNextPage).toStrictEqual(true)
@@ -1524,7 +1542,7 @@ describe('collections-rest', () => {
})
it('should query a limited set of docs', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
limit: 15,
where: {
@@ -1541,7 +1559,7 @@ describe('collections-rest', () => {
})
it('should query all docs when limit=0', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
limit: 0,
where: {
@@ -1561,12 +1579,12 @@ describe('collections-rest', () => {
})
it('can query deeply nested fields within rows, tabs, collapsibles', async () => {
const withDeeplyNestedField = await createPost({
const { doc: withDeeplyNestedField } = await createPost({
D1: { D2: { D3: { D4: 'nested message' } } },
})
const result = await restClient
.GET(`/${slug}`, {
.GET(`/${postsSlug}`, {
query: {
where: {
'D1.D2.D3.D4': {
@@ -1638,14 +1656,14 @@ describe('collections-rest', () => {
},
]
const post = await createPost({})
const { doc: post } = await createPost({})
const response = await restClient.GET(
`/${slug}/${typeof post.id === 'number' ? 1000 : randomUUID()}`,
`/${postsSlug}/${typeof post.id === 'number' ? 1000 : randomUUID()}`,
)
expect(response.status).toBe(404)
expect(collection.slug).toBe(slug)
expect(collection.slug).toBe(postsSlug)
expect(err).toBeInstanceOf(NotFound)
expect(errResult).toStrictEqual({
errors: [
@@ -1660,11 +1678,32 @@ describe('collections-rest', () => {
payload.collections.posts.config.hooks.afterError = []
})
it('should hide API errors from response', async () => {
let errorMessage: string | undefined
let statusCode: number | undefined = undefined
await createPost({
D1: {
D2: {
D3: {
// @ts-expect-error
D4: {},
},
},
},
})?.then((res) => {
statusCode = res.status
errorMessage = res?.errors?.[0]?.message
})
await expect(errorMessage).toBe('Something went wrong.')
})
})
describe('Local', () => {
it('findByID should throw NotFound if the doc was not found, if disableErrors: true then return null', async () => {
const post = await createPost()
const { doc: post } = await createPost()
const id = typeof post.id === 'string' ? randomUUID() : 999
await expect(payload.findByID({ collection: 'posts', id })).rejects.toBeInstanceOf(NotFound)
await expect(
@@ -1694,12 +1733,17 @@ describe('collections-rest', () => {
})
async function createPost(overrides?: Partial<Post>) {
const { doc } = await restClient
.POST(`/${slug}`, {
const res: {
doc: Post
errors?: [{ message: string }]
status: number
} = await restClient
.POST(`/${postsSlug}`, {
body: JSON.stringify({ title: 'title', ...overrides }),
})
.then((res) => res.json())
return doc
return res
}
async function createPosts(count: number) {
@@ -1710,7 +1754,7 @@ async function createPosts(count: number) {
async function clearDocs(): Promise<void> {
await payload.delete({
collection: slug,
collection: postsSlug,
where: { id: { exists: true } },
})
}