8.2.0 • Published 2 years ago

@ubuilder/rest v8.2.0

Weekly downloads
-
License
CC-BY-NC-ND-4.0
Repository
-
Last release
2 years ago

UBuilder REST API

REST API Plugin for Vue.js. Supports Vue 2 and 3.

WARNING: UBuilder REST v8 is not compatible with version 7.

UBuilder REST internally uses Fetch API. If target browser doen't support Fetch API, should add polyfill 'whatwg-fetch'.

Installation

npm i @ubuilder/rest

Exports

// named class export
export { Rest };
// plugin export for vue.js
export default {
  install(app, options = {}) { /* ... */ }
};

Usage

Setup on main

// on Vue 2
import Vue from 'vue';
import Rest from '@ubuilder/rest';

Vue.use(Rest, { baseURL: '/' });

// on Vue 3
import { createApp } from 'vue';
import Rest from '@ubuilder/rest';

const app = createApp(/* app options */);
app.use(Rest, { baseURL: '/' });

Use in component

// Vue 2 in methods
this.$rest.get('/').then((json) => { this.getData = json });

// Vue 3 inject
const component = {
  inject: {
    rest: {
      from: 'rest'
    }
  }
};

// Vue 3 setup inject
import { inject, ref } from 'vue';
import { Rest } from '@ubuilder/rest'; // Rest class, not default import

const getData = ref();

const rest = inject<Rest>('rest'); // check providePrefix option
rest.get('/').then((json) => { getData.value = json });

PluginOptions

  • baseURL : url base, defaults to '/'.
  • headers : default headers, defaults to {}.
  • interceptors: array of interceptors.
  • providePrefix : prefix for provide/inject. Default value is '' (empty string).

Error Handling

  • on network error, throws error from fetch API.
  • on response is not ok (check by response.ok), throws RestError { status, response }. !!! Do not rely on http error message. HTTP/2 does not have statusText. Must check by status. !!!

Interceptors

Interceptors are chained. Interceptors called on request, response, error. Global interceptors are removed on v8.

{
  onRequest?: (options: Promise<RequestInit>) => Promise<RequestInit>
  onResponse?: (response: Response) => Response
  onError?: (error: Error) => boolean
}
  • onRequest : fetch request options for manipulation. Should returns options promise. To stop processing, reject.
  • onResponse : on fetch response. Should returns response.
  • error: receive error, returns boolean. if returns true, that error is handled, chain stops and does not throws error.

Example

const json = await this.$rest.get('/url');

this.$rest.post('/url', {});

Methods

Response is fetch API Response.

fetch

To use raw fetch with interceptors.

  • fetch(url: string, options: RequestInit = {}) : Promise<Response>

GET

If params is not undefined, set using URLSearchParams.

  • get<T>(url: string, params?: object): Promise<T> - returns of Response.json()
  • fetchGet(url: string, params?: object): Promise<Response>

POST

POST JSON. If body parameter is FormData, send as is. Otherwise, converted to JSON.

  • post(url: string, body?: FormData | unknown) : Promise<Response>

PUT

PUT JSON.

  • put(url: string, body: object) : Promise<Response>

PATCH

  • patch(url: string, body: object) : Promise<Response>

DELETE

If params is not undefined, set using URLSearchParams.

  • delete(url: string, params?: object) : Promise<Response>
8.2.0

2 years ago

8.1.5

3 years ago

8.0.6

3 years ago

8.1.0

3 years ago

8.1.2

3 years ago

8.1.1

3 years ago

8.1.4

3 years ago

8.1.3

3 years ago

8.0.5

3 years ago

8.0.4

3 years ago

8.0.3

3 years ago

8.0.2

3 years ago

8.0.1

4 years ago

8.0.0

4 years ago

7.2.15

5 years ago

7.2.13

5 years ago

7.2.14

5 years ago

7.2.12

5 years ago

7.2.11

5 years ago

7.2.6

5 years ago

7.2.5

5 years ago

7.2.4

5 years ago

7.2.9

5 years ago

7.2.8

5 years ago

7.2.7

5 years ago

7.2.10

5 years ago

7.2.2

5 years ago

7.2.1

5 years ago

7.2.0

5 years ago

7.2.3

5 years ago

7.1.6

5 years ago

7.1.5

5 years ago

7.1.4

5 years ago

7.1.3

5 years ago

7.1.2

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

7.0.11

5 years ago

7.0.9

5 years ago

7.0.10

5 years ago

7.0.8

5 years ago

7.0.7

5 years ago

7.0.6

5 years ago

7.0.5

5 years ago

7.0.4

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago