diff --git a/.jshintrc b/.jshintrc index 5a1339d..6365757 100644 --- a/.jshintrc +++ b/.jshintrc @@ -21,6 +21,7 @@ "devel": true, "node": true, "globals": { + "define": true, "describe": true, "beforeEach": true, "afterEach": true, diff --git a/README.md b/README.md index 61b531c..08569c9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ Tattletale wraps around the browser’s `console` object, storing your logs in order to send them over XHR for long-term storage and analysis using a service like [Scribe](http://github.com/facebook/scribe). +## Install with npm + +``` +npm install Tattletale +``` + ## Usage ```javascript @@ -32,3 +38,11 @@ var tattletale = new Tattletale('/log', { token: window.xsrft }); ``` + +## Contributors + +### Lint, Test, Uglify + +``` +npm install && npm run build +``` diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8fa4770 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,31 @@ +// Type definitions for Tattletale.js +// Project: https://github.com/vimeo/tattletale.js +// Definitions by: iconix +// Based on: http://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-class-d-ts.html + +/*~ Write your module's methods and properties in this class */ +declare class __Tattletale { + constructor(url: string, static_request_data?: Object); + + someProperty: string[]; + + log(data: string | number | boolean): void; + + empty(): void; + + send(callback?: Function): void; +} + +/*~ This declaration specifies that the class constructor function + *~ is the exported object from the file + *~ + *~ Note that ES6 modules cannot directly export class objects. + *~ This file should be imported using the CommonJS-style: + *~ import x = require('someLibrary'); + *~ + *~ Refer to the documentation to understand common + *~ workarounds for this limitation of ES6 modules. + */ +declare module 'Tattletale' { + export = __Tattletale; +} diff --git a/package.json b/package.json index 728585c..ab3855d 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,21 @@ { "name": "Tattletale", - "description": "A utility to send console logs over XHR for server-side processing.", - "version": "0.1.0", + "description": "A UMD module for sending console logs over XHR for server-side processing.", + "version": "0.2.0", "author": "Vimeo, LLC", - "license": "Apache 2.0", + "license": "Apache-2.0", + "main": "tattletale.min.js", "repository": { "type": "git", - "url": "git@github.com:vimeo/tattletail.git" + "url": "git@github.com:vimeo/tattletale.js.git" + }, + "scripts": { + "build": "grunt" }, "contributors": [ + { + "name": "iconix" + }, { "name": "Kevin Sweeney" } @@ -19,13 +26,14 @@ "logging" ], "dependencies": { - "grunt": "~0.4", - "grunt-contrib-jshint": "~0.6", - "grunt-mocha": "~0.3", - "mocha": "~1.10", - "chai": "~1.6", - "sinon": "~1.7", - "sinon-chai": "~2.4", - "grunt-contrib-uglify": "~0.2.2" + "chai": "^3.5.0", + "grunt": "^1.0.1", + "grunt-cli": "^1.2.0", + "grunt-contrib-jshint": "^1.1.0", + "grunt-contrib-uglify": "^2.3.0", + "grunt-mocha": "^1.0.4", + "mocha": "^3.2.0", + "sinon": "^2.1.0", + "sinon-chai": "^2.9.0" } } diff --git a/tattletale.js b/tattletale.js index 8ac40c5..5ce9fbd 100644 --- a/tattletale.js +++ b/tattletale.js @@ -1,4 +1,19 @@ -;(function(window) { +// UMD conversion based on: https://github.com/umdjs/umd/blob/master/templates/returnExports.js + +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], factory); + } else if (typeof module === 'object' && module.exports) { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like environments that support module.exports, + // like Node. + module.exports = factory(); + } else { + // Browser globals (root is window) + root.Tattletale = factory(); + } +}(this, function () { 'use strict'; // Constructor _______________________________________________________________ @@ -153,16 +168,15 @@ } }; - request.open('POST', self.url, true); - request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + if (data_to_send) { + request.open('POST', self.url, true); + request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - request.send(data_to_send); + request.send(data_to_send); + } } }; -// Exports ___________________________________________________________________ - - window.Tattletale = Tattletale; - -})(window); \ No newline at end of file + return Tattletale; +})); diff --git a/tattletale.min.js b/tattletale.min.js index 74c13a2..4b89a4f 100644 --- a/tattletale.min.js +++ b/tattletale.min.js @@ -1,9 +1,10 @@ /** * Tattletale - * A utility to send console logs over XHR for server-side processing. + * A UMD module for sending console logs over XHR for server-side processing. * * @author Vimeo, LLC - * @version 0.1.0 - * @license Apache 2.0 + * @version 0.2.0 + * @license Apache-2.0 */ -!function(a){"use strict";function b(a,b){var c=this;if("string"!=typeof a)throw new Error("Tattletale instances require a URL for log submission over XHR.");c.url=a,c.request_data="object"==typeof b?b:{},c.logs=[]}function c(a,b){var d,e,f,g,h,i=[],j={};for(d in a)if(a.hasOwnProperty(d)){switch(e=a[d],b&&(d=b+"["+d+"]"),Object.prototype.toString.call(e)){case"[object Object]":f=c(e,d);break;case"[object Array]":for(g=0,h=e.length;h>g;g++)j[g]=e[g];f=c(j,d);break;default:f=d+"="+encodeURIComponent(e)}null!==e&&i.push(f)}return i.join("&")}b.prototype={log:function(){var b,c,d,e,f=this.logs,g="",h=arguments.length;if(0!==h){for(b=[].slice.call(arguments),c=0;h>c;c++)d=b[c],e=typeof d,g+="string"===e||"number"===e||"boolean"===e?d:"[Object object]",h-1>c&&(g+=", ");f.length>100&&f.splice(0,1),f.push(g),"undefined"!=typeof a.console&&"function"==typeof a.console.log&&a.console.log.apply(a.console,arguments)}},empty:function(){this.logs=[]},send:function(a){var b=this,d=new XMLHttpRequest;b.request_data.console_logs=b.logs,b.request_data=c(b.request_data),d.onreadystatechange=function(){4===d.readyState&&(b.empty(),"function"==typeof a&&a())},d.open("POST",b.url,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.setRequestHeader("Content-type","application/x-www-form-urlencoded"),d.send(b.request_data)}},a.Tattletale=b}(window); \ No newline at end of file + +!function(a,b){"function"==typeof define&&define.amd?define([],b):"object"==typeof module&&module.exports?module.exports=b():a.Tattletale=b()}(this,function(){"use strict";function a(a,b){var c=this;if("string"!=typeof a)throw new Error("Tattletale instances require a URL for log submission over XHR.");c.url=a,c.request_data="object"==typeof b?b:{},c.logs=[]}function b(a,c){var d,e,f,g,h,i=[],j={};for(d in a)if(a.hasOwnProperty(d)){switch(e=a[d],c&&(d=c+"["+d+"]"),Object.prototype.toString.call(e)){case"[object Object]":f=b(e,d);break;case"[object Array]":for(g=0,h=e.length;g100&&e.splice(0,1),e.push(f),void 0!==window.console&&"function"==typeof window.console.log&&window.console.log.apply(window.console,arguments)}},empty:function(){this.logs=[]},send:function(a){var c=this,d=new XMLHttpRequest,e=null;c.request_data.console_logs=c.logs,e=b(c.request_data),c.empty(),d.onreadystatechange=function(){4===d.readyState&&"function"==typeof a&&a()},e&&(d.open("POST",c.url,!0),d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.setRequestHeader("Content-type","application/x-www-form-urlencoded"),d.send(e))}},a}); \ No newline at end of file