首页
/ node-bitly 技术文档

node-bitly 技术文档

2024-12-20 19:55:27作者:吴年前Myrtle

1. 安装指南

NPM安装

通过NPM安装 node-bitly,输入以下命令:

npm install bitly

Git克隆

您也可以通过Git克隆的方式进行安装:

git clone https://github.com/tanepiper/node-bitly.git /path/to/bitly

2. 项目使用说明

node-bitly 是一个为Node.js提供的非官方Bitly API模块。使用此库需要OAuth令牌。获取访问令牌,请访问Bitly的OAuth Apps页面。

查看API返回对象的格式,请访问 Bitly开发者文档

TypeScript / ES6 使用示例

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;

如果您使用的不是Node.js 8版本,您仍然可以使用 Promise 来处理异步操作:

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

原始请求

您可以向任何Bitly端点发起原始请求,需要传递访问令牌到方法中:

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;
}

3. 项目API使用文档

node-bitly 模块提供的API文档可以在项目的GitHub Wiki上查看,或者您可以在本地查看生成的文档。

4. 项目安装方式

请参考上述的安装指南部分,您可以采用NPM安装或Git克隆的方式来安装项目。


本文档基于 node-bitly 的readme文件和GitHub项目wiki整理而成,旨在帮助用户更好地了解和使用该项目。

登录后查看全文
热门项目推荐