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

countapi-js

v1.0.2

Published

CountAPI is a free counting service

Downloads

510

Readme

countapi-js

npm Travis (.org)

Count API logo

This is the official promise-based wrapper for CountAPI. CountAPI is a free counting service, you can use it to track page hits, and custom events.

Installation

npm install countapi-js --save

Example

First, include the package

const countapi = require('countapi-js');

// or with ES6
import countapi from 'countapi-js';

Counting the number of visits per page

countapi.visits().then((result) => {
  console.log(result.value);
});

Refresh the page multiple times and check the console!

If you want to track the number of visits through the whole site instead

countapi.visits('global').then((result) => {
  console.log(result.value);
});

Or custom events

document.getElementById("magic-button").addEventListener("click", () => {
  countapi.event('magic-button').then((result) => {
    alert(`The magic button has been pressed ${result.value} times.`);
  });
})

Keys and namespaces

Namespaces are meant to avoid name collisions. Its recommend use the domain of the application as namespace to avoid collision with other websites. By default, the visits and event functions will use the current domain as namespace and the provided arguments as key. If the namespace is not specified the key is assigned to the default namespace.

Keys and namespaces must match ^[A-Za-z0-9_-.]{3,64}$.

Available methods

ℹ️ Note: When requesting existing keys, if the key doesn't exists the status returned will be 404 (the promise will not fail).

get

countapi.get(namespace, key)
countapi.get(key)

Get the value of a key.

countapi.get('mysite.com', 'test').then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    value: 136
}

set

countapi.set(namespace, key, value)
countapi.set(key, value)

Set the value of a key.

ℹ️ Note: To set a key, it must be created with enable_reset set to true.

countapi.set('mysite.com', 'test', 42).then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    old_value: 136,
    value: 42
}

ℹ️ Note: If the key has enable_reset set to false, status will be 403 and the old_value will match value (the key stays the same), also the promise will NOT fail.

update

countapi.update(namespace, key, amount)
countapi.update(key, amount)

Updates a key with +/- amount.

ℹ️ Note: amount must be within update_lowerbound and update_upperbound specified during the creation of the key.

// subtract 3
countapi.update('mysite.com', 'test', -3).then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    value: 42
}

ℹ️ Note: If the amount provided is invalid you will get status 403.

hit

countapi.hit(namespace, key)
countapi.hit(key)

A shorthand for update with amount=1. And useful if you don't want to create a key manually, since if you request a key that doesn't exists, a key with enable_reset=false, update_lowerbound=0 and update_upperbound=1 will be created automatically.

countapi.hit('mysite.com', 'visits').then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/visits",
    value: 27
}

visits / event

countapi.visits()
countapi.visits(page) countapi.event(name)

Useful shorthands for hit using as namespace the current hostname. For .visits you may pass page to provide your own page identifier. If page is not provided it will be extracted from the current URL. For .event you have to pass a event name.

Examples for those are shown in the example section.

create

countapi.create()
countapi.create(options)

Create a key. All parameters are optional.

| name | default | description | |-------------------|-------------|-----------------------------------------------------------------------------------------------| | key | New UUID | Name of the key | | namespace | default | Namespace to store the key | | value | 0 | The initial value | | enable_reset | 0 | Allows the key to be resetted with set | | update_lowerbound | -1 | Restrict update to not subtract more than this number. This number must be negative or zero. | | update_upperbound | 1 | Restrict update to not add more than this number. This number must be positive or zero. |

countapi.create(options).then((result) => { ... });

result may look like

{
    status: 200,
    path: "default/6d5891ff-ebda-48fb-a760-8549d6a3bf3a",
    namespace: "default",
    key: "6d5891ff-ebda-48fb-a760-8549d6a3bf3a",
    value: 0
}

If there is a problem creating the key, the promise will be rejected with meaningful information.

info

countapi.info(namespace, key)
countapi.info(key)

Retrive information about a key.

countapi.info('test').then((result) => { ... });

result may look like

{
    status: 200,
    path: "default/test",
    namespace: "default",
    key: "test",
    ttl: 15769998014,
    created: 1553794487479,
    update_lowerbound: 0,
    update_upperbound: 1,
    enable_reset: false,
    value: 69
}

stats

countapi.stats()

Get some CountAPI stats.

countapi.stats().then((result) => { ... });

result may look like

{
    keys_created: 111111,
    keys_updated: 111111,
    requests: 111111,
    version: "xxxxxx"
}

Further documentation

Visit https://countapi.xyz for the full API documentation.

Testing

npm test

Dependencies

License

MIT