From ae8e8f055e9edfe258e641df8752a070ffdd6823 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 19 Sep 2023 02:50:30 +0200 Subject: [PATCH] Use fetch helpers instead of fetch (#27026) WIP because: - [x] Some calls set a `content-type` but send no body, can likely remove the header - [x] Need to check whether `charset=utf-8` has any significance on the webauthn calls, I assume not as it is the default for json content. - [x] Maybe `no-restricted-globals` is better for eslint, but will require a lot of duplication in the yaml or moving eslint config to a `.js` extension. - [x] Maybe export `request` as `fetch`, shadowing the global. --- .eslintrc.yaml | 5 ++- .../contributing/guidelines-frontend.en-us.md | 2 +- web_src/js/components/DashboardRepoList.vue | 5 ++- web_src/js/components/DiffCommitSelector.vue | 3 +- .../js/components/RepoBranchTagSelector.vue | 4 +- web_src/js/features/common-global.js | 7 ++-- web_src/js/features/common-issue-list.js | 5 ++- web_src/js/features/comp/ImagePaste.js | 9 +---- web_src/js/features/comp/ReactionSelector.js | 14 ++----- web_src/js/features/copycontent.js | 3 +- web_src/js/features/install.js | 3 +- web_src/js/features/pull-view-file.js | 9 ++--- web_src/js/features/repo-diff-commit.js | 3 +- web_src/js/features/repo-issue-list.js | 18 ++------- web_src/js/features/repo-migrate.js | 13 ++----- web_src/js/features/user-auth-webauthn.js | 38 +++++++------------ web_src/js/modules/fetch.js | 27 ++++++++----- 17 files changed, 70 insertions(+), 98 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 846823abc..c9ea481af 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -46,6 +46,9 @@ overrides: - files: ["*.config.*"] rules: import/no-unused-modules: [0] + - files: ["web_src/js/modules/fetch.js", "web_src/js/standalone/**/*"] + rules: + no-restricted-syntax: [2, WithStatement, ForInStatement, LabeledStatement, SequenceExpression] rules: "@eslint-community/eslint-comments/disable-enable-pair": [2] @@ -420,7 +423,7 @@ rules: no-restricted-exports: [0] no-restricted-globals: [2, addEventListener, blur, close, closed, confirm, defaultStatus, defaultstatus, error, event, external, find, focus, frameElement, frames, history, innerHeight, innerWidth, isFinite, isNaN, length, location, locationbar, menubar, moveBy, moveTo, name, onblur, onerror, onfocus, onload, onresize, onunload, open, opener, opera, outerHeight, outerWidth, pageXOffset, pageYOffset, parent, print, removeEventListener, resizeBy, resizeTo, screen, screenLeft, screenTop, screenX, screenY, scroll, scrollbars, scrollBy, scrollTo, scrollX, scrollY, self, status, statusbar, stop, toolbar, top, __dirname, __filename] no-restricted-imports: [0] - no-restricted-syntax: [2, WithStatement, ForInStatement, LabeledStatement, SequenceExpression] + no-restricted-syntax: [2, WithStatement, ForInStatement, LabeledStatement, SequenceExpression, {selector: "CallExpression[callee.name='fetch']", message: "use modules/fetch.js instead"}] no-return-assign: [0] no-script-url: [2] no-self-assign: [2, {props: true}] diff --git a/docs/content/contributing/guidelines-frontend.en-us.md b/docs/content/contributing/guidelines-frontend.en-us.md index 921c2b023..0d9e510e7 100644 --- a/docs/content/contributing/guidelines-frontend.en-us.md +++ b/docs/content/contributing/guidelines-frontend.en-us.md @@ -95,7 +95,7 @@ Some lint rules and IDEs also have warnings if the returned Promise is not handl ### Fetching data To fetch data, use the wrapper functions `GET`, `POST` etc. from `modules/fetch.js`. They -accept a `data` option for the content, will automatically set CSFR token and return a +accept a `data` option for the content, will automatically set CSRF token and return a Promise for a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). ### HTML Attributes and `dataset` diff --git a/web_src/js/components/DashboardRepoList.vue b/web_src/js/components/DashboardRepoList.vue index 5b8075f07..5ff51168c 100644 --- a/web_src/js/components/DashboardRepoList.vue +++ b/web_src/js/components/DashboardRepoList.vue @@ -2,6 +2,7 @@ import {createApp, nextTick} from 'vue'; import $ from 'jquery'; import {SvgIcon} from '../svg.js'; +import {GET} from '../modules/fetch.js'; const {appSubUrl, assetUrlPrefix, pageData} = window.config; @@ -233,11 +234,11 @@ const sfc = { try { if (!this.reposTotalCount) { const totalCountSearchURL = `${this.subUrl}/repo/search?count_only=1&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`; - response = await fetch(totalCountSearchURL); + response = await GET(totalCountSearchURL); this.reposTotalCount = response.headers.get('X-Total-Count'); } - response = await fetch(searchedURL); + response = await GET(searchedURL); json = await response.json(); } catch { if (searchedURL === this.searchURL) { diff --git a/web_src/js/components/DiffCommitSelector.vue b/web_src/js/components/DiffCommitSelector.vue index 48dc9d72f..3f7100d20 100644 --- a/web_src/js/components/DiffCommitSelector.vue +++ b/web_src/js/components/DiffCommitSelector.vue @@ -1,5 +1,6 @@