Kind: Interface
Source: src/client/types.ts
Part of: Client
TypedURL represents a URL as typed component fields, including protocol, host details, path, search string, origin, and full href. It keeps derived fields such as host, origin, and href aligned with the protocol, hostname, port, pathname, and search values.
Properties
| Property | Type |
|---|---|
protocol | Protocol |
hostname | Hostname |
port | Port |
host | Port extends '' ? Hostname : ${Hostname}:${Port}`` |
origin | ``${Protocol}` |
pathname | Pathname |
search | Search |
href | ``${Protocol}` |
Diagram
mermaidgraph LR Protocol[protocol] --> Origin[origin] Hostname[hostname] --> Host[host] Port[port] --> Host Host --> Origin Origin --> Href[href] Pathname[pathname] --> Href Search[search] --> Href
Usage
tsfunction request(url: TypedURL) {
return fetch(url.href);
}
const apiUrl: TypedURL = {
protocol: "https:",
hostname: "api.example.com",
port: "",
host: "api.example.com",
origin: "https://api.example.com",
pathname: "/users",
search: "?active=true",
href: "https://api.example.com/users?active=true",
};
request(apiUrl);
AI Coding Instructions
- Keep
hostconsistent withhostnameandport; omit the colon whenportis an empty string. - Build
originfrom the protocol and host rather than accepting unrelated values. - Build
hreffrom the origin, pathname, and search fields. - Preserve literal string types when constructing values so derived template-literal types remain valid.
Was this page helpful?