• Stars
    star
    172
  • Rank 221,201 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 14 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bit.ly library for node.js - This project is looking for a new maintainer

node-bitly - Unofficial Bitly API for nodejs

CircleCI NPM version Dependencies

V6.x.x to V7.x.x transition - aka V3 of Bitly API to V4 - Breaking Changes

In March 2020, Bitly deprecated the v3 of their API, and switched to v4. Unfortunately, even with the changes to this package to make it compatible, there are several unavoidable breaking changes. These are summarized below:

  • Endpoints no longer support bulk options (multiple hashes or URLs in a single request)
    • Most importantly, this affects expand() and shorten()
    • As a general rule of thumb, none of the v4 endpoints take bulk inputs
  • Return types have changed, for multiple endpoints
  • DEPRECATED: The lookup method and corresponding endpoint have been deprecated

With these changes all previous versions of this library are now full deprecated and there is only 1 version starting with v7.0.0

Here is a simple example of how you might have to update your use of node-bitly to account for the change:

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

// v6.1.0
async function example(url) {
  const response = await bitly.shorten(url);
  console.log(`Your shortened bitlink is ${response.url}`);
}
// v7.x.x
async function example(url) {
  const response = await bitly.shorten(url);
  console.log(`Your shortened bitlink is ${response.link}`);
}

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 >= 10.0.0. 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.

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();

When the library throws an error, it should be the error object response from Bitly, but if something has gone wrong with your internet or intermediate requests, it is possible that a generic AxiosError might get returned. You can use an exported Type Guard to narrow the type:

import {BitlyClient, isBitlyErrResponse} from 'bitly';
const bitly = new BitlyClient(process.env.BITLY_API_KEY);
let data: BitlyLink;

try {
  data = await bitly.shorten('http://bit.ly/38XaXKy');
} catch (error) {
  if (isBitlyErrResponse(error)) {
    // Inferred type by TS is `BitlyErrorResponse`
    console.log(`Bitly error: ${error.description}`);
  } else if (error.isAxiosError) {
    // Infererred type is `any`, but you can cast to AxiosError safely
    const axiosError = error as unknown as AxiosError;
    console.log(`AxiosError:`, axiosError.toJSON());
  }
}

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.

Once you need to run tests that can't use a cached response and actually hit Bitly's API, you will need to pass your API key to the tests by having an environment variable BITLY_API_KEY set to the value of your key.

More Repositories

1

SublimeText-Nodejs

nodejs snippets and bindings for Sublime Text 3
Python
1,128
star
2

obsidian-garden

A knowledge management garden for https://obsidian.md, in which to grow your ideas
JavaScript
220
star
3

svelte-formula

Zero-Configuration Reactive forms for Svelte
TypeScript
126
star
4

mm-components

Music Markups components
TypeScript
49
star
5

cloud9-livecoffee-ext

A extension for Cloud9ide for CoffeeScript functionality
CoffeeScript
46
star
6

ngx-tinynodes

Monorepo for components released on NPM @tinynodes
TypeScript
38
star
7

jquery.ui.pwstrength

jQueryUI Password Strength Meter
JavaScript
29
star
8

npm-lint

A linter for npm & node package.json files with a focus on dependency security
TypeScript
26
star
9

dokku-parse-server

JavaScript
22
star
10

steal-ur-stuff

Steal Ur Stuff
21
star
11

sublime-todomanager

A simple ToDo list manager
Python
21
star
12

quorra

Hapi route handler that makes react-router your isomorphic server side router.
JavaScript
15
star
13

robots-webpack-plugin

A webpack plugin to output a robots.txt file with options per environment
JavaScript
14
star
14

web-serial-controller

A Web Serial API application inspired by Windows XP - Built with Svelte and RxJS Ninja
JavaScript
13
star
15

node-gitio

A nodejs module for accessing the git.io URL shortner
JavaScript
13
star
16

hapi-react-handler

A handler for rendering react files via react-router
JavaScript
13
star
17

cloud9-scratchpad

A scratchpad for random text and code you need to work with
CoffeeScript
13
star
18

webfs

Node.js FS API wrapper for the Web File System
JavaScript
12
star
19

nell

An opinionated nodejs clone of Octopress
JavaScript
10
star
20

Stochastic-Parrot

Polly The Stochastic Parrot - a Mastodon Bot that uses empty ChatGPT prompts to generate Toots.
JavaScript
8
star
21

dnode-upload-example

ABANDONED - A quick nodejs example of using Connect, Formidable + Dnode to handle uploads and return response of upload progress
JavaScript
7
star
22

pastemonkey

ABANDONED - An Ajax Pastebin for storing code snippets
PHP
6
star
23

browser-band

A Garageband-like app that plays any sound instruments via the browser midi interface
JavaScript
5
star
24

dnode-mediaplayer

ABANDONED - An attempt at streaming audio data over RPC
HTML
3
star
25

learning-android

ABANDONED - The source project for Learning Android
Java
2
star
26

techmech

PoC for a meta tool for Battletech
CSS
2
star
27

jmaps

ABANDONED - jMaps Google Maps library for jQuery < 1.5
JavaScript
2
star
28

jquery-password-meter

ABANDONED - Old code for a jQuery password meter. May do something with it one day
JavaScript
1
star
29

typedoc-plugin-ghpages-cname

A Typedoc Plugin for adding a CNAME file for Github Docs
TypeScript
1
star
30

node-simplesmtp

ABANDONED - A real simple SMTP client for node.js, handy for debugging
JavaScript
1
star
31

tane.dev

The generated output from hugo for tane.dev
HTML
1
star
32

gml-helpers

A collection of my helpers for Game Maker Studio 2
Game Maker Language
1
star
33

obsidian-gpt-helper

TypeScript
1
star
34

octopress_server

ABANDONED - This is a simple Express + Socket.IO server I am running my static Octopress site behind, to give it little more kick
JavaScript
1
star
35

astro-ssr-express-2

Tentei melhorar o astrojs-ssr-expressjs 1.
Astro
1
star