url.build
url.build
Function
Constructs a URL string from an object configuration.
Syntax
url(config).build() url('').build(config)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| config | Object | No | The object passed in when constructing the url | URL configuration object. |
config object field
| Field | Type | Required | Default value | Description |
|---|---|---|---|---|
| scheme | String | No | http | URL scheme, no need to write ://. |
| host | String | No | Empty string | Hostname or IP. When there is no host, the URL starting with scheme:// will still be generated according to the current implementation. |
| port | Int/String | No | None | Port number. If the field value is not empty or null, it will be converted to a string and spelled after host. |
| path | String | No | / | URL path. If it does not start with /, / will be automatically added. |
| query | Object/String/Any | No | None | Query string. Object will be URL encoded item by key value; String will be used as query string as it is; other values will be converted to string first. Empty strings are not written to ?. |
| fragment | String | No | None | URL fragment, no need to write #; percent encoding will be done when writing. |
| username | String | No | None | Username. If it is not empty, username@ will be written and percent-encoded. |
| password | String | No | None | Password. Only when username is non-empty, it will be written and percent-encoded. |
Return value
| Type | Description |
|---|---|
| String | The constructed URL. |
Code Example
result = url({scheme: 'https', host: 'btlang.org', path: '/docs', query: {q: 'BT Lang'}}).build() // Output: https://btlang.org/docs?q=BT%20Lang print result
Notes
- When
queryis Object, field names and values will be encoded according to URL component rules. -
build()only does lightweight string construction and does not check whether host is empty or whether scheme actually exists.