import configuration from '@intlayer/config/built';
import type { IntlayerConfig } from '@intlayer/types';
import { type FetcherOptions, fetcher } from '../fetcher';
import type {
CreateUserBody,
CreateUserResult,
GetUserByAccountParams,
GetUserByAccountResult,
GetUserByEmailParams,
GetUserByEmailResult,
GetUserByIdParams,
GetUserByIdResult,
GetUsersParams,
GetUsersResult,
UpdateUserBody,
UpdateUserResult,
UserAPI,
} from '../types';
export const getUserAPI = (
authAPIOptions: FetcherOptions = {},
intlayerConfig?: IntlayerConfig
) => {
const backendURL =
intlayerConfig?.editor?.backendURL ?? configuration?.editor?.backendURL;
if (!backendURL) {
throw new Error(
'Backend URL is not defined in the Intlayer configuration.'
);
}
const USER_API_ROUTE = `${backendURL}/api/user`;
/**
* Retrieves a list of users based on filters and pagination.
* @param filters - Filters and pagination options.
* @returns List of users.
*/
const getUsers = async (
filters?: GetUsersParams,
otherOptions: FetcherOptions = {}
) =>
await fetcher<GetUsersResult>(
USER_API_ROUTE,
authAPIOptions,
otherOptions,
{
cache: 'no-store',
// @ts-ignore Number of parameter will be stringified by the fetcher
params: filters,
}
);
/**
* Retrieves a user by ID.
* @param userId - User ID.
* @returns User object.
*/
const getUserById = async (
userId: GetUserByIdParams['userId'],
otherOptions: FetcherOptions = {}
) =>
await fetcher<GetUserByIdResult>(
`${USER_API_ROUTE}/${userId}`,
authAPIOptions,
otherOptions,
{
cache: 'no-store',
}
);
/**
* Retrieves a user by email.
* @param email - User email.
* @returns User object.
*/
const getUserByEmail = async (
email: GetUserByEmailParams['email'],
otherOptions: FetcherOptions = {}
) =>
await fetcher<GetUserByEmailResult>(
`${USER_API_ROUTE}/email/${email}`,
authAPIOptions,
otherOptions,
{
cache: 'no-store',
}
);
/**
* Retrieves a user by account.
* @param providerAccountId - The provider account ID.
* @param provider - The provider of the account.
*/
const getUserByAccount = async (
providerAccountId: GetUserByAccountParams['providerAccountId'],
provider: GetUserByAccountParams['provider'],
otherOptions: FetcherOptions = {}
) =>
await fetcher<GetUserByAccountResult>(
`${USER_API_ROUTE}/account/${provider}/${providerAccountId}`,
authAPIOptions,
otherOptions,
{
cache: 'no-store',
}
);
/**
* Creates a new user.
* @param user - User credentials.
* @returns User object.
*/
const createUser = async (
user: CreateUserBody,
otherOptions: FetcherOptions = {}
) =>
await fetcher<CreateUserResult>(
`${USER_API_ROUTE}/`,
authAPIOptions,
otherOptions,
{
method: 'POST',
body: user,
}
);
/**
* Updates the user with the provided data.
* @param user - Updated user data.
* @returns User object.
*/
const updateUser = async (
user: UpdateUserBody,
otherOptions: FetcherOptions = {}
) =>
await fetcher<UpdateUserResult>(
`${USER_API_ROUTE}`,
authAPIOptions,
otherOptions,
{
method: 'PUT',
body: user,
}
);
/**
* Deletes a user with the provided ID.
* @param userId - User ID.
* @returns User object.
*/
const deleteUser = async (
userId: string,
otherOptions: FetcherOptions = {}
) =>
await fetcher<UpdateUserResult>(
`${USER_API_ROUTE}/${userId}`,
authAPIOptions,
otherOptions,
{
method: 'DELETE',
}
);
/**
* Gets the verify email status URL to use in the SSE.
* @param userId - User ID.
* @returns The verify email status URL.
*/
const getVerifyEmailStatusURL = (userId: string | UserAPI['id']) =>
`${USER_API_ROUTE}/verify-email-status/${String(userId)}`;
return {
createUser,
getUsers,
getUserById,
getUserByAccount,
getUserByEmail,
updateUser,
deleteUser,
getVerifyEmailStatusURL,
};
};