Skip to content

Commit c8546b5

Browse files
committed
fix: Correct bulk action types to match Contentful SDK expectations
1 parent 2c9e43d commit c8546b5

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

src/handlers/bulk-action-handlers.ts

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ interface VersionedLink {
3232
type: "Link"
3333
linkType: "Entry" | "Asset"
3434
id: string
35-
version?: number
35+
version: number
3636
}
3737
}
3838

39+
// Define a Collection type to match SDK expectations
40+
interface Collection<T> {
41+
sys: {
42+
type: "Array"
43+
}
44+
items: T[]
45+
}
46+
3947
type BulkUnpublishParams = BulkPublishParams
4048

4149
type BulkValidateParams = {
@@ -52,7 +60,7 @@ export const bulkActionHandlers = {
5260
const contentfulClient = await getContentfulClient()
5361

5462
// Get the current version of each entity
55-
const entityVersions: VersionedLink[] = await Promise.all(
63+
const entityVersions = await Promise.all(
5664
args.entities.map(async (entity) => {
5765
try {
5866
// Get the current version of the entity
@@ -71,38 +79,34 @@ export const bulkActionHandlers = {
7179
return {
7280
sys: {
7381
type: "Link",
74-
linkType: entity.sys.type,
82+
linkType: entity.sys.type as "Entry" | "Asset",
7583
id: entity.sys.id,
7684
version: currentEntity.sys.version
7785
}
7886
};
7987
} catch (error) {
8088
console.error(`Error fetching entity ${entity.sys.id}: ${error}`);
81-
// Return without version if we can't get it
82-
return {
83-
sys: {
84-
type: "Link",
85-
linkType: entity.sys.type,
86-
id: entity.sys.id
87-
}
88-
};
89+
throw new Error(`Failed to get version for entity ${entity.sys.id}. All entities must have a version.`);
8990
}
9091
})
9192
);
9293

94+
// Create the collection object with the correct structure
95+
const entitiesCollection: Collection<VersionedLink> = {
96+
sys: {
97+
type: "Array"
98+
},
99+
items: entityVersions
100+
};
101+
93102
// Create the bulk action
94103
const bulkAction = await contentfulClient.bulkAction.publish(
95104
{
96105
spaceId,
97106
environmentId,
98107
},
99108
{
100-
entities: {
101-
sys: {
102-
type: "Array",
103-
},
104-
items: entityVersions,
105-
},
109+
entities: entitiesCollection,
106110
},
107111
)
108112

@@ -143,7 +147,7 @@ export const bulkActionHandlers = {
143147
const contentfulClient = await getContentfulClient()
144148

145149
// Get the current version of each entity
146-
const entityVersions: VersionedLink[] = await Promise.all(
150+
const entityVersions = await Promise.all(
147151
args.entities.map(async (entity) => {
148152
try {
149153
// Get the current version of the entity
@@ -162,38 +166,34 @@ export const bulkActionHandlers = {
162166
return {
163167
sys: {
164168
type: "Link",
165-
linkType: entity.sys.type,
169+
linkType: entity.sys.type as "Entry" | "Asset",
166170
id: entity.sys.id,
167171
version: currentEntity.sys.version
168172
}
169173
};
170174
} catch (error) {
171175
console.error(`Error fetching entity ${entity.sys.id}: ${error}`);
172-
// Return without version if we can't get it
173-
return {
174-
sys: {
175-
type: "Link",
176-
linkType: entity.sys.type,
177-
id: entity.sys.id
178-
}
179-
};
176+
throw new Error(`Failed to get version for entity ${entity.sys.id}. All entities must have a version.`);
180177
}
181178
})
182179
);
183180

181+
// Create the collection object with the correct structure
182+
const entitiesCollection: Collection<VersionedLink> = {
183+
sys: {
184+
type: "Array"
185+
},
186+
items: entityVersions
187+
};
188+
184189
// Create the bulk action
185190
const bulkAction = await contentfulClient.bulkAction.unpublish(
186191
{
187192
spaceId,
188193
environmentId,
189194
},
190195
{
191-
entities: {
192-
sys: {
193-
type: "Array",
194-
},
195-
items: entityVersions,
196-
},
196+
entities: entitiesCollection,
197197
},
198198
)
199199

@@ -234,7 +234,7 @@ export const bulkActionHandlers = {
234234
const contentfulClient = await getContentfulClient()
235235

236236
// Get the current version of each entry
237-
const entityVersions: VersionedLink[] = await Promise.all(
237+
const entityVersions = await Promise.all(
238238
args.entryIds.map(async (id) => {
239239
try {
240240
// Get the current version of the entry
@@ -254,31 +254,27 @@ export const bulkActionHandlers = {
254254
};
255255
} catch (error) {
256256
console.error(`Error fetching entry ${id}: ${error}`);
257-
// Return without version if we can't get it
258-
return {
259-
sys: {
260-
type: "Link",
261-
linkType: "Entry",
262-
id
263-
}
264-
};
257+
throw new Error(`Failed to get version for entry ${id}. All entries must have a version.`);
265258
}
266259
})
267260
);
268261

262+
// Create the collection object with the correct structure
263+
const entitiesCollection: Collection<VersionedLink> = {
264+
sys: {
265+
type: "Array"
266+
},
267+
items: entityVersions
268+
};
269+
269270
// Create the bulk action
270271
const bulkAction = await contentfulClient.bulkAction.validate(
271272
{
272273
spaceId,
273274
environmentId,
274275
},
275276
{
276-
entities: {
277-
sys: {
278-
type: "Array",
279-
},
280-
items: entityVersions,
281-
},
277+
entities: entitiesCollection,
282278
},
283279
)
284280

0 commit comments

Comments
 (0)