Skip to content

Commit

Permalink
chore: use openapi prefix for openapi endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 1, 2024
1 parent 2f80c38 commit 2e00df1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 49 deletions.
100 changes: 52 additions & 48 deletions apps/backend/src/modules/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class OpenAPI {

public route() {
const recordOpenapi = container.resolve(RecordOpenApi)
return new Elysia({ prefix: "/api/bases/:baseName/tables/:tableName" })
return new Elysia()
.onAfterResponse((ctx) => {
const requestId = executionContext.getStore()?.requestId
this.logger.info(
Expand All @@ -91,7 +91,7 @@ export class OpenAPI {
)
})
.get(
"/",
"/api/bases/:baseName/tables/:tableName",
async (ctx) => {
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)

Expand Down Expand Up @@ -128,52 +128,56 @@ export class OpenAPI {
},
},
)
.guard({
beforeHandle: async (context) => {
const apiToken =
context.headers[API_TOKEN_HEADER_NAME] ?? context.headers[API_TOKEN_HEADER_NAME.toLowerCase()]

this.logger.debug({ apiToken }, "Checking Authorization token in openapi")

if (apiToken) {
const userId = await this.apiTokenService.verify(apiToken)
if (userId.isSome()) {
const user = (await this.userService.findOneById(userId.unwrap())).unwrap()
const space = await this.spaceService.setSpaceContext(setContextValue, { apiToken })
await this.spaceMemberService.setSpaceMemberContext(setContextValue, space.id.value, user.id)

return
}
} else {
const userId = getCurrentUserId()

if (userId) {
return
} else {
this.logger.error("No api token found in openapi")
}
}

// throw 401 openapi error
context.set.status = 401
throw new Error("Unauthorized")
},
})
.get(
"/openapi.json",
async (ctx) => {
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)
return spec
},
{
params: t.Object({ baseName: t.String(), tableName: t.String() }),
detail: {
tags: ["Doc"],
summary: "Get OpenAPI documentation json spec for a table",
description: "Get OpenAPI documentation json spec for a table",
},
},
.group("/openapi/bases/:baseName/tables/:tableName", (app) =>
app

.guard({
beforeHandle: async (context) => {
const apiToken =
context.headers[API_TOKEN_HEADER_NAME] ?? context.headers[API_TOKEN_HEADER_NAME.toLowerCase()]

this.logger.debug({ apiToken }, "Checking Authorization token in openapi")

if (apiToken) {
const userId = await this.apiTokenService.verify(apiToken)
if (userId.isSome()) {
const user = (await this.userService.findOneById(userId.unwrap())).unwrap()
const space = await this.spaceService.setSpaceContext(setContextValue, { apiToken })
await this.spaceMemberService.setSpaceMemberContext(setContextValue, space.id.value, user.id)

return
}
} else {
const userId = getCurrentUserId()

if (userId) {
return
} else {
this.logger.error("No api token found in openapi")
}
}

// throw 401 openapi error
context.set.status = 401
throw new Error("Unauthorized")
},
})
.get(
"/openapi.json",
async (ctx) => {
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)
return spec
},
{
params: t.Object({ baseName: t.String(), tableName: t.String() }),
detail: {
tags: ["Doc"],
summary: "Get OpenAPI documentation json spec for a table",
description: "Get OpenAPI documentation json spec for a table",
},
},
)
.use(recordOpenapi.route()),
)
.use(recordOpenapi.route())
}
}
12 changes: 12 additions & 0 deletions packages/i18n/src/i18n/i18n-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ type RootTranslation = {
*/
viewer: string
}
macros: {
/**
* C​u​r​r​e​n​t​ ​U​s​e​r
*/
'@me': string
}
}
}

Expand Down Expand Up @@ -719,6 +725,12 @@ export type TranslationFunctions = {
*/
viewer: () => LocalizedString
}
macros: {
/**
* Current User
*/
'@me': () => LocalizedString
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const createOpenApiSpec = (
version: "1.0.0",
title: table.name.value,
},
servers: [{ url: "/api" }],
servers: [{ url: "/openapi" }],
})
}

Expand Down

0 comments on commit 2e00df1

Please sign in to comment.