The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head', 'trace']
export function getKnownFgaPermissions(specs) {
for (const { spec } of specs) {
const permissions =
spec?.components?.schemas?.CreateScopedAccessTokenBody?.properties?.permissions?.items?.enum
if (Array.isArray(permissions)) return new Set(permissions)
}
throw new Error(
'Could not find the list of FGA permissions at components.schemas.CreateScopedAccessTokenBody.properties.permissions.items.enum in any OpenAPI specification.'
)
}
function validateRequirement(value, knownPermissions, location) {
const isValidShape =
Array.isArray(value) &&
value.every(
(group) =>
Array.isArray(group) &&
group.length > 0 &&
group.every((permission) => typeof permission === 'string')
)
if (!isValidShape) {
throw new Error(
`Invalid x-fga-permissions for ${location}: expected an array of non-empty arrays of strings.`
)
}
for (const permission of value.flat()) {
if (!knownPermissions.has(permission)) {
throw new Error(
`Unknown FGA permission "${permission}" in x-fga-permissions for ${location}.`
)
}
}
}
export function buildFgaRequirements(specs, knownPermissions) {
const requirements = {}
const sources = {}
for (const { name, spec } of specs) {
for (const [path, pathItem] of Object.entries(spec?.paths ?? {})) {
for (const method of HTTP_METHODS) {
const operation = pathItem?.[method]
if (!operation || operation['x-fga-permissions'] === undefined) continue
const key = `${method.toUpperCase()} ${path}`
const location = `${key} in the ${name} specification`
validateRequirement(operation['x-fga-permissions'], knownPermissions, location)
// An empty annotation means the endpoint is ungated, so omit it like an unannotated one.
if (operation['x-fga-permissions'].length === 0) continue
if (key in sources) {
throw new Error(
`Duplicate endpoint ${key} with x-fga-permissions in the ${sources[key]} and ${name} specifications.`
)
}
sources[key] = name
requirements[key] = operation['x-fga-permissions'].map((group) => [...group])
}
}
}
return Object.fromEntries(
Object.keys(requirements)
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
.map((key) => [key, requirements[key]])
)
}
export function generateFgaRequirementsSource(parsedSpecs) {
return renderFgaRequirements(
buildFgaRequirements(parsedSpecs, getKnownFgaPermissions(parsedSpecs))
)
}
export function renderFgaRequirements(requirements) {
const entries = Object.entries(requirements)
.map(([key, groups]) => ` ${JSON.stringify(key)}: ${JSON.stringify(groups)},`)
.join('\n')
return `// Generated by \`pnpm --filter=api-types codegen:prod\` from the production OpenAPI specifications. Do not edit by hand.
// Maps each endpoint annotated with \`x-fga-permissions\` to the FGA permissions it requires. A caller
// needs every permission in at least one group (OR between groups, AND within a group). Keys are the
// path literals passed to openapi-fetch.
export const fgaRequirements = {
${entries}
} as const
export type FgaRequirements = typeof fgaRequirements
export type FgaGuardedEndpoint = keyof FgaRequirements
`
}