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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@wcota/wurl-client

v0.2.2

Published

NodeJS client of a URL shortener using a database of URLs and Clicks. Provides a function to get the redirected URL and another to save the access.

Readme

w-URL - URL shortener

CC BY-NC-SA 4.0

This is a simple package that provides a function to look for a id in a Url table and return the redirected URL, and save the click with IP, time, userAgent, referer, and country code of the user.

To install it, you will need to use Prisma ORM with the following models in the DB schema:

model Url {
  id        String   @id
  url       String
  title     String?
  createdBy String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  clicks    Click[]
}

model Click {
  id            Int      @id @default(autoincrement())
  time          DateTime @default(now())
  referer      String
  userAgent     String
  clickedBy     String
  countryCode   String
  urlId         String?
  originalUrlId String?
  requestedUrl  String?
  Url           Url?     @relation(fields: [urlId], references: [id])
}

Note that the write access is needed for Click table!.

Initializing

Import the client using import wurl from '@wcota/wurl-client'. Now, call wurl.init(prisma) with a given Prisma client with access to Url and Click models. It will link the w-URL client with Prisma. If debug is necessary (please do not use in production!), pass the flag debug = true in wurl.init().

Using find()

This function is async, requiring the id and an optional req object with the HTTP request object. Prisma will try to find the given URL data from for id, and if req is provided, will add the access to id in the Click table with the timestamp, referer, user agent, IP, country code, requested ID, original request URL, and URL to where the user was redirected.

In case of success (id exists), the following object is returned:

{
  status: "ok", // "ok" means that the ID was found
  data: {
    id: "example", // the requested ID
    url: "https://example.com", // the redirected URL
    title: "Example.com web page!", // title of the page, as it is in the database
    createdBy: "127.0.0.1", // IP of who created the ID
    createdAt: 2021-03-06T00:43:05.000Z, // timestamp of creation
    updatedAt: 2022-08-08T04:22:55.723Z // timestamp of last modified
  },
  errorCode: undefined, // 500 if there was an error adding the click info
  errorMessage: undefined // "error while saving click" if there was an error adding the click info
}

Note the errorCode if there is an error saving the user's information.

If the URL does not exist (id is not found), the following JSON object is returned, without data keyword:

{
  status: "error", // "error" means that the ID was not found or there was another error
  errorCode: 404, // or 500 if there was an error finding the ID
  errorMessage: "not found" // or "error while finding link" if there was an error finding the ID
}

All the errors are saved with console.error() for debugging.

Click data

The user data consists of the following information:

{
    referer, // referer address
    userAgent, // user agent
    clickedBy, // IP of the user
    countryCode, // 2 letters country code
    originalUrlId, // the id accessed when clicked
    urlId, // the id associated to the click, if the ID was found
    requestedUrl // the requested URL, preserving query strings and so on
}

Note that urlId and originalUrlId can be different. The first only exists if the respective ID was found. It may be different if the ID of the associated click has been changed in the Url table.

Using it in Next.JS

Create a file at /lib/wurl.js with the following content:

import { prisma } from '@/lib/db'
import wurl from '@wcota/wurl-client'

wurl.init(prisma, false)

export default wurl

Now, import it where you want to use, such as

import wurl from '@/lib/wurl'

export const getServerSideProps = async ({req, res, params}) => {

    const result = await wurl.find(params.id, req)

    if (result.status != 'ok') {
        return {
            redirect: {
                destination: process.env.DEFAULT_URL_REDIRECT,
                statusCode: 302
            }
        }
    }

    return {
        redirect: {
            destination: result.data.url,
            statusCode: 301
        }
    }
    
    return null
}

Licence

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0