0
Search Records
One script reply has been approved by the moderators Verified

Searches for records of a given Content Model

Created by hugo697 118 days ago Viewed 207 times
0
Submitted by hugo697 Bun
Verified 118 days ago
1
import Contentful from 'contentful-management'
2

3
type Contentful = {
4
	accessToken: string
5
	environment: string
6
	spaceId: string
7
}
8

9
const makeClient = (resource: Contentful) => {
10
	return Contentful.createClient(
11
		{ accessToken: resource.accessToken },
12
		{
13
			type: 'plain',
14
			defaults: { spaceId: resource.spaceId, environmentId: resource.environment }
15
		}
16
	)
17
}
18

19
export async function main(
20
	resource: Contentful,
21
	propsValue: {
22
		locale: string
23
		contentTypeId: string
24
		query: object
25
		limit?: number
26
		skip?: number
27
		include?: number
28
		select?: string[]
29
	}
30
) {
31
	const client = makeClient(resource)
32
	const select = propsValue.select?.join(',')
33

34
	const response = await client.entry.getMany({
35
		query: {
36
			...propsValue.query,
37
			limit: propsValue.limit || 10,
38
			skip: propsValue.skip || 0,
39
			content_type: propsValue.contentTypeId,
40
			include: propsValue.include || 1,
41
			select: select || undefined
42
		}
43
	})
44

45
	return response
46
}
47