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

Installation
npm install @openfga/syntax-transformer
Usage
Transformer
Example transform DSL model to JSON, and from JSON to DSL.
import { transformer } from "@openfga/syntax-transformer";
let dslString = `model
schema 1.1
type user
type folder
relations
define viewer: [user]`;
const generatedJsonObject = transformer.transformDSLToJSONObject(dslString);
const generatedJsonString = transformer.transformDSLToJSONString(dslString);
const generatedDsl = transformer.transformJSONStringToDSL(generatedJsonString);
Transform Mod File to JSON
import { transformer } from "@openfga/syntax-transformer";
...
const modFileContents = `schema: "1.2"
contents:
- core.fga
- board.fga
- wiki.fga`
const jsonModFile = transformer.TransformModFile(modFileContents)
Transform set of Modules To Model
import { transformer } from "@openfga/syntax-transformer";
...
const files: transformer.ModuleFile[] = [];
files.push({
name: "core.fga",
contents: `module core
type user`
},
{
name: "board.fga",
contents: `module core
type board`
},
{
name: "wiki.fga",
contents: `module core
type wiki`
}
);
const jsonModel = transformer.transformModuleFilesToModel(files, "1.2");
Validation
import { errors, validator } from "@openfga/syntax-transformer";
...
let dslString = `model
schema 1.2
type user
type folder
relations
define viewer: [user]`;
try {
validator.validateDSL(dslString);
} catch (err) {
if (err instanceof errors.BaseMultiError) {
} else {
console.error("Unhandled Exception: " + err);
}
}
License
This project is licensed under the Apache-2.0 license. See the LICENSE file for more info.