npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ubuilder/rest

v8.2.0

Published

UBuilder REST API

Downloads

95

Readme

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>