Skip to content

Commit

Permalink
Merge pull request #109 from fingerprintjs/feat/allow-pass-getoptions…
Browse files Browse the repository at this point in the history
…-to-getdata

Extend `getData` options with `fingerprintjs-pro` `GetDataOptions`.
  • Loading branch information
ilfa authored Jun 9, 2023
2 parents 512af61 + ada1f67 commit 8a6727e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 27 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/analyze-commits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Analyze Commit Messages
on: [pull_request]

permissions:
pull-requests: write
contents: write
jobs:
release-notes-comment:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Collect semantic-release-info
id: semantic_release_info
uses: fingerprintjs/action-semantic-release-info@v1
- if: ${{ steps.semantic_release_info.outputs.no_release == 'false' }}
name: Add comment to the PR
uses: marocchino/sticky-pull-request-comment@3d60a5b2dae89d44e0c6ddc69dd7536aec2071cd
with:
header: ReleasePreview
recreate: true
message: |
## This PR will create a ${{steps.semantic_release_info.outputs.type}} release :rocket:
${{steps.semantic_release_info.outputs.notes}}
- if: ${{ steps.semantic_release_info.outputs.no_release == 'true' }}
name: Add comment to the PR
uses: marocchino/sticky-pull-request-comment@3d60a5b2dae89d44e0c6ddc69dd7536aec2071cd
with:
header: ReleasePreview
recreate: true
message: |
## This PR will not create a new release :rocket:
76 changes: 76 additions & 0 deletions __tests__/use-visitor-data.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,80 @@ describe('useVisitorData', () => {

expect(hook.result.current.error?.message).toBe(ERROR_CLIENT_TIMEOUT)
})

it('`getVisitorData` `getOptions` should be passed from `getVisitorData` `getOptions`', async () => {
const useVisitorDataId = 'useVisitorDataId'
const wrapper = createWrapper()
const hook = renderHook(
() =>
useVisitorData(
{
linkedId: useVisitorDataId,
tag: { tagA: useVisitorDataId },
},
{ immediate: false }
),
{ wrapper }
)

await act(async () => {
await hook.result.current.getData()
})
expect(getVisitorData).toHaveBeenCalledTimes(1)
expect(getVisitorData).toHaveBeenCalledWith(
{ linkedId: useVisitorDataId, tag: { tagA: useVisitorDataId } },
undefined
)
})

it('`getData` `getOptions` should be more important than `getVisitorData` `getOptions`', async () => {
const getDataId = 'getDataId'
const useVisitorDataId = 'useVisitorDataId'
const wrapper = createWrapper()
const hook = renderHook(
() =>
useVisitorData(
{
linkedId: useVisitorDataId,
tag: { tagA: useVisitorDataId },
},
{ immediate: false }
),
{ wrapper }
)

await act(async () => {
await hook.result.current.getData({
linkedId: getDataId,
tag: { tagA: getDataId },
})
})
expect(getVisitorData).toHaveBeenCalledTimes(1)
expect(getVisitorData).toHaveBeenCalledWith({ linkedId: getDataId, tag: { tagA: getDataId } }, undefined)
})

