@availity/authorize
Advanced tools
Comparing version 4.0.3 to 4.1.0
@@ -5,2 +5,11 @@ # Changelog | ||
# [4.1.0](https://github.com/Availity/availity-react/compare/@availity/authorize@4.0.3...@availity/authorize@4.1.0) (2024-05-09) | ||
### Features | ||
* **authorize:** export useauthorize types ([eecb449](https://github.com/Availity/availity-react/commit/eecb4495d67487520c2f0e230a5cab9256d3aa63)) | ||
## [4.0.3](https://github.com/Availity/availity-react/compare/@availity/authorize@4.0.2...@availity/authorize@4.0.3) (2024-04-23) | ||
@@ -7,0 +16,0 @@ |
import { ReactNode } from 'react'; | ||
import { UseQueryOptions } from '@tanstack/react-query'; | ||
type RequestedPermissions = (string | string[])[]; | ||
type RequestedResources = (string | string[])[]; | ||
type QueryOptions = Omit<UseQueryOptions<boolean, unknown, boolean, (string | boolean | RequestedResources | undefined)[]>, 'queryKey' | 'queryFn'>; | ||
type Permissions = { | ||
/** - **string**: The permission ID, eg: '1234' | ||
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's | ||
* eg: ['1234', '2345', ['3456', '4567'], ['5678', '6789']]. The items in a nested array indicate | ||
* Permission ID's that must all be granted to the user to be considered authorized - they act as an "AND". | ||
* The items in the top of the array act as an "OR" - if any are granted to the user, the user is considered | ||
* authorized. For example, ['1234', '2345', ['3456', '4567'], ['5678', '6789']] is equivalent to | ||
* '1234' || '2345' || ('3456' && '4567') || ('5678' && '6789'). | ||
*/ | ||
permissions: (string | string[])[]; | ||
}; | ||
type Parameters = { | ||
/** Additional parameters | ||
* - **`organizationId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the organization. | ||
* - **`customerId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the customer. | ||
* - **`region`**: String or Boolean. Optional. Default: `true`. When a string, the permission is validated to ensure it is assigned in the provided region. When true, the permission is validated to ensure it is assigned in the current region. | ||
* - **`resources`**: (string | string[])[]. Optional. | ||
* - **string**: The Resource ID, eg: `'12345'` | ||
* - **array**: The array can contain Resource ID's as well as other arrays which contain Resource ID's eg: `['12345', '23456', ['34567', '45678'], ['56789', '67890']]`. The items in a nested array indicate the Resource ID's that must _all_ be granted to the user to be considered authorized - they act as an `"AND"`. The items in the top of the array act as an `"OR"` - if _any_ are granted to the user, the user is considered authorized. For example, `['12345', '23456', ['34567', '45678'], ['56789', '67890']]` is equivalent to `'12345' || '23456' || ('34567' && '45678') || ('56789' && '67890')`. | ||
*/ | ||
parameters?: { | ||
/** When present, the permission is validated to ensure it is assigned to the organization. */ | ||
organizationId?: string; | ||
/** When present, the permission is validated to ensure it is assigned to the customer. */ | ||
customerId?: string; | ||
/** When a string, the permission is validated to ensure it is assigned in the provided region. | ||
* When true, the permission is validated to ensure it is assigned in the current region. | ||
*/ | ||
region?: boolean | string; | ||
/** - **string**: The permission ID, eg: `'1234'` | ||
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's | ||
* eg: `['1234', '2345', ['3456', '4567'], ['5678', '6789']]`. The items in a nested array indicate | ||
* Permission ID's that must all be granted to the user to be considered authorized - they act as an `"AND"`. | ||
* The items in the top of the array act as an `"OR"` - if any are granted to the user, the user is considered | ||
* authorized. For example, `['1234', '2345', ['3456', '4567'], ['5678', '6789']]` is equivalent to | ||
* `'1234' || '2345' || ('3456' && '4567') || ('5678' && '6789')`. | ||
*/ | ||
resources?: (string | string[])[]; | ||
}; | ||
}; | ||
type Options = { | ||
/** React Query Options */ | ||
queryOptions?: Omit<UseQueryOptions<boolean, unknown, boolean, (string | boolean | (string | string[])[] | undefined)[]>, 'queryKey' | 'queryFn'>; | ||
}; | ||
type UseAuthorizeProps = Permissions & Parameters & Options; | ||
declare const useAuthorize: (permissions: UseAuthorizeProps['permissions'], parameters?: UseAuthorizeProps['parameters'], options?: UseAuthorizeProps['queryOptions']) => { | ||
authorized: boolean; | ||
isLoading: boolean; | ||
}; | ||
type AuthorizeProps = { | ||
/** Can either be a string, eg: "1234", a number, eg: 1234, or an array, which can contain permission ID strings/numbers as well as other arrays which contain permission ID strings/numbers, eg: ['1234', '2345', ['3456', '4567'], ['5678', '6789']]. The items in a nested array indicate permission IDs that must all be granted to the user to be considered authorized - they act as "AND". The items in the top of the array act as "OR" - if any are granted, the user is considered authorized. The example ['1234', '2345', ['3456', '4567'], ['5678', '6789']] is similar to '1234' OR '2345' OR ('3456' && '4567') OR ('5678' && '6789'). */ | ||
permissions: RequestedPermissions; | ||
/** When present, the permission is validated to ensure it contains the resource(s). Can either be a string, eg: "12345", a number, eg: 12345, or an array, which can contain resource ID strings/numbers as well as other arrays which contain resource ID strings/numbers, eg: ['12345', '23456', ['34567', '45678'], ['56789', '67890']]. The items in a nested array indicate resource IDs that must all be granted to the user to be considered authorized - they act as "AND". The items in the top of the array act as "OR" - if any are granted, the user is considered authorized. The example ['12345', '23456', ['34567', '45678'], ['56789', '67890']] is similar to '12345' OR '23456' OR ('34567' && '45678') OR ('56789' && '67890'). */ | ||
resources?: RequestedResources; | ||
/** When a string, the permission is validated to ensure it is assigned in the provided region. When true, the permission is validated to ensure it is assigned in the current region. Default: true. */ | ||
region?: boolean | string; | ||
/** The content that renders when the user does have the permissions required. */ | ||
children?: ReactNode; | ||
/** When true, BlockUi is used when loading the permissions. When a node, that node is rendered instead of BlockUi when loading the permissions. When false, nothing is rendered when loading the permissions. Default: true. */ | ||
loader?: boolean | ReactNode; | ||
/** When present, the permission is validated to ensure it is assigned to the organization. */ | ||
organizationId?: string; | ||
/** When present, the permission is validated to ensure it is assigned to the customer. */ | ||
customerId?: string; | ||
unauthorized?: ReactNode; | ||
/** The content that renders when the user does have the permissions required. */ | ||
children?: ReactNode; | ||
/** Negate the authorization. If the user does have the permissions specified, they are considered "unauthorized" (shown the unauthorized prop content). If the user does not have the permissions specified, they are considered "authorized" (shown the children prop content) */ | ||
negate?: boolean; | ||
queryOptions?: QueryOptions; | ||
}; | ||
/** The content that renders when the user does not have the permissions required. */ | ||
unauthorized?: ReactNode; | ||
} & Pick<UseAuthorizeProps, 'permissions' | 'queryOptions'> & UseAuthorizeProps['parameters']; | ||
declare const Authorize: ({ permissions, resources, customerId, organizationId, region, loader, negate, children, unauthorized, queryOptions, }: AuthorizeProps) => JSX.Element | null; | ||
type Parameters = { | ||
organizationId?: string; | ||
customerId?: string; | ||
region?: boolean | string; | ||
resources?: RequestedResources; | ||
}; | ||
type Result = { | ||
authorized: boolean; | ||
isLoading: boolean; | ||
}; | ||
declare const useAuthorize: (permissions: RequestedPermissions, parameters?: Parameters, options?: QueryOptions) => Result; | ||
export { AuthorizeProps, Authorize as default, useAuthorize }; | ||
export { AuthorizeProps, UseAuthorizeProps, Authorize as default, useAuthorize }; |
{ | ||
"name": "@availity/authorize", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"description": "Check user permissions to see if the current user is authorized to see your content.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
export { default } from './Authorize'; | ||
export type { AuthorizeProps } from './Authorize'; | ||
export { default as useAuthorize } from './useAuthorize'; | ||
export type { UseAuthorizeProps } from './useAuthorize'; |
@@ -18,8 +18,57 @@ import { UseQueryOptions } from '@tanstack/react-query'; | ||
export type RequestedPermissions = (string | string[])[]; | ||
export type RequestedResources = (string | string[])[]; | ||
// exporting weirdly for storybook args detection | ||
export type QueryOptions = Omit< | ||
UseQueryOptions<boolean, unknown, boolean, (string | boolean | RequestedResources | undefined)[]>, | ||
'queryKey' | 'queryFn' | ||
>; | ||
export type Permissions = { | ||
/** - **string**: The permission ID, eg: '1234' | ||
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's | ||
* eg: ['1234', '2345', ['3456', '4567'], ['5678', '6789']]. The items in a nested array indicate | ||
* Permission ID's that must all be granted to the user to be considered authorized - they act as an "AND". | ||
* The items in the top of the array act as an "OR" - if any are granted to the user, the user is considered | ||
* authorized. For example, ['1234', '2345', ['3456', '4567'], ['5678', '6789']] is equivalent to | ||
* '1234' || '2345' || ('3456' && '4567') || ('5678' && '6789'). | ||
*/ | ||
permissions: (string | string[])[]; | ||
}; | ||
export type Parameters = { | ||
/** Additional parameters | ||
* - **`organizationId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the organization. | ||
* - **`customerId`**: String. Optional. When present, the permission is validated to ensure it is assigned to the customer. | ||
* - **`region`**: String or Boolean. Optional. Default: `true`. When a string, the permission is validated to ensure it is assigned in the provided region. When true, the permission is validated to ensure it is assigned in the current region. | ||
* - **`resources`**: (string | string[])[]. Optional. | ||
* - **string**: The Resource ID, eg: `'12345'` | ||
* - **array**: The array can contain Resource ID's as well as other arrays which contain Resource ID's eg: `['12345', '23456', ['34567', '45678'], ['56789', '67890']]`. The items in a nested array indicate the Resource ID's that must _all_ be granted to the user to be considered authorized - they act as an `"AND"`. The items in the top of the array act as an `"OR"` - if _any_ are granted to the user, the user is considered authorized. For example, `['12345', '23456', ['34567', '45678'], ['56789', '67890']]` is equivalent to `'12345' || '23456' || ('34567' && '45678') || ('56789' && '67890')`. | ||
*/ | ||
parameters?: { | ||
/** When present, the permission is validated to ensure it is assigned to the organization. */ | ||
organizationId?: string; | ||
/** When present, the permission is validated to ensure it is assigned to the customer. */ | ||
customerId?: string; | ||
/** When a string, the permission is validated to ensure it is assigned in the provided region. | ||
* When true, the permission is validated to ensure it is assigned in the current region. | ||
*/ | ||
region?: boolean | string; | ||
/** - **string**: The permission ID, eg: `'1234'` | ||
* - **array**: The array can contain Permission ID's as well as other arrays which contain Permission ID's | ||
* eg: `['1234', '2345', ['3456', '4567'], ['5678', '6789']]`. The items in a nested array indicate | ||
* Permission ID's that must all be granted to the user to be considered authorized - they act as an `"AND"`. | ||
* The items in the top of the array act as an `"OR"` - if any are granted to the user, the user is considered | ||
* authorized. For example, `['1234', '2345', ['3456', '4567'], ['5678', '6789']]` is equivalent to | ||
* `'1234' || '2345' || ('3456' && '4567') || ('5678' && '6789')`. | ||
*/ | ||
resources?: (string | string[])[]; | ||
}; | ||
}; | ||
export type Options = { | ||
/** React Query Options */ | ||
queryOptions?: Omit< | ||
UseQueryOptions<boolean, unknown, boolean, (string | boolean | (string | string[])[] | undefined)[]>, | ||
'queryKey' | 'queryFn' | ||
>; | ||
}; | ||
export type RequestedPermissions = Permissions['permissions']; | ||
export type RequestedResources = Required<Parameters>['parameters']['resources']; | ||
export type QueryOptions = Options['queryOptions']; |
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { checkPermissions, getRegion } from './api'; | ||
import type { QueryOptions, RequestedPermissions, RequestedResources } from './types'; | ||
import { Options, Parameters, Permissions } from './types'; | ||
type Parameters = { | ||
organizationId?: string; | ||
customerId?: string; | ||
region?: boolean | string; | ||
resources?: RequestedResources; | ||
}; | ||
export type UseAuthorizeProps = Permissions & Parameters & Options; | ||
type Result = { authorized: boolean; isLoading: boolean }; | ||
const useAuthorize = ( | ||
permissions: RequestedPermissions, | ||
parameters: Parameters = {}, | ||
options?: QueryOptions | ||
): Result => { | ||
permissions: UseAuthorizeProps['permissions'], | ||
parameters: UseAuthorizeProps['parameters'] = {}, | ||
options?: UseAuthorizeProps['queryOptions'] | ||
): { authorized: boolean; isLoading: boolean } => { | ||
const queryClient = useQueryClient(); | ||
@@ -21,0 +14,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50183
707