0.3.2 • Published 5 years ago

@commite/ajax-client v0.3.2

Weekly downloads
-
License
LGPL-3.0-or-later
Repository
github
Last release
5 years ago

Ajax Client

Build Status Codacy Badge Codacy Badge

Observable based HTTP client on top of rxjs/ajax for browser and node.js.

Table of contents

Installation

npm i --save @commite/ajax-client rxjs

Dependencies

"rxjs": "^6.3.3"

API

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.

Config

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

[get|delete]<T>(url: string, options?: Partial<AjaxClientRequest<null>>): Observable<AjaxClientResponse<T>>

Get example

const ajaxClient = new AjaxClient();

ajaxClient.get<GetUsersResponse>('/users', { headers: ... }).subscribe((userRes) => {
  [...]
});

Back to top

Post/Put/Patch

[post|put|patch]<T, Y>(url: string, body: Y, options?: Partial<AjaxClientRequest<Y>): Observable<AjaxClientResponse<T>>

Post example

const ajaxClient = new AjaxClient();

ajaxClient.post<PostUserResponse, PostUserBody>('/users', { email: 'test@test.com'}, { headers: ... }).subscribe((userRes) => {
  [...]
});

Back to top

Request

request<T, Y>(options: Partial<AjaxClientRequest<Y>>): Observable<AjaxClientResponse<T>>

Request example

ajaxClient.request<RequestResponse, RequestBody>({
  method: 'POST',
  body: {...},
  headers: {...}
}).subscribe((reqRes) => {
  [...]
})

Back to top

Interceptors

Request and response could be intercepted.

Request interceptor

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}`
  };
});

Response interceptor

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(',')
  };
});

Back to top

TypeScript

Ajax client is coded entirely in TypeScript with target:'es5'.

Building/Testing

  • npm install
  • npm run build
  • npm run lint
  • npm run test or npm run test:watch

Contributing

License

Copyright (c) 2018 Commite Inc under LGPLv3 license.

0.3.2

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago