git.iko.soy/rowtype-yoga/purescript-yoga-next-fastify
on main help sign in
6 files changed+181−12
since splitsnapshots
unifiedsplit
CLAUDE.md+111 README.md+15 spago.lock+26−4 spago.yaml+7−2 src/Yoga/Next/Fastify.purs+8−6 test/Test/Main.purs+14
CLAUDE.mdnew file+111
@@ -0,0 +1,111 @@
1+---
2+description: Use Bun instead of Node.js, npm, pnpm, or vite.
3+globs: "*.ts, *.tsx, *.html, *.css, *.js, *.jsx, package.json"
4+alwaysApply: false
5+---
6+
7+Default to using Bun instead of Node.js.
8+
9+- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
10+- Use `bun test` instead of `jest` or `vitest`
11+- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
12+- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
13+- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
14+- Bun automatically loads .env, so don't use dotenv.
15+
16+## APIs
17+
18+- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
19+- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
20+- `Bun.redis` for Redis. Don't use `ioredis`.
21+- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
22+- `WebSocket` is built-in. Don't use `ws`.
23+- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
24+- Bun.$`ls` instead of execa.
25+
26+## Testing
27+
28+Use `bun test` to run tests.
29+
30+```ts#index.test.ts
31+import { test, expect } from "bun:test";
32+
33+test("hello world", () => {
34+ expect(1).toBe(1);
35+});
36+```
37+
38+## Frontend
39+
40+Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
41+
42+Server:
43+
44+```ts#index.ts
45+import index from "./index.html"
46+
47+Bun.serve({
48+ routes: {
49+ "/": index,
50+ "/api/users/:id": {
51+ GET: (req) => {
52+ return new Response(JSON.stringify({ id: req.params.id }));
53+ },
54+ },
55+ },
56+ // optional websocket support
57+ websocket: {
58+ open: (ws) => {
59+ ws.send("Hello, world!");
60+ },
61+ message: (ws, message) => {
62+ ws.send(message);
63+ },
64+ close: (ws) => {
65+ // handle close
66+ }
67+ },
68+ development: {
69+ hmr: true,
70+ console: true,
71+ }
72+})
73+```
74+
75+HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
76+
77+```html#index.html
78+<html>
79+ <body>
80+ <h1>Hello, world!</h1>
81+ <script type="module" src="./frontend.tsx"></script>
82+ </body>
83+</html>
84+```
85+
86+With the following `frontend.tsx`:
87+
88+```tsx#frontend.tsx
89+import React from "react";
90+
91+// import .css files directly and it works
92+import './index.css';
93+
94+import { createRoot } from "react-dom/client";
95+
96+const root = createRoot(document.body);
97+
98+export default function Frontend() {
99+ return <h1>Hello, world!</h1>;
100+}
101+
102+root.render(<Frontend />);
103+```
104+
105+Then, run index.ts
106+
107+```sh
108+bun --hot ./index.ts
109+```
110+
111+For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.
README.mdnew file+15
@@ -0,0 +1,15 @@
1+# purescript-yoga-next-fastify
2+
3+To install dependencies:
4+
5+```bash
6+bun install
7+```
8+
9+To run:
10+
11+```bash
12+bun run index.ts
13+```
14+
15+This project was created using `bun init` in bun v1.2.21. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
spago.lock+26−4
@@ -12,7 +12,7 @@
1212 "effect": ">=4.0.0 <5.0.0"
1313 },
1414 {
15− "http-methods": ">=6.0.0 <7.0.0"
15+ "http-methods": ">=6.1.0 <7.0.0"
1616 },
1717 {
1818 "js-promise": ">=1.0.0 <2.0.0"
@@ -29,7 +29,9 @@
2929 ]
3030 },
3131 "test": {
32− "dependencies": []
32+ "dependencies": [
33+ "assert"
34+ ]
3335 }
3436 }
3537 },
@@ -626,6 +628,7 @@
626628 }
627629 },
628630 "extra_packages": {
631+ "http-methods": "6.1.0",
629632 "yoga-fastify": "0.4.0",
630633 "yoga-http-api": "0.2.1",
631634 "yoga-options": "0.1.0"
@@ -674,6 +677,16 @@
674677 "unsafe-coerce"
675678 ]
676679 },
680+ "assert": {
681+ "type": "registry",
682+ "version": "6.0.0",
683+ "integrity": "sha256-hCZ1J8/71nQiRwsSV2j7iicppScOegBFZrLI6sPf9F8=",
684+ "dependencies": [
685+ "console",
686+ "effect",
687+ "prelude"
688+ ]
689+ },
677690 "bifunctors": {
678691 "type": "registry",
679692 "version": "6.1.0",
@@ -700,6 +713,15 @@
700713 "unfoldable"
701714 ]
702715 },
716+ "console": {
717+ "type": "registry",
718+ "version": "6.1.0",
719+ "integrity": "sha256-3T5PPz1mATZmV0C4GzkCkpLlCa5xG7u8ZNgnLrrrI1Q=",
720+ "dependencies": [
721+ "effect",
722+ "prelude"
723+ ]
724+ },
703725 "const": {
704726 "type": "registry",
705727 "version": "6.0.0",
@@ -943,8 +965,8 @@
943965 },
944966 "http-methods": {
945967 "type": "registry",
946− "version": "6.0.0",
947− "integrity": "sha256-S0UjrJSgwJxEsFHM/pX77dM8V4HfvvDqdHPckQrtxMs=",
968+ "version": "6.1.0",
969+ "integrity": "sha256-WbjEmCv6rysY/aKwlrcspyzShi5bIQy+AFpDzPMnSP4=",
948970 "dependencies": [
949971 "either",
950972 "prelude",
spago.yaml+7−2
@@ -1,7 +1,7 @@
11package:
22 name: yoga-next-fastify
33 publish:
4− version: 0.1.0
4+ version: 0.2.0
55 license: MIT
66 location:
77 githubOwner: rowtype-yoga
@@ -11,13 +11,18 @@ package:
1111 - effect: ">=4.0.0 <5.0.0"
1212 - js-promise: ">=1.0.0 <2.0.0"
1313 - js-promise-aff: ">=1.0.0 <2.0.0"
14− - http-methods: ">=6.0.0 <7.0.0"
14+ - http-methods: ">=6.1.0 <7.0.0"
1515 - prelude: ">=6.0.2 <7.0.0"
1616 - yoga-fastify: ">=0.3.0 <1.0.0"
17+ test:
18+ main: Test.Main
19+ dependencies:
20+ - assert
1721workspace:
1822 packageSet:
1923 registry: 73.3.0
2024 extraPackages:
25+ http-methods: "6.1.0"
2126 yoga-fastify: "0.4.0"
2227 yoga-http-api: "0.2.1"
2328 yoga-options: "0.1.0"
src/Yoga/Next/Fastify.purs+8−6
@@ -1,5 +1,6 @@
11module Yoga.Next.Fastify
2− ( NextApp
2+ ( allMethods
3+ , NextApp
34 , createNextApp
45 , nextRequestHandler
56 , registerNextHandler
@@ -28,12 +29,13 @@ foreign import nextRequestHandlerImpl :: EffectFn1 NextApp (HttpRequest -> HttpR
2829nextRequestHandler :: NextApp -> Effect (HttpRequest -> HttpResponse -> Effect Unit)
2930nextRequestHandler = runEffectFn1 nextRequestHandlerImpl
3031
32+allMethods :: Array String
33+allMethods = show <$>
34+ [ Method.GET, Method.POST, Method.PUT, Method.DELETE
35+ , Method.PATCH, Method.HEAD, Method.OPTIONS, Method.QUERY
36+ ]
37+
3138registerNextHandler :: NextApp -> Fastify -> Effect Unit
3239registerNextHandler app server = do
3340 handler <- nextRequestHandler app
3441 F.rawRoute allMethods handler server
35− where
36− allMethods = show <$>
37− [ Method.GET, Method.POST, Method.PUT, Method.DELETE
38− , Method.PATCH, Method.HEAD, Method.OPTIONS
39− ]
test/Test/Main.pursnew file+14
@@ -0,0 +1,14 @@
1+module Test.Main where
2+
3+import Effect (Effect)
4+import Prelude (Unit)
5+
6+import Test.Assert (assertEqual)
7+import Yoga.Next.Fastify (allMethods)
8+
9+main :: Effect Unit
10+main =
11+ assertEqual
12+ { actual: allMethods
13+ , expected: [ "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "QUERY" ]
14+ }