Skip to content

Instantly share code, notes, and snippets.

@muz3
Forked from paulsturgess/service.js
Created November 30, 2020 06:23
Show Gist options
  • Select an option

  • Save muz3/bc6ded8dd9fa1c61894f55ab3397debe to your computer and use it in GitHub Desktop.

Select an option

Save muz3/bc6ded8dd9fa1c61894f55ab3397debe to your computer and use it in GitHub Desktop.

Revisions

  1. Paul Sturgess revised this gist Feb 16, 2017. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions service.js
    Original file line number Diff line number Diff line change
    @@ -16,12 +16,20 @@ class Service {
    handleError = (error) => {
    switch (error.response.status) {
    case 401:
    // perhaps redirect to login page
    case 500:
    // perhaps redirect to 500 error page
    this.redirectTo(document, '/')
    break;
    case 404:
    this.redirectTo(document, '/404')
    break;
    default:
    // perhaps trigger an action to update the store with the error
    this.redirectTo(document, '/500')
    break;
    }
    return Promise.reject(error)
    }

    redirectTo = (document, path) => {
    document.location = path
    }

    get(path, callback) {
  2. Paul Sturgess revised this gist Feb 16, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion service.js
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ class Service {
    return response;
    }

    handleError(error) {
    handleError = (error) => {
    switch (error.response.status) {
    case 401:
    // perhaps redirect to login page
  3. Paul Sturgess revised this gist Feb 9, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion service.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ class Service {
    }

    handleError(error) {
    switch (error.status) {
    switch (error.response.status) {
    case 401:
    // perhaps redirect to login page
    case 500:
  4. Paul Sturgess revised this gist Feb 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion service.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ class Service {
    handleError(error) {
    switch (error.status) {
    case 401:
    // perhaps redirect to 404 not found page
    // perhaps redirect to login page
    case 500:
    // perhaps redirect to 500 error page
    default:
  5. Paul Sturgess created this gist Feb 8, 2017.
    52 changes: 52 additions & 0 deletions service.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import axios from 'axios';

    class Service {
    constructor() {
    let service = axios.create({
    headers: {csrf: 'token'}
    });
    service.interceptors.response.use(this.handleSuccess, this.handleError);
    this.service = service;
    }

    handleSuccess(response) {
    return response;
    }

    handleError(error) {
    switch (error.status) {
    case 401:
    // perhaps redirect to 404 not found page
    case 500:
    // perhaps redirect to 500 error page
    default:
    // perhaps trigger an action to update the store with the error
    }
    }

    get(path, callback) {
    return this.service.get(path).then(
    (response) => callback(response.status, response.data)
    );
    }

    patch(path, payload, callback) {
    return this.service.request({
    method: 'PATCH',
    url: path,
    responseType: 'json',
    data: payload
    }).then((response) => callback(response.status, response.data));
    }

    post(path, payload, callback) {
    return this.service.request({
    method: 'POST',
    url: path,
    responseType: 'json',
    data: payload
    }).then((response) => callback(response.status, response.data));
    }
    }

    export default new Service;