| 1 |  |  | import dayjs from "dayjs"; | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import rootAxios from "axios"; | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | import { baseApiUrl } from "../helpers/routes"; | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | export interface ResponseData { | 
            
                                                        
            
                                    
            
            
                | 6 |  |  |   [key: string]: string & ResponseData & [ResponseData]; | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | export interface ApiResponse { | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |   data: ResponseData; | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | export const baseUrl = baseApiUrl; | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 14 |  |  | export const parseDateStrict = (date: string): Date => dayjs(date).toDate(); | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 16 |  |  | export const parseDate = (date: string | null): Date | null => { | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |   return date !== null ? parseDateStrict(date) : null; | 
            
                                                        
            
                                    
            
            
                | 18 |  |  | }; | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 20 |  |  | export const addQueryParameters = ( | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |   url: string, | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |   parameters: Map<string, string>, | 
            
                                                        
            
                                    
            
            
                | 23 |  |  | ): string => { | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |   const toKeyValuePairs = ([key, value]: string[]): string => | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     `${encodeURI(key)}=${encodeURI(value)}`; | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |   const parameterString = Array.from(parameters.entries()) | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     .map(toKeyValuePairs) | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     .join("&"); | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |   return `${url}?${parameterString}`; | 
            
                                                        
            
                                    
            
            
                | 30 |  |  | }; | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 32 |  |  | /** | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  * Get the page's CSRF token, which laravel uses to validate requests. | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |  */ | 
            
                                                        
            
                                    
            
            
                | 35 |  |  | // TODO: is this the best way to get this? | 
            
                                                        
            
                                    
            
            
                | 36 |  |  | const csrfElement = document.head.querySelector('meta[name="csrf-token"]'); | 
            
                                                        
            
                                    
            
            
                | 37 |  |  | const csrfToken: string = | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |   csrfElement && csrfElement.getAttribute("content") | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |     ? (csrfElement.getAttribute("content") as string) | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     : ""; | 
            
                                                        
            
                                    
            
            
                | 41 |  |  | const axiosConfig = { | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |   headers: { | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |     "X-Requested-With": "XMLHttpRequest", | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |     "X-CSRF-TOKEN": csrfToken, | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |   }, | 
            
                                                        
            
                                    
            
            
                | 46 |  |  | }; | 
            
                                                        
            
                                    
            
            
                | 47 |  |  | export const axios = rootAxios.create(axiosConfig); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 49 |  |  | export default { | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |   baseUrl, | 
            
                                                        
            
                                    
            
            
                | 51 |  |  | }; | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  |