kotlin-webextensions-declarations icon indicating copy to clipboard operation
kotlin-webextensions-declarations copied to clipboard

webRequest.onAuthRequired.addListener takes multiple arguments

Open ghost opened this issue 7 years ago • 3 comments

It takes filter and extraInfoSpec. Also, the function in the listener should return a webRequest.BlockingResponse or a Promise

The current declarations only allow adding a listener that returns nothing.

ghost avatar Oct 20 '18 12:10 ghost

The schema for onAuthRequired is rather complicated

{
  "name": "onAuthRequired",
  "type": "function",
  "description": "...",
  "parameters": [
    {
      "type": "object",
      "name": "details",
      "properties": {
        ...
      }
    },
    {
      "type": "function",
      "optional": true,
      "name": "callback",
      "parameters": [
        {"name": "response", "$ref": "BlockingResponse"}
      ]
    }
  ],
  "extraParameters": [
    {
      "$ref": "RequestFilter",
      "name": "filter",
      "description": "A set of filters that restricts the events that will be sent to this listener."
    },
    {
      "type": "array",
      "optional": true,
      "name": "extraInfoSpec",
      "description": "Array of extra information that should be passed to the listener function.",
      "items": {
        "$ref": "OnAuthRequiredOptions"
      }
    }
  ],
  "returns": {
    "$ref": "BlockingResponse",
    "description": "If \"blocking\" is specified in the \"extraInfoSpec\" parameter, the event listener should return an object of this type.",
    "optional": true
  }
}

The extraParameters field is currently not parsed at all.

@deepanshu-zendrive How would you imagine the signature of the Kotlin declaration too look like?

cypressious avatar Oct 21 '18 21:10 cypressious

Maybe something like:

onAuthRequired.addListener(callback: (Details) -> BlockingResponse?, 
                           filter: RequestFilter,
                           extraInfoSpec: Array<String>?)

And another similar overload for Promise. The classes for RequestFilter, Details and BlockingResponse are same as what's generated in the current version

ghost avatar Oct 22 '18 17:10 ghost

A dirty fix would be something like

FunSpec.builder("addListener")
    .addModifiers(KModifier.EXTERNAL)
    .addParameter(ParameterSpec.builder("listener", TypeVariableName("T")).build())
    .addParameter(
        ParameterSpec.builder(
            "filter", ClassName.bestGuess("webRequest.RequestFilter")
        ).defaultValue("%L", "definedExternally").build()
    )
    .addParameter(
        ParameterSpec.builder(
            "extraInfoSpec", Array::class.asTypeName().parameterizedBy(String::class.asTypeName())
        ).defaultValue("%L", "definedExternally").build()
    )
    .returns(ClassName.bestGuess("webRequest.BlockingResponse").copy(nullable = true))
    .build()

in Generator.kt

binarynoise avatar Oct 27 '23 16:10 binarynoise