| 
									
										
										
										
											2023-08-16 18:52:31 -04:00
										 |  |  | let csrfToken: string | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  | let unraidCsrfToken: string | undefined // required for unraid POST requests (#8062)
 | 
					
						
							| 
									
										
										
										
											2023-08-16 18:52:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 13:20:25 -04:00
										 |  |  | // apiFetch wraps the standard JS fetch function with csrf header
 | 
					
						
							|  |  |  | // management and param additions specific to the web client.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // apiFetch adds the `api` prefix to the request URL,
 | 
					
						
							|  |  |  | // so endpoint should be provided without the `api` prefix
 | 
					
						
							|  |  |  | // (i.e. provide `/data` rather than `api/data`).
 | 
					
						
							| 
									
										
										
										
											2023-08-16 18:52:31 -04:00
										 |  |  | export function apiFetch( | 
					
						
							| 
									
										
										
										
											2023-08-29 13:20:25 -04:00
										 |  |  |   endpoint: string, | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  |   method: "GET" | "POST", | 
					
						
							|  |  |  |   body?: any, | 
					
						
							|  |  |  |   params?: Record<string, string> | 
					
						
							| 
									
										
										
										
											2023-08-16 18:52:31 -04:00
										 |  |  | ): Promise<Response> { | 
					
						
							| 
									
										
										
										
											2023-08-29 13:20:25 -04:00
										 |  |  |   const urlParams = new URLSearchParams(window.location.search) | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  |   const nextParams = new URLSearchParams(params) | 
					
						
							| 
									
										
										
										
											2023-08-29 13:20:25 -04:00
										 |  |  |   const token = urlParams.get("SynoToken") | 
					
						
							|  |  |  |   if (token) { | 
					
						
							|  |  |  |     nextParams.set("SynoToken", token) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const search = nextParams.toString() | 
					
						
							|  |  |  |   const url = `api${endpoint}${search ? `?${search}` : ""}` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  |   var contentType: string | 
					
						
							|  |  |  |   if (unraidCsrfToken) { | 
					
						
							|  |  |  |     const params = new URLSearchParams() | 
					
						
							|  |  |  |     params.append("csrf_token", unraidCsrfToken) | 
					
						
							|  |  |  |     if (body) { | 
					
						
							|  |  |  |       params.append("ts_data", JSON.stringify(body)) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     body = params.toString() | 
					
						
							|  |  |  |     contentType = "application/x-www-form-urlencoded;charset=UTF-8" | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     body = body ? JSON.stringify(body) : undefined | 
					
						
							|  |  |  |     contentType = "application/json" | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 13:20:25 -04:00
										 |  |  |   return fetch(url, { | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  |     method: method, | 
					
						
							|  |  |  |     headers: { | 
					
						
							|  |  |  |       Accept: "application/json", | 
					
						
							|  |  |  |       "Content-Type": contentType, | 
					
						
							|  |  |  |       "X-CSRF-Token": csrfToken, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     body, | 
					
						
							| 
									
										
										
										
											2023-08-16 18:52:31 -04:00
										 |  |  |   }).then((r) => { | 
					
						
							|  |  |  |     updateCsrfToken(r) | 
					
						
							|  |  |  |     if (!r.ok) { | 
					
						
							|  |  |  |       return r.text().then((err) => { | 
					
						
							|  |  |  |         throw new Error(err) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return r | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function updateCsrfToken(r: Response) { | 
					
						
							|  |  |  |   const tok = r.headers.get("X-CSRF-Token") | 
					
						
							|  |  |  |   if (tok) { | 
					
						
							|  |  |  |     csrfToken = tok | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-08-29 18:02:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function setUnraidCsrfToken(token?: string) { | 
					
						
							|  |  |  |   unraidCsrfToken = token | 
					
						
							|  |  |  | } |