yarn add type-api or npm install type-api
import {RestApi, rest} from 'type-api'
/**
* Define rest api
*/
@rest({
baseUrl: 'https://jsonplaceholder.typicode.com',
endpoint: '/posts'
})
class PostApi extends RestApi {
// Here you can define additional methods as needed
}
/**
* Initialize api
*/
export const postApi = new PostApi()
// Get one by ID
try {
const response = postApi.findById(1)
console.log(response)
} catch (error) {
console.error(error)
}
// Create entity
try {
const postData = {
"userId": 1,
"title": "New user",
"body": "Some content"
},
const response = postApi.create(postData)
console.log(response)
} catch (error) {
console.error(error)
}
// Update entity
try {
const postData = {
"userId": 1,
"title": "New user",
"body": "Some content"
},
const response = postApi.update(1, postData)
console.log(response)
} catch (error) {
console.error(error)
}findById(id): GET - retrieve one record as objectfindAll(): GET - retrieve all records as listfind({limit: 3}): GET - retrieve records as list and generate query string from objectcreate({name: 'Some Name'}): POST - submit object for creationupdate(1, {name: 'Some Name'}): PUT - submit object for update
get('custom')post('custom', postData)put('custom', postData)