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

wallet-mongoose

v1.0.1

Published

Implementation of Apple Wallet Web Service CRUD operations using mongoose

Downloads

6

Readme

This module provides API's for performing CRUD operations listed in Wallet Developer Guide and PassKit Web Service Reference for wallet push notifications.

wallet-mongoose relies on the mongoose module and provides error handling, caching and unit tests.

Plug this in between any Authentication module and a Pass generating module

Steps:

1.Generate a Pass and save user data in the Pass schema

2.Wallet app makes REST calls when user adds Pass to the wallet app

3.Wallet requests -> Authenticate -> wallet-mongoose

Installation

$ npm install wallet-mongoose

Usage :

Sample schema with mandatory fields,

var deviceSchema = new Schema({
	deviceLibraryIdentifier : String,
	pushToken : String
});

var registrationSchema = new Schema({
	deviceLibraryIdentifier : String,
	passTypeIdentifier : String,
	serialNumber : String
});

var passSchema = new Schema({
	passTypeIdentifier : String,
	serialNumber : String,
	lastUpdated : {type : Number , default : 0}, // This can be UNIX time
	userName : String	// (Optional) Add in your custom fields for storing user data
});

var Device = mongoose.model('Device' , deviceSchema);
var Pass = mongoose.model('Pass' , passSchema);
var Registration = mongoose.model('Registration' , registrationSchema);

Generate Pass:

Generate a Pass using any pass generating module and save the document in the Pass schema

Eg:

var now = Date.now()
Pass.update({
        serialNumber : '1234',
        passTypeIdentifier : 'pass.com.company.team'
    },{
        passTypeIdentifier : 'pass.com.company.team',
        serialNumber : '1234',
        lastUpdated : now,
        userName : 'John Doe'
        }
    },{
        upsert : true,
        setDefaultsOnInsert : true
    },function(err , raw){
        if(err){
            ......
        }
   });

   /*
    For caching, wallet-mongoose uses the 'if-modified-since' header in the request to getPass(...) to send 304 if Pass.lastUpdated is <= req.headers['if-modified-since'] .
   */
    res.setHeader('Last-Modified', now);

On clicking Add pass to the wallet app, the following REST calls are used by the wallet app.

REST API's:

1.Register

var express = require('express');
var router = express.Router();
var authenticate = require('./authenticate'); // Plug in your authentication module, using jsonwebtoken for instance
var wallet = require('wallet-mongoose');

router.post('/webServiceURL/*/devices/*/registrations/*/*', authenticate.verify, function(req, res, next){
	wallet.register(req, Device, Registration, function(err, statusCode){
		if(err){
			res.sendStatus(statusCode);	// Send appropriate error status code
		}
		res.sendStatus(statusCode);	// Send appropriate success status code
	});
});

2.Get Serial Numbers

router.get('/webServiceURL/*/devices/*/registrations/*', function(req, res, next){
	wallet.getSerialNumbers(req, Registration, Pass, function(err, statusCode, responseBody){
		if(err){
			res.sendStatus(statusCode);
		}
		res.status(statusCode).send(responseBody);
	});
});

3.Get Pass

router.get('/webServiceURL/*/passes/*/*', authenticate.verify , function(req, res, next){
	wallet.getPass(req, Pass, function(err, statusCode, passData){
		if(err){
			res.sendStatus(statusCode);
		}
		// passData is the document in the Pass schema holding user's data
		/*
		 Call next module which uses passData and generates an actual Pass
		 The passbook module for example is capable of doing this
	 	*/
	 	next();
	});
});

4.Unregister

router.delete('/webServiceURL/*/devices/*/registrations/*/*' , authenticate.verify, function(req, res, next){
	wallet.unregister(req, Registration, Device, function(err, statusCode){
		if(err){
			res.sendStatus(statusCode);
		}
		res.sendStatus(statusCode);
	});
});

5.Log

router.post('/webServiceURL/*/log' , function(req, res, next){
	wallet.log(function(err, statusCode, log){
		console.log(log);	// Just logs to console
		res.sendStatus(statusCode);
	});
});

Testing

Run mongod on mongodb://localhost:27017, then

cd wallet-mongoose
npm install
npm test

Sharding

Device collection can be hash sharded on the deviceLibraryIdentifier key.

Pass collection can be sharded on the serialNumber + passTypeIdentifier composite key.