Last active
September 1, 2025 23:37
-
-
Save JaysonChiang/fa704307bacffe0f17d51acf6b1292fc to your computer and use it in GitHub Desktop.
Revisions
-
JaysonChiang revised this gist
Jun 12, 2021 . 1 changed file with 31 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,12 +4,14 @@ import token from './somewhere'; interface Todo { id: string; title: string; } interface User { id: string; name: string; } axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com'; axios.interceptors.request.use((config) => { if (token) { @@ -18,52 +20,54 @@ axios.interceptors.request.use((config) => { return config; }); axios.interceptors.response.use( (res) => res, (error: AxiosError) => { const { data, status, config } = error.response!; switch (status) { case 400: console.error(data); break; case 401: console.error('unauthorised'); break; case 404: console.error('/not-found'); break; case 500: console.error('/server-error'); break; } return Promise.reject(error); } ); const responseBody = <T>(response: AxiosResponse<T>) => response.data; const request = { get: <T>(url: string) => axios.get<T>(url).then(responseBody), post: <T>(url: string, body: {}) => axios.post<T>(url, body).then(responseBody), }; const todos = { list: () => request.get<Todo[]>('/todos'), details: (id: string) => request.get<Todo>(`/todos/${id}`), create: (data: Todo) => request.post<void>('/todos', data), }; const users = { list: () => request.get<User[]>('/users'), details: (id: string) => request.get<User>(`/users/${id}`), create: (data: User) => request.post<User>('/users', data), }; const api = { todos, users, }; export default api; -
JaysonChiang renamed this gist
Jun 12, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
JaysonChiang revised this gist
Jun 12, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,9 +61,9 @@ const users = { create: (data: User) => request.post<User>('/users', data) }; const api = { todos, users }; export default api; -
JaysonChiang created this gist
Jun 12, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ import axios, { AxiosError, AxiosResponse } from 'axios'; import token from './somewhere'; interface Todo { id: string; title: string; }; interface User { id: string; name: string; }; axios.interceptors.request.use((config) => { if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }); axios.defaults.baseURL = 'https://jsonplaceholder.typicode.com'; axios.interceptors.response.use(res => res, (error: AxiosError) => { const { data, status, config } = error.response!; switch(status) { case 400: console.error(data); break; case 401: console.error('unauthorised'); break; case 404: console.error('/not-found'); break; case 500: console.error('/server-error'); break; } return Promise.reject(error); }); const responseBody = <T>(response: AxiosResponse<T>) => response.data; const request = { get: <T>(url: string) => axios.get<T>(url).then(responseBody), post: <T>(url:string, body: {}) => axios.post<T>(url, body).then(responseBody) }; const todos = { list: () => request.get<Todo[]>('/todos'), details: (id: string) => request.get<Todo>(`/todos/${id}`), create: (data: Todo) => request.post<void>('/todos', data) }; const users = { list: () => request.get<User[]>('/users'), details: (id: string) => request.get<User>(`/users/${id}`), create: (data: User) => request.post<User>('/users', data) }; const agent = { todos, users }; export default agent;