data-api-types.ts45 lines · main
| 1 | import type { DeepReadonly } from './type-helpers' |
| 2 | import type { TablePrivilegesGrant } from '@/data/privileges/table-privileges-grant-mutation' |
| 3 | |
| 4 | export const API_ACCESS_ROLES = ['anon', 'authenticated', 'service_role'] as const |
| 5 | export type ApiAccessRole = (typeof API_ACCESS_ROLES)[number] |
| 6 | |
| 7 | export const isApiAccessRole = (value: string): value is ApiAccessRole => { |
| 8 | return API_ACCESS_ROLES.includes(value as ApiAccessRole) |
| 9 | } |
| 10 | |
| 11 | export type ApiPrivilegeType = Extract< |
| 12 | TablePrivilegesGrant['privilegeType'], |
| 13 | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' |
| 14 | > |
| 15 | export const API_PRIVILEGE_TYPES = [ |
| 16 | 'SELECT', |
| 17 | 'INSERT', |
| 18 | 'UPDATE', |
| 19 | 'DELETE', |
| 20 | ] as const satisfies readonly ApiPrivilegeType[] |
| 21 | |
| 22 | export const isApiPrivilegeType = (value: string): value is ApiPrivilegeType => { |
| 23 | return API_PRIVILEGE_TYPES.includes(value as ApiPrivilegeType) |
| 24 | } |
| 25 | |
| 26 | export type ApiPrivilegesByRole = Record<ApiAccessRole, ApiPrivilegeType[]> |
| 27 | |
| 28 | export const DEFAULT_DATA_API_PRIVILEGES: DeepReadonly<ApiPrivilegesByRole> = { |
| 29 | anon: [...API_PRIVILEGE_TYPES], |
| 30 | authenticated: [...API_PRIVILEGE_TYPES], |
| 31 | service_role: [...API_PRIVILEGE_TYPES], |
| 32 | } |
| 33 | |
| 34 | export const EMPTY_DATA_API_PRIVILEGES: DeepReadonly<ApiPrivilegesByRole> = { |
| 35 | anon: [], |
| 36 | authenticated: [], |
| 37 | service_role: [], |
| 38 | } |
| 39 | |
| 40 | export const checkDataApiPrivilegesNonEmpty = ( |
| 41 | privileges: DeepReadonly<ApiPrivilegesByRole> | undefined |
| 42 | ): boolean => { |
| 43 | if (!privileges) return false |
| 44 | return Object.values(privileges).some((privs) => privs.length > 0) |
| 45 | } |