All files / src/helper apihelper.js

0% Statements 0/32
0% Branches 0/6
0% Functions 0/5
0% Lines 0/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                                                                                                                                                                                                                                   
import { API_GATEWAY_URL, REGISTRY_UNIT_NAME } from './constants.js';
import { DOMAIN_NAME, HTTP_ONLY, SAME_SITE, SECURE_COOKIES } from './constants';
 
import Cookies from 'universal-cookie';
 
const APIHelper = function() {};
Object.defineProperty(APIHelper.prototype, 'Url', {
    value: API_GATEWAY_URL,
    writable: false
});
 
Object.defineProperty(APIHelper.prototype, 'Resources', {
    get() {
        return {
            Common: this.Url + 'common',
            Practice: this.Url + 'practice',
            Provider: this.Url + 'provider',
            Patient: this.Url + 'patient',
            RenewToken: this.Url + 'renewtoken',
            patientVisit: this.Url + 'patient',
            RegistryUnit: REGISTRY_UNIT_NAME
        };
    },
    readable: true
});
 
Object.defineProperty(APIHelper.prototype, 'Actions', {
    get() {
        return {
            View: 'View',
            Create: 'Create',
            Update: 'Update',
            Delete: 'Delete'
        };
    },
    readable: true
});
 
Object.defineProperty(APIHelper.prototype, 'PegasusAPI', {
    get() {
        return async (url, requestOptions, callback) => {
            var response = await fetch(url, requestOptions).then(async res => {
                if (res.ok) return res.json();
                if (res.status === 401) {
                    const cookies = new Cookies();
                    cookies.set('interval', '', {
                        path: '/',
                        secure: parseInt(SECURE_COOKIES),
                        maxAge: 1,
                        sameSite: SAME_SITE,
                        domain: DOMAIN_NAME,
                        httpOnly: parseInt(HTTP_ONLY)
                    });
                    const cookiesJwt = new Cookies();
                    cookiesJwt.set('jwt-token', '', {
                        path: '/',
                        secure: parseInt(SECURE_COOKIES),
                        maxAge: 1,
                        sameSite: SAME_SITE,
                        domain: DOMAIN_NAME,
                        httpOnly: parseInt(HTTP_ONLY)
                    });
                    window.location.href = '/';
                } else if (res.status === 403) {
                    return res;
                } else {
                    const error = new Error(res.statusText);
                    error.response = res;
                    return error;
                }
            });
            return response;
        };
    },
    readable: true
});
 
Object.defineProperty(APIHelper.prototype, 'getRequestOption', {
    get() {
        return (query, variables, action, token, signal) => ({
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
                Action: action,
                Authorization: token
            },
            body: JSON.stringify({ query, variables }),
            signal
        });
    },
    readable: true
});
 
Object.defineProperty(APIHelper.prototype, 'JiraGetRequestOption', {
    get() {
        return (body, action, token) => ({
            method: 'POST',
            mode: 'cors',
            headers: {
                'Content-Type': 'application/json',
                Accept: 'application/json',
                Action: action,
                Authorization: token
            },
            body: JSON.stringify(body)
        });
    },
    readable: true
});
 
export default APIHelper;