An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

# rx-ajax

A basic utility wrapper for rxjs allowing you to make configuration presets.

[![CI](https://github.com/Alorel/rx-ajax/workflows/Core/badge.svg?branch=master)](https://github.com/Alorel/rx-ajax/actions?query=workflow%3ACore+branch%3Amaster+)
[![Coverage Status](https://coveralls.io/repos/github/Alorel/rx-ajax/badge.svg?branch=master)](https://coveralls.io/github/Alorel/rx-ajax)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Alorel/rx-ajax.svg?logo=lgtm&logoWidth=18)](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;
```