mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00:00 
			
		
		
		
	Add fetch wrappers, ignore network errors in actions view (#26985)
1. Introduce lightweight `fetch` wrapper functions that automatically sets csfr token, content-type and use it in `RepoActionView.vue`. 2. Fix a specific issue on `RepoActionView.vue` where a fetch network error is shortly visible during page reload sometimes. It can be reproduced by F5-in in quick succession on the actions view page and was also producing a red error box on the page. Once approved, we can replace all current `fetch` uses in UI with this in another PR. --------- Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
		
							
								
								
									
										38
									
								
								web_src/js/modules/fetch.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								web_src/js/modules/fetch.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| import {isObject} from '../utils.js'; | ||||
|  | ||||
| const {csrfToken} = window.config; | ||||
|  | ||||
| // fetch wrapper, use below method name functions and the `data` option to pass in data | ||||
| // which will automatically set an appropriate content-type header. For json content, | ||||
| // only object and array types are currently supported. | ||||
| function request(url, {headers, data, body, ...other} = {}) { | ||||
|   let contentType; | ||||
|   if (!body) { | ||||
|     if (data instanceof FormData) { | ||||
|       contentType = 'multipart/form-data'; | ||||
|       body = data; | ||||
|     } else if (data instanceof URLSearchParams) { | ||||
|       contentType = 'application/x-www-form-urlencoded'; | ||||
|       body = data; | ||||
|     } else if (isObject(data) || Array.isArray(data)) { | ||||
|       contentType = 'application/json'; | ||||
|       body = JSON.stringify(data); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return fetch(url, { | ||||
|     headers: { | ||||
|       'x-csrf-token': csrfToken, | ||||
|       ...(contentType && {'content-type': contentType}), | ||||
|       ...headers, | ||||
|     }, | ||||
|     ...(body && {body}), | ||||
|     ...other, | ||||
|   }); | ||||
| } | ||||
|  | ||||
| export const GET = (url, opts) => request(url, {method: 'GET', ...opts}); | ||||
| export const POST = (url, opts) => request(url, {method: 'POST', ...opts}); | ||||
| export const PATCH = (url, opts) => request(url, {method: 'PATCH', ...opts}); | ||||
| export const PUT = (url, opts) => request(url, {method: 'PUT', ...opts}); | ||||
| export const DELETE = (url, opts) => request(url, {method: 'DELETE', ...opts}); | ||||
							
								
								
									
										11
									
								
								web_src/js/modules/fetch.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								web_src/js/modules/fetch.test.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import {test, expect} from 'vitest'; | ||||
| import {GET, POST, PATCH, PUT, DELETE} from './fetch.js'; | ||||
|  | ||||
| // tests here are only to satisfy the linter for unused functions | ||||
| test('exports', () => { | ||||
|   expect(GET).toBeTruthy(); | ||||
|   expect(POST).toBeTruthy(); | ||||
|   expect(PATCH).toBeTruthy(); | ||||
|   expect(PUT).toBeTruthy(); | ||||
|   expect(DELETE).toBeTruthy(); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user