aboutsummaryrefslogtreecommitdiff
path: root/lib/vnlib.browser/src/helpers/apiCall.ts
blob: b98fecb0fbb4232c3ec1a6b3eb82c7378b783b5d (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2023 Vaughn Nugent
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import { type MaybeRef, type Ref } from 'vue' 
import { AxiosError, type Axios } from 'axios';
import { defaultTo, isArray, isNil, isEqual } from 'lodash-es';
import { get, useConfirmDialog } from '@vueuse/core';
import { useFormToaster, useToaster } from '../toast';
import { useWait } from './wait';
import { useAxiosInternal } from '../axios';
import type { IErrorNotifier, CombinedToaster } from '../toast';

export interface IApiHandle<T> {
    /**
     * Called to get the object to pass to apiCall is invoked
     */
    getCallbackObject(): T;

    /**
     * Called to get the notifier to use for the api call
     */
    getNotifier(): IErrorNotifier;

    /**
     * Called to set the waiting flag
     */
    setWaiting: (waiting: boolean) => void;
}

export interface IApiPassThrough {
    readonly axios: Axios;
    readonly toaster: CombinedToaster;
}

export interface IElevatedCallPassThrough extends IApiPassThrough {
    readonly password: string;
}

export interface ApiCall<T> {
    <TR>(callback: (data: T) => Promise<TR | undefined>): Promise<TR | undefined>;
}

export type CustomMessageHandler = (message: string) => void;

const useApiCallInternal = <T>(args: IApiHandle<T>): ApiCall<T> => {

    /**
     * Provides a wrapper method for making remote api calls to a server
     * while capturing context and errors and common api arguments.
     * @param {*} callback The method to call within api request context
     * @returns A promise that resolves to the result of the async function
     */
    return async <TR>(callback: (data: T) => Promise<TR | undefined>): Promise<TR | undefined> => {
        const notifier = args.getNotifier();

        // Set the waiting flag
        args.setWaiting(true);

        try {
            //Close the current toast value
            notifier.close();

            const obj = args.getCallbackObject();

            //Exec the async function
            return await callback(obj);

            // eslint-disable-next-line @typescript-eslint/no-explicit-any
        } catch (errMsg: any) {
            console.error(errMsg)
            // See if the error has an axios response
            if (isNil(errMsg.response)) {
                if (errMsg.message === 'Network Error') {
                    notifier.notifyError('Please check your internet connection')
                } else {
                    notifier.notifyError('An unknown error occured')
                }
                return
            }
            // Axios error message
            const response = errMsg.response
            const errors = response?.data?.errors
            const hasErrors = isArray(errors) && errors.length > 0

            const SetMessageWithDefault = (message: string) => {
                if (hasErrors) {
                    const title = 'Please verify your ' + defaultTo(errors[0].property, 'form')
                    notifier.notifyError(title, errors[0].message)
                } else {
                    notifier.notifyError(defaultTo(response?.data?.result, message))
                }
            }

            switch (response.status) {
                case 200:
                    SetMessageWithDefault('')
                    break
                case 400:
                    SetMessageWithDefault('Bad Request')
                    break
                case 422:
                    SetMessageWithDefault('The server did not accept the request')
                    break
                case 401:
                    SetMessageWithDefault('You are not logged in.')
                    break
                case 403:
                    SetMessageWithDefault('Please clear you cookies/cache and try again')
                    break
                case 404:
                    SetMessageWithDefault('The requested resource was not found')
                    break
                case 409:
                    SetMessageWithDefault('Please clear you cookies/cache and try again')
                    break
                case 410:
                    SetMessageWithDefault('The requested resource has expired')
                    break
                case 423:
                    SetMessageWithDefault('The requested resource is locked')
                    break
                case 429:
                    SetMessageWithDefault('You have made too many requests, please try again later')
                    break
                case 500:
                    SetMessageWithDefault('There was an error processing your request')
                    break
                default:
                    SetMessageWithDefault('An unknown error occured')
                    break
            }
        } finally {
            // Clear the waiting flag
            args.setWaiting(false);
        }
    }
}

const creatApiHandle = (notifier: MaybeRef<IErrorNotifier>, axios: Ref<Axios>): IApiHandle<IApiPassThrough> => {

    const toaster = useToaster();
    const { setWaiting } = useWait();

    const getCallbackObject = (): IApiPassThrough =>  ({ axios: get(axios), toaster })
    const getNotifier = (): IErrorNotifier => get(notifier);

    return { getCallbackObject, getNotifier, setWaiting }
}

/**
 * Provides a wrapper method for making remote api calls to a server
 * while capturing context and errors and common api arguments.
 * @param {*} asyncFunc The method to call within api request context
 * @returns A promise that resolves to the result of the async function
 */
export const apiCall = (() =>{

    const axios = useAxiosInternal(null);
    const errorNotifier = useFormToaster();

    //Create the api call handle
    const handle = creatApiHandle(errorNotifier, axios);
    //Confiugre the api call to use global configuration
    return useApiCallInternal(handle);
})();

/**
 * Customizes the api call to use a custom error message
 * @param msg The message to display when an error occurs
 * @returns {Object} The api call object {apiCall: Promise }
 */
export const configureApiCall = (msg: CustomMessageHandler): { apiCall: ApiCall<IApiPassThrough> } =>{
    
    const notifier = ((): IErrorNotifier => {
        return{
            notifyError: (t: string, m?: string) => {
                msg(t);
                return m;
            },
            close(id: string) {
                msg('')
                return id;
            },
        }
    })()

    const axios = useAxiosInternal(null);

    //Create custom api handle
    const handle = creatApiHandle(notifier, axios);

    //Confiugre the api call to use global configuration
    const apiCall = useApiCallInternal(handle);
    return { apiCall }
}

/**
 * Gets the shared password prompt object and the elevated api call method handler 
 * to allow for elevated api calls that require a password.
 * @returns {Object} The password prompt configuration object, and the elevated api call method
 */
export const usePassConfirm = (() => {

    //Shared confirm object
    const confirm = useConfirmDialog();

    /**
     * Displays the password prompt and executes the api call with the password
     * captured from the prompt. If the api call returns a 401 error, the password
     * prompt is re-displayed and the server error message is displayed in the form
     * error toaster.
     * @param callback The async callback method that invokes the elevated api call.
     * @returns A promise that resolves to the result of the async function
     */
    const elevatedApiCall = <T>(callback: (api: IElevatedCallPassThrough) => Promise<T>): Promise<T | undefined> => {
        //Invoke api call method but handle 401 errors by re-displaying the password prompt
        return apiCall<T>(async (api: IApiPassThrough) : Promise<T | undefined> => {
            // eslint-disable-next-line no-constant-condition
            while (1) {

                //Display the password prompt
                const { data, isCanceled } = await confirm.reveal()
                
                if (isCanceled) {
                    break;
                }

                try {
                    //Execute the api call with prompt response
                    return await callback({...api, ...data });
                }
                //Catch 401 errors and re-display the password prompt, otherwise throw the error
                catch (err) {
                    if(!(err instanceof AxiosError)){
                       throw err;
                    }

                    const { response } = err;

                    if(isNil(response)){
                        throw err;
                    }

                    //Check status code, if 401, re-display the password prompt
                    if (!isEqual(response?.status, 401)) {
                        throw err;
                    } 

                    //Display the error message
                    api.toaster.form.error({ title: response.data.result });

                    //Re-display the password prompt
                }
            }
        })
    }

    //Pass through confirm object and elevated api call
    return () => ({ ...confirm, elevatedApiCall })
})();