generated from fingerprintjs/library-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mixins.ts
159 lines (148 loc) · 4.07 KB
/
mixins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import type { FpjsVisitorQueryData } from '../types'
import type { FpjsGetVisitorDataMethod, GetVisitorDataMethodParams } from './mixins.types'
function setMixinData<Key extends keyof FpjsVisitorQueryData<boolean>>(
this: any,
dataName: string,
key: Key,
value: FpjsVisitorQueryData<boolean>[Key]
) {
this.$data[dataName][key] = value
}
function createMixin<TExtended extends boolean>(extended: TExtended) {
const suffix = extended ? 'Extended' : ''
const dataName = extended ? 'visitorDataExtended' : 'visitorData'
const methodName = `$getVisitorData${suffix}` as const
const getVisitorData: FpjsGetVisitorDataMethod = async function (options) {
/**
* We use this.$root as a fallback, because in nuxt sometimes this.$fpjs might be empty, but it might exist in $root
* */
const fpjs = this.$fpjs ?? this.$root?.$fpjs
const boundSetData = setMixinData.bind(this)
const setData = <Key extends keyof FpjsVisitorQueryData<TExtended>>(
key: Key,
value: FpjsVisitorQueryData<TExtended>[Key]
) => {
return boundSetData(dataName, key, value)
}
if (!fpjs) {
throw new TypeError('$fpjs is not defined.')
}
try {
setData('isLoading', true)
setData(
'data',
await fpjs.getVisitorData(
{
...(options ?? {}),
extendedResult: extended,
},
options?.ignoreCache
)
)
} catch (error) {
setData('error', error as Error)
} finally {
setData('isLoading', false)
}
}
return {
data() {
return {
[dataName]: {
isLoading: false,
data: undefined,
error: undefined,
} as FpjsVisitorQueryData<TExtended>,
}
},
methods: {
[methodName]: getVisitorData as (options?: GetVisitorDataMethodParams) => Promise<void>,
},
} as const
}
/**
* Mixin for fetching normal visitorData
*
* @example ```vue
*
* <script>
* import { fpjsGetVisitorDataMixin } from '@fingerprintjs/fingerprintjs-pro-vue-v3';
* //import { fpjsGetVisitorDataMixin } from '@fingerprintjs/fingerprintjs-pro-vue-v2';
*
* export default {
* // Include our mixin
* mixins: [fpjsGetVisitorDataMixin],
* async mounted() {
* // You can also fetch data on mount
* // await this.$getVisitorData();
* }
* };
* </script>
*
* <template>
* <div>
* <button @click='$getVisitorData'>
* Get visitor data
* </button>
* <span v-if='visitorData.isLoading'>
* Loading...
* </span>
* <span v-else-if='visitorData.isError'>
* Error: {{ visitorData.error }}
* </span>
* <span v-else>
* <!--Do something with visitorData here-->
* </span>
* </div>
* </template>
* ```
* */
export const fpjsGetVisitorDataMixin = createMixin(false) as {
data: () => { visitorData: FpjsVisitorQueryData<false> }
methods: {
$getVisitorData: FpjsGetVisitorDataMethod<any>
}
}
/**
* Mixin for fetching extended visitorData
*
* @example ```vue
*
* <script>
* import { fpjsGetVisitorDataExtendedMixin } from '@fingerprintjs/fingerprintjs-pro-vue-v3';
* //import { fpjsGetVisitorDataExtendedMixin } from '@fingerprintjs/fingerprintjs-pro-vue-v2';
*
* export default {
* // Include our mixin
* mixins: [fpjsGetVisitorDataExtendedMixin],
* async mounted() {
* // You can also fetch data on mount
* // await this.$getVisitorDataExtended();
* }
* );
* </script>
*
* <template>
* <div>
* <button @click='$getVisitorDataExtended'>
* Get visitor data
* </button>
* <span v-if='visitorDataExtended.isLoading'>
* Loading...
* </span>
* <span v-else-if='visitorDataExtended.isError'>
* Error: {{ visitorDataExtended.error }}
* </span>
* <span v-else>
* <!--Do something with visitorData here-->
* </span>
* </div>
* </template>
* ```
* */
export const fpjsGetVisitorDataExtendedMixin = createMixin(true) as {
data: () => { visitorDataExtended: FpjsVisitorQueryData<true> }
methods: {
$getVisitorDataExtended: FpjsGetVisitorDataMethod<any>
}
}