npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shlink-client

v1.0.1

Published

Shlink JavaScript API client

Downloads

110

Readme

npm version npm bundle size npm downloads npm licence GitHub Repo stars

shlink-client

Interact with your Shlink.io API more easily. This package covers the endpoints provided by Shlink and types every input and output between your app and your API.

Installation

$ npm i shlink-client

# Or with yarn

$ yarn add shlink-client

API

Instanciate a new Shlink Client:

import { ShlinkClient } from 'shlink-client';

const client = new ShlinkClient({
  url: 'https://yourdomain.tld',
  token: 'xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
});

Main entites declarations

ShortUrl:

interface ShortUrl {
  shortCode: string;
  shortUrl: string;
  longUrl: string;
  dateCreated: string;
  visitsCount: number;
  tags: string[];
  meta: {
    validSince: string | null;
    validUntil: string | null;
    maxVisits: number;
  };
  domain: string | null;
}

client.shortenUrl(baseUrl, apiKey, longUrl, [options])

Creates a short URL in a single API call. Useful for third party integrations. This is the only static method for short URLs.

Parameters

baseUrl: URL of your Shlink API.

apiKey: Your Shlink API Key.

longUrl: URL to shorten

options: Specify the type of the payload you wish to get back:

interface ShortenUrlOptions {
  format: 'json' | 'txt';
}

Response

When no options are provided, the default payload is a ShortUrl object. If txt is chosen, the full shortened URL is returned.

client#getShortUrls([options])

Returns the list of short URLs.

Options

All fields are optional.

interface ShortUrlGetOptions {
  // The page to be displayed. Defaults to 1
  page?: number
  // A query used to filter results by searching for it on the longUrl and shortCode fields
  searchTerm?: string
  // A list of tags used to filter the result set. Only short URLs tagged with at least one of the provided tags will be returned
  tags?: string[]
  // The field from which you want to order the result
  orderBy?:  'longUrl-ASC' | 'longUrl-DESC' | 'shortCode-ASC' | 'shortCode-DESC' | 'dateCreated-ASC' | 'dateCreated-DESC' | 'visits-ASC' | 'visits-DESC'
  // The date (in ISO-8601 format) from which we want to get short URLs
  startDate?: string
  // The date (in ISO-8601 format) until which we want to get short URLs
  endDate?: string
}

Response

interface {
  data: ShortUrl[]
  pagination: {
    currentPage: number
    pagesCount: number
    itemsPerPage: number
    itemsInCurrentPage: number
    totalItems: number
  }
}

client#getShortUrl(shortCode)

Get the long URL behind a short URL's short code.

Parameters

shortCode: The short code to edit.

Response

Returns a single ShortUrl object if found.

client#editShortUrl(shortCode, [options, [domain]])

Update certain meta arguments from an existing short URL.

Parameters

shortCode: The short code to edit.

options: ShortUrl options. At least one field is required.

interface ShortUrlPatchOptions {
  longUrl?: string;
  validSince?: string;
  validUntil?: string;
  maxVisits?: number;
  validateUrl: boolean;
}

domain: The domain in which the short code should be searched for.

Response

Returns the updated ShortUrl object.

client#createShortUrl(options)

Creates a new short URL.

Parameters

Shares most of the specificities of the edit method, except the longUrl property is required.

interface ShortUrlOptions {
  // URL to shorten
  longUrl: string;
  // Array of tags to attach to this short URL
  tags?: string[];
  // The date (in ISO-8601 format) from which the short URL is valid
  validSince?: string;
  // The date (in ISO-8601 format) from which the URL is no longer valid
  validUntil?: string;
  // Set your own short url instead of autogenerating a URL
  customSlug?: string;
  // Maximum visits allowed on this link
  maxVisits?: number;
  // Don't create another short URL if another one already uses this longUrl
  findIfExists?: boolean;
  // The domain in which the short code should be saved in
  domain?: string;
  // Length of the code
  shortCodeLength?: number;
  validateUrl?: boolean;
}

Response

Returns the created ShortUrl object.

client#deleteShortUrl(shortCode)

Deletes the short URL for provided short code.

Parameters

shortCode: The short code to delete.

Response

Returns the delete ShortUrl shortCode.

client#setShortUrlTags(shortCode, tags)

Edit the tags on URL identified by provided short code.

Parameters

shortCode: The short code to which you want to set the tags.

tags: Array of tags to assign the this short code. Older tags are not merged, but replaced with the new ones.

Response

Returns the shortCode's new tags.

client#listTags(options)

List all available tags

Parameters

options:

interface ListTagsOptions {
  // Whether to add additional details about the tags visits / links
  withStats: boolean;
}

Response

Returns the list of available tags, and the details if requested

client#renameTag(oldName, newName)

Renames a tag

Parameters

oldName: string of the previous tag name

newName: string of the new tag name

Response

An object with the new tag name, if succeeded.

client#deleteTags(...tags)

Deletes tags

Parameters

tags: Takes as many arguments as tags to remove. Inexistant tags won't throw error.

Response

An object with the list of deleted tags

client#getDomains()

Lists available domains

Response

Returns the list of available domains to create URLs

client#getPixel(shortCode)

Generates a 1px transparent image which can be used to track emails with a short URL

Parameters

shortCode: The short code to which you want to get the pixel.

Response

A base64 encoded string of the pixel

client#getQR(shortCode, options)

Generates a QR code image pointing to a short URL

Parameters

shortCode: The short code to which you want to get the QR Code.

options:

interface QRCodeOptions {
  format?: 'png' | 'svg'; // Format of the response (defaults to png)
  size?: number; // size in px of the QR Code (defaults to 300px)
} 

Response

An objet containing the MIME type of the QR Code, and the data in the requested format

client#countVisits()

Get general visits stats not linked to one specific short URL.

Response

Returns an object containing general data about link statistics

client#getLinkVisits(shortCode, options)

Get the list of visits on the short URL behind provided short code.

Parameters

shortCode: The short code to which you want to get the visits stats.

options:

interface PaginationOptions {
  startDate?: string; // ISO-8601 date format
  endDate?: string; // ISO-8601 date format
  page?: number; // Page number, defaults to 1
  itemsPerPage?: number; // Items per page, defaults to all results
}

Response

A paginated response containing visits stats for the given short code.

client#getTagVisits(tag, options)

Get the list of visits on any short URL which is tagged with provided tag.

Parameters

shortCode: The short code to which you want to get the visits stats.

options:

interface PaginationOptions {
  startDate?: string; // ISO-8601 date format
  endDate?: string; // ISO-8601 date format
  page?: number; // Page number, defaults to 1
  itemsPerPage?: number; // Items per page, defaults to all results
}

Response

A paginated response containing visits stats for the given tag.