dispatch:short-url

v1.1.0Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

dispatch:short-url Build Status

Create short urls, supports android and ios intents / custom-url shemes.

Usage examples

1  // Setup a url shortener
2  shortUrl = new UrlShortener({
3    prefix: '/s'     // If the shortener is to co-exist with
4                     // the application then prefix, default is /s/XXXXXXXX
5    // rateLimit: {  // Set rate limit options on the http point
6    //   totalRequests: 10,
7    //   every: 60000
8    // },
9    // collectionName: '', // Overwrite the default collection for short urls
10    //
11    // fallBack URL can either be a string or a function, for dynamic fallbacks
12    fallbackUrl: function(request) {
13      return 'http://other/shortener/service/' + request.url;
14    },
15    // debug: true // Make it verbose,
16    // payload: { foo: bar } // Abillity to add payload for events
17    startServer: true // Start the http(s) restpoint, default is false
18  });
19
20  // Create a short url
21  url = shortUrl.createShortUrl('https://this.is.a.long.url.please.shorten.me');
22
23  // Create a short url for android and iOS
24  // Note: Provide the android fallback url in the intent for the best
25  // user experience
26  url = shortUrl.createShortUrl('https://if.this.is.a.long.url.then.shorten.me', {
27    androidUrl: 'intent://?platform=android#Intent;scheme=dispatchdeeplinktest;'+
28              'package=me.dispatch.qa.test.deep.link;' +
29              'S.browser_fallback_url=http://www.google.com;end',
30      iosUrl: 'dispatchdeeplinktest://?shorturl=ios'
31  });
32
33  // Limit usage
34  url = shortUrl.createShortUrl('https://if.this.is.a.long.url.then.shorten.me', {
35    oneTimeLink:true // Set true and this link will only work once
36  });
37
38  // or... have it expire
39  url = shortUrl.createShortUrl('https://if.this.is.a.long.url.then.shorten.me', {
40    expireAt:new Date(new Date().getTime() + 10000) // Set expiration time/date
41  });
1  // Add the "dispatch:deep-link" package
2  // Initialize the deepLink - customURL scheme
3  deepLink = new DeepLink('dispatchdeeplinktest', {
4    fallbackUrl: 'https:/go.to.here.if.the.redirect.fails', // Adding a fallback here will add it to the android intent
5    appId: 'me.dispatch.qa.test.deep.link'
6  });
7
8  // Setup a url shortener
9  shortUrl = new UrlShortener({
10    prefix: '/s',
11    fallbackUrl: 'https://go.to.here.if.the.redirect.fails', // In case the url is not found
12  });
13
14  // Create some data to send
15  var payload = {
16    foo: 'bar',
17    createdAt: new Date() // Supports EJSON
18    bar: {                // and
19      foo: 'bar2'         // Nested data
20    }
21  };
22
23  var tinyPayload = 12; // For internet explorer
24
25  // Create the shortUrl
26  shortUrl = createShortUrl(deepLink.browserLink('', payload), {
27    androidUrl: deepLink.androidLink('', payload), // This will fallback to the main url
28    iosUrl: deepLink.iosLink('', payload), // This will fallback to the main url
29    ieUrl: deepLink.browserLink('', tinyPayload)
30  });

Manually cleanup expired

You can call cleanExpiredFromDatabase to remove all expired links. If the user tries using an expired link it will be removed in terms of usage.

1  // var shortUrl = createShortUrl(....)
2  // call cleanExpiredFromDatabase at intervals or by service
3  shortUrl.cleanExpiredFromDatabase();

Events emitted

The urlShortner will emit the following event:

  • this.emit('redirect', event); (redirect or by html template)
  • this.emit('expired', event); (By date or usage)
  • this.emit('error', event);
  • this.emit('cleanup'); When cleanExpiredFromDatabase is done cleaning expired data from the database