| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | import { AxiosResponse } from "axios"; | 
					
						
							|  |  |  | import { useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-05-25 09:38:21 -08:00
										 |  |  | import type { NuxtAxiosInstance } from "@nuxtjs/axios"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { ApiRequestInstance, RequestResponse } from "~/lib/api/types/non-generated"; | 
					
						
							|  |  |  | import { AdminAPI, PublicApi, UserApi } from "~/lib/api"; | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const request = { | 
					
						
							| 
									
										
										
										
											2022-05-06 11:18:06 -08:00
										 |  |  |   async safe<T, U>( | 
					
						
							|  |  |  |     funcCall: (url: string, data: U) => Promise<AxiosResponse<T>>, | 
					
						
							|  |  |  |     url: string, | 
					
						
							|  |  |  |     data: U | 
					
						
							|  |  |  |   ): Promise<RequestResponse<T>> { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     let error = null; | 
					
						
							|  |  |  |     const response = await funcCall(url, data).catch(function (e) { | 
					
						
							|  |  |  |       console.log(e); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       // Insert Generic Error Handling Here
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       error = e; | 
					
						
							|  |  |  |       return null; | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     return { response, error, data: response?.data ?? null }; | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | function getRequests(axiosInstance: NuxtAxiosInstance): ApiRequestInstance { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2021-08-08 20:52:44 -08:00
										 |  |  |     async get<T>(url: string, params = {}): Promise<RequestResponse<T>> { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |       let error = null; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       const response = await axiosInstance.get<T>(url, params).catch((e) => { | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |         error = e; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       if (response != null) { | 
					
						
							|  |  |  |         return { response, error, data: response?.data }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return { response: null, error, data: null }; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     async post<T, U>(url: string, data: U) { | 
					
						
							|  |  |  |       // eslint-disable-next-line @typescript-eslint/unbound-method
 | 
					
						
							|  |  |  |       return await request.safe<T, U>(axiosInstance.post, url, data); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     async put<T, U = T>(url: string, data: U) { | 
					
						
							|  |  |  |       // eslint-disable-next-line @typescript-eslint/unbound-method
 | 
					
						
							|  |  |  |       return await request.safe<T, U>(axiosInstance.put, url, data); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     async patch<T, U = Partial<T>>(url: string, data: U) { | 
					
						
							|  |  |  |       // eslint-disable-next-line @typescript-eslint/unbound-method
 | 
					
						
							|  |  |  |       return await request.safe<T, U>(axiosInstance.patch, url, data); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async delete<T>(url: string) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       // eslint-disable-next-line @typescript-eslint/unbound-method
 | 
					
						
							|  |  |  |       return await request.safe<T, undefined>(axiosInstance.delete, url, undefined); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 11:24:17 -08:00
										 |  |  | export const useAdminApi = function (): AdminAPI { | 
					
						
							| 
									
										
										
										
											2022-04-10 14:07:35 -08:00
										 |  |  |   const { $axios, i18n } = useContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $axios.setHeader("Accept-Language", i18n.locale); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 11:24:17 -08:00
										 |  |  |   const requests = getRequests($axios); | 
					
						
							|  |  |  |   return new AdminAPI(requests); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-08-21 00:46:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | export const useUserApi = function (): UserApi { | 
					
						
							| 
									
										
										
										
											2022-04-10 14:07:35 -08:00
										 |  |  |   const { $axios, i18n } = useContext(); | 
					
						
							|  |  |  |   $axios.setHeader("Accept-Language", i18n.locale); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   const requests = getRequests($axios); | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |   return new UserApi(requests); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2022-05-06 11:18:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const usePublicApi = function (): PublicApi { | 
					
						
							|  |  |  |   const { $axios, i18n } = useContext(); | 
					
						
							|  |  |  |   $axios.setHeader("Accept-Language", i18n.locale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const requests = getRequests($axios); | 
					
						
							|  |  |  |   return new PublicApi(requests); | 
					
						
							|  |  |  | }; |