# reflective

> 前端监控库

Latest version **1.0.0** (published 2022-01-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install reflective
pnpm add reflective
yarn add reflective
bun add reflective
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-01-09 |
| First published | 2022-01-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 10 |
| Unpacked size | 253.7 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Xiaodong Liu |
| Maintainers | lxd301491 |

## Links

- npm: https://www.npmjs.com/package/reflective
- npm.io page: https://npm.io/package/reflective

## Dependencies (10)

- [vue](https://npm.io/package/vue.md) ^3.2.26
- [pako](https://npm.io/package/pako.md) ^2.0.4
- [axios](https://npm.io/package/axios.md) ^0.24.0
- [nanoid](https://npm.io/package/nanoid.md) ^3.1.25
- [@types/pako](https://npm.io/package/@types/pako.md) ^1.0.3
- [localforage](https://npm.io/package/localforage.md) ^1.10.0
- [ua-parser-js](https://npm.io/package/ua-parser-js.md) ^1.0.2
- [compressed-json](https://npm.io/package/compressed-json.md) ^1.0.16
- [@types/crypto-js](https://npm.io/package/@types/crypto-js.md) ^4.1.0
- [@types/ua-parser-js](https://npm.io/package/@types/ua-parser-js.md) ^0.7.36

## Recent versions

- 1.0.0 (latest) — 2022-01-09

## README

# reflective-core
用于前端监控的核心库

## 手工代码埋点
```javascirpt
  let reflective = new rft.Reflective();
  reflective.collect({});
```

## 声明代码埋点
目前支持的指令有rft-click，rft-input

vue目前支持的指令有v-rft-click，v-rft-input
```javascirpt
  import { createApp } from 'vue'
  import App from './App.vue'
  import router from './router'
  import store from './store'
  import * as rft from 'reflective'
  import localforage from 'localforage'
  import { gzip } from 'pako'
  import axios from 'axios'

  // 创建Rft核心对象
  const refective = rft.Reflective.getInstance()
  // 创建vue根节点，但是别mount节点
  const app = createApp(App).use(store).use(router)

  const { ActionCollector, Vue2ActionCollector, Vue3ActionCollector } = {...rft.collecor}

  // 下面几种声明式收集器，根据平台环境选择一种即可
  // 1 基于Vue3自定义指令的声明埋点初始化 (根节点不能使用自定义指令)
  refective.inject(new Vue3ActionCollector(app))
  // 2 基于手工扫描自定义指令的声明埋点初始化
  // reflective.inject(new ActionCollector());
  // 扫描当前元素是否声明埋点，scan(true)时，会连同子节点遍历一起扫描
  // document.querySelector('.test').scan();
  // 3 基于Vue2自定义指令的声明埋点初始化
  // reflective.inject(new Vue2ActionCollector(Vue));

  const { LocalForageStore } = {...rft.store}
  // 基于LocalForage api的持久化插件
  // 参数1：localforage实例或者实现LocalForage api的自定义对象
  // 参数2：多长时间上报一次
  // 参数3：一次上报多少条埋点
  refective.inject(new LocalForageStore(localforage.createInstance({
  }), 5000, 10))

  const { StringTransformer } = {...rft.transformer}
  // 转换埋点信息为string的插件，传入一个将MonitorInfo[]转成string的方法，该方法中可用添加额外逻辑，比较对数据进行压缩
  // 如果没有改插件，则默认使用JSON.stringify对数据进行序列化
  refective.inject(new StringTransformer(function (data: MonitorInfo[]): string {
    return gzip(JSON.stringify(data), { to: 'string' })
  }))

  const { RestfulReport } = {...rft.reporter}
  // 上报组件，实现restful风格接口
  // 参数1：接口
  // 参数2：实现axios风格接口的对象
  refective.inject(new RestfulReport('http://www.baidu.com', axios))

  app.mount('#app')
```

## web基本埋点
```
  import * as rft from 'reflective'

  const { WebCommonCollect } = {...rft.collecor}
  // 性能（基于performace获取当前页面加载速度）， 异常(全局异常，资源加载异常，未捕获promise异常)， 路由（通过拦截pushState和replaceState实现）
  const { PERFORMANCE, EXCEPTION, ROUTE } = {...rft.collector.WebCollectType}

  refective.inject(new WebCommonCollect([PERFORMANCE, EXCEPTION, ROUTE]))
```

---
_Source: https://npm.io/package/reflective · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
