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

vapic

v0.4.0

Published

Versioned API Cache

Downloads

13

Readme

= vapic ifndef::env-github[]

View on Github

endif::[] :toc!: ifdef::env-github[] :tip-caption: :bulb: :note-caption: :information_source: :important-caption: :heavy_exclamation_mark: :caution-caption: :fire: :warning-caption: :warning: endif::[]

https://github.com/bguiz/vapic[Versioned API Cache^]

== Installation

[source,bash]

npm install vapic --save

== Usage

=== .set()

Stores the value in the cache, against the current version.

[source,javascript]

const vapic = require('vapic');

vapic.set({ url: '/foo', value: 'bar'), maxVersions: 3, // <1> redisClient: require('redis').createClient(), }, (err, result) => { /* ... */ });

<1> If maxVersions is set, cullOld() is automatically called.

Options:

url:: Used to construct the cache key value:: The value to store in the cache maxVersions:: The maximum number of cached versions to store; when set .cullOld() is called. versionMatchType:: How to determine which version to match. + When exact (the default), only an exact version match with the current version will be returned. + When skipWhenSameAsLatest, does not insert a new version if the value of the most recent version is the same as the new one you are inserting; use when multiple version are likely to contain the same value.

=== .cullOld()

Deletes older versions that have been cached. The versions to keep/ remove are determined using semver.

[source,javascript]

const vapic = require('vapic');

vapic.cullOld({ url: '/foo', maxVersions: 3, redisClient: require('redis').createClient(), }, (err, result) => { /* ... */ });

Options:

Same as .set().

=== express middleware

[source,javascript]

const vapic = require('vapic'); const express = require('express');

const vapicOptions = { permittedAge: 3600, // one hour cacheVersion: require('package.json').version, redisClient: require('redis').createClient(), };

const router = express.Router(); router.get('/foo', vapic.expressMiddleware(vapicOptions), (req, res) => { if (req.vapicError) { res.status(404).json({ message: 'Data unavailable', }); return; } else { const data = JSON.parse(req.vapicResult); res.json(data); return; } });

module.exports = router;

Options:

prefix:: Prefix for the cache key. Defaults to vapic:/ cacheVersion:: Cached version to use. Defaults to the version number of the current module, or process.env.npm_package_version versionMatchType:: How to determine which version to match. + When exact (the default), only an exact version match with the current version will be returned. + When latestUpToCurrent, the latest version that is less than or equal to the current version will be returned. + readVersionFromHeader:: If set to true, will read the contents of the vapic HTTP request header, and if the version property is present, it will use that instead of the specified cacheVersion. + This is most useful when there are multiple versions of clients being served by the same server, and each of them needs to lock down their expected responses to a particular version. + The vapic HTTP request header's value is expected to be a base64 encoded JSON string, e.g. {version:'0.0.1'} => 'eyJ2ZXJzaW9uIjoiMC4wLjEifQ=='. permittedAge:: The number of seconds the Cache-Control header in the response should set its max-age to. Defaults to 60 (One minute). logger:: An object that has an error function. Defaults to console redisClient:: A Redis client instance. Defaults to require('redis').createClient().

[TIP]

vapic.expressMiddleware() where versionMatchType=latestUpToCurrent is intended for use in conjunction with vapic.set() where versionMatchType=skipWhenSameAsLatest

[TIP]

Convenience object to JSON conversion utilities are exposed in vapic.util. If you wish to import this without require-ing all of vapic, you can also require('vapic/util.js') directly.

== Development

If you would like to contribute, fork the git repo, and create a branch off the develop branch, and submit your pull request when you are done.

[NOTE]

This repo uses the git flow branching strategy.

To run tests:

[source,bash]

npm run test

== Author

http://bguiz.com[Brendan Graetz^]

== Licence

GPL-3.0