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

not-one-link

v1.0.2

Published

A simple Node.js library that allows you to re-map Amazon links from one country to another (Like Amazon OneLink™️ but simpler to use)

Downloads

8

Readme

not-one-link

npm version CI codecov

A simple Node.js library that allows you to re-map Amazon links from one country to another (Like Amazon OneLink™️ but simpler to use)

Rationale

If you want to direct your users to a product page on Amazon and you have an international audience, chances are that you would like to send users from UK to amazon.co.uk, users from Italy to amazon.it and so on...

As far as I am aware the only viable option to do this today is Amazon OneLink™️.

If you have tried to setup Amazon OneLink™️, you have probably been frustrated with the long process and chances are that, in the end, it didn't work for you. Or if it did work for you, you probably already realised that your links won't work with a URL-shortner or in any external website like Twitter or YouTube.

This library is born with the intent to help you to implement your own self-hosted Amazon OneLink™️ alternative.

Installation and usage

Install it using npm:

npm install --save not-one-link

Example usage:

const rewriteUrl = require('not-one-link')

rewriteUrl('https://www.amazon.com/dp/1839214112/', 'UK') // -> https://www.amazon.co.uk/dp/1839214112/

API

The rewriteUrl accepts the following parameters:

  • url the original Amazon URL you want to rewrite (generally a product url from amazon.com)
  • userCountryCode the detected country code of the user you want to redirect to an Amazon page (in ISO 3166 alpha-2 format)
  • tagsMap (optional) a map of amazon domains and Amazon affiliate tags. If used it will rewrite the URL by replacing/inserting the correct affiliate tag for the destination website. (See an example below)

NOTE: if an invalid URL is passed, the function will throw a TypeError, make sure you handle this potential error in your code.

Use with affiliate tags

If you are using Amazon affiliates, most likely you have created affilaited tags for different countries (unfortunately, as of today you can't have the same tag on different Amazon websites). If you provide the map of your tags per amazon domain, the library will automatically insert/replace the tags for you.

Let's see an example:

const rewriteUrl = require('not-one-link')

const tagsMap = {
  'www.amazon.co.uk': 'myUKTag',
  'www.amazon.es': 'myESTag',
  'www.amazon.it': 'myITTag'
}

const url = 'https://www.amazon.com/dp/1839214112/'
rewriteUrl(url, 'UK', tagsMap) // -> https://www.amazon.co.uk/dp/1839214112/?tag=myUKTag
rewriteUrl(url, 'ES', tagsMap) // -> https://www.amazon.es/dp/1839214112/?tag=myESTag
rewriteUrl(url, 'IT', tagsMap) // -> https://www.amazon.it/dp/1839214112/?tag=myITTag

Detecting the user country

This library does not make any assumption on how you want to detect the user country. For instance you might have registered users and the user themselves might have provided their preferred country. Providing this business logic is entirely left to you based on your use cases.

Anyway, a common way to detect a visitor country might be by IP address using something like GeoLite MaxMind DB and mmdb-reader. If you have a GeoLite database file you could detect a country for a given IP as follows:

const MMDBReader = require('mmdb-reader')

const reader = new MMDBReader('path/to/your/GeoLite2-Country.mmdb')
const res = reader.lookup('<the user ip here>')
console.log(res.country.iso_code)

Creating a URL rewriting web server

If you want to create a webserver that you can use as a proxy to redirect people to their nearest amazon store you could write something like this:

const { createServer } = require('http')
const { URL } = require('url')
const rewriteUrl = require('not-one-link')
const MMDBReader = require('mmdb-reader')

const reader = new MMDBReader('path/to/your/GeoLite2-Country.mmdb')
const server = createServer((req, res) => {
  const requestUrl = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
  const amazonUrl = requestUrl.searchParams.get('url')
  if (!amazonUrl) {
    res.statusCode = 400
    return res.end('Invalid request, `url` querystring parameter not provided')
  }

  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
  const lookupRes = reader.lookup(ip)
  if (!lookupRes) {
    // if cannot find a match for the user ip returns the original url
    res.statusCode = 302
    res.setHeader('Location', amazonUrl)
    return res.end()
  }

  const userCountry = lookupRes.country.iso_code

  try {
    const redirectUrl = rewriteUrl(amazonUrl, userCountry)
    res.statusCode = 302
    res.setHeader('Location', redirectUrl)
    return res.end()
  } catch (err) {
    res.statusCode = 400
    return res.end('Invalid `url` queryString provided')
  }
})

server.listen(8080)

Now you can use the URL https://yourwebsite:8080?url=https%3A%2F%2Fwww.amazon.com%2Fdp%2F1839214112%3Ftag%3Dloige0e-20

Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

You can also submit PRs as long as you adhere with the code standards and write tests for the proposed changes.

License

Licensed under MIT License. © Luciano Mammino.