I'm currently exploring ways to automatically generate TypeScript code from the API of my Laravel application. I have been using scribe for generating API documentation and then utilizing it to create TypeScript definitions. However, I am facing an issue related to handling server messages. My approach involves creating OpenAPI docs based on the Resources returned by the APIs. If I proceed in this direction, each endpoint would require its own designated resource which should include the 'server message' to ensure proper generation of API docs.
To illustrate, consider the following example:
/**
* @apiResource App\Http\Admin\Resources\PermissionCreateResource
* @apiResourceModel App\Models\Permission
* @param PermissionCreateRequest $request
*/
public function create(PermissionCreateRequest $request): JsonResponse
{
$input = $request->validated();
$permission = $this->permissionFactory->createPermission($input);
return new JsonResponse(new PermissionCreateResource($permission), Response::HTTP_CREATED);
}
Here is how the PermissionCreateResource
class might look:
public function toArray(Request $request): array
{
return [
'message' => 'Permission created',
'data' => [
'id' => $this->id,
'name' => $this->name,
]
];
}
In order to adhere to best practices and maintain clarity, it is essential to carefully design the structure of resources and endpoints. Any suggestions on what would be the most appropriate course of action?
Thank you in advance! Cheers!