diff --git a/apps/backend/src/modules/openapi/openapi.ts b/apps/backend/src/modules/openapi/openapi.ts index 91ba8c0ee..5b27f4224 100644 --- a/apps/backend/src/modules/openapi/openapi.ts +++ b/apps/backend/src/modules/openapi/openapi.ts @@ -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( @@ -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) @@ -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()) } } diff --git a/packages/i18n/src/i18n/i18n-types.ts b/packages/i18n/src/i18n/i18n-types.ts index 70f81b7f3..5053db4b7 100644 --- a/packages/i18n/src/i18n/i18n-types.ts +++ b/packages/i18n/src/i18n/i18n-types.ts @@ -364,6 +364,12 @@ type RootTranslation = { */ viewer: string } + macros: { + /** + * C​u​r​r​e​n​t​ ​U​s​e​r + */ + '@me': string + } } } @@ -719,6 +725,12 @@ export type TranslationFunctions = { */ viewer: () => LocalizedString } + macros: { + /** + * Current User + */ + '@me': () => LocalizedString + } } } diff --git a/packages/openapi/src/index.ts b/packages/openapi/src/index.ts index fbd2cce4b..8d137299d 100644 --- a/packages/openapi/src/index.ts +++ b/packages/openapi/src/index.ts @@ -97,7 +97,7 @@ export const createOpenApiSpec = ( version: "1.0.0", title: table.name.value, }, - servers: [{ url: "/api" }], + servers: [{ url: "/openapi" }], }) }