Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-memes-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/lit-query': minor
---

Add initial @tanstack/lit-query package
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ yarn.lock
build
coverage
dist
dist-cjs
dist-ts

# misc
Expand Down
5 changes: 5 additions & 0 deletions examples/lit/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example

To run this example from the repo root:

- `pnpm --dir examples/lit/basic run dev`
12 changes: 12 additions & 0 deletions examples/lit/basic/basic-query.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lit Query Basic Example</title>
</head>
<body>
<basic-query-root></basic-query-root>
<script type="module" src="/src/basic-query.ts"></script>
</body>
</html>
1 change: 1 addition & 0 deletions examples/lit/basic/config/port.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEMO_PORT: number
22 changes: 22 additions & 0 deletions examples/lit/basic/config/port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const DEFAULT_DEMO_PORT = 4173
const envPort = process.env.DEMO_PORT

function resolvePort() {
if (!envPort) {
return DEFAULT_DEMO_PORT
}

const parsedPort = Number.parseInt(envPort, 10)
const isValidPort =
Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535

Comment on lines +9 to +12
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

In JavaScript, does Number.parseInt("4173abc", 10) return 4173, and what is the recommended strict validation pattern for environment variable ports?

💡 Result:

Yes, Number.parseInt("4173abc", 10) returns 4173. This is because parseInt parses until it encounters a non-digit character in the specified radix (base 10 here), ignoring trailing invalid characters. The recommended strict validation pattern for environment variable ports in Node.js combines parsing with validation to ensure the entire string consists only of valid digits (no trailing junk, decimals, etc.) and the value is in the valid port range (typically 0-65535, often >0 and <=65535 for TCP ports). A common pattern, seen in Express generator's normalizePort and various best practices: function normalizePort(val) { const port = parseInt(val, 10); if (isNaN(port)) return val; // Allow named pipes if (port >= 0) return port; return false; } For stricter integer-only validation (rejecting anything not purely digits): - Test if the string matches /^\d+$/ (optionally allowing leading + or - if needed, but ports are unsigned). - Then parseInt and check range. Example strict port parser: function parseStrictPort(str) { if (!/^\d+$/.test(str)) return NaN; const port = Number.parseInt(str, 10); return (port >= 0 && port <= 65535) ? port : NaN; } Usage: const port = parseStrictPort(process.env.PORT); if (isNaN(port)) { throw new Error('Invalid PORT'); }

Citations:


🏁 Script executed:

cat -n packages/lit-query/examples/lit-query-e2e-app/config/port.js | sed -n '1,20p'

Repository: TanStack/query

Length of output: 649


Use strict numeric validation for DEMO_PORT.

Lines 9-12 currently accept values like "4173abc" because Number.parseInt is prefix-tolerant, silently accepting invalid environment input. Enforce digit-only validation before parsing.

Proposed fix
-  const parsedPort = Number.parseInt(envPort, 10)
-  const isValidPort =
-    Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535
+  const isDigitsOnly = /^\d+$/.test(envPort)
+  const parsedPort = Number(envPort)
+  const isValidPort =
+    isDigitsOnly && Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const parsedPort = Number.parseInt(envPort, 10)
const isValidPort =
Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535
const isDigitsOnly = /^\d+$/.test(envPort)
const parsedPort = Number(envPort)
const isValidPort =
isDigitsOnly && Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/lit-query/examples/lit-query-e2e-app/config/port.js` around lines 9
- 12, The port validation currently uses Number.parseInt on envPort which
accepts prefixes like "4173abc"; update the logic around
envPort/parsedPort/isValidPort to first verify envPort contains only digits
(e.g., /^\d+$/) before parsing, then parse to a number and enforce
Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535 so
non-numeric suffixes are rejected; adjust any error/default branch to rely on
this stricter check.

if (!isValidPort) {
throw new Error(
`Invalid DEMO_PORT "${envPort}". Expected an integer between 1 and 65535.`,
)
}

return parsedPort
}

export const DEMO_PORT = resolvePort()
12 changes: 12 additions & 0 deletions examples/lit/basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TanStack Lit Query E2E Demo</title>
</head>
<body>
<demo-root></demo-root>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/lit/basic/lifecycle-contract.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TanStack Lit Query Lifecycle Contract Fixture</title>
</head>
<body>
<lifecycle-contract-root></lifecycle-contract-root>
<script type="module" src="/src/lifecycle-contract.ts"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/lit/basic/mutation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lit Query Mutation Example</title>
</head>
<body>
<mutation-example-root></mutation-example-root>
<script type="module" src="/src/mutation.ts"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/lit/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@tanstack/query-example-lit-basic",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview"
},
"dependencies": {
"@tanstack/lit-query": "^0.1.0",
"@tanstack/query-core": "^5.99.0",
"lit": "^3.3.1"
},
"devDependencies": {
"typescript": "5.8.3",
"vite": "^6.4.1"
}
}
90 changes: 90 additions & 0 deletions examples/lit/basic/src/basic-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { html, LitElement } from 'lit'
import { QueryClient } from '@tanstack/query-core'
import { QueryClientProvider, createQueryController } from '@tanstack/lit-query'
import {
fetchTodosFromServer,
resetTodoApi,
type TodosResponse,
} from './todoApi'

resetTodoApi()

const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})

class BasicQueryProvider extends QueryClientProvider {
constructor() {
super()
this.client = queryClient
}

protected override createRenderRoot(): HTMLElement | DocumentFragment {
return this
}
}

customElements.define('basic-query-provider', BasicQueryProvider)

class BasicQueryExample extends LitElement {
private readonly todos = createQueryController<TodosResponse, Error>(this, {
queryKey: ['todos'],
queryFn: fetchTodosFromServer,
})

protected override createRenderRoot(): HTMLElement | DocumentFragment {
return this
}

render() {
const query = this.todos()
return html`
<main>
<h1>Basic Query Example</h1>
<p data-testid="basic-query-status">
Status: <strong>${query.status}</strong>
</p>
<button
data-testid="basic-refetch"
@click=${() => this.todos.refetch()}
>
Refetch
</button>

${query.isPending
? html`<p data-testid="basic-loading">Loading...</p>`
: null}
${query.isError
? html`<p data-testid="basic-error">Error: ${String(query.error)}</p>`
: null}

<ul data-testid="basic-todo-list">
${(query.data?.items ?? []).map(
(todo) =>
html`<li data-testid="basic-todo-item">${todo.title}</li>`,
)}
</ul>
</main>
`
}
}

customElements.define('basic-query-example', BasicQueryExample)

class BasicQueryRoot extends LitElement {
protected override createRenderRoot(): HTMLElement | DocumentFragment {
return this
}

render() {
return html`
<basic-query-provider>
<basic-query-example></basic-query-example>
</basic-query-provider>
`
}
}

customElements.define('basic-query-root', BasicQueryRoot)
Loading