1.1.47 • Published 4 months ago

@frontendnetwork/vegancheck v1.1.47

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

VeganCheck.me API Wrapper

A wrapper for the official VeganCheck.me API for React, Vue and Vanilla JavaScript.

hero

Documentation

Installation

Install the API Wrapper with npm or yarn:

npm install @frontendnetwork/vegancheck

Initialization

You can use this library in TypeScript and JavaScript. Import it with:

import VeganCheck from "@frontendnetwork/vegancheck";

and then initialize it with one of its functions.

Error Handling

Please refer to the VeganCheck.me API Documentation for all error codes.

Functions

  • getProductByBarcode: Gives out information about a product by its barcode.

    getProductByBarcode(barcode);
  • checkIngredientsList: Checks ingredients. Ingredients have to be comma-seperated.

    checkIngredientsList(ingredientsList);
  • getPetaCrueltyFreeBrands: Gives out a list of cruelty free brands.

    getPetaCrueltyFreeBrands();

Example usage

React

import VeganCheck from "@frontendnetwork/vegancheck";
import React, { useState } from "react";

const ExampleComponent = () => {
  const [barcode, setBarcode] = useState("");
  const [productInfo, setProductInfo] = useState(null);

  const handleBarcodeSubmit = async (e) => {
    e.preventDefault();

    try {
      const productData = await VeganCheck.getProductByBarcode(barcode);
      setProductInfo(productData);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <div>
      <form onSubmit={handleBarcodeSubmit}>
        <input
          type="text"
          value={barcode}
          onChange={(e) => setBarcode(e.target.value)}
          placeholder="Enter barcode"
        />
        <button type="submit">Submit</button>
      </form>
      {productInfo && (
        <div>
          <h2>Product Information</h2>
          <pre>{JSON.stringify(productInfo, null, 2)}</pre>
        </div>
      )}
    </div>
  );
};

export default ExampleComponent;

Vue

<template>
  <div>
    <form @submit.prevent="handleBarcodeSubmit">
      <input type="text" v-model="barcode" placeholder="Enter barcode" />
      <button type="submit">Submit</button>
    </form>
    <div v-if="productInfo">
      <h2>Product Information</h2>
      <pre>{{ productInfo }}</pre>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import VeganCheck from "@frontendnetwork/vegancheck";

export default {
  setup() {
    const barcode = ref("");
    const productInfo = ref(null);

    const handleBarcodeSubmit = async () => {
      try {
        const productData = await VeganCheck.getProductByBarcode(barcode.value);
        productInfo.value = productData;
      } catch (error) {
        console.error(error);
      }
    };

    return { barcode, productInfo, handleBarcodeSubmit };
  },
};
</script>

Vanilla JavaScript

You will need a bundler like Webpack or Parcel to be able to use ES6 import in the browser.

<div>
  <form id="barcode-form">
    <input type="text" id="barcode-input" placeholder="Enter barcode" />
    <button type="submit">Submit</button>
  </form>
  <div id="product-info">
    <h2>Product Information</h2>
    <pre id="product-data"></pre>
  </div>
</div>

<script type="module">
  import VeganCheck from "@frontendnetwork/vegancheck";

  document.getElementById("product-info").style.display = "none";

  document
    .getElementById("barcode-form")
    .addEventListener("submit", async (e) => {
      e.preventDefault();

      const barcodeInput = document.getElementById("barcode-input");
      const barcode = barcodeInput.value;

      try {
        const productData = await VeganCheck.getProductByBarcode(barcode);
        document.getElementById("product-data").textContent = JSON.stringify(
          productData,
          null,
          2
        );
        document.getElementById("product-info").style.display = "block";
      } catch (error) {
        console.error(error);
      }
    });
</script>
1.1.47

4 months ago

1.1.45

5 months ago

1.1.46

5 months ago

1.1.44

5 months ago

1.1.29

9 months ago

1.1.28

9 months ago

1.1.30

9 months ago

1.1.34

9 months ago

1.1.33

9 months ago

1.1.31

9 months ago

1.1.15

10 months ago

1.1.39

8 months ago

1.1.41

7 months ago

1.1.40

8 months ago

1.1.23

10 months ago

1.1.43

6 months ago

1.1.42

7 months ago

1.1.27

9 months ago

1.1.26

10 months ago

1.1.25

10 months ago

1.1.24

10 months ago

1.1.12

11 months ago

1.1.14

11 months ago

1.1.13

11 months ago

1.1.11

12 months ago

1.1.10

12 months ago

1.1.9

12 months ago

1.1.7

12 months ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.6

1 year ago

1.0.4

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago