From 146b4ed1d73054ecb56fa18924d3a89673cf25f4 Mon Sep 17 00:00:00 2001 From: Rohin Bhargava Date: Mon, 16 Dec 2024 19:19:13 -0500 Subject: [PATCH] compiler breaks --- servers/fdr/src/api/index.ts | 1 + .../controllers/api/getApiLatestService.ts | 35 +++++++++++++++++++ .../controllers/docs/v1/getDocsReadService.ts | 2 +- servers/fdr/src/db/api/APIDefinitionDao.ts | 21 ++++++++++- servers/fdr/src/healthchecks/checkRedis.ts | 1 + 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 servers/fdr/src/controllers/api/getApiLatestService.ts diff --git a/servers/fdr/src/api/index.ts b/servers/fdr/src/api/index.ts index fe240b9774..43e36a183d 100644 --- a/servers/fdr/src/api/index.ts +++ b/servers/fdr/src/api/index.ts @@ -1,4 +1,5 @@ export * as Algolia from "./generated/api/resources/algolia"; +export { LatestService as APILatestService } from "./generated/api/resources/api/resources/latest/service/LatestService"; export { ReadService as APIV1ReadService } from "./generated/api/resources/api/resources/v1/resources/read/service/ReadService"; export { RegisterService as APIV1WriteService } from "./generated/api/resources/api/resources/v1/resources/register/service/RegisterService"; export { DiffService } from "./generated/api/resources/diff/service/DiffService"; diff --git a/servers/fdr/src/controllers/api/getApiLatestService.ts b/servers/fdr/src/controllers/api/getApiLatestService.ts new file mode 100644 index 0000000000..82fbd4baeb --- /dev/null +++ b/servers/fdr/src/controllers/api/getApiLatestService.ts @@ -0,0 +1,35 @@ +import { APILatestService } from "../../api"; +import { UserNotInOrgError } from "../../api/generated/api"; +import { ApiDoesNotExistError } from "../../api/generated/api/resources/api/resources/v1/resources/read/errors"; +import type { FdrApplication } from "../../app"; + +export function getApiLatestService(app: FdrApplication): APILatestService { + return new APILatestService({ + getApiLatest: async (req, res) => { + try { + // if the auth header belongs to fern, return the api definition + await app.services.auth.checkUserBelongsToOrg({ + authHeader: req.headers.authorization, + orgId: "fern", + }); + } catch (e) { + if (e instanceof UserNotInOrgError) { + const orgId = await app.dao.apis().getOrgIdForApiDefinition(req.params.apiDefinitionId); + if (orgId == null) { + throw new ApiDoesNotExistError(); + } + await app.services.auth.checkUserBelongsToOrg({ + authHeader: req.headers.authorization, + orgId, + }); + } + throw e; + } + const apiDefinition = await app.dao.apis().loadAPILatestDefinition(req.params.apiDefinitionId); + if (apiDefinition == null) { + throw new ApiDoesNotExistError(); + } + return res.send(apiDefinition); + }, + }); +} diff --git a/servers/fdr/src/controllers/docs/v1/getDocsReadService.ts b/servers/fdr/src/controllers/docs/v1/getDocsReadService.ts index b3c9178689..678ceec1df 100644 --- a/servers/fdr/src/controllers/docs/v1/getDocsReadService.ts +++ b/servers/fdr/src/controllers/docs/v1/getDocsReadService.ts @@ -126,7 +126,7 @@ export async function getDocsDefinition({ app.services.db.prisma.apiDefinitionsLatest.findMany({ where: { apiDefinitionId: { - in: Array.from(docsDbDefinition.referencedApisLatest), + in: Array.from(docsDbDefinition.referencedApis), }, }, }), diff --git a/servers/fdr/src/db/api/APIDefinitionDao.ts b/servers/fdr/src/db/api/APIDefinitionDao.ts index 4d33b4f91b..e3be4f8824 100644 --- a/servers/fdr/src/db/api/APIDefinitionDao.ts +++ b/servers/fdr/src/db/api/APIDefinitionDao.ts @@ -1,4 +1,4 @@ -import { APIV1Db } from "@fern-api/fdr-sdk"; +import { APIV1Db, FdrAPI } from "@fern-api/fdr-sdk"; import { PrismaClient } from "@prisma/client"; import { readBuffer } from "../../util"; @@ -7,6 +7,8 @@ export interface APIDefinitionDao { loadAPIDefinition(apiDefinitionId: string): Promise; + loadAPILatestDefinition(apiDefinitionId: string): Promise; + loadAPIDefinitions(apiDefinitionIds: string[]): Promise>; } @@ -40,6 +42,23 @@ export class APIDefinitionDaoImpl implements APIDefinitionDao { return readBuffer(apiDefinition.definition) as APIV1Db.DbApiDefinition; } + public async loadAPILatestDefinition( + apiDefinitionId: string, + ): Promise { + const apiDefinition = await this.prisma.apiDefinitionsLatest.findFirst({ + where: { + apiDefinitionId, + }, + select: { + definition: true, + }, + }); + if (apiDefinition == null) { + return undefined; + } + return readBuffer(apiDefinition.definition) as FdrAPI.api.latest.ApiDefinition; + } + public async loadAPIDefinitions(apiDefinitionIds: string[]): Promise> { const apiDefinitions = await this.prisma.apiDefinitionsV2.findMany({ where: { diff --git a/servers/fdr/src/healthchecks/checkRedis.ts b/servers/fdr/src/healthchecks/checkRedis.ts index 01fe4c739f..1f228cd532 100644 --- a/servers/fdr/src/healthchecks/checkRedis.ts +++ b/servers/fdr/src/healthchecks/checkRedis.ts @@ -16,6 +16,7 @@ const HEALTHCHECK_DOCS_RESPONSE: CachedDocsResponse = { definition: { pages: {}, apis: {}, + apisLatest: {}, config: { navigation: { items: [],