Skip to content

Commit

Permalink
feat: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Dec 7, 2023
1 parent 66c6d32 commit ffd12e0
Show file tree
Hide file tree
Showing 10 changed files with 578 additions and 27 deletions.
17 changes: 17 additions & 0 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "fastify",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "node --watch ./src/index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fastify": "^4.24.3",
"@faker-js/faker": "^8.3.1",
"@ilteoood/re-flusso": "workspace:^"
}
}
27 changes: 27 additions & 0 deletions examples/fastify/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { faker } from '@faker-js/faker';
import fastify from 'fastify';
import { Readable } from 'node:stream';

const server = fastify();

const itemsToGenerate = 1_000_000;

server.get('/users', (request, reply) => {
return reply
.header('Content-Type', 'application/octet-stream')
.send(
Readable.from(function* () {
for (let i = 0; i < itemsToGenerate; i++) {
yield JSON.stringify({
name: faker.person.firstName(),
surname: faker.person.lastName(),
age: faker.number.int({ min: 0, max: 100 }),
}).concat(i === itemsToGenerate ? '' : '\n');
}
}())
);
});

await server.listen({ port: 3000 }, (error, address) => {
server.log.info(`server listening on ${address}`);
})
16 changes: 16 additions & 0 deletions examples/vanilla-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div style="display:flex; flex-direction: column;">
<ul id="adultList"></ul>
<ul id="childList"></ul>
</div>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/vanilla-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "vanilla-ts",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@ilteoood/re-flusso": "workspace:^"
},
"devDependencies": {
"typescript": "^5.3.3",
"vite": "^5.0.6"
}
}
41 changes: 41 additions & 0 deletions examples/vanilla-ts/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { fetchText } from '@ilteoood/re-flusso/fetchText'
import { filter } from '@ilteoood/re-flusso/filter'
import { map } from '@ilteoood/re-flusso/map'
import { parser } from '@ilteoood/re-flusso/ndJson/parser'
import { pipeline } from '@ilteoood/re-flusso/pipeline'

interface User {
name: string
surname: string
age: number
}

const adultContainer = document.querySelector<HTMLUListElement>('#adultList') as HTMLUListElement
const childContainer = document.querySelector<HTMLUListElement>('#childList') as HTMLUListElement

const response = await fetch('/users')

const [response1, response2] = fetchText(response).tee()

const appendElement = (container: HTMLUListElement) => (user: User) => {
const li = document.createElement('li')
li.textContent = `${user.name} ${user.surname} - ${user.age}`
container.appendChild(li)
}

await Promise.all([
pipeline(
response1,
parser(),
filter(user => user.age >= 18),
map(user => user.name),
new WritableStream({ write: appendElement(adultContainer) })
),
pipeline(
response2,
parser(),
filter(user => user.age < 18),
map(user => user.name),
new WritableStream({ write: appendElement(childContainer) })
)
])
1 change: 1 addition & 0 deletions examples/vanilla-ts/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions examples/vanilla-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
11 changes: 11 additions & 0 deletions examples/vanilla-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vite';

export default defineConfig({
server: {
proxy: {
'/users': {
target: 'http://localhost:3000'
},
},
},
});
Loading

0 comments on commit ffd12e0

Please sign in to comment.