1.1.23 • Published 2 months ago

@virgodev/bazaar v1.1.23

Weekly downloads
15
License
ISC
Repository
-
Last release
2 months ago

Virgo Development Bazaar

This package contains various functions that can be used in any JavaScript project. It also has some Vue-specific code.

Some of the functions have documentation written, which will show up in some code editors by hovering over the name of the function.

The most frequently used function in this package is api, so that function has special attention in this README.

The api Function

The api function is a wrapper for fetch. We will now go through various things it can do.

GET Request

import api from "@virgodev/bazaar/functions/api";

async function fetchDogBreeds() {
  const responseData = await api({
    url: "https://dog.ceo/api/breeds/list/all",
  });

  if (responseData.status === 200) {
    console.log(responseData.body);
  } else {
    console.log(`Request failed (status code: ${responseData.status})`);
  }
}

fetchDogBreeds();

POST Request (JSON)

import api from "@virgodev/bazaar/functions/api";

async function postJSON() {
  const responseData = await api({
    url: "https://jsonplaceholder.typicode.com/posts",
    method: "POST",
    json: {
      title: "foo",
      body: "bar",
      userId: 1,
    },
  });

  if (responseData.status === 201) {
    console.log(responseData.body);
  } else {
    console.log(`Request failed (status code: ${responseData.status})`);
  }
}

postJSON();

POST Request (FormData)

import api from "@virgodev/bazaar/functions/api";

async function postFormData() {
  const body = new FormData();
  body.append("title", "foo");
  body.append("body", "bar");
  body.append("userId", "1");

  const responseData = await api({
    url: "https://httpbin.org/post",
    method: "POST",
    body,
  });

  if (responseData.status === 200) {
    console.log(responseData.body);
  } else {
    console.log(`Request failed (status code: ${responseData.status})`);
  }
}

postFormData();

Setting a Base URL

There is a good chance that most or all of the requests your project will be making will have the same base URL (e.g. https://dog.ceo/api/). If you are using Vite or Vue CLI, you can specify a base URL in your .env and .env.production files.

Vite:

VITE_APP_API_URL=https://www.example.com/api/

Vue CLI:

VUE_APP_API_URL=https://www.example.com/api/

Setting a Global Request Header

This allows you to always have a header present in the requests you make:

import { setGlobalRequestHeader } from "@virgodev/bazaar/functions/api";

setGlobalRequestHeader("Authorization", "Token 1234abcd");

Adding a Request Handler

This allows you to run a function before each request is made. An example of when this could be useful is if you are developing locally with your front end and back end on different ports and need cookies to work. In that scenario, you could do something like this:

import { addRequestHandler } from "@virgodev/bazaar/functions/api";

addRequestHandler(({ options }) => {
  options.credentials = "include";
});

Adding a Response Handler

This allows you to run a function after each request is made. You can limit it so that your function runs only if the request resulted in a specific status code or you can have your function run after all requests.

import { addResponseHandler } from "@virgodev/bazaar/functions/api";

addResponseHandler(403, () => {
  console.log("A request that resulted in a 403 finished.");
});

addResponseHandler("all", () => {
  console.log("A request finished.");
});
1.1.23

2 months ago

1.1.22

4 months ago

1.1.9

10 months ago

1.1.8

10 months ago

1.1.12

9 months ago

1.1.11

9 months ago

1.1.16

7 months ago

1.1.15

7 months ago

1.1.14

7 months ago

1.1.13

8 months ago

1.1.19

6 months ago

1.1.18

6 months ago

1.1.17

7 months ago

1.1.21

6 months ago

1.1.20

6 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

12 months ago

1.1.4

12 months ago

1.1.3

12 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.2

1 year ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.44

1 year ago

1.0.43

2 years ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.40

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.33

2 years ago

1.0.35

2 years ago

2.0.10

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

2.0.9

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.29

2 years ago

1.0.30

2 years ago

1.0.22

3 years ago

2.0.8

3 years ago

1.0.21

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago