Build
build
Method Documentation
The build()
method constructs a URL from the base URL, applies path parameters, transforms query parameters (if necessary), validates the parameters (if a validation schema is provided), and then appends the query string to the URL.
Note:
When using .build()
method make sure you have set a getter on your eloquent model.
User::find(1)->get();
What It Does
-
Path Parameter Replacement:
- If there are any path parameters specified, it replaces placeholders (e.g.,
{key}
) in the URL with the corresponding values. The values are URL-encoded to ensure they are safe to use in a URL.
- If there are any path parameters specified, it replaces placeholders (e.g.,
-
Parameter Transformation:
- If the
transformer
option is provided, it will apply the function to transform the query parameters before appending them to the URL.
- If the
-
Parameter Validation:
- If a validation schema is provided, it will attempt to validate the query parameters using the schema before continuing with URL construction. If validation fails, an error is thrown.
-
Building Query String:
- It constructs a query string by appending key-value pairs to the URL. If the values are arrays or objects, they will be serialized into JSON strings before appending.
-
Final URL Construction:
- If there are query parameters, they will be appended to the base URL as a query string (e.g.,
?key=value
). If no query parameters exist, the URL will be returned without a query string.
- If there are query parameters, they will be appended to the base URL as a query string (e.g.,
How It's Used
The build()
method is used to generate a complete URL for making HTTP requests. This is typically called after setting up any necessary path parameters and query parameters using the class's methods (such as path()
, query()
, etc.).
Example
Assume we have a createQuery
class that allows us to build URLs with path parameters and query parameters.
import {createQuery} from 'query-builder'
const query = new createQuery('api/users');
const url = query.where([
['name', 'like', '%John%']
['email', 'not like', '%@gmail.com%']
])
.build();
console.log(url);
// Output: https://api.example.com/api/users?where%5Bname%5D%5Blike%5D=%25John%25
Explanation:
- The base URL is
https://api.example.com/users/{userId}
. - The
path()
method replaces{userId}
with123
, resulting inhttps://api.example.com/users/123
. - The
query()
method adds two query parameters:sort=desc
andfilter=["active", "admin"]
. Thefilter
array is serialized as a JSON string. - The final URL is generated with the query string:
https://api.example.com/users/123?sort=desc&filter=%5B%22active%22%2C%22admin%22%5D
.
Method Signature
build(): string
- Returns: The method returns a string representing the full URL with the applied path parameters and query string.
No Parameters Required
- The
build()
method does not take any parameters. It uses the current state of thepathParams
,queryParams
, and any options set during the object configuration.
Edge Cases
-
Empty Parameters: If no path parameters or query parameters are set, the method will return only the base URL.
import {createQuery} from 'query-builder' const query = new createQuery('https://api.example.com/users'); const url = query.build(); console.log(url); // Output: https://api.example.com/users
The build()
method is an essential utility for constructing fully qualified URLs based on dynamic parameters. It ensures flexibility in URL building, supports path and query parameters, and allows for query parameter transformation and validation.