Build 🔧 ​
The first step is to construct a BuildInput object for a generic Record <T>
. Pass the object to the buildQuery method to convert it to a transportable string.
The BuildInput<T>
can contain a configuration for each Parameter/URLParameter.
- Fields:
FieldsBuildInput<T>
- Filter(s):
FiltersBuildInput<T>
- Pagination:
PaginationBuildInput<T>
- Relations:
RelationsBuildInput<T>
- Sort:
SortBuildInput<T>
TIP
Check out the API-Reference of each parameter for acceptable input formats and examples.
After building, the string can be passed to a backend application as http query string argument. The backend application can process the request, by parsing the query string.
Example ​
The following example is based on the assumption, that the following packages are installed:
It should give an insight on how to use this library. Therefore, a type which will represent a User
and a method getAPIUsers
are defined. The method should perform a request to the resource API to receive a collection of entities.
import axios from "axios";
import {
buildQuery,
BuildInput
} from "rapiq";
export type Realm = {
id: string,
name: string,
description: string,
}
export type Item = {
id: string,
realm: Realm,
user: User
}
export type User = {
id: number,
name: string,
email: string,
age: number,
realm: Realm,
items: Item[]
}
type ResponsePayload = {
data: User[],
meta: {
limit: number,
offset: number,
total: number
}
}
const record: BuildInput<User> = {
pagination: {
limit: 20,
offset: 10
},
filters: {
id: 1
},
fields: ['id', 'name'],
sort: '-id',
relations: ['realm']
};
const query = buildQuery(record);
// console.log(query);
// ?filter[id]=1&fields=id,name&page[limit]=20&page[offset]=10&sort=-id&include=realm
async function getAPIUsers(
record: BuildInput<User>
): Promise<ResponsePayload> {
const response = await axios.get('users' + buildQuery(record));
return response.data;
}
(async () => {
let response = await getAPIUsers(record);
// do something with the response :)
})();
The next section will describe, how to parse the query string on the backend side.