Meteor EJSON support constructors

58 views Asked by At

I would like to send constructors via EJSON through methods:

server.js

Meteor.methods({
  'testConstructor' (doc) {
    console.log(doc) // {}
  }
})

client.js

Meteor.call({ type: String })

I thought of adding the types via EJSON.addType but it currently only supports instances and not constructors. I tried to wrap the String constructor inside a class like this:

Meteor.startup(function () {
  class StrWrap {
    constructor (val) {
      this.val = val
    }

    toJSONValue () {
      return {
        value: this.val
      }
    }

    typeName () {
      return String.name
    }
  }

  EJSON.addType(String, function (json) {
    return String
  })

  const str = EJSON.stringify({ type: String})
  console.log(str) // {}
})

Still no chance. In a related issue it has been mentioned, that the EJSON supports String, Number etc. but I think that was targeting the instances of these classes.

I currently work this around using native JSON replacer for JSON.stringify and resolver for JSON.parse but this adds a full conversion layer to every DDP protocol interaction and I'd like to support constructors out of the box, so I can send schemas around for service discovery.

0

There are 0 answers