diff --git a/packages/shared/api/client.ts b/packages/shared/api/client.ts index b7dcbf36d4..a0745847fb 100644 --- a/packages/shared/api/client.ts +++ b/packages/shared/api/client.ts @@ -36,8 +36,9 @@ export const initSSR = (headers: Record) => { 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) => { diff --git a/packages/shared/api/oauth/index.ts b/packages/shared/api/oauth/index.ts index d26d066218..784cfc54c1 100644 --- a/packages/shared/api/oauth/index.ts +++ b/packages/shared/api/oauth/index.ts @@ -14,10 +14,14 @@ import { } from "../../utils/oauth/types"; export const getClient = async (clientId: string): Promise => { - const client = (await request({ - method: "get", - url: `/clients/${clientId}`, - })) as IClientResDTO; + const client = (await request( + { + method: "get", + url: `/clients/${clientId}`, + }, + false, + true, + )) as IClientResDTO; return transformToClientProps(client); }; @@ -26,10 +30,14 @@ export const getClientList = async ( page: number, limit: number, ): Promise => { - const data = (await request({ - method: "get", - url: `/clients?page=${page}&limit=${limit}`, - })) as IClientListDTO; + const data = (await request( + { + method: "get", + url: `/clients?page=${page}&limit=${limit}`, + }, + false, + true, + )) as IClientListDTO; const clients: IClientListProps = { ...data, data: [] }; @@ -45,66 +53,94 @@ export const getClientList = async ( export const addClient = async (data: IClientReqDTO): Promise => { data.logout_redirect_uri = data.website_url; - const client = (await request({ - method: "post", - url: `/clients`, - data, - })) as IClientResDTO; + const client = (await request( + { + method: "post", + url: `/clients`, + data, + }, + false, + true, + )) as IClientResDTO; return transformToClientProps(client); }; export const updateClient = async (clientId: string, data: IClientReqDTO) => { - await request({ - method: "put", - url: `/clients/${clientId}`, - data, - }); + await request( + { + method: "put", + url: `/clients/${clientId}`, + data, + }, + false, + true, + ); }; export const changeClientStatus = async ( clientId: string, status: boolean, ): Promise => { - await request({ - method: "patch", - url: `/clients/${clientId}/activation`, - data: { status }, - }); + 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({ - method: "patch", - url: `/clients/${clientId}/regenerate`, - })) as { client_secret: string }; + const clientSecret = (await request( + { + method: "patch", + url: `/clients/${clientId}/regenerate`, + }, + false, + true, + )) as { client_secret: string }; return clientSecret; }; export const deleteClient = async (clientId: string): Promise => { - await request({ - method: "delete", - url: `/clients/${clientId}`, - }); + await request( + { + method: "delete", + url: `/clients/${clientId}`, + }, + false, + true, + ); }; export const getScope = async (name: string): Promise => { - const scope = (await request({ - method: "get", - url: `/scopes/${name}`, - })) as TScope; + const scope = (await request( + { + method: "get", + url: `/scopes/${name}`, + }, + false, + true, + )) as TScope; return scope; }; export const getScopeList = async (): Promise => { - const scopeList = (await request({ - method: "get", - url: `/scopes`, - })) as TScope[]; + const scopeList = (await request( + { + method: "get", + url: `/scopes`, + }, + false, + true, + )) as TScope[]; return scopeList; }; @@ -113,10 +149,14 @@ export const getConsentList = async ( page: number = 0, limit: number = 100, ): Promise => { - const consentList = (await request({ - method: "get", - url: `/clients/consents?page=${page}&limit=${limit}`, - })) as TConsentList; + const consentList = (await request( + { + method: "get", + url: `/clients/consents?page=${page}&limit=${limit}`, + }, + false, + true, + )) as TConsentList; const consents: IClientProps[] = []; @@ -138,10 +178,14 @@ export const getConsentList = async ( }; export const revokeUserClient = async (clientId: string): Promise => { - await request({ - method: "delete", - url: `/clients/${clientId}/revoke`, - }); + await request( + { + method: "delete", + url: `/clients/${clientId}/revoke`, + }, + false, + true, + ); }; export const onOAuthSubmit = ( @@ -158,15 +202,19 @@ export const onOAuthSubmit = ( formData.append("scope", s); }); - return request({ - method: "post", - url: `/oauth2/authorize`, - data: formData, - withRedirect: true, - headers: { - "X-Disable-Redirect": "true", + return request( + { + method: "post", + url: `/oauth2/authorize`, + data: formData, + withRedirect: true, + headers: { + "X-Disable-Redirect": "true", + }, }, - }); + false, + true, + ); }; export const onOAuthCancel = (clientId: string, clientState: string) => { @@ -175,13 +223,17 @@ export const onOAuthCancel = (clientId: string, clientState: string) => { formData.append("client_id", clientId); formData.append("state", clientState); - return request({ - method: "post", - url: `/oauth2/authorize`, - data: formData, - withRedirect: true, - headers: { - "X-Disable-Redirect": "true", + return request( + { + method: "post", + url: `/oauth2/authorize`, + data: formData, + withRedirect: true, + headers: { + "X-Disable-Redirect": "true", + }, }, - }); + false, + true, + ); }; diff --git a/packages/shared/utils/axiosClient.ts b/packages/shared/utils/axiosClient.ts index 91390cd47c..c64fa9295b 100644 --- a/packages/shared/utils/axiosClient.ts +++ b/packages/shared/utils/axiosClient.ts @@ -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) => {