Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fetch APIs in userstore.ts to SWR hooks #7840

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { userConfig } from "@wso2is/admin.extensions.v1/configs/user";
import {
ServerConfigurationsConstants
} from "@wso2is/admin.server-configurations.v1/constants/server-configurations-constants";
import { useUserStore } from "@wso2is/admin.userstores.v1/api/user-stores";
import { useGetUserStore } from "@wso2is/admin.userstores.v1/api/use-get-user-store";
import { USERSTORE_REGEX_PROPERTIES } from "@wso2is/admin.userstores.v1/constants";
import { UserStoreProperty } from "@wso2is/admin.userstores.v1/models";
import { ValidationDataInterface, ValidationFormInterface } from "@wso2is/admin.validation.v1/models";
Expand Down Expand Up @@ -150,7 +150,7 @@ export const AddUserUpdated: React.FunctionComponent<AddUserProps> = (
// Hook to get the userstore details of the selected userstore.
const {
data: originalUserStore
} = useUserStore(
} = useGetUserStore(
userStore
);

Expand Down
60 changes: 60 additions & 0 deletions features/admin.userstores.v1/api/use-get-user-store-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import useRequest, {
RequestConfigInterface,
RequestErrorInterface,
RequestResultInterface
} from "@wso2is/admin.core.v1/hooks/use-request";
import { store } from "@wso2is/admin.core.v1/store";
import { HttpMethods } from "@wso2is/core/models";
import { QueryParams, UserStoreAttributes } from "../models/user-stores";

export const useGetUserStoreAttributes = <Data = UserStoreAttributes, Error = RequestErrorInterface>(
id: string,
params: QueryParams,
shouldFetch: boolean = true
): RequestResultInterface<Data, Error> => {
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
},
method: HttpMethods.GET,
params,
url: `${store.getState().config.endpoints.userStores}/meta/types/${id}/attributes`
};

const {
data,
error,
isValidating,
mutate,
response
} = useRequest<Data, Error>(shouldFetch ? requestConfig: null);

return {
data,
error,
isLoading: !error && !data,
isValidating,
mutate,
response
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import useRequest, {
RequestConfigInterface,
RequestErrorInterface,
RequestResultInterface
} from "@wso2is/admin.core.v1/hooks/use-request";
import { store } from "@wso2is/admin.core.v1/store";
import { HttpMethods } from "@wso2is/core/models";
import { QueryParams, UserstoreType } from "../models/user-stores";

/**
* Retrieves meta data of a single userstore type.
*
* @param id - The ID of the userstore type.
* @param params - Optional query params such as limit, offset, filter, sort, attributes, etc.
* @param shouldFetch - Whether the request should be made.
*
* @returns A RequestResultInterface containing data, error, isLoading, etc.
*/
export const useGetUserStoreMetaDataType = <
Data = UserstoreType,
Error = RequestErrorInterface
>(
id: string,
params?: QueryParams,
shouldFetch: boolean = true
): RequestResultInterface<Data, Error> => {

// Build the request configuration.
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
},
method: HttpMethods.GET,
params,
url: `${store.getState().config.endpoints.userStores}/meta/types/${ id }`
};

// Pass the config to `useRequest`, or null if we don't want to fetch yet.
const {
data,
error,
isValidating,
mutate,
response
} = useRequest<Data, Error>(shouldFetch ? requestConfig : null);

return {
data,
error,
isLoading: !error && !data,
isValidating,
mutate,
response
};
};
58 changes: 58 additions & 0 deletions features/admin.userstores.v1/api/use-get-user-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import useRequest, {
RequestConfigInterface,
RequestErrorInterface,
RequestResultInterface
} from "@wso2is/admin.core.v1/hooks/use-request";
import { store } from "@wso2is/admin.core.v1/store";
import { HttpMethods } from "@wso2is/core/models";
import { UserStore } from "../models/user-stores";

export const useGetUserStore = <Data = UserStore, Error = RequestErrorInterface>(
id: string,
shouldFetch: boolean = true
): RequestResultInterface<Data, Error> => {
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
},
method: HttpMethods.GET,
url: `${store.getState().config.endpoints.userStores}/${id}`
};

const {
data,
error,
isValidating,
mutate,
response
} = useRequest<Data, Error>(shouldFetch ? requestConfig: null);

return {
data,
error,
isLoading: !error && !data,
isValidating,
mutate,
response
};
};
108 changes: 3 additions & 105 deletions features/admin.userstores.v1/api/user-stores.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2023-2024, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2023-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -17,10 +17,8 @@
*/

import { AsgardeoSPAClient, HttpClientInstance } from "@asgardeo/auth-react";
import useRequest, {
RequestConfigInterface,
RequestErrorInterface,
RequestResultInterface
import {
RequestConfigInterface
} from "@wso2is/admin.core.v1/hooks/use-request";
import { store } from "@wso2is/admin.core.v1/store";
import { UserstoreConstants } from "@wso2is/core/constants";
Expand All @@ -32,8 +30,6 @@ import {
PatchData,
QueryParams,
TestConnection,
UserStore,
UserStoreAttributes,
UserStorePostData
} from "../models";

Expand Down Expand Up @@ -86,70 +82,6 @@ export const getUserStoreList = (): Promise<UserstoreListResponseInterface[] | a
});
};

/**
* Hook to get details of a userstore with a given ID
*/
export const useUserStore = <Data = UserStore, Error = RequestErrorInterface>(
id: string,
shouldFetch: boolean = true
): RequestResultInterface<Data, Error> => {
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
},
method: HttpMethods.GET,
url: `${store.getState().config.endpoints.userStores}/${id}`
};

const {
data,
error,
isValidating,
mutate,
response
} = useRequest<Data, Error>(shouldFetch ? requestConfig: null);

return {
data,
error,
isLoading: !error && !data,
isValidating,
mutate,
response
};
};

/**
* Fetch types of userstores.
*
* @returns userstore types.
*/
export const getUserstoreTypes = (): Promise<any> => {
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json"
},
method: HttpMethods.GET,
url: `${store.getState().config.endpoints.userStores}/meta/types`
};

return httpClient(requestConfig)
.then((response: AxiosResponse) => {
if (response.status !== 200) {
return Promise.reject(`An error occurred. The server returned ${response.status}`);
}

return Promise.resolve(response.data);
})
.catch((error: AxiosError) => {
return Promise.reject(error?.response?.data);
});
};

/**
* Gets the meta data of a type.
*
Expand Down Expand Up @@ -375,40 +307,6 @@ export const testConnection = (data: TestConnection): Promise<any> => {
});
};

/**
* Gets the user store attributes.
*
* @param id - Type ID.
* @param params - limit, offset, filter, sort, attributes.
*
* @returns userstore attributes.
*/
export const getUserStoreAttributes = (id: string, params: QueryParams): Promise<UserStoreAttributes> => {
const requestConfig: RequestConfigInterface = {
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": store.getState().config.deployment.clientHost,
"Content-Type": "application/json",
params
},
method: HttpMethods.GET,
params,
url: `${store.getState().config.endpoints.userStores}/meta/types/${id}/attributes`
};

return httpClient(requestConfig)
.then((response: AxiosResponse) => {
if (response.status !== 200) {
return Promise.reject(`An error occurred. The server returned ${response.status}`);
}

return Promise.resolve(response.data);
})
.catch((error: AxiosError) => {
return Promise.reject(error?.response?.data);
});
};

/**
* Update the secondary user store attribute mappings by it's domain id.
*
Expand Down
Loading
Loading