mogular / powerquery-formatter-examples

9 stars 1 forks source link

About powerquery-formatter API #10

Closed DavisZHANG-BlogOnly closed 4 years ago

DavisZHANG-BlogOnly commented 4 years ago

Description Can I ignore the 'iconfig' value to keep all settings default?

type RequestBody = { code: string, config: IConfig (note: remove this? ) }

I try the API using python like below but it returns 400 error (info: "kind": "INVALID_REQUEST_BODY")

` string = 'let Source = Table.Combine({#"TABLE 1", #"TABLE 2", #"TABLE 3", #"TABLE 4", #"TABLE 5", #"TABLE 6", #"TABLE 7"}),#"Added Custom" = Table.AddColumn(Source, "Link", each "text-functions") in #"Added Custom"'

IConfig = { 'indentation?': "  ", 'indentationLength?': 2, 'lineEnd?': "
", 'ws?': " ", 'lineWidth?': 100, 'numBrackets?': 3, 'escapeHtmlText?': 'true', 'alignPairedExpressionsByEqual?': 'false', 'inlineCss?': 'null', 'includeComments?': 'true' }

equestBody = { 'code': string, 'config': IConfig } ` Anything wrong with that?

(I think ~ It would be great if you provide a python version example for the API)

UliPlabst commented 4 years ago

Hi, sorry for the late response, I was on holidays until now.
I can see several issues with your code.

IConfig = {
'lineEnd': "  
"  #this is not valid python as normal string literals may not have newlines. You can either use the escape sequence "\n" or a multi line string literal with leading and trailing """,
'inlineCss': "null"  # what you want to do is set the value to null which is equivalent to None in python. "null" means an actual string that contains "null" which is not what you want to have.
'includeComments': 'true' # this is the same as 'null'. What you want to have is a boolean value of true which is equivalent to True (no quotations) in python. 'true' is an actual string literal which is not what you want. The same is wrong with properties you set to 'false' (use False)
}

Also the api type declarations are typescript type declarations (I didn't make that clear, I'm going to fix the documentation about that). The ? after a propertyName means just that it is optional, hence you can just leave it out and it will take a default value. It does not mean that the ? must be used in the actual property name.
Further on the config object is already optional (it will take default values if not supplied). So if you just write

requestBody = {
'code': string
}

that will work (I tested it).
What you will get back is the formatted html. If you want just text use

requestBody = {
'code': string,
'resultType': 'text'
}

If you still want to tweak the configuration you can tweak this python code

exampleConfig = {
  indentation: "  ",
  indentationLength: 2,
  lineEnd: "<br/>",
  ws: "&nbsp;",
  lineWidth: 100,
  numBrackets: 3,
  escapeHtmlText: True,
  alignPairedExpressionsByEqual: False,
  includeComments: True,
  inlineCss: """
.constant.keyword {
color: #c586c0;
}
.constant {
color: #d4d4d4;
}
.constant.unknown-node {
  color: red;
}
.identifier {
color: #9cdcfe;
}
.method-call {
  color:#795E26;
}
.operator {
color: #d4d4d4;
}
.bracket {
font-weight: bold;
}
.bracket-0 {
color: Gold;
}
.bracket-1 {
color: GoldenRod;
}
.bracket-2 {
color: DarkGoldenRod;
}
.type {
color: #4ec9b0;
}
.literal.null {
color: #569cd6;
}
.literal.string {
color: #ce9178;
}
.literal {
color: #dcdcaa;
}
body {
font-family: monospace;
background-color: #1e1e1e;
}
"""
}
DavisZHANG-BlogOnly commented 4 years ago

@UliPlabst Thanks! It works for me now.

UliPlabst commented 4 years ago

@DavisZHANG-BlogOnly You're welcome.