Skip to content

Commit 329a1a4

Browse files
committed
Typescript for js
1 parent 5522390 commit 329a1a4

File tree

11 files changed

+65
-32
lines changed

11 files changed

+65
-32
lines changed

docs/events-api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Events API reference
33
---
44

5-
import {Required, Optional} from '../src/js/api-helpers.js';
5+
import {Required, Optional} from '../src/js/api-helpers.tsx';
66

77
The Plausible Events API can be used to record pageviews and custom events. This is useful when tracking Android or iOS mobile apps, or for server side tracking.
88

@@ -90,10 +90,10 @@ URL of the page where the event was triggered. If the URL contains UTM parameter
9090
The maximum size of the URL, excluding the domain and the query string, is 2,000 characters. Additionally, URLs using the [data URI scheme](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs) are not supported by the API.
9191

9292
:::tip The URL parameter will feel strange in a mobile app but you can manufacture something that looks like a web URL
93-
If you name your mobile app screens like page URLs, Plausible will know how to handle it. So for example, on your login screen you could send something like:
93+
If you name your mobile app screens like page URLs, Plausible will know how to handle it. So for example, on your login screen you could send something like:
9494

95-
event: pageview
96-
url: app://localhost/login
95+
event: pageview
96+
url: app://localhost/login
9797

9898
The pathname (/login) is what will be shown as the page value in the Plausible dashboard
9999
:::

docs/sites-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Site provisioning API reference
33
---
44

5-
import {Required, Optional} from '../src/js/api-helpers.js';
5+
import {Required, Optional} from '../src/js/api-helpers.tsx';
66

77
The Plausible Site provisioning API offers a way to create and manage sites in your Plausible account programmatically. This is useful if you run many websites or if you're [offering a web analytics dashboard powered by Plausible to your customers](https://plausible.io/white-label-web-analytics). The Site API allows these operations:
88

docs/stats-api-v2.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ toc_max_heading_level: 4
44
sidebar_label: Stats API v2 reference (Beta)
55
---
66

7-
import ApiV2Example from '../src/js/apiv2-example.js';
8-
import {Required, Optional} from '../src/js/api-helpers.js';
9-
import Examples, { ExamplesTip } from '../src/js/examples.js';
7+
import ApiV2Example from '../src/js/apiv2-example.tsx';
8+
import {Required, Optional} from '../src/js/api-helpers.tsx';
9+
import Examples, { ExamplesTip } from '../src/js/examples.tsx';
1010
import CodeBlock from '@theme/CodeBlock';
11-
import { SiteContextProvider } from '../src/js/sites.js';
11+
import { SiteContextProvider } from '../src/js/sites.tsx';
1212

1313
:::warning[Beta feature]
1414
Please be advised that this feature is currently in beta phase. It might change or break in the coming months as it is refined.
1515
:::
1616

17-
Plausible Stats API v2 is a powerful single endpoint HTTP interface to **view historical and real-time stats**. In a nutshell, the endpoint `/api/v2/query` accepts both simple and complex stats queries in the POST request body and returns the metrics as JSON.
17+
Plausible Stats API v2 is a powerful single endpoint HTTP interface to **view historical and real-time stats**. In a nutshell, the endpoint `/api/v2/query` accepts both simple and complex stats queries in the POST request body and returns the metrics as JSON.
1818

1919
[Try it now for your own site!](#examples)
2020

docs/stats-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Stats API reference
33
---
44

5-
import {Required, Optional} from '../src/js/api-helpers.js';
5+
import {Required, Optional} from '../src/js/api-helpers.tsx';
66

77
The Plausible Stats API offers a way to retrieve your stats programmatically. It's a read-only interface to present historical and real-time stats only. Take a look at our [events API](events-api.md) if you want to send pageviews or custom events to our backend and our [sites API](sites-api.md) if you want to manage your sites through the API.
88

package-lock.json

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

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"start": "docusaurus start",
77
"build": "docusaurus build",
88
"swizzle": "docusaurus swizzle",
9-
"deploy": "docusaurus deploy"
9+
"deploy": "docusaurus deploy",
10+
"serve": "docusaurus serve"
1011
},
1112
"dependencies": {
1213
"@docusaurus/core": "^3.5.2",
@@ -31,5 +32,11 @@
3132
"last 1 firefox version",
3233
"last 1 safari version"
3334
]
35+
},
36+
"devDependencies": {
37+
"@docusaurus/module-type-aliases": "^3.5.2",
38+
"@docusaurus/tsconfig": "^3.5.2",
39+
"@docusaurus/types": "^3.5.2",
40+
"typescript": "^5.5.4"
3441
}
3542
}

src/js/api-helpers.js renamed to src/js/api-helpers.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22

