Skip to content

Repository files navigation


Browser Support

ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔11 ✔

Installing

Package manager

Using npm:

$ npm install api-worker

Using bower:

$ bower install api-worker

Using yarn:

$ yarn add api-worker

Using pnpm:

$ pnpm add api-worker

Once the package is installed, you can import the library using import or require approach:

import{apiWorker}from'api-worker';

Types

api-worker props:

exportenumApiWorkerMethod{GET='GET',POST='POST',DELETE='DELETE',PUT='PUT',PATCH='PATCH',PURGE='PURGE',}exportenumApiWorkerResponse{BLOB='BLOB',JSON='JSON',ARRAY_BUFFER='ARRAY_BUFFER',TEXT='TEXT',}exporttypeApiWorkerProps={url: string;method?: ApiWorkerMethod;body?: any;headers?: object;abortController?: AbortController;timeout?: number;responseType?: ApiWorkerResponse;urlParams?: string;onSuccess?: (data: any)=>void;onError?: (data: any)=>void;};

Example

First declare, yours endpoint in a file. -services.ts

import{apiWorker}from'api-worker';// First you need configurate, your endpoint// services/endpoints.tsexportfunctiongetExample({
onSuccess =(data: any)=>{},
onError =(data: any)=>{},}: {onSuccess: (data: any)=>void,onError: (data: any)=>void,}){apiWorker({// Is comming a new feature, for you declare only, your endpointurl: 'https://api.github.com/orgs/Wasyto/',method: 'GET',
onSuccess,
onError,});}exportfunctionsetExample({
body,
onSuccess =(data: any)=>{},
onError =(data: any)=>{},}: {body={}onSuccess: (data: any)=>void,onError: (data: any)=>void,}){apiWorker({// Is comming a new feature, for you declare only, your endpointurl: 'https://api.github.com/orgs/Wasyto/',method: 'POST',
body =body,
onSuccess,
onError,});}

Second and last step, is you call your function.

import{setExample,getExample}from"./services.ts"importuseEffectfrom"react"functionApp(){UseEffect(()=>{setExample({body: {example: 'Wasyto'},onSucces: (data)=>console.log(data),onError: (error)=>console.log(error),});getExample({onSucces: (data)=>console.log(data),onError: (error)=>console.log(error),});},[])return(<divclassName="app"></div>)}

About

Javascript Library, with pourpouse to make to do better requests API REST.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages