0

I'm new to TypeScript, and would like to create a d.ts definition file for a JavaScript library, moviedb-js, which can retreive movie information from TheMovieDb. I've looked for an existing definition at the Definitely Typed repository, and read Fenton's guide on creating a definition file, among others, however, I'm stuck with translating the following JavaScript bit:

var MovieDB = require('moviedb')('your api key');

When creating a module, I have something along the lines of

declare module 'moviedb' { ... }

And in my TypeScript code, I import it with:

import moviedb = require('moviedb')

Clearly, without an API key, this doesn't work, and adding the key as in the JavaScript example didn't work either. So how can I rewrite the module definition in such a way that I can define the API key that is needed?

Erik Vullings
  • 5,550
  • 2
  • 28
  • 23

2 Answers2

1

@basarat: I've checked your response, and it was working well. Thanks again!

As a side note, I found this post also really helpful in understanding the construction that was used to add the API key.

So what do I have:

MovieScanner.ts

export class TmbdMovieScanner {
    movieDb: MovieDB.IMovieDB;

    constructor() {
        this.movieDb = moviedb('YOUR API KEY');

        // For testing purposes
        this.movieDb.searchMovie({ query: "Aliens" }, (err, movies) => {
            if (err) {
                console.log('Error: ' + err);
                return;
            }
            console.log(movies);            
        });
        this.movieDb.movieInfo({ id: 666 }, (err, curMovie) => {
            if (err) {
                console.log('Error: ' + err);
                return;
            }
            console.log(curMovie.overview);
        });
    }
}

moviedb.d.ts (still incomplete):

declare module 'moviedb' {
    function apiKeyAcceptor(key: string): MovieDB.IMovieDB;
    export = apiKeyAcceptor;
}

declare module MovieDB {
    export interface IMovieDB {
        searchMovie(params: SearchOptions, callback: (err: any, movies: SearchResults) => void): void;
        movieInfo(options: InfoOptions, callback: (err: any, curMovie: Movie) => void): void;
    }

    export interface SearchOptions {
        query: string;
    }

    export interface InfoOptions {
        id: number;
    }

    export interface SearchResults {
        page         : number;
        results      : SearchResult[];
        total_Pages  : number;
        total_results: number;
    }

    export interface SearchResult {
        adult         : boolean;
        backdrop_path : string;
        id            : number;
        original_title: string;
        release_date  : Date;
        poster_path   : string;
        popularity    : number;
        title         : string;
        vote_average  : number;
        vote_count    : number;
    }

    export interface Movie {
        adult                : boolean;
        backdrop_path        : string;
        belongs_to_collection: any;
        budget               : number;
        genres               : Genre[];
        homepage             : string;
        id                   : number;
        imdb_id              : number;
        original_title       : string;
        overview             : string;
        popularity           : number;
        poster_path          : string;
        production_companies : ProductionCompany[];
        production_countries : ProductionCountry[];
        release_date         : Date;
        revenue              : number;
        runtime              : number;
        spoken_languages     : SpokenLanguage[];
        status               : string;
        tagline              : string;
        title                : string;
        vote_average         : number;
        vote_count           : number;
    }

    export interface Genre {
        id  : number;
        name: string;
    }

    export interface ProductionCompany {
        id  : number;
        name: string;
    }

    export interface ProductionCountry {
        iso_3166_1: number;
        name      : string;
    }

    export interface SpokenLanguage {
        iso_639_1: number;
        name     : string;
    }
}
Community
  • 1
  • 1
Erik Vullings
  • 5,550
  • 2
  • 28
  • 23
0

Here is the concept moviedb.d.ts

declare module MovieDB{
     export interface IMovieDB{
         searchMovie(params:any,callback:Function):IMovieDB;
         // Add other methods here
     }
}
declare module 'moviedb' { 
    function apiKetAcceptor(key:string):MovieDB.IMovieDB;
    export = apiKeyAcceptor;
}

To use it you reference + import:

/// <reference path='../path/to/moviedb.d.ts'/>
import moviedb = require('moviedb');
var MovieDB = moviedb('your api key');
MovieDB.searchMovie({query: 'Alien' }, function(err, res){
  console.log(res);
});

For a detailed example look at Node.js definitions https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node.d.ts

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thanks for the update, but I'm not sure I completely get it (and I don't have my code with me now to test it). Basically, I would need to replace apiKeyAcceptor with the appropriate function in moviedb.js (https://github.com/danzajdband/moviedb/blob/master/lib/moviedb.js). Is that so? Or maybe you would be so kind, and have a look at this link, and complete the example, i.e. which function do I need to add, and how would I call it? – Erik Vullings Jun 20 '14 at 10:15
  • @ErikVullings alright, I've updated the answer with *untested* code. Let me know if something is *off* – basarat Jun 20 '14 at 10:34
  • Thanks for your update! I'll test it later, but it is looking good, so I've already accepted the answer. If something is wrong, I will tell you, of course. – Erik Vullings Jun 20 '14 at 12:31