Skip to content

Commit a6ca77c

Browse files
committed
add very basic analytics for script downloads
1 parent 309e464 commit a6ca77c

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ node_modules
66
.vercel_build_output
77
cli/dist/prebuild.mjs
88
cli/dist
9-
.svelte-kit
9+
.svelte-kit
10+
11+
.env

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@sveltejs/kit": "^1.0.0-next.138",
2323
"@types/fs-extra": "^9.0.9",
2424
"@types/js-search": "^1.4.0",
25+
"@types/redis": "^2.8.31",
2526
"autoprefixer": "^10.2.5",
2627
"esbuild": "^0.12.0",
2728
"eslint": "^7.25.0",
@@ -43,6 +44,7 @@
4344
"@nick-mazuk/ui-svelte": "0.29.6",
4445
"@sveltejs/adapter-static": "^1.0.0-next.13",
4546
"date-fns": "^2.23.0",
46-
"js-search": "^2.0.0"
47+
"js-search": "^2.0.0",
48+
"redis": "^3.1.2"
4749
}
4850
}

pnpm-lock.yaml

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/components/script.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
1717
const handleDownload = async () => {
1818
isDownloading = true
19+
fetch('/api/download-script', {
20+
method: 'POST',
21+
body: JSON.stringify({
22+
author: data.author.name,
23+
scriptName: data.fileName,
24+
}),
25+
})
1926
const response = await fetch(
2027
`https://raw.githubusercontent.com/Nick-Mazuk/jw-lua-scripts/master/dist/${data.fileName}`
2128
)

src/routes/api/download-script.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { promisify } from 'util'
2+
3+
import type { RequestHandler } from '@sveltejs/kit'
4+
import { createClient } from 'redis'
5+
6+
import { dev } from '$app/env'
7+
8+
export const post: RequestHandler = async ({ rawBody }) => {
9+
const body = JSON.parse(rawBody.toString())
10+
11+
const { scriptName, author } = body
12+
13+
if (typeof scriptName !== 'string' || typeof author !== 'string')
14+
return { status: 400, body: {} }
15+
16+
if (dev) return { body: {} }
17+
18+
const client = createClient({
19+
host: import.meta.env.VITE_REDIS_ENDPOINT,
20+
port: import.meta.env.VITE_REDIS_PORT,
21+
password: import.meta.env.VITE_REDIS_PASSWORD,
22+
tls: {},
23+
})
24+
25+
const increment = promisify(client.incr).bind(client)
26+
27+
await Promise.all([
28+
increment(`downloads:${scriptName}`),
29+
increment(`downloads:${author}`),
30+
increment('downloads'),
31+
])
32+
33+
client.quit()
34+
35+
return { body: {} }
36+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"moduleResolution": "node",
44
"target": "es2018",
5+
"module": "ESNext",
56
"importsNotUsedAsValues": "error",
67
"isolatedModules": true,
78
"sourceMap": true,

types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
declare module 'stork-search'
1+
interface ImportMetaEnv {
2+
VITE_REDIS_ENDPOINT: string
3+
VITE_REDIS_PORT: number
4+
VITE_REDIS_PASSWORD: string
5+
}

0 commit comments

Comments
 (0)