Shared: AOuth: fix axios response data

This commit is contained in:
Alexey Safronov 2024-07-12 11:37:27 +04:00
parent f1e6f79704
commit 9dd2820abc
3 changed files with 121 additions and 67 deletions

View File

@ -36,8 +36,9 @@ export const initSSR = (headers: Record<string, string>) => {
export const request = (
options: TReqOption & AxiosRequestConfig,
skipRedirect = false,
isOAuth = false,
) => {
return client.request(options, skipRedirect);
return client.request(options, skipRedirect, isOAuth);
};
export const setWithCredentialsStatus = (state: boolean) => {

View File

@ -14,10 +14,14 @@ import {
} from "../../utils/oauth/types";
export const getClient = async (clientId: string): Promise<IClientProps> => {
const client = (await request({
const client = (await request(
{
method: "get",
url: `/clients/${clientId}`,
})) as IClientResDTO;
},
false,
true,
)) as IClientResDTO;
return transformToClientProps(client);
};
@ -26,10 +30,14 @@ export const getClientList = async (
page: number,
limit: number,
): Promise<IClientListProps> => {
const data = (await request({
const data = (await request(
{
method: "get",
url: `/clients?page=${page}&limit=${limit}`,
})) as IClientListDTO;
},
false,
true,
)) as IClientListDTO;
const clients: IClientListProps = { ...data, data: [] };
@ -45,66 +53,94 @@ export const getClientList = async (
export const addClient = async (data: IClientReqDTO): Promise<IClientProps> => {
data.logout_redirect_uri = data.website_url;
const client = (await request({
const client = (await request(
{
method: "post",
url: `/clients`,
data,
})) as IClientResDTO;
},
false,
true,
)) as IClientResDTO;
return transformToClientProps(client);
};
export const updateClient = async (clientId: string, data: IClientReqDTO) => {
await request({
await request(
{
method: "put",
url: `/clients/${clientId}`,
data,
});
},
false,
true,
);
};
export const changeClientStatus = async (
clientId: string,
status: boolean,
): Promise<void> => {
await request({
await request(
{
method: "patch",
url: `/clients/${clientId}/activation`,
data: { status },
});
},
false,
true,
);
};
export const regenerateSecret = async (
clientId: string,
): Promise<{ client_secret: string }> => {
const clientSecret = (await request({
const clientSecret = (await request(
{
method: "patch",
url: `/clients/${clientId}/regenerate`,
})) as { client_secret: string };
},
false,
true,
)) as { client_secret: string };
return clientSecret;
};
export const deleteClient = async (clientId: string): Promise<void> => {
await request({
await request(
{
method: "delete",
url: `/clients/${clientId}`,
});
},
false,
true,
);
};
export const getScope = async (name: string): Promise<TScope> => {
const scope = (await request({
const scope = (await request(
{
method: "get",
url: `/scopes/${name}`,
})) as TScope;
},
false,
true,
)) as TScope;
return scope;
};
export const getScopeList = async (): Promise<TScope[]> => {
const scopeList = (await request({
const scopeList = (await request(
{
method: "get",
url: `/scopes`,
})) as TScope[];
},
false,
true,
)) as TScope[];
return scopeList;
};
@ -113,10 +149,14 @@ export const getConsentList = async (
page: number = 0,
limit: number = 100,
): Promise<TConsentList & { consents: IClientProps[] }> => {
const consentList = (await request({
const consentList = (await request(
{
method: "get",
url: `/clients/consents?page=${page}&limit=${limit}`,
})) as TConsentList;
},
false,
true,
)) as TConsentList;
const consents: IClientProps[] = [];
@ -138,10 +178,14 @@ export const getConsentList = async (
};
export const revokeUserClient = async (clientId: string): Promise<void> => {
await request({
await request(
{
method: "delete",
url: `/clients/${clientId}/revoke`,
});
},
false,
true,
);
};
export const onOAuthSubmit = (
@ -158,7 +202,8 @@ export const onOAuthSubmit = (
formData.append("scope", s);
});
return request({
return request(
{
method: "post",
url: `/oauth2/authorize`,
data: formData,
@ -166,7 +211,10 @@ export const onOAuthSubmit = (
headers: {
"X-Disable-Redirect": "true",
},
});
},
false,
true,
);
};
export const onOAuthCancel = (clientId: string, clientState: string) => {
@ -175,7 +223,8 @@ export const onOAuthCancel = (clientId: string, clientState: string) => {
formData.append("client_id", clientId);
formData.append("state", clientState);
return request({
return request(
{
method: "post",
url: `/oauth2/authorize`,
data: formData,
@ -183,5 +232,8 @@ export const onOAuthCancel = (clientId: string, clientState: string) => {
headers: {
"X-Disable-Redirect": "true",
},
});
},
false,
true,
);
};

View File

@ -185,6 +185,7 @@ class AxiosClient {
request = (
options: TReqOption & AxiosRequestConfig,
skipRedirect = false,
isOAuth = false,
) => {
const onSuccess = (response: TRes) => {
const error = this.getResponseError(response);
@ -216,9 +217,9 @@ class AxiosClient {
if (options.baseURL === "/apisystem" && !response.data.response)
return response.data;
return typeof response.data.response !== "undefined"
? response.data.response
: response.data;
if (isOAuth && !response.data.response) return response.data;
return response.data.response;
};
const onError = (error: TError) => {