kabiroberai / node-swift

Create Node modules in Swift
MIT License
492 stars 16 forks source link

Issue with NodeValue, documentation maybe ? #20

Open Nytrm opened 6 months ago

Nytrm commented 6 months ago

Hi, great project but the example seems just a little minimal.

I am trying to access the iTunesLibrary from node but i am not sure how to map the values/types to return types. In this example its just a string that does not seem to work for me.

no exact matches in call to initializer return NodeValue(self.library.applicationVersion)

my plan was to map playlist but i got stuck with just the string. https://developer.apple.com/documentation/ituneslibrary/itlibrary/allplaylists


import iTunesLibrary
import NodeAPI

@NodeClass final class iTunesLibraryClass {

    private let library: ITLibrary

    @NodeActor
    @NodeConstructor init() throws {
        library = try ITLibrary(apiVersion: "1.0")
    }

    @NodeActor
    @NodeMethod func allPlaylists() throws -> NodeValueConvertible {
        return NodeValue(self.library.applicationVersion)
    }
}

#NodeModule(exports: ["iTunesLibrary": iTunesLibraryClass.deferredConstructor])`

Any documentation or examples that i can check out how this works ?
kabiroberai commented 5 months ago

Could you try return self.library.applicationVersion? iirc String conforms to NodeValueConvertible. Let me know if that works.

Regarding docs, definitely agree that there's a lot of room for improvement. I'd ideally like to create a DocC catalog but haven't got around to it yet due to a lack of time. It's on the roadmap though.

fpirsch commented 3 weeks ago

The string can be returned from your class simply like this.

    @NodeProperty var version: String { return library.applicationVersion }

I couldn't get the playlists to work from a NodeClass, but you can wrap each ITLibPlaylist item directly from the NodeFunction

import NodeAPI
import iTunesLibrary

@NodeClass final class NodePlaylist {
  var playlist: ITLibPlaylist
  init(playlist: ITLibPlaylist) { self.playlist = playlist }

  // simple types
  @NodeProperty var name: String { playlist.name }
  // some type mapping might be necessary (NSNumber? -> Double?)
  @NodeProperty var parendID: Double? { playlist.parentID == nil ? nil : Double(exactly: playlist.parentID!) }
  // ... map other properties
}

#NodeModule(exports: [
     "allPlaylists": try NodeFunction { () in 
        let library = try ITLibrary(apiVersion: "1.0")
        return library.allPlaylists.map { NodePlaylist(playlist: $0) }
    }
])