Observable based HTTP client on top of rxjs/ajax for browser and node.js.
npm i --save @commite/ajax-client rxjs"rxjs": "^6.3.3"Ajax client uses rxjs/ajax API to perform calls and returns rxjs/observable.
All request accepts an Ajax request options, post, put and patch requests body param can also be typed, other way any type will be assumed.
All request returns an Ajax response, response param can also be typed, other way any type will be assumed.
A base url can be setted using library constructor.
const ajaxClient = new AjaxClient({
baseUrl: 'my-base-url.com'
});Equivalent to adding an interceptor like:
interceptors.request.push(request => ({
...request,
url: `${baseUrl}${request.url}`
}));[get|delete]<T>(url: string, options?: Partial<AjaxClientRequest<null>>): Observable<AjaxClientResponse<T>>const ajaxClient = new AjaxClient();
ajaxClient.get<GetUsersResponse>('/users', { headers: ... }).subscribe((userRes) => {
[...]
});[post|put|patch]<T, Y>(url: string, body: Y, options?: Partial<AjaxClientRequest<Y>): Observable<AjaxClientResponse<T>>const ajaxClient = new AjaxClient();
ajaxClient.post<PostUserResponse, PostUserBody>('/users', { email: 'test@test.com'}, { headers: ... }).subscribe((userRes) => {
[...]
});request<T, Y>(options: Partial<AjaxClientRequest<Y>>): Observable<AjaxClientResponse<T>>ajaxClient.request<RequestResponse, RequestBody>({
method: 'POST',
body: {...},
headers: {...}
}).subscribe((reqRes) => {
[...]
})Request and response could be intercepted.
A request interceptor is a function that accepts a Partial<AjaxClientRequest<any>> object and returns a modified (or not) Partial<AjaxClientRequest<any>>.
RequestInterceptor = (
options: Partial<AjaxRequest<any>>
) => Partial<AjaxRequest<any>>Request interceptors can be managed through ajaxClient.interceptors.request array.
ajaxClient.interceptors.request.push(options => {
return {
...options,
url: `https://my-base-url.com/${options.url}`
};
});A response interceptor is a function that accepts a AjaxClientResponse<any> object and returns a modified (or not) AjaxClientResponse<any>.
ResponseInterceptor = (response: AjaxClientResponse<any>) => AjaxClientResponse<any>;Response interceptors can be managed through ajaxClient.interceptors.response array.
ajaxClient.interceptors.response.push(ajaxResponse => {
return {
...ajaxResponse,
response: ajaxResponse.response.split(',')
};
});Ajax client is coded entirely in TypeScript with target:'es5'.
npm installnpm run buildnpm run lintnpm run testornpm run test:watch
- Maintaining 100% unit tests coverage and lintering.
- Gitflow
- Commit convention
- Semver
Copyright (c) 2018 Commite Inc under LGPLv3 license.