https://github.com/alorel/rx-ajax
A basic utility wrapper for the rxjs ajax function
https://github.com/alorel/rx-ajax
rxjs
Last synced: 28 days ago
JSON representation
A basic utility wrapper for the rxjs ajax function
- Host: GitHub
- URL: https://github.com/alorel/rx-ajax
- Owner: Alorel
- License: mit
- Created: 2020-09-27T14:30:46.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-10T19:54:40.000Z (over 4 years ago)
- Last Synced: 2025-02-04T15:24:57.479Z (3 months ago)
- Topics: rxjs
- Language: TypeScript
- Homepage:
- Size: 1.58 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# rx-ajax
A basic utility wrapper for rxjs allowing you to make configuration presets.
[](https://github.com/Alorel/rx-ajax/actions?query=workflow%3ACore+branch%3Amaster+)
[](https://coveralls.io/github/Alorel/rx-ajax)
[](https://lgtm.com/projects/g/Alorel/rx-ajax/context:javascript)-----
# Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Extending an existing config](#extending-an-existing-config)
- [Middleware](#middleware)
- [API](#api)
- [RxAjaxErrorResponse](#rxajaxerrorresponse)
- [RxAjaxOptions](#rxajaxoptions)
- [RxAjaxResponse](#rxajaxresponse)
- [RxAjax](#rxajax)# Installation
Add the registry to `.npmrc`:
```bash
@alorel:registry=https://npm.pkg.github.com
```Then install the library:
```bash
npm install rxjs@^6.0.0 @alorel/rx-ajax
```# Usage
```javascript
import {rxAjax} from '@alorel/rx-ajax';
import {ajax} from 'rxjs/ajax';const api = rxAjax({
// Same options as regular rxjs ajax() plus some extras
headers: {
'content-type': 'text/json'
},
responseType: 'json'
});api.post('/foo', {qux: 'baz'}, {timeout: 1, headers: {'x-foo': 'bar'}}).subscribe();
// Equivalent to:
ajax({
body: {qux: 'baz'},
headers: {
'content-type': 'text/json',
'x-foo': 'bar'
},
responseType: 'json',
timeout: 1
}).subscribe();
```## Extending an existing config
Each `RxAjax` instance has an `.extend()` method that allows you to make a copy with a merged config:
```javascript
api.extend({
headers: {/*...*/}
})
```## Middleware
`pre` middleware transforms the request, `post` middleware transforms a response. `preAsync` and `postAsync` do the
same, but return an `ObservableInput` instead.```javascript
api.getJSON('/foo', {
pre: [],
preAsync: [],
post: [],
postAsync: []
})
```Refer to the API for middleware function signatures.
# API
## RxAjaxErrorResponse
```typescript
export interface RxAjaxErrorResponse extends Omit {
request: RxAjaxRequestOptions;
response: T;
}
```## RxAjaxOptions
```typescript
export declare type RxAjaxPostAsyncHook = (request: UnprocessedResponse) => ObservableInput>;export declare type RxAjaxRequestOptions = Omit;
export declare type RxAjaxBodiedRequestOptions = Omit;
export interface RxAjaxOptions extends Omit {
headers?: Obj;
/** Middleware for the response */
post?: Arrayish<((response: UnprocessedResponse) => UnprocessedResponse)>;
/** Async middleware for the response */
postAsync?: Arrayish;
/** Middleware for the request */
pre?: Arrayish<((req: RxAjaxOptions) => RxAjaxOptions)>;
/** Async middleware for the request */
preAsync?: Arrayish<((req: RxAjaxOptions) => ObservableInput)>;
}
```## RxAjaxResponse
```typescript
export interface RxAjaxResponse extends Omit {
request: RxAjaxOptions;
response: T;
}/** Successful response */
export interface UnprocessedSuccessResponse {
ok: true;
response: RxAjaxResponse;
}
/** Unsuccessful response */
export interface UnprocessedErrorResponse {
ok: false;
response: RxAjaxErrorResponse;
}
export declare type UnprocessedResponse = UnprocessedSuccessResponse | UnprocessedErrorResponse;
```## RxAjax
```typescript
export interface RxAjax {
/** Make a DELETE request */
delete(url: string, opts?: RxAjaxRequestOptions): Observable>;
/**
* Extend the current default configuration with the given options
* @return a new {@link RxAjax} instance with the merged options
*/
extend(opts: RxAjaxOptions): RxAjax;
/** Make a GET request */
get(url: string, opts?: RxAjaxRequestOptions): Observable>;
/** Make a GET request & set the responseType to "json" */
getJSON(url: string, opts?: RxAjaxRequestOptions): Observable>;
/** Make a PATCH request */
patch(url: string, body: any, opts?: RxAjaxBodiedRequestOptions): Observable>;
/** Make a POST request */
post(url: string, body: any, opts?: RxAjaxBodiedRequestOptions): Observable>;
/** Make a PUT request */
put(url: string, body: any, opts?: RxAjaxBodiedRequestOptions): Observable>;
/** Generic request function */ (urlOrRequest: string | RxAjaxOptions): Observable>;
}/** Create an rxjs.ajax wrapper with the given default options */
export declare function rxAjax(defaults?: RxAjaxOptions): RxAjax;
```