Skip to content

Instantly share code, notes, and snippets.

@JaysonChiang
Last active September 1, 2025 23:37
Show Gist options
  • Save JaysonChiang/fa704307bacffe0f17d51acf6b1292fc to your computer and use it in GitHub Desktop.
Save JaysonChiang/fa704307bacffe0f17d51acf6b1292fc to your computer and use it in GitHub Desktop.

Revisions

  1. JaysonChiang revised this gist Jun 12, 2021. 1 changed file with 31 additions and 27 deletions.
    58 changes: 31 additions & 27 deletions api.ts
    Original 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.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;

    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 401:
    console.error('unauthorised');
    break;

    case 404:
    console.error('/not-found');
    break;
    case 404:
    console.error('/not-found');
    break;

    case 500:
    console.error('/server-error');
    break;
    case 500:
    console.error('/server-error');
    break;
    }
    return Promise.reject(error);
    }
    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)
    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)
    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)
    create: (data: User) => request.post<User>('/users', data),
    };

    const api = {
    todos,
    users
    users,
    };

    export default api;
    export default api;
  2. JaysonChiang renamed this gist Jun 12, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. JaysonChiang revised this gist Jun 12, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions agent.ts
    Original file line number Diff line number Diff line change
    @@ -61,9 +61,9 @@ const users = {
    create: (data: User) => request.post<User>('/users', data)
    };

    const agent = {
    const api = {
    todos,
    users
    };

    export default agent;
    export default api;
  4. JaysonChiang created this gist Jun 12, 2021.
    69 changes: 69 additions & 0 deletions agent.ts
    Original 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;