I have one more question about node.js (particular...
# javascript
g
I have one more question about node.js (particularly about common.js modules) and Kotlin. To declare Firebase Function you have to declare module for this function like:
Copy code
module.exports.myFunctionName = functions.https.onRequest(myFunction)
//instead of https.onRequest can be other function builder
So looks like I can just declare top level public property with this firebase function builder:
Copy code
val myFunctionName =  functions.https.onRequest(myFunction)
Generated code looks like:
Copy code
(function (_, Kotlin, $module$firebase_functions) {
  'use strict';
  var println = Kotlin.kotlin.io.println_s8jyv4$;
  var Unit = Kotlin.kotlin.Unit;
  function myFunctionName$lambda() {
    println('Triggered!');
    return Unit;
  }
  var myFunctionName;
  Object.defineProperty(_, 'myFunctionName', {
    get: function () {
      return myFunctionName;
    }
  });
  myFunctionName = $module$firebase_functions.https.onRequest(myFunctionName$lambda);
  Kotlin.defineModule('index', _);
  return _;
}(module.exports, require('kotlin'), require('firebase-functions')));
So, Kotlin defines propery in module.exports, looks like valid way and can be used if you run
module.exports.myFunctionName()
But unfortunately this approach doesn’t work with firebase functions, because defined property is not in modules list. This is reproduction of this behaviour in JS:
Copy code
var myFunctionName = function() { console.log("Triggered") }
Object.defineProperty(module.exports, 'doSomething', {
   get: function () {
     return myFunctionName;
   }
});

console.log(module.exports.doSomething); // [Function: myFunctionName] -- looks like everything is fine
console.log(module.exports) // {} -- but modules are empty
module.exports.doSomethingModule = myFunctionName // Declare another module directly
console.log(module.exports.doSomethingModule); // [Function: myFunctionName] -- same result as for module.exports.doSomething
console.log(module.exports) // { doSomethingModule: [Function: myFunctionName] } -- but now module is visible in list of all modules
So, looks like Firebase get list of registered modules from module.exports. I understand that it’s specific case, but maybe it can be somehow resolved without manual module registration using
external val module: dynamic
k
It's quite easy to implement, but this issue is marked as "design required". I don't know what author of this tag meant. You better ask @bashor
g
Okay, thanks. I’ve got this issue. So we need a way to define
enumerable: true
for such properties. Just curious which solution do you prefer?
k
I prefer soltuion 1
👍 1
n
Isn't porting Typescript to Kotlin code a unsupported use case for Kotlin JS?
g
But it’s not a porting of Typescript, it’s usage of JS library
n
Is the Firebase lib written in Typescript or JS?
g
Typescript that compiled to JS. And can be used by JS without problems and actually by Kotlin as well