API Generator
Generate FE/BE code from .proto files.
Flow
- •Input:
bff/<pkg>/<pkg>.protowith service +google.api.httpannotations - •Run:
npm run gen -- <pkg> - •Output: Types, API calls, hooks, routes, DTOs
Command
bash
# Generate for a package npm run gen -- <package_name> # Genrate using task file task gen -- <package_name>
Output Files
| File | Location | Regenerated? |
|---|---|---|
| FE Types | fe/src/types/<pkg>.d.ts | Yes |
| BE Types | be/types/<pkg>.d.ts | Yes |
| FE API | fe/src/services/<pkg>/api.ts | Yes |
| FE Hooks | fe/src/services/<pkg>/useQuery.ts | Yes |
| BE Routes | be/core/api/<pkg>/<pkg>.routes.ts | Yes |
| BE DTO | be/core/dto/<pkg>.dto.ts | Once only |
Proto Example
protobuf
// bff/product/product.proto
syntax = "proto3";
package product;
import "google/api/annotations.proto";
service ProductService {
rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse) {
option (google.api.http) = {
post: "/v1/product/CreateProduct"
body: "*"
};
}
rpc GetProductById(GetProductByIdRequest) returns (ProductResponse) {
option (google.api.http) = {
get: "/v1/product/GetProductById"
};
}
}
message CreateProductRequest {
string name = 1;
string sku = 2;
}
message ProductResponse {
string id = 1;
string name = 2;
}
Rules
- •Proto is source of truth for API contracts
- •Never manually edit generated API/hooks/routes
- •DTO files are created once—add validation manually
- •Types are regenerated with content replacement
HTTP Methods
| Proto | HTTP | FE Hook | BE Status |
|---|---|---|---|
post | POST | useMutation | 201 |
get | GET | useQuery | 200 |
Extending
To add PUT/PATCH/DELETE:
- •Extend
httpMethodunion in generator - •Map
google.api.httpfields - •Adjust FE/BE writers similarly to POST logic