The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: entitlements (#39338)
Ignacio Dobronichd57279dde600
diff --git a/apps/studio/data/entitlements/entitlements-query.ts b/apps/studio/data/entitlements/entitlements-query.tsnew file mode 100644index 00000000000..cf922714eaf--- /dev/null+++ b/apps/studio/data/entitlements/entitlements-query.ts@@ -0,0 +1,38 @@+import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'+import { get, handleError } from 'data/fetchers'+import { ResponseError } from 'types/base'+import type { components } from 'api-types'++export type EntitlementsVariables = {+ slug: string+}++export type EntitlementConfig =+ components['schemas']['ListEntitlementsResponse']['entitlements'][0]['config']+export type Entitlement = components['schemas']['ListEntitlementsResponse']['entitlements'][0]++export async function getEntitlements({ slug }: EntitlementsVariables, signal?: AbortSignal) {+ if (!slug) throw new Error('slug is required')++ const { data, error } = await get('/platform/organizations/{slug}/entitlements', {+ params: { path: { slug } },+ signal,+ })+ if (error) handleError(error)++ return data+}++export type EntitlementsData = Awaited<ReturnType<typeof getEntitlements>>+export type EntitlementsError = ResponseError++export const useEntitlementsQuery = <TData = EntitlementsData>(+ { slug }: EntitlementsVariables,+ { enabled = true, ...options }: UseQueryOptions<EntitlementsData, EntitlementsError, TData> = {}+) => {+ return useQuery<EntitlementsData, EntitlementsError, TData>(+ ['entitlements', slug],+ ({ signal }) => getEntitlements({ slug }, signal),+ { enabled: enabled && typeof slug !== 'undefined', ...options, staleTime: 1 * 60 * 1000 }+ )+}diff --git a/apps/studio/hooks/misc/useCheckEntitlements.ts b/apps/studio/hooks/misc/useCheckEntitlements.tsnew file mode 100644index 00000000000..bf55549b277--- /dev/null+++ b/apps/studio/hooks/misc/useCheckEntitlements.ts@@ -0,0 +1,55 @@+import { useEntitlementsQuery } from 'data/entitlements/entitlements-query'+import { useMemo } from 'react'+import { useSelectedOrganizationQuery } from './useSelectedOrganization'+import type { EntitlementConfig } from 'data/entitlements/entitlements-query'++export function useCheckEntitlements(+ featureKey: string,+ organizationSlug?: string,+ options?: {+ enabled?: boolean+ }+) {+ // If no organizationSlug provided, try to get it from the selected organization+ const shouldGetSelectedOrg = !organizationSlug && options?.enabled !== false+ const {+ data: selectedOrg,+ isLoading: isLoadingSelectedOrg,+ isSuccess: isSuccessSelectedOrg,+ } = useSelectedOrganizationQuery({+ enabled: shouldGetSelectedOrg,+ })++ const finalOrgSlug = organizationSlug || selectedOrg?.slug+ const enabled = options?.enabled !== false && !!finalOrgSlug++ const {+ data: entitlementsData,+ isLoading: isLoadingEntitlements,+ isSuccess: isSuccessEntitlements,+ } = useEntitlementsQuery({ slug: finalOrgSlug! }, { enabled })++ const { hasAccess, entitlementConfig } = useMemo((): {+ hasAccess: boolean+ entitlementConfig: EntitlementConfig+ } => {+ // If no organization slug, no access+ if (!finalOrgSlug) return { hasAccess: false, entitlementConfig: { enabled: false } }++ const entitlement = entitlementsData?.entitlements.find(+ (entitlement) => entitlement.feature.key === featureKey+ )+ const entitlementConfig = entitlement?.config ?? { enabled: false }++ if (!entitlement) return { hasAccess: false, entitlementConfig: { enabled: false } }++ return { hasAccess: entitlement.hasAccess, entitlementConfig }+ }, [entitlementsData, featureKey, finalOrgSlug])++ const isLoading = shouldGetSelectedOrg ? isLoadingSelectedOrg : isLoadingEntitlements+ const isSuccess = shouldGetSelectedOrg+ ? isSuccessSelectedOrg && isSuccessEntitlements+ : isSuccessEntitlements++ return { hasAccess, entitlementConfig, isLoading, isSuccess }+}diff --git a/packages/api-types/types/platform.d.ts b/packages/api-types/types/platform.d.tsindex fdfe2f25611..15dde790e7c 100644--- a/packages/api-types/types/platform.d.ts+++ b/packages/api-types/types/platform.d.ts@@ -1251,6 +1251,46 @@ export interface paths {patch?: nevertrace?: never}+ '/platform/organizations/{slug}/entitlements': {+ parameters: {+ query?: never+ header?: never+ path?: never+ cookie?: never+ }+ /**+ * Get entitlements for an organization+ * @description Returns the entitlements available to the organization based on their plan and any overrides.+ */+ get: operations['OrganizationEntitlementsController_getEntitlements']+ put?: never+ post?: never+ delete?: never+ options?: never+ head?: never+ patch?: never+ trace?: never+ }+ '/platform/organizations/{slug}/entitlements/entitlements': {+ parameters: {+ query?: never+ header?: never+ path?: never+ cookie?: never+ }+ /**+ * Get entitlements for an organization+ * @description Returns the entitlements available to the organization based on their plan and any overrides.+ */+ get: operations['OrganizationEntitlementsController_getEntitlements']+ put?: never+ post?: never+ delete?: never+ options?: never+ head?: never+ patch?: never+ trace?: never+ }'/platform/organizations/{slug}/members': {parameters: {query?: never@@ -5731,6 +5771,11 @@ export interface components {billing_name?: stringbilling_via_partner: booleanemail: string+ tax_id: {+ country: string+ type: string+ value: string+ } | null}DatabaseDetailResponse: {/** @enum {string} */@@ -6942,6 +6987,29 @@ export interface components {LinkClazarBuyerBody: {buyer_id: string}+ ListEntitlementsResponse: {+ entitlements: {+ config:+ | {+ enabled: boolean+ }+ | {+ enabled: boolean+ unlimited: boolean+ value: number+ }+ | {+ enabled: boolean+ set: string[]+ }+ feature: {+ key: string+ /** @enum {string} */+ type: 'boolean' | 'numeric' | 'set'+ }+ hasAccess: boolean+ }[]+ }ListGitHubConnectionsResponse: {connections: {branch_limit: number@@ -14178,6 +14246,92 @@ export interface operations {}}}+ OrganizationEntitlementsController_getEntitlements: {+ parameters: {+ query?: never+ header?: never+ path: {+ /** @description Organization slug */+ slug: string+ }+ cookie?: never+ }+ requestBody?: never+ responses: {+ 200: {+ headers: {+ [name: string]: unknown+ }+ content: {+ 'application/json': components['schemas']['ListEntitlementsResponse']+ }+ }+ /** @description Unauthorized */+ 401: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Forbidden action */+ 403: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Rate limit exceeded */+ 429: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ }+ }+ OrganizationEntitlementsController_getEntitlements: {+ parameters: {+ query?: never+ header?: never+ path: {+ /** @description Organization slug */+ slug: string+ }+ cookie?: never+ }+ requestBody?: never+ responses: {+ 200: {+ headers: {+ [name: string]: unknown+ }+ content: {+ 'application/json': components['schemas']['ListEntitlementsResponse']+ }+ }+ /** @description Unauthorized */+ 401: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Forbidden action */+ 403: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ /** @description Rate limit exceeded */+ 429: {+ headers: {+ [name: string]: unknown+ }+ content?: never+ }+ }+ }MembersController_getMembers: {parameters: {query?: never