Skip to content

Commit 4b8e2fc

Browse files
committed
fix: hubspot plugin handle missing authors
1 parent 7f897b7 commit 4b8e2fc

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

plugins/hubspot/src/PluginError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export class PluginError extends Error {
22
constructor(
33
public title: string,
4-
message: string
4+
message: string,
5+
public status?: number
56
) {
67
super(message)
78
Object.setPrototypeOf(this, PluginError.prototype)

plugins/hubspot/src/api.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ const request = async ({ path, method, query, body }: RequestOptions): Promise<u
149149
}
150150

151151
if (!res.ok) {
152-
throw new PluginError("Fetch Failed", `Failed to fetch HubSpot API: ${res.status}`)
152+
throw new PluginError("Fetch Failed", `Failed to fetch HubSpot API: ${res.status}`, res.status)
153153
}
154154

155155
return await res.json()
@@ -179,11 +179,20 @@ export const fetchAllBlogPosts = (limit: number, properties: string[]): Promise<
179179
)
180180
}
181181

182-
export const fetchBlogAuthor = (authorId: string): Promise<BlogAuthor> => {
183-
return cachedFetch(
184-
queryKeys.blogAuthor(authorId),
185-
() => request({ path: `/cms/v3/blogs/authors/${authorId}` }) as Promise<BlogAuthor>
186-
)
182+
export async function fetchBlogAuthor(authorId: string): Promise<BlogAuthor | undefined> {
183+
try {
184+
return await cachedFetch(
185+
queryKeys.blogAuthor(authorId),
186+
() =>
187+
request({
188+
path: `/cms/v3/blogs/authors/${authorId}`,
189+
query: { archived: "true" },
190+
}) as Promise<BlogAuthor>
191+
)
192+
} catch (e) {
193+
if (e instanceof PluginError && e.status === 404) return undefined
194+
throw e
195+
}
187196
}
188197

189198
export const fetchPublishedTables = (limit: number): Promise<CMSPaging<HubDbTableV3>> => {

plugins/hubspot/src/blog.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ export async function syncBlogs({ fields, includedFieldIds }: SyncBlogMutation)
251251

252252
const authorNamesById = new Map<string, string>()
253253
if (hasAuthorNameField) {
254-
const authorIds = Array.from(new Set(posts.map(post => post.blogAuthorId)))
255-
const authors = authorIds.length ? await Promise.all(authorIds.map(fetchBlogAuthor)) : []
254+
// Blog posts should have a blogAuthorId, but drafts or unexpected data may not.
255+
// Gracefully fall back to the last updated author name in those cases.
256+
const authorIds = Array.from(
257+
new Set(
258+
posts.map(post => post.blogAuthorId).filter((id): id is string => typeof id === "string" && id !== "")
259+
)
260+
)
261+
const authors = authorIds.length ? (await Promise.all(authorIds.map(fetchBlogAuthor))).filter(isDefined) : []
256262

257263
for (const author of authors) {
258264
authorNamesById.set(author.id, author.displayName)

0 commit comments

Comments
 (0)