@yqg/monitor
Advanced tools
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global.YqgMonitor = factory()); | ||
| }(this, (function () { 'use strict'; | ||
| var objectToUrlParams = function (data) { return (Object | ||
| .keys(data) | ||
| .map(function (key) { | ||
| var value = encodeURIComponent(data[key]); | ||
| return key + "=" + value; | ||
| }) | ||
| .join('&')); }; | ||
| var imageGet = function (path, data) { return new Promise(function (resolve) { | ||
| if (!document) | ||
| resolve(); | ||
| // create image element to send request | ||
| var imgEle = new Image(); | ||
| var params = Object.assign(data, { _timestamp: Date.now() }); | ||
| imgEle.src = path + "?" + objectToUrlParams(data); | ||
| // no matter request successed or failed, resolve the promise as if nothing happened | ||
| var callbackHandler = function () { | ||
| resolve(); | ||
| }; | ||
| imgEle.onload = callbackHandler; | ||
| imgEle.onerror = callbackHandler; | ||
| }); }; | ||
| var reportPath = '/mayuri/api/monitor/report'; | ||
| var YqgMonitor = (function () { | ||
| function YqgMonitor(projectKey, options) { | ||
| this.projectKey = projectKey; | ||
| this.options = options; | ||
| } | ||
| YqgMonitor.prototype.report = function (moduleKey, eventKey, data) { | ||
| var _a = this, projectKey = _a.projectKey, apiHost = _a.options.apiHost; | ||
| var path = "" + apiHost + reportPath; | ||
| try { | ||
| imageGet(path, Object.assign(data, { projectKey, moduleKey, eventKey })); | ||
| } | ||
| catch (err) { | ||
| } | ||
| }; | ||
| return YqgMonitor; | ||
| }()); | ||
| return YqgMonitor; | ||
| }))); |
| const objectToUrlParams = (data: object): string => ( | ||
| Object | ||
| .keys(data) | ||
| .map((key) => { | ||
| const value = encodeURIComponent(data[key]); | ||
| return `${key}=${value}`; | ||
| }) | ||
| .join('&') | ||
| ); | ||
| export const imageGet = (path: string, data: object): Promise<any> => new Promise((resolve) => { | ||
| if (!document) resolve(); | ||
| // create image element to send request | ||
| const imgEle: HTMLImageElement = new Image(); | ||
| const params: object = Object.assign(data, {_timestamp: Date.now()}); | ||
| imgEle.src = `${path}?${objectToUrlParams(data)}`; | ||
| // no matter request successed or failed, resolve the promise as if nothing happened | ||
| const callbackHandler = (): void => { | ||
| resolve(); | ||
| }; | ||
| imgEle.onload = callbackHandler; | ||
| imgEle.onerror = callbackHandler; | ||
| }); |
| import {imageGet} from './helper/helper'; | ||
| const reportPath: string = '/mayuri/api/monitor/report'; | ||
| interface MonitorOptions { | ||
| apiHost: string; | ||
| } | ||
| class YqgMonitor { | ||
| constructor(private projectKey: string, private options: MonitorOptions) {} | ||
| report(moduleKey: string, eventKey: string, data: object): void { | ||
| const {projectKey, options: {apiHost}} = this; | ||
| const path = `${apiHost}${reportPath}`; | ||
| try { | ||
| imageGet(path, Object.assign(data, {projectKey, moduleKey, eventKey})); | ||
| } catch (err) { | ||
| // doing nothing | ||
| } | ||
| } | ||
| } | ||
| export default YqgMonitor; |
+3
-6
| { | ||
| "name": "@yqg/monitor", | ||
| "version": "0.0.1", | ||
| "main": "dist/monitor.js", | ||
| "version": "0.0.2", | ||
| "main": "dist/yqg-monitor.js", | ||
| "typings": "index.d.ts", | ||
| "devDependencies": { | ||
| "@babel/core": "7.0.0-beta.47", | ||
| "@babel/preset-env": "7.0.0-beta.47", | ||
| "@babel/preset-stage-3": "7.0.0-beta.47", | ||
| "rollup": "0.59.4", | ||
| "rollup-plugin-babel": "4.0.0-beta.0" | ||
| "rollup-plugin-typescript": "0.8.1" | ||
| }, | ||
@@ -13,0 +10,0 @@ "scripts": { |
+18
-1
@@ -7,2 +7,19 @@ /** | ||
| export default {}; // To be continued | ||
| import typescript from 'rollup-plugin-typescript'; | ||
| export default { | ||
| entry: './src/yqg-monitor.ts', | ||
| output: { | ||
| file: 'dist/yqg-monitor.js', | ||
| format: 'umd', | ||
| name: 'YqgMonitor' | ||
| }, | ||
| plugins: [ | ||
| typescript({ | ||
| target: 'es5', | ||
| module: 'es2015' | ||
| }) | ||
| ] | ||
| }; |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
3977
584.51%2
-60%6
100%105
1650%0
-100%