JSPM

  • Created
  • Published
  • Downloads 19725
  • Score
    100M100P100Q156359F
  • License Apache-2.0

Package Exports

  • @openfga/syntax-transformer
  • @openfga/syntax-transformer/dist/errors
  • @openfga/syntax-transformer/dist/errors.js
  • @openfga/syntax-transformer/dist/gen/OpenFGAParser
  • @openfga/syntax-transformer/dist/gen/OpenFGAParser.js
  • @openfga/syntax-transformer/dist/index.js
  • @openfga/syntax-transformer/dist/transformer/dsltojson
  • @openfga/syntax-transformer/dist/transformer/dsltojson.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@openfga/syntax-transformer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

OpenFGA Language - JS

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

@openfga/syntax-transformer

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]`;

// Transform from DSL model to a JSON object
const generatedJsonObject = transformer.transformDSLToJSONObject(dslString);

// Transform from DSL to a JSON string
const generatedJsonString = transformer.transformDSLToJSONString(dslString);

// Transform from a JSON string to DSL
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`

// Transform from fga.mod to an object
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`
  }
);

// Compile module files into a complete model
const jsonModel = transformer.transformModuleFilesToModel(files, "1.2");

/*
{
  "schema_version": "1.2",
  "type_definitions": [
    {
      "type": "user",
      "metadata": {
        "module": "core",
        "source_info": {
          "file": "core.fga"
        }
      }
    },
    {
      "type": "board",
      "metadata": {
        "module": "core",
        "source_info": {
          "file": "board.fga"
        }
      }
    },
    {
      "type": "wiki",
      "metadata": {
        "module": "core",
        "source_info": {
          "file": "wiki.fga"
        }
      }
    }
  ]
}
*/

Validation

import { errors, validator } from "@openfga/syntax-transformer";

...

let dslString = `model
  schema 1.2
type user
type folder
  relations
    define viewer: [user]`;

// Attempt validation of model
try {
  validator.validateDSL(dslString);
} catch (err) {
  if (err instanceof errors.BaseMultiError) {
    // Handle generated errors
  } else {
    console.error("Unhandled Exception: " + err);
  }
}

License

This project is licensed under the Apache-2.0 license. See the LICENSE file for more info.