UNPKG

@openfga/syntax-transformer

Version:

Javascript implementation of ANTLR Grammar for the OpenFGA DSL and parser from and to the OpenFGA JSON Syntax

76 lines (75 loc) 3.84 kB
import type { AuthorizationModel, TypeDefinition } from "@openfga/sdk"; import { ErrorListener, RecognitionException, Recognizer } from "antlr4"; import OpenFGAListener from "../gen/OpenFGAParserListener"; import { ConditionContext, ConditionExpressionContext, ConditionParameterContext, ModelHeaderContext, ModuleHeaderContext, RelationDeclarationContext, RelationDefDirectAssignmentContext, RelationDefPartialsContext, RelationDefRewriteContext, RelationDefTypeRestrictionContext, TypeDefContext, TypeDefsContext } from "../gen/OpenFGAParser"; import { DSLSyntaxSingleError } from "../errors"; /** * This Visitor walks the tree generated by parsers and produces Python code * * @returns {object} */ declare class OpenFgaDslListener extends OpenFGAListener { authorizationModel: Partial<AuthorizationModel>; typeDefExtensions: Map<string, TypeDefinition>; private currentTypeDef; private currentRelation; private currentCondition; private isModularModel; private moduleName?; private rewriteStack; exitModuleHeader: (ctx: ModuleHeaderContext) => void; exitModelHeader: (ctx: ModelHeaderContext) => void; enterTypeDefs: (_ctx: TypeDefsContext) => void; exitTypeDefs: (_ctx: TypeDefsContext) => void; enterTypeDef: (ctx: TypeDefContext) => void; exitTypeDef: (ctx: TypeDefContext) => void; enterRelationDeclaration: (_ctx: RelationDeclarationContext) => void; exitRelationDeclaration: (ctx: RelationDeclarationContext) => void; enterRelationDefDirectAssignment: (_ctx: RelationDefDirectAssignmentContext) => void; exitRelationDefDirectAssignment: (_ctx: RelationDefDirectAssignmentContext) => void; exitRelationDefTypeRestriction: (ctx: RelationDefTypeRestrictionContext) => void; exitRelationDefRewrite: (ctx: RelationDefRewriteContext) => void; exitRelationRecurse: () => void; enterRelationRecurseNoDirect: () => void; exitRelationRecurseNoDirect: () => void; enterRelationDefPartials: (ctx: RelationDefPartialsContext) => void; enterCondition: (ctx: ConditionContext) => void; exitConditionParameter: (ctx: ConditionParameterContext) => void; exitConditionExpression: (ctx: ConditionExpressionContext) => void; exitCondition: () => void; } declare class OpenFgaDslErrorListener<T> extends ErrorListener<T> { errors: DSLSyntaxSingleError[]; syntaxError(_recognizer: Recognizer<T>, offendingSymbol: T, line: number, // line is one based, i.e. the first line will be 1 column: number, // column is zero based, i.e. the first column will be 0 msg: string, e: RecognitionException | undefined): void; } export declare function parseDSL(data: string): { listener: OpenFgaDslListener; errorListener: OpenFgaDslErrorListener<unknown>; }; /** * transformDSLToJSONObject - Converts models authored in FGA DSL syntax to the json syntax accepted by the OpenFGA API * @param {string} data * @returns {AuthorizationModel} */ export declare function transformDSLToJSONObject(data: string): Omit<AuthorizationModel, "id">; /** * transformDSLToJSONObject - Converts models authored in FGA DSL syntax to a stringified json representation * @param {string} data * @returns {string} */ export declare function transformDSLToJSON(data: string): string; interface ModularDSLTransformResult { authorizationModel: Omit<AuthorizationModel, "id">; typeDefExtensions: Map<string, TypeDefinition>; } /** * transformModularDSLToJSONObject - Converts a part of a modular model in DSL syntax to the json syntax accepted by * OpenFGA API and also returns the type definitions that are extended in the DSL if any are. * @internal * @param {string} data * @returns {ModularDSLTransformResult} */ export declare function transformModularDSLToJSONObject(data: string): ModularDSLTransformResult; export {};