https://github.com/virtualstate/listen
Fetch listener
https://github.com/virtualstate/listen
Last synced: 2 months ago
JSON representation
Fetch listener
- Host: GitHub
- URL: https://github.com/virtualstate/listen
- Owner: virtualstate
- License: mit
- Created: 2022-09-04T04:39:28.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-14T09:14:26.000Z (over 3 years ago)
- Last Synced: 2025-09-13T19:43:55.686Z (10 months ago)
- Language: TypeScript
- Homepage:
- Size: 487 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE-OF-CONDUCT.md
Awesome Lists containing this project
README
# `@virtualstate/listen`
Fetch listener
[//]: # (badges)
### Support
  
### Test Coverage
   
[//]: # (badges)
# Usage
```typescript
import { listen } from "@virtualstate/listen";
const { url, close } = await listen(
event => event.respondWith(new Response("Hello!"))
);
console.log(`Listening on ${url}`);
const response = await fetch(url);
const text = await response.text();
console.log(text);
if (text !== "Hello!") throw new Error("Expected Hello!");
await close();
```
## JSX
```jsx
import { listen, toResponse, toAsyncString, Fetch } from "@virtualstate/listen";
import { h, descendants, name, properties } from "@virtualstate/focus";
async function *App({ request }) {
if (request.method === "POST") {
const body = JSON.parse(
await toAsyncString(request)
);
yield
}
}
const { url, close } = await listen(
event => event.respondWith(
toResponse()
)
);
const random = Math.random()
const {
echo: [echo]
} = descendants(
).group(name);
const body = properties(await echo);
console.log(body);
if (body.random !== random) throw new Error("Expected body to contain random")
```