Code Monkey home page Code Monkey logo

node-bitly's Introduction

node-bitly - Unofficial Bitly API for nodejs

CircleCI NPM version Dependencies

Current Versions

  • v6.0.x - Support for Node >=6. Available on npm as npm install bitly@latest
  • v5.1.x - Support for Node >=4. Available on npm as npm install bitly@stable

Version 5 is end-of-life and will only recieve minor updates in the future and is considered stable. This will only ever support the Bitly v3 API

Version 6 is the current in-development version, re-written in Typescript. This version currently only supports the Bitly v3 API and will continue to do so in v6.0.x. Version 6.1.x will introduce Bitly v4 support.

Module Features

This module provides calls to the Bitly API for Nodejs.

For more information on the API request and responses visit the Bitly API docs

node-bitly is programmed with TypeScript but is compiled to JavaScript and supports node 6, 8, 10. When you import the client you get full type information. There maybe be some gaps in the information but this will be filled in, in future releases.

Currently node-bitly only supports Bitly's v3 API and has this hard coded in the parameter type. Support for version 4 will be added in a future release

Installation

To install via NPM type the following: npm install bitly

You can also install via git by cloning: git clone https://github.com/tanepiper/node-bitly.git /path/to/bitly

Usage

This library uses the API provided by bitly and requires an OAuth token to use. To get your access token, visit OAuth Apps (under Generic Access Token)

See http://dev.bitly.com for format of returned objects from the API

To see the available libary APIs, you can view the API Documentation offline, or you can view the index here (the generated documentation does not work on Github).

Code

TypeScript / ES6 Imports

import { BitlyClient } from 'bitly';
const bitly = new BitlyClient('<accessToken>', {});

async function init() {
  let result;
  try {
    result = await bitly.shorten('https://github.com/tanepiper/node-bitly');
  } catch (e) {
    throw e;
  }
  return result;
}

init();

JavaScript

const { BitlyClient } = require('bitly');
const bitly = new BitlyClient('<accessToken>', {});

let result;
try {
  result = await bitly.shorten(uri);
} catch(e) {
  throw e;
}
return result;

If you are not using node 8 then you can still use the library with Promise values:

const BitlyClient = require('bitly').BitlyClient;
const bitly = new BitlyClient('<accessToken>');

bitly
  .shorten('https://github.com/tanepiper/node-bitly')
  .then(function(result) {
    console.log(result);
  })
  .catch(function(error) {
    console.error(error);
  });

You can also do raw requests to any Bitly endpoint. With this you need to pass the access token to the method

const BitlyClient = require('bitly').BitlyClient;
const bitly = new BitlyClient('<accessToken>');

try {
  return await bitly.bitlyRequest('link/referrers_by_domain', {
    link: 'https://github.com/tanepiper/node-bitly',
    unit: 'hour',
    timezone: 'Europe/Amsterdam'
  });
} catch(e) {
  throw e;
}

Tests

To run tests type npm test.

The tests use replay, which caches the responses from Bitly under the /fixtures directory, until you edit a test's requests payload. This means you can run the test suite without having a Bitly API key, until you need to edit or add a new test.

Support This Project

This module is a side project of mine and I don't actively use the module except to completly over-engineer the CI pipeline and re-write it in Typescript all in the name of learning. But to add features like the v4 API would take a lot of work, so if you use this library a lot please consider donating using the links below. Or if you learned something useful from one of my blog posts talking about the changes I've done with this module please consider leaving a tip.

Beerpay Beerpay

You can also PayPal Me.

node-bitly's People

Contributors

coaxial avatar ecwyne avatar francois2metz avatar g-ongenae avatar gayanhewa avatar georgetaveras1231 avatar jessefulton avatar joshuatz avatar sergebuilds avatar shajanjp avatar specious avatar tanepiper avatar zephrax avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.