npm i larapi
const api = new API({
host: 'https://my-host'
});
...
API.setHost('https://my-host');
type TTodo = {
id: number;
userId: number;
title: string;
completed: boolean;
};
type TApiTodos = {
success: {
'todos': () => TTodo[],
[path: `todos/${number}`]: (request: Omit<TTodo, 'id'>) => TTodo;
},
error: {
message: string;
}
};
const api = new API<TApiTodos>({
host: 'https://jsonplaceholder.typicode.com/',
});
...
api.get({
path: 'todos/1',
success: (e) => {
console.log(e.id);
},
error: (e) => {
console.log(e);
}
});
api.setToken('token');
api.setUser({...object-user});
api.getUser();
...
api.user;
api.get({
path: '/test',
success: (e) => {
console.log(e)
}
});
api.post({
path: '/test',
data: {
message: 'hello world'
}
success: (e) => {
console.log(e)
}
});
const request = api.get({
path: '/test',
cache: true,
...
});
request.clearCache();
first argument in the method is object updated data, and last argument is key field for search in the current cache, if you need update all data use null
request.updateCache({
message: 'update this string'
}, null);