api-studio
v1.1.0
Published
Http Request made easy using API Layer
Downloads
4
Maintainers
Readme
API studio!
Minimalist package to help you make awesome http request over native fetch methods
Installation
| yarn | npm | |--|--| | yarn add api-studio | npm i api-studio |
Examples
Example N°1
Example using React (Inside Functionnal components)
const [users, setUsers] = useState<User[]>([]);
api.get<User[]>("http://your-route.com/users")
.then(setUsers)
.catch(error=>{console.log(error.message)})
Example N°2
Example using common TypeScript
const handleUsersList = (users:User[])=>{
//Do your stuff
}
const handleError = (error:Error)=>{
//Handle raised error
}
api.get<User[]>("http://your-route.com/users")
.then(handleUsersList)
.catch(handleError)
Example N°3
Example with route Data's Type params.
Note that for GET requests, data params are parsed and replaced into route using awsm-url-builder.
For POST/PUT/PATCH/DELETE verbs, DataType is considered as body part and formated using FormData
Example using POST
ex /POST http://market.com/article/save
- body :
cost:1000
name:"Soap"
category:3
usinf fectch will be
fecth("http://market.com/article/save", {
method:"POST",
mode:"cors",
body:JSON.stringify(data)
}).then(responseHandler).cacth(errorHandler)
------
Using api-studio
type Params = {
cost:number
name:string
category:number
...
}
This request will return Product object
api.get<Product, Params>("http://market.com/article/save", {cost:1000, name:"Soap",category:3})
.then(responseHandler)
.catch(errorHandler)