@cerbos/react
Version:
A collection of React hooks for interacting with Cerbos policy decision points
155 lines • 3.97 kB
TypeScript
import type { CheckResourceRequest, CheckResourcesRequest, CheckResourcesResponse, CheckResourcesResult, IsAllowedRequest, RequestOptions } from "@cerbos/core";
/**
* The result of calling an async method on a client.
*
* @public
*/
export type AsyncResult<T> = {
isLoading: true;
data: undefined;
error: undefined;
} | {
isLoading: false;
data: T;
error: undefined;
} | {
isLoading: false;
data: undefined;
error: Error;
};
/**
* Check the principal's permissions on a resource.
*
* @example
* ```typescript
* import { useCheckResource } from "@cerbos/react";
*
* function SomeComponent() {
* const check = useCheckResource({
* resource: {
* kind: "document",
* id: "1",
* attr: { owner: "user@example.com" },
* },
* actions: ["view", "edit"],
* });
*
* if (check.isLoading) {
* // show spinner
* return "Loading...";
* }
*
* if (check.error) {
* // handle error
* return "Error...";
* }
*
* return (
* <div>
* {check.data.allAllowed() && <button>a button</button>}
* {check.data.isAllowed("view") && <button>another button</button>}
* </div>
* );
* }
* ```
* @public
*/
export declare function useCheckResource(request: Omit<CheckResourceRequest, "principal">, options?: Omit<RequestOptions, "signal">): AsyncResult<CheckResourcesResult>;
/**
* Check the principal's permissions on a set of resources.
*
* @example
* ```typescript
* import { useCheckResources } from "@cerbos/react";
*
* function SomeComponent() {
* const check = useCheckResources({
* resources: [
* {
* resource: {
* kind: "document",
* id: "1",
* attr: { owner: "user@example.com" },
* },
* actions: ["view", "edit"],
* },
* {
* resource: {
* kind: "document",
* id: "2",
* attr: { owner: "another-user@example.com" },
* },
* actions: ["view", "edit"],
* },
* ],
* });
*
* if (check.isLoading) {
* // show spinner
* return "Loading...";
* }
*
* if (check.error) {
* // handle error
* return "Error...";
* }
*
* return (
* <div>
* {check.data.allAllowed({
* kind: "document",
* id: "1",
* }) && <button>a button document 1</button>}
* {check.data.allAllowed({
* kind: "document",
* id: "2",
* }) && <button>a button document 2</button>}
* {check.data.isAllowed({
* resource: { kind: "document", id: "1" },
* action: "edit",
* }) && <button>another button for document 1</button>}
* {check.data.isAllowed({
* resource: { kind: "document", id: "2" },
* action: "edit",
* }) && <button>another button for document 2</button>}
* </div>
* );
* }
* ```
* @public
*/
export declare function useCheckResources(request: Omit<CheckResourcesRequest, "principal">, options?: Omit<RequestOptions, "signal">): AsyncResult<CheckResourcesResponse>;
/**
* Check if the principal is allowed to perform an action on a resource.
*
* @example
* ```typescript
* import { useIsAllowed } from "@cerbos/react";
*
* function SomeComponent() {
* const check = useIsAllowed({
* resource: {
* kind: "document",
* id: "1",
* attr: { owner: "user@example.com" },
* },
* action: "view",
* });
*
* if (check.isLoading) {
* // show spinner
* return "Loading...";
* }
*
* if (check.error) {
* // handle error
* return "Error...";
* }
*
* return <div>{check.data && <button>a button document 1</button>}</div>;
* }
* ```
* @public
*/
export declare function useIsAllowed(request: Omit<IsAllowedRequest, "principal">, options?: Omit<RequestOptions, "signal">): AsyncResult<boolean>;
//# sourceMappingURL=use-cerbos-request.d.ts.map