data-api-types.ts 1.4 KB

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