Skip to content

A Trakt.tv API wrapper for JavaScript environments without node core libraries (such as React Native)

Notifications You must be signed in to change notification settings

kdemoya/nodeless-trakt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nodeless-trakt

Trakt.tv API wrapper for JavaScript environments without node core libraries (such as React Native), featuring:

  • All Trakt.tv API v2 methods
  • Promises
  • Forget JSON, use Objects, Arrays and Strings directly
  • Enhanced protection against: CSRF (session riding) and XSS (content spoofing) attacks.

Note: This is loosely based on vankasteelj/trakt.tv node wrapper, we are just stripping the node parts to make it useful in React Native and other nodeless JavaScript environments.

For more information about Trakt.tv API, read http://docs.trakt.apiary.io/

Example usage

Setup

npm install nodeless-trakt

Initialize

const Trakt = require('nodeless-trakt');

const trakt = new Trakt({
  client_id: <the_client_id>,
  client_secret: <the_client_secret>,
  redirect_uri: null,   // defaults to 'urn:ietf:wg:oauth:2.0:oob'
  api_url: null         // defaults to 'https://api.trakt.tv'
});

Generate Auth URL

const traktAuthUrl = trakt.get_url();

Verify code (and optionally state) from returned auth

trakt.exchange_code('code', 'csrf token (state)').then(result => {
    // contains tokens & session information
    // API can now be used with authorized requests
});

Alternate OAUTH "device" method

trakt.get_codes().then(poll => {
    // poll.verification_url: url to visit in a browser
    // poll.user_code: the code the user needs to enter on trakt

    // verify if app was authorized
    return trakt.poll_access(poll);
}).catch(error => {
    // error.message == 'Expired' will be thrown if timeout is reached
});

Refresh token

trakt.refresh_token().then(results => {
    // results are auto-injected in the main module cache
});

Storing token over sessions

// get token, store it safely.
const token = trakt.export_token();

// injects back stored token on new session.
trakt.import_token(token).then(newTokens => {
    // Contains token, refreshed if needed (store it back)
});

Actual API requests

See methods in methods.json or wiki.

trakt.calendars.all.shows({
    start_date: '2015-11-13',
    days: '7',
    extended: 'full'
}).then(shows => {
    // Contains Object{} response from API (show data)
});
trakt.search.text({
    query: 'tron',
    type: 'movie,person'
}).then(response => {
    // Contains Array[] response from API (search data)
});
trakt.search.id({
    id_type: 'imdb',
    id: 'tt0084827'
}).then(response => {
    // Contains Array[] response from API (imdb data)
});

Using pagination

You can extend your calls with pagination: true to get the extra pagination info from headers.

trakt.movies.popular({
    pagination: true
}).then(movies => {
    /**
    movies = Object {
        data: [<actual data from API>],
        pagination: {
            item-count: "80349",
            limit: "10",
            page: "1",
            page-count: "8035"
        }
    }
    **/
});

Note: this will contain data and pagination for all calls, even if no pagination is available. it's typically for really advanced use only

Notes

  • You can use 'me' as username if the user is authenticated.
  • Timestamps (such as token expires property) are Epoch in milliseconds.

LICENSE

The MIT License (MIT) - author: Jean van Kasteel [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A Trakt.tv API wrapper for JavaScript environments without node core libraries (such as React Native)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published