roles.ts•7.25 kB
/**
* Generated by orval v7.2.0 🍺
* Do not edit manually.
* storyden
* Storyden social API for building community driven platforms.
The Storyden API does not adhere to semantic versioning but instead applies a rolling strategy with deprecations and minimal breaking changes. This has been done mainly for a simpler development process and it may be changed to a more fixed versioning strategy in the future. Ultimately, the primary way Storyden tracks versions is dates, there are no set release tags currently.
* OpenAPI spec version: v1.25.8-canary
*/
import useSwr from "swr";
import type { Arguments, Key, SWRConfiguration } from "swr";
import useSWRMutation from "swr/mutation";
import type { SWRMutationConfiguration } from "swr/mutation";
import { fetcher } from "../client";
import type {
InternalServerErrorResponse,
NotFoundResponse,
RoleCreateBody,
RoleCreateOKResponse,
RoleGetOKResponse,
RoleListOKResponse,
RoleUpdateBody,
UnauthorisedResponse,
} from "../openapi-schema";
/**
* Creates a role with the specified permissions granted.
*/
export const roleCreate = (roleCreateBody: RoleCreateBody) => {
return fetcher<RoleCreateOKResponse>({
url: `/roles`,
method: "POST",
headers: { "Content-Type": "application/json" },
data: roleCreateBody,
});
};
export const getRoleCreateMutationFetcher = () => {
return (
_: Key,
{ arg }: { arg: RoleCreateBody },
): Promise<RoleCreateOKResponse> => {
return roleCreate(arg);
};
};
export const getRoleCreateMutationKey = () => [`/roles`] as const;
export type RoleCreateMutationResult = NonNullable<
Awaited<ReturnType<typeof roleCreate>>
>;
export type RoleCreateMutationError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse;
export const useRoleCreate = <
TError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse,
>(options?: {
swr?: SWRMutationConfiguration<
Awaited<ReturnType<typeof roleCreate>>,
TError,
Key,
RoleCreateBody,
Awaited<ReturnType<typeof roleCreate>>
> & { swrKey?: string };
}) => {
const { swr: swrOptions } = options ?? {};
const swrKey = swrOptions?.swrKey ?? getRoleCreateMutationKey();
const swrFn = getRoleCreateMutationFetcher();
const query = useSWRMutation(swrKey, swrFn, swrOptions);
return {
swrKey,
...query,
};
};
/**
* List all roles and their permissions.
*/
export const roleList = () => {
return fetcher<RoleListOKResponse>({ url: `/roles`, method: "GET" });
};
export const getRoleListKey = () => [`/roles`] as const;
export type RoleListQueryResult = NonNullable<
Awaited<ReturnType<typeof roleList>>
>;
export type RoleListQueryError = InternalServerErrorResponse;
export const useRoleList = <TError = InternalServerErrorResponse>(options?: {
swr?: SWRConfiguration<Awaited<ReturnType<typeof roleList>>, TError> & {
swrKey?: Key;
enabled?: boolean;
};
}) => {
const { swr: swrOptions } = options ?? {};
const isEnabled = swrOptions?.enabled !== false;
const swrKey =
swrOptions?.swrKey ?? (() => (isEnabled ? getRoleListKey() : null));
const swrFn = () => roleList();
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
swrOptions,
);
return {
swrKey,
...query,
};
};
/**
* Retreives a role and all its permissions.
*/
export const roleGet = (roleId: string) => {
return fetcher<RoleGetOKResponse>({ url: `/roles/${roleId}`, method: "GET" });
};
export const getRoleGetKey = (roleId: string) => [`/roles/${roleId}`] as const;
export type RoleGetQueryResult = NonNullable<
Awaited<ReturnType<typeof roleGet>>
>;
export type RoleGetQueryError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse;
export const useRoleGet = <
TError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse,
>(
roleId: string,
options?: {
swr?: SWRConfiguration<Awaited<ReturnType<typeof roleGet>>, TError> & {
swrKey?: Key;
enabled?: boolean;
};
},
) => {
const { swr: swrOptions } = options ?? {};
const isEnabled = swrOptions?.enabled !== false && !!roleId;
const swrKey =
swrOptions?.swrKey ?? (() => (isEnabled ? getRoleGetKey(roleId) : null));
const swrFn = () => roleGet(roleId);
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(
swrKey,
swrFn,
swrOptions,
);
return {
swrKey,
...query,
};
};
/**
* Updates a role's attributes.
*/
export const roleUpdate = (roleId: string, roleUpdateBody: RoleUpdateBody) => {
return fetcher<RoleGetOKResponse>({
url: `/roles/${roleId}`,
method: "PATCH",
headers: { "Content-Type": "application/json" },
data: roleUpdateBody,
});
};
export const getRoleUpdateMutationFetcher = (roleId: string) => {
return (
_: Key,
{ arg }: { arg: RoleUpdateBody },
): Promise<RoleGetOKResponse> => {
return roleUpdate(roleId, arg);
};
};
export const getRoleUpdateMutationKey = (roleId: string) =>
[`/roles/${roleId}`] as const;
export type RoleUpdateMutationResult = NonNullable<
Awaited<ReturnType<typeof roleUpdate>>
>;
export type RoleUpdateMutationError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse;
export const useRoleUpdate = <
TError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse,
>(
roleId: string,
options?: {
swr?: SWRMutationConfiguration<
Awaited<ReturnType<typeof roleUpdate>>,
TError,
Key,
RoleUpdateBody,
Awaited<ReturnType<typeof roleUpdate>>
> & { swrKey?: string };
},
) => {
const { swr: swrOptions } = options ?? {};
const swrKey = swrOptions?.swrKey ?? getRoleUpdateMutationKey(roleId);
const swrFn = getRoleUpdateMutationFetcher(roleId);
const query = useSWRMutation(swrKey, swrFn, swrOptions);
return {
swrKey,
...query,
};
};
/**
* Deletes a role.
*/
export const roleDelete = (roleId: string) => {
return fetcher<void>({ url: `/roles/${roleId}`, method: "DELETE" });
};
export const getRoleDeleteMutationFetcher = (roleId: string) => {
return (_: Key, __: { arg: Arguments }): Promise<void> => {
return roleDelete(roleId);
};
};
export const getRoleDeleteMutationKey = (roleId: string) =>
[`/roles/${roleId}`] as const;
export type RoleDeleteMutationResult = NonNullable<
Awaited<ReturnType<typeof roleDelete>>
>;
export type RoleDeleteMutationError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse;
export const useRoleDelete = <
TError =
| UnauthorisedResponse
| NotFoundResponse
| InternalServerErrorResponse,
>(
roleId: string,
options?: {
swr?: SWRMutationConfiguration<
Awaited<ReturnType<typeof roleDelete>>,
TError,
Key,
Arguments,
Awaited<ReturnType<typeof roleDelete>>
> & { swrKey?: string };
},
) => {
const { swr: swrOptions } = options ?? {};
const swrKey = swrOptions?.swrKey ?? getRoleDeleteMutationKey(roleId);
const swrFn = getRoleDeleteMutationFetcher(roleId);
const query = useSWRMutation(swrKey, swrFn, swrOptions);
return {
swrKey,
...query,
};
};