@putdotio/api-client

JS client for Put.io API

Usage no npm install needed!

<script type="module">
  import putdotioApiClient from 'https://cdn.skypack.dev/@putdotio/api-client';
</script>

README

@putdotio/api-client

Build Status Coverage Status npm (scoped) npm bundle size (scoped) GitHub

JavaScript library for Put.io API v2.

Installation

yarn add @putdotio/api-client

npm install @putdotio/api-client

ES Modules / TypeScript

import PutioAPI from '@putdotio/api-client'

CommonJS

const PutioAPI = require('@putdotio/api-client').default

Usage

// you can pass the options in constructor
const putioAPI = new PutioAPI({ clientID: 'OAUTH_CLIENT_ID' })

// or use `configure` method
MyApp.bootstrap(config => {
  putioAPI.configure({ clientID: config.OAUTH_CLIENT_ID })
})

// setToken will send the given auth token with every request, in Authorization header
MyApp.onLogin(token => {
  putioAPI.setToken(token)

  putioAPI.Account.Info()
    .then(r => console.log('Fetched user info: ', r))
    .catch(e => console.log('An error occurred while fetching user info: ', e))
})

// clearToken will perform a clean-up and stop sending the token in Authorization header
MyApp.onLogout(() => {
  putioAPI.clearToken()
})

API

Options

Prop Type Default Value Description
clientID number 1 OAuth app client ID, defaults to put.io web app
baseURL string api.put.io/v2 Base URL of the API
webAppURL string app.put.io Base URL of the Put.io web app, used in the authentication flow
generateUUID () => string - UUID generator function to be used for correlationId header, uses uuid/v4 if not given.

Methods

Name Parameters Return Value
configure (options: IPutioAPIClientOptions) PutioAPIClient Instance
setToken (token: string) PutioAPIClient Instance

Events

Value Payload Description
ERROR IPutioAPIClientError Fired when an HTTP request fails
CLIENT_IP_CHANGED { IP: string, newIP: string } Fired when the IP of the client changes