Web:Client:Store: rewrite OAuth store to typescipt

This commit is contained in:
Timofey Boyko 2023-09-25 17:03:57 +03:00
parent dc1274fb88
commit 74e94c0a10
2 changed files with 107 additions and 106 deletions

View File

@ -1,106 +0,0 @@
import { makeAutoObservable, runInAction } from "mobx";
import {
addClient,
deleteClient,
getClient,
getClientList,
regenerateSecret,
updateClient,
} from "@docspace/common/api/oauth";
const clients = [...Array(5)].map((value, index) => {
return {
enabled: true,
id: index,
name: `7383bc80-b0ad-4fad-8850-${index}`,
description: "Application description",
client_id: "46192a5a-e19c-453c-aec3-50617290edbe",
client_secret: "e5ff57e4-4ce2-4dac-a265-88bc7e726684",
root_url: "https://google.com",
redirect_uris: ["https://openidconnect.net/callback"],
allowed_origins: [
"https://google.com",
"https://openidconnect.net/callback",
],
scopes: ["accounts:write", "accounts:read"],
logo_uri: "https://logo2.example",
policy_uri: "https://policy2.example",
terms_uri: "https://terms2.example",
state: "draft",
};
});
const scopes = [
"files:read",
"files:write",
"rooms:read",
"rooms:write",
"account.self:read",
"account.self:write",
"accounts:read",
"accounts:write",
];
class OAuthStore {
clients = [];
currentClient = clients[0];
constructor() {
makeAutoObservable(this);
}
fetchClient = async (clientId) => {
await getClient(clientId);
};
fetchClients = async (page, limit) => {
this.clients = clients;
await getClientList(0, 20);
};
saveClient = async (client) => {
await addClient(client);
};
updateClient = async (clientId, client) => {
await updateClient(clientId, client);
};
regenerateSecret = async (clientId) => {
await regenerateSecret(clientId);
};
deleteClient = async (clientId) => {
await deleteClient(clientId);
};
setCurrentClient = (client) => {
this.currentClient = client;
};
addClient = (client) => {
this.clients = [...this.clients, client];
};
setEnabled = (id) => {
const index = this.clients.findIndex((proj) => proj.id === id);
this.clients[index].enabled = !this.clients[index].enabled;
};
// deleteClient = (id) => {
// this.clients = this.clients.filter((proj) => proj.id !== id);
// };
editClient = (client) => {
const index = this.clients.findIndex((proj) => proj.id === client.id);
runInAction(() => {
this.clients[index] = client;
});
};
}
export default OAuthStore;

View File

@ -0,0 +1,107 @@
import { makeAutoObservable } from "mobx";
import {
addClient,
deleteClient,
getClient,
getClientList,
getScope,
getScopeList,
regenerateSecret,
updateClient,
} from "@docspace/common/api/oauth";
import {
ClientDTO,
ClientListDTO,
ClientListProps,
ClientProps,
ScopeDTO,
} from "@docspace/common/utils/oauth/dto";
export interface OAuthStoreProps {
clients: ClientProps[];
currentClient: null | ClientProps;
scopes: ScopeDTO[];
fetchClient: (clientId: string) => Promise<ClientProps>;
fetchClients: (page: number, limit: number) => Promise<void>;
saveClient: (client: ClientProps) => Promise<ClientProps>;
updateClient: (clientId: string, client: ClientProps) => Promise<ClientProps>;
regenerateSecret: (clientId: string) => Promise<string>;
deleteClient: (clientId: string) => Promise<void>;
fetchScope: (name: string) => Promise<ScopeDTO>;
fetchScopes: () => Promise<void>;
clientList: ClientProps[];
scopeList: ScopeDTO[];
}
class OAuthStore implements OAuthStoreProps {
clients: ClientProps[] = [];
currentClient: ClientProps | null = null;
scopes: ScopeDTO[] = [];
constructor() {
makeAutoObservable(this);
}
fetchClient = async (clientId: string) => {
const client = await getClient(clientId);
return client;
};
fetchClients = async (page: number, limit: number) => {
const clientList: ClientListProps = await getClientList(0, 20);
this.clients = clientList.content;
};
saveClient = async (client: ClientProps) => {
const newClient = await addClient(client);
return newClient;
};
updateClient = async (clientId: string, client: ClientProps) => {
const newClient = await updateClient(clientId, client);
return newClient;
};
regenerateSecret = async (clientId: string) => {
const secret = await regenerateSecret(clientId);
return secret;
};
deleteClient = async (clientId: string) => {
await deleteClient(clientId);
};
fetchScope = async (name: string) => {
const scope = await getScope(name);
return scope;
};
fetchScopes = async () => {
const scopes = await getScopeList();
this.scopes = scopes;
};
get clientList() {
return this.clients;
}
get scopeList() {
return this.scopes;
}
}
export default OAuthStore;