scoutforpets / jsonapi-mapper

JSON API-Compliant Serialization for your Node ORM
The Unlicense
42 stars 24 forks source link

update dependencies, primarily new version of `jsonapi-serializer` #19

Closed jamesdixon closed 8 years ago

jamesdixon commented 8 years ago

See https://github.com/SeyZ/jsonapi-serializer/wiki/Migrate-from-2.0-to-3.0

jamesdixon commented 8 years ago

@ShadowManu trying to update dependencies and the signature has changed a bit for the serializer (see link above). I tried updating the Serializer class in jsonapi-serializer.d.ts to

  class Serializer {
    constructor(type: string,
                options: Serializer.ISerializerOptions);

    serialize(data: any);
  }

and then changed the import statement in mapper.ts to import { Serializer } from 'jsonapi-serializer';, but keep getting errors that there isn't a named import. Would appreciate any tips -- want to get these deps updated before they fall too far behind. Thanks!

ShadowManu commented 8 years ago

Well its a thing between how generally people had exported stuff in node (module.exports), and how ES6 imports are. We are used to the fact on node that you can write:

var Library = require('library');
var myresult = Library();

but the problem is that this is not very ES6 friendly: you define exports (and a default export) from your ES6 module, not the module giving an export object that itself can be called. Then you see what I've done in the internal jsonapi-serializer type definitions I set up in our sources (or some other similar variant:

// declare the output module
declare module 'jsonapi-serializer' {

  // declare an internal module
  module Serializer { }

  // declare a class (with the same name)
  class Serializer { }

  // declare that the output module is by all means, what Serializer is. In this case, both a module and a class in itself
  export = Serializer;

  // The 'export =' part is an old typescript construct that was used before TS gave support to ES6 import/export syntax. Not recommended for new usage but existing for backwards-compatibility and for this type of hacks.
}

So yeah, thats the long version of saying that import { Serializer } from 'jsonapi-serializer' won't work, because the Serializer class is not an export of the module but, through a TS hack allowing the node-style export, the module itself. Also the reason that existing code imports the serializer as import * as Serializer from 'jsonapi-serializer'.

ShadowManu commented 8 years ago

As a relevant follow-up, I see they are changing the API to ES6 style. Then our internal type definition (hopefully theirs one day) should be along the lines of:

declare module 'jsonapi-serializer' {
  export class Serializer {
    serialize(...) { ... }
  }

  export class Deserializer {
    deserialize(...) { ... }
  }
}

so then is correctly posible to do the named import you tried first ;)

jamesdixon commented 8 years ago

Closed via d03a72d