Skip to content

Commit 2e00df1

Browse files
committed
chore: use openapi prefix for openapi endpoint
1 parent 2f80c38 commit 2e00df1

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed

apps/backend/src/modules/openapi/openapi.ts

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class OpenAPI {
7676

7777
public route() {
7878
const recordOpenapi = container.resolve(RecordOpenApi)
79-
return new Elysia({ prefix: "/api/bases/:baseName/tables/:tableName" })
79+
return new Elysia()
8080
.onAfterResponse((ctx) => {
8181
const requestId = executionContext.getStore()?.requestId
8282
this.logger.info(
@@ -91,7 +91,7 @@ export class OpenAPI {
9191
)
9292
})
9393
.get(
94-
"/",
94+
"/api/bases/:baseName/tables/:tableName",
9595
async (ctx) => {
9696
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)
9797

@@ -128,52 +128,56 @@ export class OpenAPI {
128128
},
129129
},
130130
)
131-
.guard({
132-
beforeHandle: async (context) => {
133-
const apiToken =
134-
context.headers[API_TOKEN_HEADER_NAME] ?? context.headers[API_TOKEN_HEADER_NAME.toLowerCase()]
135-
136-
this.logger.debug({ apiToken }, "Checking Authorization token in openapi")
137-
138-
if (apiToken) {
139-
const userId = await this.apiTokenService.verify(apiToken)
140-
if (userId.isSome()) {
141-
const user = (await this.userService.findOneById(userId.unwrap())).unwrap()
142-
const space = await this.spaceService.setSpaceContext(setContextValue, { apiToken })
143-
await this.spaceMemberService.setSpaceMemberContext(setContextValue, space.id.value, user.id)
144-
145-
return
146-
}
147-
} else {
148-
const userId = getCurrentUserId()
149-
150-
if (userId) {
151-
return
152-
} else {
153-
this.logger.error("No api token found in openapi")
154-
}
155-
}
156-
157-
// throw 401 openapi error
158-
context.set.status = 401
159-
throw new Error("Unauthorized")
160-
},
161-
})
162-
.get(
163-
"/openapi.json",
164-
async (ctx) => {
165-
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)
166-
return spec
167-
},
168-
{
169-
params: t.Object({ baseName: t.String(), tableName: t.String() }),
170-
detail: {
171-
tags: ["Doc"],
172-
summary: "Get OpenAPI documentation json spec for a table",
173-
description: "Get OpenAPI documentation json spec for a table",
174-
},
175-
},
131+
.group("/openapi/bases/:baseName/tables/:tableName", (app) =>
132+
app
133+
134+
.guard({
135+
beforeHandle: async (context) => {
136+
const apiToken =
137+
context.headers[API_TOKEN_HEADER_NAME] ?? context.headers[API_TOKEN_HEADER_NAME.toLowerCase()]
138+
139+
this.logger.debug({ apiToken }, "Checking Authorization token in openapi")
140+
141+
if (apiToken) {
142+
const userId = await this.apiTokenService.verify(apiToken)
143+
if (userId.isSome()) {
144+
const user = (await this.userService.findOneById(userId.unwrap())).unwrap()
145+
const space = await this.spaceService.setSpaceContext(setContextValue, { apiToken })
146+
await this.spaceMemberService.setSpaceMemberContext(setContextValue, space.id.value, user.id)
147+
148+
return
149+
}
150+
} else {
151+
const userId = getCurrentUserId()
152+
153+
if (userId) {
154+
return
155+
} else {
156+
this.logger.error("No api token found in openapi")
157+
}
158+
}
159+
160+
// throw 401 openapi error
161+
context.set.status = 401
162+
throw new Error("Unauthorized")
163+
},
164+
})
165+
.get(
166+
"/openapi.json",
167+
async (ctx) => {
168+
const spec = await this.getSpec(ctx.params.baseName, ctx.params.tableName)
169+
return spec
170+
},
171+
{
172+
params: t.Object({ baseName: t.String(), tableName: t.String() }),
173+
detail: {
174+
tags: ["Doc"],
175+
summary: "Get OpenAPI documentation json spec for a table",
176+
description: "Get OpenAPI documentation json spec for a table",
177+
},
178+
},
179+
)
180+
.use(recordOpenapi.route()),
176181
)
177-
.use(recordOpenapi.route())
178182
}
179183
}

packages/i18n/src/i18n/i18n-types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ type RootTranslation = {
364364
*/
365365
viewer: string
366366
}
367+
macros: {
368+
/**
369+
* C​u​r​r​e​n​t​ ​U​s​e​r
370+
*/
371+
'@me': string
372+
}
367373
}
368374
}
369375

@@ -719,6 +725,12 @@ export type TranslationFunctions = {
719725
*/
720726
viewer: () => LocalizedString
721727
}
728+
macros: {
729+
/**
730+
* Current User
731+
*/
732+
'@me': () => LocalizedString
733+
}
722734
}
723735
}
724736

packages/openapi/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const createOpenApiSpec = (
9797
version: "1.0.0",
9898
title: table.name.value,
9999
},
100-
servers: [{ url: "/api" }],
100+
servers: [{ url: "/openapi" }],
101101
})
102102
}
103103

0 commit comments

Comments
 (0)