Use structured types instead of generic strings in API definitions to improve type safety and clarity. Prefer enums over string literals, specific ID types over generic strings, and maintain consistent data type usage across request/response models.
Key practices:
pub status: SubscriptionStatus
not pub status: String
merchant_id: impl Into<id_type::MerchantId>
not merchant_id: String
connector: Connector
not connector: String
base_url: Url
not base_url: String
Example:
// Good - structured types
#[derive(Debug, Clone, serde::Serialize)]
pub struct CreateSubscriptionResponse {
pub status: SubscriptionStatus, // enum instead of String
pub merchant_id: id_type::MerchantId, // specific ID type
pub connector: Connector, // enum instead of String
}
// Avoid - generic strings
pub struct CreateSubscriptionResponse {
pub status: String, // unclear what values are valid
pub merchant_id: String, // loses type safety
pub connector: String, // no validation
}
This approach prevents runtime errors, improves API documentation, enables better IDE support, and makes the codebase more maintainable.
Enter the URL of a public GitHub repository