it('`getData` `getOptions` should extend `getVisitorData` `getOptions`', async () => {
const getDataId = 'getDataId'
const useVisitorDataId = 'useVisitorDataId'
const wrapper = createWrapper()
const hook = renderHook(
() =>
useVisitorData(
{
linkedId: useVisitorDataId,
tag: { tagA: useVisitorDataId },
},
{ immediate: false }
),
{ wrapper }
)

await act(async () => {
await hook.result.current.getData({
linkedId: getDataId,
})
})
expect(getVisitorData).toHaveBeenCalledTimes(1)
expect(getVisitorData).toHaveBeenCalledWith({ linkedId: getDataId, tag: { tagA: useVisitorDataId } }, undefined)
})
})
4 changes: 2 additions & 2 deletions examples/preact/src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const App: FunctionalComponent = () => {
const [extendedResult, updateExtendedResult] = useState(false)
const { isLoading, error, data, getData } = useVisitorData({ extendedResult }, { immediate: true })

const reloadData = () => {
const reloadData = (): void => {
getData({ ignoreCache: true })
}

const onChangeExtendedResult = (e: TargetedEvent<HTMLInputElement, Event>) => {
const onChangeExtendedResult = (e: TargetedEvent<HTMLInputElement, Event>): void => {
updateExtendedResult((e.target as HTMLInputElement).checked)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"semantic-release": "19.0.3",
"ts-jest": "^27.1.5",
"tslib": "2.5.0",
"typedoc": "^0.23.24",
"typedoc": "^0.24.8",
"typescript": "^4.9.5"
},
"lint-staged": {
Expand Down
4 changes: 2 additions & 2 deletions src/fpjs-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface VisitorQueryResult<TExtended extends boolean> extends QueryResu
data?: VisitorData<TExtended>
}

export interface GetDataOptions {
export interface GetDataOptions<TExtended extends boolean> extends GetOptions<TExtended> {
/**
* When set to true, the visitor data will always be fetched from our API.
* */
Expand All @@ -31,7 +31,7 @@ export interface VisitorQueryContext<TExtended extends boolean> extends VisitorQ
/**
* Performs identification request to server and returns visitors data.
* */
getData: (getDataOptions?: GetDataOptions) => Promise<VisitorData<TExtended>>
getData: (getDataOptions?: GetDataOptions<TExtended>) => Promise<VisitorData<TExtended>>
}

const stub = (): never => {
Expand Down
8 changes: 5 additions & 3 deletions src/use-visitor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import deepEquals from 'fast-deep-equal'
import { toError } from './utils/to-error'
import { assertIsTruthy } from './utils/assert-is-truthy'

export type UseVisitorDataOptions<TExtended extends boolean> = GetOptions<TExtended> & Partial<GetDataOptions>
export type UseVisitorDataOptions<TExtended extends boolean> = GetDataOptions<TExtended>

/**
* @example
Expand Down Expand Up @@ -43,15 +43,17 @@ export function useVisitorData<TExtended extends boolean>(
async (params = {}) => {
assertIsTruthy(params, 'getDataParams')

const { ignoreCache } = params
const { ignoreCache, ...getDataPassedOptions } = params

try {
setState((state) => ({ ...state, isLoading: true }))

const { ignoreCache: defaultIgnoreCache, ...getVisitorDataOptions } = getOptions

const getDataOptions: GetOptions<TExtended> = { ...getVisitorDataOptions, ...getDataPassedOptions }

const result = await getVisitorData(
getVisitorDataOptions ?? {},
getDataOptions,
typeof ignoreCache === 'boolean' ? ignoreCache : defaultIgnoreCache
)
setState((state) => ({ ...state, data: result, isLoading: false, error: undefined }))
Expand Down
44 changes: 25 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,11 @@ ansi-regex@^6.0.1:
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==

ansi-sequence-parser@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ansi-sequence-parser/-/ansi-sequence-parser-1.1.0.tgz#4d790f31236ac20366b23b3916b789e1bde39aed"
integrity sha512-lEm8mt52to2fT8GhciPCGeCXACSz2UwIN4X2e2LJSnZ5uAbn2/dsYdOmUXq0AtWS5cpAupysIneExOgH0Vd2TQ==

ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
Expand Down Expand Up @@ -6225,10 +6230,10 @@ marked@^4.0.10:
resolved "https://registry.yarnpkg.com/marked/-/marked-4.0.17.tgz#1186193d85bb7882159cdcfc57d1dfccaffb3fe9"
integrity sha512-Wfk0ATOK5iPxM4ptrORkFemqroz0ZDxp5MWfYA7H/F+wO17NRWV5Ypxi6p3g2Xmw2bKeiYOl6oVnLHKxBA0VhA==

marked@^4.2.5:
version "4.2.12"
resolved "https://registry.yarnpkg.com/marked/-/marked-4.2.12.tgz#d69a64e21d71b06250da995dcd065c11083bebb5"
integrity sha512-yr8hSKa3Fv4D3jdZmtMMPghgVt6TWbk86WQaWhDloQjRSQhMMYCAro7jP7VDJrjjdV8pxVxMssXS8B8Y5DZ5aw==
marked@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3"
integrity sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==

meow@^8.0.0:
version "8.1.2"
Expand Down Expand Up @@ -6329,10 +6334,10 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"

minimatch@^5.1.2:
version "5.1.6"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96"
integrity sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==
minimatch@^9.0.0:
version "9.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.1.tgz#8a555f541cf976c622daf078bb28f29fb927c253"
integrity sha512-0jWhJpD/MdhPXwPuiRkCbfYfSKp2qnn2eOc279qI7f+osl/l+prKSrvhg157zSYvx/1nmgn2NqdT6k2Z7zSH9w==
dependencies:
brace-expansion "^2.0.1"

Expand Down Expand Up @@ -7844,11 +7849,12 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==

shiki@^0.12.1:
version "0.12.1"
resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.12.1.tgz#26fce51da12d055f479a091a5307470786f300cd"
integrity sha512-aieaV1m349rZINEBkjxh2QbBvFFQOlgqYTNtCal82hHj4dDZ76oMlQIX+C7ryerBTDiga3e5NfH6smjdJ02BbQ==
shiki@^0.14.1:
version "0.14.2"
resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.14.2.tgz#d51440800b701392b31ce2336036058e338247a1"
integrity sha512-ltSZlSLOuSY0M0Y75KA+ieRaZ0Trf5Wl3gutE7jzLuIcWxLp5i/uEnLoQWNvgKXQ5OMpGkJnVMRLAuzjc0LJ2A==
dependencies:
ansi-sequence-parser "^1.1.0"
jsonc-parser "^3.2.0"
vscode-oniguruma "^1.7.0"
vscode-textmate "^8.0.0"
Expand Down Expand Up @@ -8636,15 +8642,15 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

typedoc@^0.23.24:
version "0.23.24"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.23.24.tgz#01cf32c09f2c19362e72a9ce1552d6e5b48c4fef"
integrity sha512-bfmy8lNQh+WrPYcJbtjQ6JEEsVl/ce1ZIXyXhyW+a1vFrjO39t6J8sL/d6FfAGrJTc7McCXgk9AanYBSNvLdIA==
typedoc@^0.24.8:
version "0.24.8"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.24.8.tgz#cce9f47ba6a8d52389f5e583716a2b3b4335b63e"
integrity sha512-ahJ6Cpcvxwaxfu4KtjA8qZNqS43wYt6JL27wYiIgl1vd38WW/KWX11YuAeZhuz9v+ttrutSsgK+XO1CjL1kA3w==
dependencies:
lunr "^2.3.9"
marked "^4.2.5"
minimatch "^5.1.2"
shiki "^0.12.1"
marked "^4.3.0"
minimatch "^9.0.0"
shiki "^0.14.1"

typescript@^4.6.4:
version "4.7.4"
Expand Down

0 comments on commit 8a6727e

Please sign in to comment.