![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|---|---|---|---|---|---|
| Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
Using npm:
$ npm install api-workerUsing bower:
$ bower install api-workerUsing yarn:
$ yarn add api-workerUsing pnpm:
$ pnpm add api-workerOnce the package is installed, you can import the library using import or require approach:
import{apiWorker}from'api-worker';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;};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>)}




