Api Documentation

The formatting service is hosted as an azure function. To use the api you need to send a POST request to https://m-formatter.azurewebsites.net/api/format/v1 with content type application/json and the following body structure:
type RequestBody =
{
     code: string,
     config: IConfig
}
A usage example is provided in our example repository.
The config object is defined by
export interface IConfig
{
    //String that is used for indentation, default = "  "
    indentation?: string;
    //Length of the indentation string (is used for determining when the max line width is reached)
    indentationLength?: number;
    //String that is used to terminate a line, default = "<br/>"
    lineEnd?: string;
    //String that is used as a whitespace, default = "&nbsp;"
    ws?: string;
    //Maximum line width, default = 100. The formatter forces expression to break into several lines if the maximum line width would be reached. Please not some expressions cannot be broken into multiple lines such as very long identifiers and therefore the maximum line width can be exceeded in these cases. As of now there is no warning to give you info about that.
    lineWidth?: number;
    //Number of different bracket classes, default = 3. The formatter assigns bracket pairs the classes 'bracket' and 'bracket-{num}' where num = 1...{numBrackets}. You can then style your matching brackets with configurable depth.
    numBrackets?: number;
    //All text (code) is escaped into html format, default = true.
    escapeHtmlText?: boolean;
    //Align paired expressions by their equal sign, default = false.
    alignPairedExpressionsByEqual?: boolean;
    //If null the formatter will put classes onto the resulting html but not styles (can be used for websites where you want to style the formatted html with css yourself). If non null the classes will be translated to inline styles on the html elements. See below for more information about the used classes.
    inlineCss?: string;
    //Toggle include comments
    includeComments?: boolean;
}
The css classes assigned by the formatter that can be used to customize the style either via the inlineCss config option or via css on your webpage are
  • comment
  • constant
    • keyword
    • identifier
      • method-call
    • operator
    • bracket
    • bracket-{num}
    • type
  • literal
    • string
    • list
    • boolean
    • number
    • null
    • record
    • string

Response

You will receive a response with Status 200 on success or Status 400 if an error occurred. In both cases the response body is a json object of type
type ResponseBody = {
  result: string, //HTML format result
  errors: FormatError[]
}
              
type FormatError = {
  kind: FormatErrorKind,
  innerError?: Error,   //the inner exception if there was one
  meta?: any,            //additional meta information,
  message: string
}
              
export type FormatErrorKind = 
  //an unknown error occurred. If this happens look at the innerError and possibly report the bug
  "UNKNOWN"             
  //parsing was unsuccessfull. As we use the official ms parser the problem is most likely within your code
  | "PARSER_ERROR"      
  //formatting was unsuccessfull. This is a strong indication of a bug, consider writing a bug report if this happens
  | "FORMATTER_ERROR"   
  //the custom css that you provided is invalid
  | "CSS_ERROR"         
  //you provided an invalid config object
  | "INVALID_CONFIG"    
  //the formatter encountered an unknown node kind in the AST. Please note that section documents are currently not supported.
  //If this happens for code without sections then we are missing a node kind implementation. There are probabably some that we are missing but since our test bench does not include any of them it is hard for me to track them down. 
  //If you provide your query (or at least the portion of it that causes the error) it would help me out.
  | "UNKNOWN_ENTITY";  

Terms of use

You are free to use the api as long as you give credit and (if you use the formatter for your website) link back to this site.