JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q105294F
  • License MIT

Package to handle api requests with a hook, provider and web worker

Package Exports

  • @mainframework/api-reqpuest-provider-worker-hook
  • @mainframework/api-reqpuest-provider-worker-hook/dist/esm/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mainframework/api-reqpuest-provider-worker-hook) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

create an npm package

Installation:

npm install package-name yarn pacakge-name

urls

Usage :

App.tsx

Wrap your application with the ApiWorkerProvider

import {App} from "./App";
import { ApiWorkerProvider } from "@mainframework/api-reqpuest-provider-worker-hook";

export const App = () => (
  <ApiWorkerProvider>
    <App />
  </ApiWorkerProvider>
);

making a request

In a component, where you need to make a request, use the useApiWorker hook for each request. You can use multiple instances of the hook, and make: get, post, patch and delete reqeusts

import { useEffect } from "react";
import { useApiWorker } from "@mainframework/api-reqpuest-provider-worker-hook";

export const App = () => (
 const [todos, todosRequest] = useApiWorker({
    type: "Get",
    url: "https://jsonplaceholder.typicode.com/todos/1",
    "x-api-key":
      "live_YedloihKi9ObVaF7LovnmMzpe6PYkvT6NpZhRupWl0Z6VDi9WWTpHk6zqlsaqi7z",
  });

  const [cats, catRequest] = useApiWorker({
    type: "Get",
    url: "https://api.thecatapi.com/v1/images/search?limit=10",
  });

  const [posts, postsRequest] = useApiWorker({
    type: "Post",
    url: "https://jsonplaceholder.typicode.com/posts",
    payload:{
      title: 'foo',
      body: 'bar',
      userId: 1,
    },headers: {
        "Content-type": "application/json; charset=UTF-8"
    },
  });

  useEffect(() => {
    catRequest();
    todosRequest();
    postsRequest();
  }, []);

  return (
    <div>
      {todos && (
        <div>
          <span>Todos</span>
          <div>{JSON.stringify(todos)}</div>
        </div>
      )}
      <hr />
      {cats && (
        <div>
          <span>Cats</span>
          <div>{JSON.stringify(cats)}</div>
        </div>
      )} <hr />
      {posts && (
        <div>
          <span>Posts</span>
          <div>{JSON.stringify(posts)}</div>
        </div>
      )}
    </div>
  );
);