Skip to content

Commit

Permalink
feat: add preact example
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Jun 16, 2022
1 parent 75e96ad commit f280bed
Show file tree
Hide file tree
Showing 15 changed files with 11,978 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/preact/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PREACT_APP_FPJS_PUBLIC_API_KEY=<your-public-api-key>
20 changes: 20 additions & 0 deletions examples/preact/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
root: true,
env: {
browser: true,
es6: true,
node: true,
},
parser: '@typescript-eslint/parser',
extends: ['preact', 'plugin:@typescript-eslint/recommended'],
plugins: ['@typescript-eslint', 'prettier'],
ignorePatterns: ['build/'],
rules: {
semi: ['error', 'never'],
'linebreak-style': ['error', 'unix'],
'prefer-const': 'error',
'prettier/prettier': 'error',
'@typescript-eslint/no-unused-vars': ['error'],
curly: [2, 'all'],
},
}
5 changes: 5 additions & 0 deletions examples/preact/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
/build
/*.log
.env
/size-plugin.json
24 changes: 24 additions & 0 deletions examples/preact/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Setting up
To try out these examples, you need to set your public API key as the value of the PREACT_APP_FPJS_PUBLIC_API_KEY variable\
in the `.env`. You can just copy `.env.example` file located in this directory.

More info about the API key you can find on the [documentation page](https://dev.fingerprintjs.com/docs/js-agent#agent-initialization).

### Installing dependencies
1. Go to the root of the project, install dependencies with `yarn install` and build it with `yarn build`.
2. Come back to the example folder (`cd examples/preact`) and run `yarn install`.

After you've completed all the steps and inserted the API key, you can run:
```shell
yarn dev
```

Runs the app in the development mode.\
Open [http://localhost:8080](http://localhost:8080) to view it in the browser.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Preact Documentation](https://preactjs.com/) - learn about Preact features and API.
- [Preact CLI Readme](https://github.com/developit/preact-cli/blob/master/README.md) - detailed explanation on how things work.
50 changes: 50 additions & 0 deletions examples/preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"private": true,
"name": "preact",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"build": "preact build --no-sw",
"serve": "sirv build --port 8080 --cors --single",
"dev": "preact watch --no-sw",
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
"extends": [
"preact",
"plugin:@typescript-eslint/recommended"
],
"ignorePatterns": [
"build/"
]
},
"dependencies": {
"@fingerprintjs/fingerprintjs-pro-react": "link:../../",
"preact": "^10.3.1",
"preact-render-to-string": "^5.1.4"
},
"devDependencies": {
"@types/enzyme": "^3.10.5",
"@types/jest": "^26.0.8",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"dotenv": "^16.0.1",
"enzyme": "^3.11.0",
"enzyme-adapter-preact-pure": "^3.1.0",
"eslint": "^6.8.0",
"eslint-config-preact": "^1.1.1",
"jest": "^27.2.5",
"jest-preset-preact": "^4.0.2",
"preact-cli": "^3.0.0",
"sirv-cli": "^1.0.0-next.3",
"typescript": "^4.5.2"
},
"jest": {
"preset": "jest-preset-preact",
"setupFiles": [
"<rootDir>/tests/__mocks__/browserMocks.ts",
"<rootDir>/tests/__mocks__/setupTests.ts"
]
}
}
11 changes: 11 additions & 0 deletions examples/preact/preact.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import dotenv from 'dotenv'

dotenv.config()

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default (config, env, helpers) => {
const { plugin } = helpers.getPluginsByName(config, 'DefinePlugin')[0]
plugin.definitions['process.env.PREACT_APP_FPJS_PUBLIC_API_KEY'] = JSON.stringify(
process.env.PREACT_APP_FPJS_PUBLIC_API_KEY
)
}
Binary file added examples/preact/src/assets/favicon.ico
Binary file not shown.
50 changes: 50 additions & 0 deletions examples/preact/src/components/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { FunctionalComponent, h } from 'preact'
import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react'
import { useState } from 'preact/hooks'
import { TargetedEvent } from 'preact/compat'

const App: FunctionalComponent = () => {
const [extendedResult, updateExtendedResult] = useState(false)
const { isLoading, error, data, getData } = useVisitorData({ extendedResult }, { immediate: true })

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

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

return (
<div id='preact_root' className='container'>
<h1>FingerprintJS Pro Preact Demo</h1>
<div className='testArea'>
<div className='description'>
Lets load FingerprintJS Pro Agent using react integration and check next things:
</div>
<ol className='actionPoints'>
<li>There is no errors on server</li>
<li>There is no errors on client</li>
<li>In the field below visitor data was loaded</li>
<li>Try controls to test additional params</li>
</ol>
<div className='controls'>
<button onClick={reloadData} type='button'>
Reload data
</button>
<label>
<input type='checkbox' onInput={onChangeExtendedResult} checked={extendedResult} />
Extended result
</label>
</div>
<h4>
VisitorId: <span className='visitorId'>{isLoading ? 'Loading...' : data?.visitorId}</span>
</h4>
<h4>Full visitor data:</h4>
<pre className='data'>{error ? error.message : JSON.stringify(data, null, 2)}</pre>
</div>
</div>
)
}

export default App
4 changes: 4 additions & 0 deletions examples/preact/src/declaration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.css' {
const mapping: Record<string, string>
export default mapping
}
17 changes: 17 additions & 0 deletions examples/preact/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FunctionalComponent, h } from 'preact'
import './style/index.css'
import App from './components/app'
import { FpjsProvider } from '@fingerprintjs/fingerprintjs-pro-react'

const WrappedApp: FunctionalComponent = () => {
const apiKey = process.env.PREACT_APP_FPJS_PUBLIC_API_KEY as string;
return (
<FpjsProvider
loadOptions={{ apiKey }}
>
<App />
</FpjsProvider>
)
}

export default WrappedApp
10 changes: 10 additions & 0 deletions examples/preact/src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "preact",
"short_name": "preact",
"start_url": "/",
"display": "standalone",
"orientation": "portrait",
"background_color": "#fff",
"theme_color": "#673ab8",
"icons": []
}
67 changes: 67 additions & 0 deletions examples/preact/src/style/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell',
'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
color: rgba(0, 0, 0, 0.87);
}

a {
color: inherit;
text-decoration: none;
}

* {
box-sizing: border-box;
}

button {
padding: 8px 12px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
background-color: #FF5D22;
color: #fff;
border-radius: 4px;
border: none;
}

#preact_root {
height: 100%;
}

.container {
padding: 100px;
display: flex;
flex-direction: column;
justify-content: center;
}

.testArea {
border: 2px solid #FF5D22;
box-shadow: 2px 2px 4px rgba(255, 93, 34, 0.6);
border-radius: 16px;
padding: 20px;
}

.controls {
display: grid;
grid-auto-columns: 150px;
grid-auto-flow: column;
gap: 20px;
justify-items: start;
align-items: center;
}

.actionPoints > li + li {
margin-top: 4px;
}

.visitorId {
font-weight: normal;
}

.data {
overflow: scroll;
}
14 changes: 14 additions & 0 deletions examples/preact/src/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><% preact.title %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<% preact.headEnd %>
</head>
<body>
<% preact.bodyEnd %>
</body>
</html>
61 changes: 61 additions & 0 deletions examples/preact/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"compilerOptions": {
/* Basic Options */
"target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation: */
"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"jsxFactory": "h", /* Specify the JSX factory function to use when targeting react JSX emit, e.g. React.createElement or h. */
"jsxFragmentFactory": "Fragment", /* Specify the JSX fragment factory function to use when targeting react JSX emit with jsxFactory compiler option is specified, e.g. Fragment. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true, /* */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */
},
"include": ["src/**/*", "tests/**/*"]
}

0 comments on commit f280bed

Please sign in to comment.