Typescript enabled API library
Consumption of published library:
yarn add type-api
or npm install type-api
Webpack
import {RestApi} from 'type-api'
class PostApi extends RestApi {
baseUrl = 'https://jsonplaceholder.typicode.com'
endpoint = '/posts'
}
export const postApi = new PostApi()
try {
const response = postApi.findById(1)
console.log(response)
} catch (error) {
console.error(error)
}
try {
const postData = {
"userId": 1,
"title": "New user",
"body": "Some content"
},
const response = postApi.create(postData)
console.log(response)
} catch (error) {
console.error(error)
}
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)
}
Available methods
Rest API
findById(id)
: GET - retrieve one record as objectfindAll()
: GET - retrieve all records as listfindOne({name: 'some name'})
: GET - retrieve one record from list of response resultsfind({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
Base API
get('custom')
post('custom', postData)
put('custom', putData)
patch('custom', patchData)