3-
export const Required = ({children, color}) => (
3+
export const Required = () => (
44
<span
55
style={{
66
color: '#ED8936',
@@ -13,7 +13,7 @@ export const Required = ({children, color}) => (
1313
</span>
1414
);
1515

16-
export const Optional = ({children, color}) => (
16+
export const Optional = () => (
1717
<span
1818
style={{
1919
color: '#718096',

src/js/apiv2-example.js renamed to src/js/apiv2-example.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ export default function ApiV2Example(props) {
3030
setCode(code)
3131
setCanReset(true)
3232
setResponse(null)
33-
})
33+
}, [])
3434

3535
const resetCode = useCallback(() => {
3636
setCode(getCode(props.request, selectedSite))
3737
setCanReset(false)
3838
setResponse(null)
39-
})
39+
}, [])
4040

4141
const copyCode = useCallback(() => {
4242
navigator.clipboard.writeText(code)
43-
})
43+
}, [])
4444

4545
const runCode = useCallback(async () => {
4646
setResponse(null)
@@ -54,7 +54,7 @@ export default function ApiV2Example(props) {
5454
})
5555

5656
setActiveTab("response")
57-
})
57+
}, [])
5858

5959
return (
6060
<Tabs activeTab={activeTab} onTabChange={setActiveTab}>
@@ -121,9 +121,16 @@ export default function ApiV2Example(props) {
121121
)
122122
}
123123

124-
function JsonSchemaEditor({ value, schema, onChange, readOnly }) {
124+
type EditorProps = {
125+
value: string,
126+
schema?: string,
127+
onChange?: (code: string) => void,
128+
readOnly?: boolean
129+
}
130+
131+
function JsonSchemaEditor({ value, schema, onChange, readOnly }: EditorProps) {
125132
const [height, setHeight] = useState(MIN_HEIGHT)
126-
const editorRef = useRef()
133+
const editorRef = useRef<any>()
127134

128135
const onMount = useCallback((editor, monaco) => {
129136
editorRef.current = editor
@@ -140,14 +147,14 @@ function JsonSchemaEditor({ value, schema, onChange, readOnly }) {
140147
}
141148
editor.onDidChangeModelContent(handleEditorChange)
142149
handleEditorChange()
143-
})
150+
}, [schema])
144151

145152
const handleEditorChange = useCallback(() => {
146153
const editAreaHeight = editorRef.current.getContentHeight()
147154
if (editAreaHeight > height && height < MAX_HEIGHT) {
148155
setHeight(editAreaHeight)
149156
}
150-
}, []);
157+
}, [editorRef]);
151158

152159
return (
153160
<Editor
@@ -164,7 +171,6 @@ function JsonSchemaEditor({ value, schema, onChange, readOnly }) {
164171
glyphMargin: false,
165172
wordWrap: 'off',
166173
lineNumbers: 'on',
167-
tabFocusMode: false,
168174
overviewRulerLanes: 0,
169175
hideCursorInOverviewRuler: false,
170176
scrollBeyondLastLine: false,
@@ -198,7 +204,7 @@ async function postQuery(query) {
198204
}
199205
}
200206

201-
function getCode(name, selectedSite) {
207+
function getCode(name: string, selectedSite: string | null) {
202208
let exampleCode = Examples[name] || ""
203209

204210
if (selectedSite) {

src/js/examples.js renamed to src/js/examples.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ export function ExamplesTip() {
4444
)
4545
}
4646

47-
export default Object.fromEntries(examplePaths.map((path) => [path, read(path)]))
47+
export default Object.fromEntries(examplePaths.map((path) => [path, read(path)])) as Record<string, string | undefined>

src/js/sites.js renamed to src/js/sites.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useEffect, useState } from 'react'
1+
import React, { ReactNode, createContext, useEffect, useState } from 'react'
22
import BrowserOnly from '@docusaurus/BrowserOnly'
33

44
export const SiteContext = createContext(null)
@@ -12,7 +12,7 @@ function NestedProvider({ children }) {
1212

1313
const [selectedSite, setSelectedSite] = useState(localStorage.getItem("plausible_docs_site"))
1414

15-
function selectSite(siteDomain) {
15+
function selectSite(siteDomain: string) {
1616
localStorage.setItem("plausible_docs_site", siteDomain)
1717
setSelectedSite(siteDomain)
1818
}
@@ -53,7 +53,7 @@ function NestedProvider({ children }) {
5353
}
5454

5555
// Ensure that content requiring site data is only rendered in the browser
56-
export function SiteContextProvider(props) {
56+
export function SiteContextProvider(props: { children: ReactNode }) {
5757
return (
5858
<BrowserOnly>
5959
{() => (<NestedProvider>{props.children}</NestedProvider>)}

0 commit comments

Comments
 (0)