Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/hubspot/src/PluginError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export class PluginError extends Error {
constructor(
public title: string,
message: string
message: string,
public status?: number
) {
super(message)
Object.setPrototypeOf(this, PluginError.prototype)
Expand Down
21 changes: 15 additions & 6 deletions plugins/hubspot/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const request = async ({ path, method, query, body }: RequestOptions): Promise<u
}

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

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

export const fetchBlogAuthor = (authorId: string): Promise<BlogAuthor> => {
return cachedFetch(
queryKeys.blogAuthor(authorId),
() => request({ path: `/cms/v3/blogs/authors/${authorId}` }) as Promise<BlogAuthor>
)
export async function fetchBlogAuthor(authorId: string): Promise<BlogAuthor | undefined> {
try {
return await cachedFetch(
queryKeys.blogAuthor(authorId),
() =>
request({
path: `/cms/v3/blogs/authors/${authorId}`,
query: { archived: "true" },
}) as Promise<BlogAuthor>
)
} catch (e) {
if (e instanceof PluginError && e.status === 404) return undefined
throw e
}
}

export const fetchPublishedTables = (limit: number): Promise<CMSPaging<HubDbTableV3>> => {
Expand Down
10 changes: 8 additions & 2 deletions plugins/hubspot/src/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,14 @@ export async function syncBlogs({ fields, includedFieldIds }: SyncBlogMutation)

const authorNamesById = new Map<string, string>()
if (hasAuthorNameField) {
const authorIds = Array.from(new Set(posts.map(post => post.blogAuthorId)))
const authors = authorIds.length ? await Promise.all(authorIds.map(fetchBlogAuthor)) : []
// Blog posts should have a blogAuthorId, but drafts or unexpected data may not.
// Gracefully fall back to the last updated author name in those cases.
const authorIds = Array.from(
new Set(
Comment thread
djohalo2 marked this conversation as resolved.
posts.map(post => post.blogAuthorId).filter((id): id is string => typeof id === "string" && id !== "")
)
)
const authors = authorIds.length ? (await Promise.all(authorIds.map(fetchBlogAuthor))).filter(isDefined) : []

for (const author of authors) {
authorNamesById.set(author.id, author.displayName)
Expand Down
Loading