Skip to content

Commit

Permalink
Support #[Route(path: ...)]
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Jun 23, 2023
1 parent e6083a3 commit 9366022
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Bridge/Symfony/SymfonyControllerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ private function createApiEndpoint(ClassMethod $node): void
$nameArg = $this->getAttributeArgumentByName($routeAttribute, 'name');
if ($nameArg?->value instanceof Node\Scalar\String_) {
$route = $nameArg->value->value;
} else {
throw new \Exception('Could not find route path. Make sure your route looks like this #[Route(\'/api/users\')] or #[Route(name: \'/api/users/\')]');
}
$pathArg = $this->getAttributeArgumentByName($routeAttribute, 'path');
if ($pathArg?->value instanceof Node\Scalar\String_) {
$route = $pathArg->value->value;
}

if (!$route) {
throw new \Exception('Could not find route path. Make sure your route looks like this #[Route(\'/api/users\')] or #[Route(name: \'/api/users/\')] or #[Route(path: \'/api/users/\')]');
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/TypeScriptGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ public function getUser(User $userToUpdate, #[Input] UpdateUserInput $input) {}
#[DtoEndpoint(returnMany: UserOutput::class)]
#[Route('/api/users-with-filters', methods: ['GET'])]
public function getUsersWithFilters(#[Query] FilterQuery $query) {}
#[DtoEndpoint()]
#[Route(path: '/api/route-with-path', methods: ['GET'])]
public function routeWithPath() {}
#[DtoEndpoint()]
#[Route(name: '/api/route-with-name', methods: ['GET'])]
public function routeWithName() {}
}
CODE;

Expand All @@ -562,6 +570,7 @@ public function getUsersWithFilters(#[Query] FilterQuery $query) {}
new SymfonyControllerVisitor('DtoEndpoint'),
]);
$result = $converter->convert([$codeWithDateTime]);
$this->assertCount(7, $result->apiEndpointList->getList());
$this->assertMatchesGeneratedTypeScriptApi($result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export type UserOutput = {
id: string;
};

export const apiRouteWithNameGet = (): Promise<null> => {
return axios
.get<null>(`/api/route-with-name`)
.then((response) => response.data);
}

export const apiRouteWithPathGet = (): Promise<null> => {
return axios
.get<null>(`/api/route-with-path`)
.then((response) => response.data);
}

export const apiUsersGet = (): Promise<UserOutput[]> => {
return axios
.get<UserOutput[]>(`/api/users`)
Expand Down

0 comments on commit 9366022

Please sign in to comment.