Remove workspaceId from path parameter for route endpoints (#14258)
- remove workspaceId from path parameter for route endpoints - use host to infer workspaceId
This commit is contained in:
@@ -2,7 +2,6 @@ import {
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Put,
|
||||
@@ -18,67 +17,47 @@ import { RouteService } from 'src/engine/metadata-modules/route/route.service';
|
||||
import { HTTPMethod } from 'src/engine/metadata-modules/route/route.entity';
|
||||
import { RestApiExceptionFilter } from 'src/engine/api/rest/rest-api-exception.filter';
|
||||
|
||||
@Controller('s/:workspaceId')
|
||||
@Controller('s')
|
||||
@UseGuards(PublicEndpointGuard)
|
||||
@UseFilters(RestApiExceptionFilter)
|
||||
export class RouteController {
|
||||
constructor(private readonly routeService: RouteService) {}
|
||||
|
||||
@Get('*')
|
||||
async get(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
async get(@Req() request: Request) {
|
||||
return await this.routeService.handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod: HTTPMethod.GET,
|
||||
});
|
||||
}
|
||||
|
||||
@Post('*')
|
||||
async post(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
async post(@Req() request: Request) {
|
||||
return await this.routeService.handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod: HTTPMethod.POST,
|
||||
});
|
||||
}
|
||||
|
||||
@Put('*')
|
||||
async put(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
async put(@Req() request: Request) {
|
||||
return await this.routeService.handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod: HTTPMethod.PUT,
|
||||
});
|
||||
}
|
||||
|
||||
@Patch('*')
|
||||
async patch(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
async patch(@Req() request: Request) {
|
||||
return await this.routeService.handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod: HTTPMethod.PATCH,
|
||||
});
|
||||
}
|
||||
|
||||
@Delete('*')
|
||||
async delete(
|
||||
@Param('workspaceId') workspaceId: string,
|
||||
@Req() request: Request,
|
||||
) {
|
||||
async delete(@Req() request: Request) {
|
||||
return await this.routeService.handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod: HTTPMethod.DELETE,
|
||||
});
|
||||
|
||||
@@ -16,37 +16,57 @@ import {
|
||||
} from 'src/engine/metadata-modules/route/route.entity';
|
||||
import { AccessTokenService } from 'src/engine/core-modules/auth/token/services/access-token.service';
|
||||
import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service';
|
||||
import { workspaceValidator } from 'src/engine/core-modules/workspace/workspace.validate';
|
||||
import {
|
||||
AuthException,
|
||||
AuthExceptionCode,
|
||||
} from 'src/engine/core-modules/auth/auth.exception';
|
||||
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
|
||||
|
||||
@Injectable()
|
||||
export class RouteService {
|
||||
constructor(
|
||||
private readonly accessTokenService: AccessTokenService,
|
||||
private readonly serverlessFunctionService: ServerlessFunctionService,
|
||||
private readonly domainManagerService: DomainManagerService,
|
||||
@InjectRepository(Route)
|
||||
private readonly routeRepository: Repository<Route>,
|
||||
) {}
|
||||
|
||||
private async getOneRouteWithPathParamsOrFail({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
request: Request;
|
||||
httpMethod: HTTPMethod;
|
||||
}): Promise<{
|
||||
route: Route;
|
||||
pathParams: Partial<Record<string, string | string[]>>;
|
||||
}> {
|
||||
const host = `${request.protocol}://${request.get('host')}`;
|
||||
|
||||
const workspace =
|
||||
await this.domainManagerService.getWorkspaceByOriginOrDefaultWorkspace(
|
||||
host,
|
||||
);
|
||||
|
||||
workspaceValidator.assertIsDefinedOrThrow(
|
||||
workspace,
|
||||
new AuthException(
|
||||
'Workspace not found',
|
||||
AuthExceptionCode.WORKSPACE_NOT_FOUND,
|
||||
),
|
||||
);
|
||||
|
||||
const routes = await this.routeRepository.find({
|
||||
where: {
|
||||
httpMethod,
|
||||
workspaceId,
|
||||
workspaceId: workspace.id,
|
||||
},
|
||||
relations: ['serverlessFunction'],
|
||||
});
|
||||
|
||||
const requestPath = request.path.replace(`/s/${workspaceId}/`, '');
|
||||
const requestPath = request.path.replace(/^\/s\//, '/');
|
||||
|
||||
for (const route of routes) {
|
||||
const routeMatcher = match(route.path, { decode: decodeURIComponent });
|
||||
@@ -83,16 +103,13 @@ export class RouteService {
|
||||
}
|
||||
|
||||
async handle({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
request: Request;
|
||||
httpMethod: HTTPMethod;
|
||||
}) {
|
||||
const routeWithPathParams = await this.getOneRouteWithPathParamsOrFail({
|
||||
workspaceId,
|
||||
request,
|
||||
httpMethod,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user