data-api-types.ts45 lines · main
1import type { DeepReadonly } from './type-helpers'
2import type { TablePrivilegesGrant } from '@/data/privileges/table-privileges-grant-mutation'
3
4export const API_ACCESS_ROLES = ['anon', 'authenticated', 'service_role'] as const
5export type ApiAccessRole = (typeof API_ACCESS_ROLES)[number]
6
7export const isApiAccessRole = (value: string): value is ApiAccessRole => {
8 return API_ACCESS_ROLES.includes(value as ApiAccessRole)
9}
10
11export type ApiPrivilegeType = Extract<
12 TablePrivilegesGrant['privilegeType'],
13 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE'
14>
15export const API_PRIVILEGE_TYPES = [
16 'SELECT',
17 'INSERT',
18 'UPDATE',
19 'DELETE',
20] as const satisfies readonly ApiPrivilegeType[]
21
22export const isApiPrivilegeType = (value: string): value is ApiPrivilegeType => {
23 return API_PRIVILEGE_TYPES.includes(value as ApiPrivilegeType)
24}
25
26export type ApiPrivilegesByRole = Record<ApiAccessRole, ApiPrivilegeType[]>
27
28export 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
34export const EMPTY_DATA_API_PRIVILEGES: DeepReadonly<ApiPrivilegesByRole> = {
35 anon: [],
36 authenticated: [],
37 service_role: [],
38}
39
40export 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}