refactor: integration loader throws if unauthenticated client#35
refactor: integration loader throws if unauthenticated client#35sea-grass wants to merge 3 commits into
Conversation
Greptile SummaryThis PR refactors the integration loader so it receives an injected
Confidence Score: 3/5Not ready to merge — the debug console.log leaks response data on every build, and the sequential fetch loop will noticeably slow builds until fixed. Two issues in the loader itself need to be addressed before this lands: a forgotten debug log that dumps workspace JSON to stdout unconditionally, and a regression from parallel to sequential integration fetching that will add meaningful latency to every build. The structural refactor (injected client, moved integrations map, auth-check before fetching) is otherwise clean and intentional. src/bach/loaders/integrations.ts needs the most attention — the debug log and sequential fetch loop are both in that file. Important Files Changed
Sequence DiagramsequenceDiagram
participant CC as content.config.ts
participant IL as integrationsLoader
participant ECA as ensureClientAuthenticated
participant BP as Botpress API
CC->>CC: Check BOTPRESS_API_TOKEN (throws if missing)
CC->>CC: "new Client({ token })"
CC->>IL: "integrationsLoader({ client, integrations })"
Note over IL: During Astro build load()
IL->>ECA: ensureClientAuthenticated(client)
ECA->>BP: "listPublicWorkspaces({ pageSize: 0 })"
alt 401 Unauthorized
BP-->>ECA: "Error { code: 401 }"
ECA-->>IL: throw not-authenticated Error
IL-->>CC: Build fails
else Other error
BP-->>ECA: Error
ECA-->>IL: re-throw
else Success
BP-->>ECA: workspace list
ECA->>ECA: console.log(JSON.stringify(w))
ECA-->>IL: return void
end
loop Sequential for...in
IL->>BP: "getPublicIntegration({ name, version: latest })"
alt Success
BP-->>IL: integration data
IL->>IL: "store.set({ id: slug, data })"
else Error
BP-->>IL: Error
IL->>IL: logger.warn(...)
end
end
Reviews (1): Last reviewed commit: "comments" | Re-trigger Greptile |
| const w = await client.listPublicWorkspaces({ | ||
| pageSize: 0, | ||
| }) | ||
| console.log(JSON.stringify(w, null, 2)) |
There was a problem hiding this comment.
Debug
console.log left in production code
ensureClientAuthenticated calls console.log(JSON.stringify(w, null, 2)) unconditionally, which dumps the full workspace API response as formatted JSON to stdout on every build. This is clearly a debug artifact — remove it before merging.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| for (const slug in integrations) { | ||
| const name = integrations[slug] | ||
| try { | ||
| const data = await getLatestIntegrationData(client, name) | ||
| store.set({ id: slug, data }) | ||
| } catch (err) { | ||
| logger.warn(`Failed to fetch integration "${name}" (slug: "${slug}"): ${err}`) | ||
| } | ||
| } |
There was a problem hiding this comment.
Sequential fetching replaces parallel — significant build slowdown
The original loader used Promise.all to fetch all integrations concurrently. The new loop processes each of the ~40 integrations one at a time. Each getPublicIntegration call involves a network round-trip, so this change could easily multiply build time by the number of integrations (e.g. 40 × 200 ms ≈ 8 s extra). Converting the loop back to Promise.all(Object.entries(integrations).map(...)) would restore the original performance.
| const token = import.meta.env.BOTPRESS_API_TOKEN | ||
| if (!token) { | ||
| throw new Error('Missing BOTPRESS_API_TOKEN') | ||
| } |
There was a problem hiding this comment.
Hard module-level throw blocks all builds without a token
The previous loader gracefully degraded (warned and returned empty data) when BOTPRESS_API_TOKEN was absent. Throwing at module-load time means any environment — local development, preview deploys, CI jobs that build non-integration pages — will fail immediately rather than falling back to empty integration data. Is this the intended trade-off, or should there be a way to opt out of the hard failure for local dev? Was the intent to require the token in all build environments, or only in CI/production? The previous warn-and-skip behavior allowed local builds without the token.
1470efa to
7878e28
Compare
This PR refactors the integration loader to perform an auth check when loading the content. It will throw an error (stopping the build) if the check is unsuccessful.
Also moves the integration mapping from the integration loader into the content config to keep content config in one place.