Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: re-implement oauth store module with pinia (#1026)

Liu Ji9d5d9c579a89
diff --git a/frontend/src/components/RepositorySelectionPanel.vue b/frontend/src/components/RepositorySelectionPanel.vue
index ad48a2a2ce..cc4fab057c 100644
--- a/frontend/src/components/RepositorySelectionPanel.vue
+++ b/frontend/src/components/RepositorySelectionPanel.vue
@@ -47,6 +47,7 @@ import {
OAuthToken,
ProjectRepositoryConfig,
} from "../types";
+import { useOAuthStore } from "@/store";
interface LocalState {
repositoryList: ExternalRepositoryInfo[];
@@ -73,8 +74,8 @@ onMounted(() => {
const prepareRepositoryList = () => {
if (props.config.vcs.type == "GITLAB_SELF_HOST") {
- store
- .dispatch("oauth/exchangeVCSToken", {
+ useOAuthStore()
+ .exchangeVCSToken({
vcsId: props.config.vcs.id,
code: props.config.code,
})
diff --git a/frontend/src/store/modules/oauth.ts b/frontend/src/store/modules/oauth.ts
deleted file mode 100644
index 61b7894c2e..0000000000
--- a/frontend/src/store/modules/oauth.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import axios from "axios";
-import { VCSId, OAuthToken, ResourceObject } from "../../types";
-
-const convert = (raw: ResourceObject): OAuthToken => {
- const attr = raw.attributes;
- return {
- accessToken: attr.accessToken as string,
- refreshToken: attr.refreshToken as string,
- expiresTs: attr.expiresTs as number,
- };
-};
-
-const actions = {
- async exchangeVCSToken(
- {}: any,
- {
- vcsId,
- code,
- }: {
- vcsId: VCSId;
- code: string;
- }
- ): Promise<OAuthToken> {
- const data = (
- await axios.post(`/api/oauth/vcs/${vcsId}/exchange-token`, null, {
- headers: { code },
- })
- ).data.data;
-
- const token = convert(data);
- return token;
- },
-};
-export default {
- namespaced: true,
- actions,
-};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index c21844976a..8abadd8b81 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -2,6 +2,7 @@ export * from "./actuator";
export * from "./command";
export * from "./debug";
export * from "./label";
+export * from "./oauth";
export * from "./router";
export * from "./subscription";
export * from "./tab";
diff --git a/frontend/src/store/pinia-modules/oauth.ts b/frontend/src/store/pinia-modules/oauth.ts
new file mode 100644
index 0000000000..5f1670969b
--- /dev/null
+++ b/frontend/src/store/pinia-modules/oauth.ts
@@ -0,0 +1,27 @@
+import axios from "axios";
+import { VCSId, OAuthToken, ResourceObject } from "@/types";
+import { defineStore } from "pinia";
+
+const convert = (raw: ResourceObject): OAuthToken => {
+ const attr = raw.attributes;
+ return {
+ accessToken: attr.accessToken as string,
+ refreshToken: attr.refreshToken as string,
+ expiresTs: attr.expiresTs as number,
+ };
+};
+
+export const useOAuthStore = defineStore("oauth", {
+ actions: {
+ async exchangeVCSToken({ vcsId, code }: { vcsId: VCSId; code: string }) {
+ const data = (
+ await axios.post(`/api/oauth/vcs/${vcsId}/exchange-token`, null, {
+ headers: { code },
+ })
+ ).data.data;
+
+ const token = convert(data);
+ return token;
+ },
+ },
+});
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index aa53921ca9..b25dc804b6 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -157,6 +157,7 @@ import {
OAuthWindowEventPayload,
OAuthToken,
} from "../types";
+import { useOAuthStore } from "@/store";
interface LocalState {
name: string;
@@ -200,8 +201,8 @@ export default defineComponent({
const payload = (event as CustomEvent).detail as OAuthWindowEventPayload;
if (isEmpty(payload.error)) {
if (vcs.value.type == "GITLAB_SELF_HOST") {
- store
- .dispatch("oauth/exchangeVCSToken", {
+ useOAuthStore()
+ .exchangeVCSToken({
vcsId: idFromSlug(props.vcsSlug),
code: payload.code,
})