create-marcapony-app 0.4.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/template/base/_package.json +2 -2
- package/template/base/src/server/marcapony.ts +14 -0
- package/template/base/src/ui/main.tsx +2 -0
- package/template/base/src/ui/site-theme.ts +71 -0
- package/template/base/src/ui/styles.css +3 -2
- package/template/blank/README.md +11 -5
- package/template/blank/src/server/index.ts +7 -1
- package/template/booking/src/server/index.ts +2 -0
- package/template/character/src/server/index.ts +7 -1
- package/template/game/README.md +71 -0
- package/template/game/marcapony.json +40 -0
- package/template/game/src/server/index.ts +62 -0
- package/template/game/src/ui/App.tsx +231 -0
- package/template/shop/src/server/index.ts +7 -1
- package/template/team-tool/src/server/index.ts +2 -0
package/README.md
CHANGED
|
@@ -31,6 +31,7 @@ up and build on it.
|
|
|
31
31
|
| `shop` | Products, a cart and a checkout that sends the buyer to pay | Your site |
|
|
32
32
|
| `team-tool` | Your team approves, rejects and completes the day's reservations | Dashboard |
|
|
33
33
|
| `character` | A character from your business that chats with visitors | Your site |
|
|
34
|
+
| `game` | A game anyone can play; signing in keeps the best score | Its own address |
|
|
34
35
|
|
|
35
36
|
Each template's `README.md` says which placement, data access, scopes and
|
|
36
37
|
permissions to choose when you create the app under **My Apps** in the
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,9 @@ export declare const TEMPLATES: readonly [{
|
|
|
14
14
|
}, {
|
|
15
15
|
readonly name: "character";
|
|
16
16
|
readonly summary: "A character from your business that chats with visitors";
|
|
17
|
+
}, {
|
|
18
|
+
readonly name: "game";
|
|
19
|
+
readonly summary: "A game anyone can play; signing in keeps the best score";
|
|
17
20
|
}];
|
|
18
21
|
export type TemplateName = (typeof TEMPLATES)[number]['name'];
|
|
19
22
|
export declare const DEFAULT_TEMPLATE: TemplateName;
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export const TEMPLATES = [
|
|
|
19
19
|
{ name: 'shop', summary: 'Products, a cart and checkout with a payment link' },
|
|
20
20
|
{ name: 'team-tool', summary: "Your team approves and updates today's reservations" },
|
|
21
21
|
{ name: 'character', summary: 'A character from your business that chats with visitors' },
|
|
22
|
+
{ name: 'game', summary: 'A game anyone can play; signing in keeps the best score' },
|
|
22
23
|
];
|
|
23
24
|
export const DEFAULT_TEMPLATE = 'blank';
|
|
24
25
|
const BASE_DIR = 'base';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-marcapony-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Scaffold a Marcapony app \u2014 a Worker backed by the Marcapony API, ready for `marcapony app link` and `marcapony app deploy`",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"hono": "^4.13.0",
|
|
17
|
-
"marcapony-sdk": "^0.
|
|
17
|
+
"marcapony-sdk": "^0.15.0",
|
|
18
18
|
"react": "^19.3.0",
|
|
19
19
|
"react-dom": "^19.3.0"
|
|
20
20
|
},
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"@types/react-dom": "^19.3.0",
|
|
25
25
|
"@vitejs/plugin-react": "^5.2.0",
|
|
26
26
|
"esbuild": "^0.28.0",
|
|
27
|
-
"marcapony": "^0.
|
|
27
|
+
"marcapony": "^0.17.0",
|
|
28
28
|
"typescript": "^5.9.0",
|
|
29
29
|
"vite": "^7.3.0"
|
|
30
30
|
}
|
|
@@ -53,6 +53,20 @@ export async function identityOf(
|
|
|
53
53
|
return mp.identity(request)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* The website's theme for the interface (`/api/site-theme`), read with the app
|
|
58
|
+
* key so the browser never holds it. Null when the key lacks `site:read` or
|
|
59
|
+
* there is no key (local dev without `bun run link`): the interface then keeps
|
|
60
|
+
* its default look.
|
|
61
|
+
*/
|
|
62
|
+
export async function siteThemeResponse(env: MarcaponyBindings): Promise<Response> {
|
|
63
|
+
try {
|
|
64
|
+
return Response.json(await marcaponyFromEnv(env).site.theme())
|
|
65
|
+
} catch {
|
|
66
|
+
return Response.json(null)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
56
70
|
/**
|
|
57
71
|
* Turns a failed call into a JSON answer for the browser. A Marcapony error
|
|
58
72
|
* keeps its status (401 not signed in, 403 not allowed, 404, 422 invalid
|
|
@@ -2,9 +2,11 @@ import { StrictMode } from 'react'
|
|
|
2
2
|
import { createRoot } from 'react-dom/client'
|
|
3
3
|
import { App } from './App'
|
|
4
4
|
import { followPortalTheme } from './portal-theme'
|
|
5
|
+
import { followSiteTheme } from './site-theme'
|
|
5
6
|
import './styles.css'
|
|
6
7
|
|
|
7
8
|
followPortalTheme()
|
|
9
|
+
followSiteTheme()
|
|
8
10
|
|
|
9
11
|
const root = document.getElementById('root')
|
|
10
12
|
if (!root) throw new Error('#root is missing from index.html')
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { SiteTheme } from 'marcapony-sdk'
|
|
2
|
+
import { api } from './api'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Follow the business's website theme when the app is NOT inside the customer
|
|
6
|
+
* portal (inside it, portal-theme.ts applies the portal's theme instead).
|
|
7
|
+
*
|
|
8
|
+
* The server reads the theme with the app key (`/api/site-theme`, which needs
|
|
9
|
+
* the `site:read` scope) and this writes it onto the same CSS variables
|
|
10
|
+
* `styles.css` reads. Without the scope, or locally without a key, the answer
|
|
11
|
+
* is null and the defaults in `styles.css` stay. Every value is checked before
|
|
12
|
+
* it reaches a style.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const COLORS: ReadonlyArray<readonly [keyof SiteTheme['light'], string]> = [
|
|
16
|
+
['background', '--background'],
|
|
17
|
+
['foreground', '--foreground'],
|
|
18
|
+
['card', '--card'],
|
|
19
|
+
['primary', '--primary'],
|
|
20
|
+
['primaryForeground', '--primary-foreground'],
|
|
21
|
+
['muted', '--muted'],
|
|
22
|
+
['mutedForeground', '--muted-foreground'],
|
|
23
|
+
['destructive', '--destructive'],
|
|
24
|
+
['border', '--border'],
|
|
25
|
+
['ring', '--ring'],
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
const RGB = /^rgb\(\d{1,3} \d{1,3} \d{1,3}\)$/
|
|
29
|
+
const RADIUS = /^\d+(\.\d+)?(rem|px|em)$/
|
|
30
|
+
const FAMILY = /^[\w\s-]{1,80}$/
|
|
31
|
+
const GOOGLE_FONTS = 'https://fonts.googleapis.com/css2?'
|
|
32
|
+
|
|
33
|
+
export function applySiteTheme(
|
|
34
|
+
theme: SiteTheme,
|
|
35
|
+
root: HTMLElement = document.documentElement,
|
|
36
|
+
): void {
|
|
37
|
+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
38
|
+
// A theme with a dark palette follows the visitor; one without keeps its own look.
|
|
39
|
+
const dark = theme.dark ? prefersDark : theme.defaultAppearance === 'dark'
|
|
40
|
+
const palette = (dark ? theme.dark : null) ?? theme.light
|
|
41
|
+
|
|
42
|
+
for (const [key, variable] of COLORS) {
|
|
43
|
+
const value = palette[key]
|
|
44
|
+
if (typeof value === 'string' && RGB.test(value)) root.style.setProperty(variable, value)
|
|
45
|
+
}
|
|
46
|
+
if (theme.radius && RADIUS.test(theme.radius)) root.style.setProperty('--radius', theme.radius)
|
|
47
|
+
|
|
48
|
+
const body = theme.fonts.body
|
|
49
|
+
if (body && FAMILY.test(body)) {
|
|
50
|
+
const url = theme.fontStylesheetUrl
|
|
51
|
+
if (url?.startsWith(GOOGLE_FONTS)) {
|
|
52
|
+
const link = document.createElement('link')
|
|
53
|
+
link.rel = 'stylesheet'
|
|
54
|
+
link.href = url
|
|
55
|
+
document.head.append(link)
|
|
56
|
+
}
|
|
57
|
+
root.style.setProperty('--font-sans', `"${body}", ui-sans-serif, system-ui, sans-serif`)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
root.dataset.appearance = dark ? 'dark' : 'light'
|
|
61
|
+
root.style.colorScheme = dark ? 'dark' : 'light'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function followSiteTheme(): void {
|
|
65
|
+
if (window.parent !== window) return
|
|
66
|
+
api<SiteTheme | null>('/api/site-theme')
|
|
67
|
+
.then((theme) => {
|
|
68
|
+
if (theme) applySiteTheme(theme)
|
|
69
|
+
})
|
|
70
|
+
.catch(() => {})
|
|
71
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
:root {
|
|
2
2
|
color-scheme: light dark;
|
|
3
3
|
/* Theme tokens. Inside the customer portal, portal-theme.ts replaces them
|
|
4
|
-
with the portal's own colours, radius and font (light or dark)
|
|
5
|
-
|
|
4
|
+
with the portal's own colours, radius and font (light or dark); outside
|
|
5
|
+
it, site-theme.ts applies the website's theme. Style with these
|
|
6
|
+
variables, never fixed colours, so the app follows both. */
|
|
6
7
|
--background: #ffffff;
|
|
7
8
|
--foreground: #18181b;
|
|
8
9
|
--card: #ffffff;
|
package/template/blank/README.md
CHANGED
|
@@ -9,15 +9,21 @@ Create the app under **My Apps** with:
|
|
|
9
9
|
|
|
10
10
|
- **Placement:** public (`placement` is not set in `marcapony.json`)
|
|
11
11
|
- **Data access:** visitor
|
|
12
|
-
- **Scopes:** `products:read`
|
|
12
|
+
- **Scopes:** `products:read`, `site:read`
|
|
13
13
|
- **Permissions:** none
|
|
14
14
|
|
|
15
15
|
## Routes
|
|
16
16
|
|
|
17
|
-
| Route
|
|
18
|
-
|
|
|
19
|
-
| `GET /api/health`
|
|
20
|
-
| `GET /api/
|
|
17
|
+
| Route | What it does |
|
|
18
|
+
| --------------------- | ---------------------------------------------- |
|
|
19
|
+
| `GET /api/health` | Answers `{ "ok": true }` |
|
|
20
|
+
| `GET /api/site-theme` | The website's theme, or null without `site:read` |
|
|
21
|
+
| `GET /api/products` | Up to 12 published products of the account |
|
|
22
|
+
|
|
23
|
+
On the site, the app takes the website's colours, radius and font:
|
|
24
|
+
`src/ui/site-theme.ts` reads `/api/site-theme` and writes them onto the CSS
|
|
25
|
+
variables `src/ui/styles.css` uses. Inside the customer portal the portal's
|
|
26
|
+
own theme (`src/ui/portal-theme.ts`) applies instead.
|
|
21
27
|
|
|
22
28
|
## What to change first
|
|
23
29
|
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Hono } from 'hono'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
errorResponse,
|
|
4
|
+
marcaponyFromEnv,
|
|
5
|
+
siteThemeResponse,
|
|
6
|
+
type MarcaponyBindings,
|
|
7
|
+
} from './marcapony'
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
10
|
* The app's server. Only `/api/*` reaches this worker — everything else is
|
|
@@ -9,6 +14,7 @@ import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marca
|
|
|
9
14
|
const app = new Hono<{ Bindings: MarcaponyBindings }>()
|
|
10
15
|
|
|
11
16
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
17
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
12
18
|
|
|
13
19
|
/**
|
|
14
20
|
* Example read. The app key never leaves the server: the browser calls this
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
errorResponse,
|
|
5
5
|
identityOf,
|
|
6
6
|
marcaponyFromEnv,
|
|
7
|
+
siteThemeResponse,
|
|
7
8
|
type Marcapony,
|
|
8
9
|
type MarcaponyBindings,
|
|
9
10
|
} from './marcapony'
|
|
@@ -39,6 +40,7 @@ async function customerOf(
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
43
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
42
44
|
|
|
43
45
|
app.get('/api/me', async (c) => {
|
|
44
46
|
const mp = marcaponyFromEnv(c.env)
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Hono } from 'hono'
|
|
2
2
|
import { personasHandler } from 'marcapony-sdk'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
errorResponse,
|
|
5
|
+
marcaponyFromEnv,
|
|
6
|
+
siteThemeResponse,
|
|
7
|
+
type MarcaponyBindings,
|
|
8
|
+
} from './marcapony'
|
|
4
9
|
|
|
5
10
|
/**
|
|
6
11
|
* A character from the business that chats with visitors. The browser talks
|
|
@@ -45,6 +50,7 @@ function personaSlug(env: Bindings): string {
|
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
53
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
48
54
|
|
|
49
55
|
/** The character the interface shows: name, greeting and starter questions. */
|
|
50
56
|
app.get('/api/character', async (c) => {
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Marcapony app: game
|
|
2
|
+
|
|
3
|
+
A small game anyone can play. A visitor who signs in keeps their best score.
|
|
4
|
+
It shows the two ways to sign a visitor in from an app: an e-mail code on the
|
|
5
|
+
app's own screen, and the portal's full sign-in page.
|
|
6
|
+
|
|
7
|
+
## Set up in the dashboard
|
|
8
|
+
|
|
9
|
+
Create the app under **My Apps** with:
|
|
10
|
+
|
|
11
|
+
- **Placement:** customer portal (`"placement": "portal"`)
|
|
12
|
+
- **Data access:** customer (`"dataAccess": "customer"`)
|
|
13
|
+
- **Scopes:** none
|
|
14
|
+
- **Permissions** (declared in `marcapony.json`; choose who gets them):
|
|
15
|
+
- `keep-score`, "Save my best score": `records.own.read`, `records.own.write`.
|
|
16
|
+
Give it to every customer, or the score is not saved.
|
|
17
|
+
|
|
18
|
+
`"signIn": "optional"` in `marcapony.json` is what lets a visitor open the app
|
|
19
|
+
without an account. It needs `"runtime": "2026-12"` or newer and the app's own
|
|
20
|
+
address (the one `bun run deploy` prints).
|
|
21
|
+
|
|
22
|
+
## Routes
|
|
23
|
+
|
|
24
|
+
| Route | What it does |
|
|
25
|
+
| ---------------- | ---------------------------------------------------------------- |
|
|
26
|
+
| `GET /api/health` | Answers `{ "ok": true }` |
|
|
27
|
+
| `GET /api/score` | The signed-in customer's best score, or `{ "best": null }` |
|
|
28
|
+
| `PUT /api/score` | Saves `{ score }` when it beats the best; 401 if nobody is signed in |
|
|
29
|
+
|
|
30
|
+
Sign-in itself has no route here. The interface calls
|
|
31
|
+
`marcapony-sdk/browser`, which talks to routes Marcapony serves on the app's
|
|
32
|
+
address under `/_marcapony/`:
|
|
33
|
+
|
|
34
|
+
| Call | What it does |
|
|
35
|
+
| ------------------------------------- | ------------------------------------------------------ |
|
|
36
|
+
| `auth.me()` | Who is signed in, and whether they may use the app |
|
|
37
|
+
| `auth.options()` | Whether a code can be sent, and how new accounts start |
|
|
38
|
+
| `auth.sendCode(email)` | E-mails a six-digit code and a sign-in link |
|
|
39
|
+
| `auth.verifyCode(email, code)` | Signs the visitor in |
|
|
40
|
+
| `auth.signInUrl({ returnTo })` | The full sign-in page: password, Google, registration |
|
|
41
|
+
| `auth.signOut()` | Signs the visitor out of this app |
|
|
42
|
+
|
|
43
|
+
The server never sees a password, a code or the session. It asks
|
|
44
|
+
`identityOf(...)` who the request is from, and the platform limits
|
|
45
|
+
`me.records` to that customer.
|
|
46
|
+
|
|
47
|
+
## What to change first
|
|
48
|
+
|
|
49
|
+
1. The game in `src/ui/App.tsx`. The browser reports the score here, so a
|
|
50
|
+
player can cheat; a game that matters keeps the round on the server.
|
|
51
|
+
2. `signInScreen` in `marcapony.json`: the title, text, logo and background of
|
|
52
|
+
the sign-in page for visitors who come from this app. Put the images in
|
|
53
|
+
`public/` and name them by path, such as `"/logo.svg"`.
|
|
54
|
+
3. The menu title in `marcapony.json` (`portal.title`).
|
|
55
|
+
4. Test sign-in on the deployed app: locally there are no sign-in routes and
|
|
56
|
+
nobody is signed in, so the game is played without an account.
|
|
57
|
+
|
|
58
|
+
## Develop and publish
|
|
59
|
+
|
|
60
|
+
Requires [Bun](https://bun.sh).
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
bun install
|
|
64
|
+
bun run link # paste the connection code from the app's page; writes .env.local and AGENTS.md
|
|
65
|
+
bun run dev # interface and /api routes on your machine
|
|
66
|
+
bun run deploy # build and publish; prints the address
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`.env.local` holds the app key. Never commit it, and never send it to the
|
|
70
|
+
browser: the interface calls this app's own `/api` routes, and only those talk
|
|
71
|
+
to Marcapony.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/marcapony/manifest.schema.json",
|
|
3
|
+
"manifest": 2,
|
|
4
|
+
"build": "bun run build",
|
|
5
|
+
"server": "dist/server/index.js",
|
|
6
|
+
"public": {
|
|
7
|
+
"dir": "dist/client"
|
|
8
|
+
},
|
|
9
|
+
"routes": {
|
|
10
|
+
"server": ["/api/*"],
|
|
11
|
+
"notFound": "spa"
|
|
12
|
+
},
|
|
13
|
+
"runtime": "2026-12",
|
|
14
|
+
"ignore": ["coverage/", "*.log"],
|
|
15
|
+
"placement": "portal",
|
|
16
|
+
"dataAccess": "customer",
|
|
17
|
+
"signIn": "optional",
|
|
18
|
+
"signInScreen": {
|
|
19
|
+
"title": "Keep your best score",
|
|
20
|
+
"text": "Sign in and your best round is saved for next time."
|
|
21
|
+
},
|
|
22
|
+
"permissions": [
|
|
23
|
+
{
|
|
24
|
+
"id": "keep-score",
|
|
25
|
+
"label": "Save my best score",
|
|
26
|
+
"grants": ["records.own.read", "records.own.write"]
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"collections": [
|
|
30
|
+
{
|
|
31
|
+
"name": "scores",
|
|
32
|
+
"read": "own",
|
|
33
|
+
"write": "own",
|
|
34
|
+
"maxRows": 10
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"portal": {
|
|
38
|
+
"title": "Game"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Hono } from 'hono'
|
|
2
|
+
import {
|
|
3
|
+
errorResponse,
|
|
4
|
+
identityOf,
|
|
5
|
+
marcaponyFromEnv,
|
|
6
|
+
siteThemeResponse,
|
|
7
|
+
type MarcaponyBindings,
|
|
8
|
+
} from './marcapony'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A game anyone can play; a signed-in customer keeps their best score. Only
|
|
12
|
+
* `/api/*` reaches this worker; everything else is served from `dist/client`
|
|
13
|
+
* (see `run_worker_first` in marcapony.json).
|
|
14
|
+
*
|
|
15
|
+
* `"signIn": "optional"` in marcapony.json means a visitor reaches the app
|
|
16
|
+
* without signing in, so `identityOf` answers null for them. Signing in happens
|
|
17
|
+
* in the browser (see `src/ui/App.tsx`); this server only ever asks who the
|
|
18
|
+
* request is from, and never sees a password, a code or the session.
|
|
19
|
+
*/
|
|
20
|
+
const app = new Hono<{ Bindings: MarcaponyBindings }>()
|
|
21
|
+
|
|
22
|
+
// A type alias, not an interface: a record's data must be assignable to plain JSON.
|
|
23
|
+
type Score = { score: number }
|
|
24
|
+
|
|
25
|
+
const BEST = 'best'
|
|
26
|
+
const MAX_SCORE = 500
|
|
27
|
+
|
|
28
|
+
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
29
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
30
|
+
|
|
31
|
+
app.get('/api/score', async (c) => {
|
|
32
|
+
const mp = marcaponyFromEnv(c.env)
|
|
33
|
+
const who = await identityOf(mp, c.env, c.req.raw)
|
|
34
|
+
if (!who || who.kind !== 'customer') return c.json({ best: null })
|
|
35
|
+
const record = await mp.forRequest(c.req.raw).me.records<Score>('scores').get(BEST)
|
|
36
|
+
return c.json({ best: record?.data.score ?? null })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
app.put('/api/score', async (c) => {
|
|
40
|
+
const mp = marcaponyFromEnv(c.env)
|
|
41
|
+
const who = await identityOf(mp, c.env, c.req.raw)
|
|
42
|
+
if (!who) return c.json({ error: 'Sign in to keep your score.' }, 401)
|
|
43
|
+
if (who.kind !== 'customer') return c.json({ error: 'Scores are kept for customers.' }, 403)
|
|
44
|
+
|
|
45
|
+
const body = (await c.req.json().catch(() => ({}))) as { score?: unknown }
|
|
46
|
+
const { score } = body
|
|
47
|
+
if (typeof score !== 'number' || !Number.isInteger(score) || score < 0 || score > MAX_SCORE) {
|
|
48
|
+
return c.json({ error: 'score is a whole number from 0 to 500.' }, 400)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// The browser reports the score, so a player can cheat. A game that matters
|
|
52
|
+
// keeps the round on the server and counts there.
|
|
53
|
+
const scores = mp.forRequest(c.req.raw).me.records<Score>('scores')
|
|
54
|
+
const current = await scores.get(BEST)
|
|
55
|
+
if (current && current.data.score >= score) return c.json({ best: current.data.score })
|
|
56
|
+
const saved = await scores.put(BEST, { score })
|
|
57
|
+
return c.json({ best: saved.data.score })
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
app.onError((err) => errorResponse(err))
|
|
61
|
+
|
|
62
|
+
export default app
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
2
|
+
import { auth, type SignedInState, type SignInOptions } from 'marcapony-sdk/browser'
|
|
3
|
+
import { api, errorMessage } from './api'
|
|
4
|
+
|
|
5
|
+
const ROUND_SECONDS = 5
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sign-in inside the app. `auth` calls routes Marcapony serves on the app's
|
|
9
|
+
* own address, so there is no key here and the app never sees a password or
|
|
10
|
+
* the session. Those routes exist on the deployed app only: locally every call
|
|
11
|
+
* fails, and the game is played signed out.
|
|
12
|
+
*/
|
|
13
|
+
function SignIn({ offer, onSignedIn }: { offer: SignInOptions; onSignedIn: () => void }) {
|
|
14
|
+
const [email, setEmail] = useState('')
|
|
15
|
+
const [code, setCode] = useState('')
|
|
16
|
+
const [sent, setSent] = useState(false)
|
|
17
|
+
const [busy, setBusy] = useState(false)
|
|
18
|
+
const [error, setError] = useState<string | null>(null)
|
|
19
|
+
|
|
20
|
+
async function send() {
|
|
21
|
+
setBusy(true)
|
|
22
|
+
setError(null)
|
|
23
|
+
try {
|
|
24
|
+
await auth.sendCode(email.trim())
|
|
25
|
+
setSent(true)
|
|
26
|
+
} catch (err) {
|
|
27
|
+
setError(errorMessage(err))
|
|
28
|
+
} finally {
|
|
29
|
+
setBusy(false)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function verify() {
|
|
34
|
+
setBusy(true)
|
|
35
|
+
setError(null)
|
|
36
|
+
try {
|
|
37
|
+
const { signedIn } = await auth.verifyCode(email.trim(), code.trim())
|
|
38
|
+
if (signedIn) onSignedIn()
|
|
39
|
+
else setError('That code is wrong or has expired. Ask for a new one.')
|
|
40
|
+
} catch (err) {
|
|
41
|
+
setError(errorMessage(err))
|
|
42
|
+
} finally {
|
|
43
|
+
setBusy(false)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<section>
|
|
49
|
+
<h2>Keep your best score</h2>
|
|
50
|
+
{offer.emailCode && !sent ? (
|
|
51
|
+
<form
|
|
52
|
+
onSubmit={(event) => {
|
|
53
|
+
event.preventDefault()
|
|
54
|
+
void send()
|
|
55
|
+
}}
|
|
56
|
+
>
|
|
57
|
+
<label>
|
|
58
|
+
E-mail
|
|
59
|
+
<input
|
|
60
|
+
type="email"
|
|
61
|
+
required
|
|
62
|
+
autoComplete="email"
|
|
63
|
+
value={email}
|
|
64
|
+
onChange={(event) => setEmail(event.target.value)}
|
|
65
|
+
/>
|
|
66
|
+
</label>
|
|
67
|
+
<button type="submit" disabled={busy || !email.trim()}>
|
|
68
|
+
Send me a code
|
|
69
|
+
</button>
|
|
70
|
+
</form>
|
|
71
|
+
) : null}
|
|
72
|
+
{offer.emailCode && sent ? (
|
|
73
|
+
<form
|
|
74
|
+
onSubmit={(event) => {
|
|
75
|
+
event.preventDefault()
|
|
76
|
+
void verify()
|
|
77
|
+
}}
|
|
78
|
+
>
|
|
79
|
+
<p className="muted">
|
|
80
|
+
If {email.trim()} can sign in, a code is on its way. The e-mail also has a link that
|
|
81
|
+
signs you in.
|
|
82
|
+
{offer.signUp === 'app' ? ' New here? The code creates your account.' : null}
|
|
83
|
+
</p>
|
|
84
|
+
<label>
|
|
85
|
+
Code
|
|
86
|
+
<input
|
|
87
|
+
inputMode="numeric"
|
|
88
|
+
autoComplete="one-time-code"
|
|
89
|
+
maxLength={6}
|
|
90
|
+
required
|
|
91
|
+
value={code}
|
|
92
|
+
onChange={(event) => setCode(event.target.value)}
|
|
93
|
+
/>
|
|
94
|
+
</label>
|
|
95
|
+
<p className="actions">
|
|
96
|
+
<button type="submit" disabled={busy || code.trim().length !== 6}>
|
|
97
|
+
Sign in
|
|
98
|
+
</button>
|
|
99
|
+
<button type="button" disabled={busy} onClick={() => setSent(false)}>
|
|
100
|
+
Use another address
|
|
101
|
+
</button>
|
|
102
|
+
</p>
|
|
103
|
+
</form>
|
|
104
|
+
) : null}
|
|
105
|
+
{error ? <p className="error">{error}</p> : null}
|
|
106
|
+
<p>
|
|
107
|
+
{/* Password, Google and registration live on the full sign-in page. */}
|
|
108
|
+
<a href={auth.signInUrl({ returnTo: '/' })}>
|
|
109
|
+
{offer.emailCode ? 'Other ways to sign in' : 'Sign in'}
|
|
110
|
+
</a>
|
|
111
|
+
{offer.signUp === 'hosted' ? (
|
|
112
|
+
<span className="muted"> (new accounts start there)</span>
|
|
113
|
+
) : null}
|
|
114
|
+
</p>
|
|
115
|
+
</section>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function App() {
|
|
120
|
+
// undefined while loading; null when sign-in is not available (local dev).
|
|
121
|
+
const [who, setWho] = useState<SignedInState | null | undefined>(undefined)
|
|
122
|
+
const [offer, setOffer] = useState<SignInOptions | null>(null)
|
|
123
|
+
const [best, setBest] = useState<number | null>(null)
|
|
124
|
+
const [score, setScore] = useState(0)
|
|
125
|
+
const [left, setLeft] = useState<number | null>(null)
|
|
126
|
+
const [error, setError] = useState<string | null>(null)
|
|
127
|
+
|
|
128
|
+
const refresh = useCallback(async () => {
|
|
129
|
+
try {
|
|
130
|
+
const state = await auth.me()
|
|
131
|
+
setWho(state)
|
|
132
|
+
if (!state.signedIn) setOffer(await auth.options())
|
|
133
|
+
} catch {
|
|
134
|
+
setWho(null)
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
setBest((await api<{ best: number | null }>('/api/score')).best)
|
|
138
|
+
} catch (err) {
|
|
139
|
+
setError(errorMessage(err))
|
|
140
|
+
}
|
|
141
|
+
}, [])
|
|
142
|
+
|
|
143
|
+
// Also covers a visitor who comes back from the sign-in page or the e-mail link.
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
void refresh()
|
|
146
|
+
}, [refresh])
|
|
147
|
+
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
if (left === null || left <= 0) return
|
|
150
|
+
const timer = setTimeout(() => setLeft(left - 1), 1000)
|
|
151
|
+
return () => clearTimeout(timer)
|
|
152
|
+
}, [left])
|
|
153
|
+
|
|
154
|
+
const playing = left !== null && left > 0
|
|
155
|
+
const finished = left === 0
|
|
156
|
+
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
if (!finished || !who?.signedIn || !who.access) return
|
|
159
|
+
api<{ best: number }>('/api/score', { method: 'PUT', body: { score } })
|
|
160
|
+
.then((saved) => setBest(saved.best))
|
|
161
|
+
.catch((err: unknown) => setError(errorMessage(err)))
|
|
162
|
+
}, [finished, who, score])
|
|
163
|
+
|
|
164
|
+
function start() {
|
|
165
|
+
setError(null)
|
|
166
|
+
setScore(0)
|
|
167
|
+
setLeft(ROUND_SECONDS)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function signOut() {
|
|
171
|
+
try {
|
|
172
|
+
await auth.signOut()
|
|
173
|
+
} catch (err) {
|
|
174
|
+
setError(errorMessage(err))
|
|
175
|
+
}
|
|
176
|
+
setBest(null)
|
|
177
|
+
await refresh()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<main>
|
|
182
|
+
<h1>Tap as fast as you can</h1>
|
|
183
|
+
<p className="muted">
|
|
184
|
+
{ROUND_SECONDS} seconds a round. Anyone can play; sign in to keep your best score.
|
|
185
|
+
</p>
|
|
186
|
+
|
|
187
|
+
<p className="actions">
|
|
188
|
+
{playing ? (
|
|
189
|
+
<button type="submit" onClick={() => setScore((value) => value + 1)}>
|
|
190
|
+
Tap ({left}s)
|
|
191
|
+
</button>
|
|
192
|
+
) : (
|
|
193
|
+
<button type="submit" onClick={start}>
|
|
194
|
+
{finished ? 'Play again' : 'Start'}
|
|
195
|
+
</button>
|
|
196
|
+
)}
|
|
197
|
+
</p>
|
|
198
|
+
<p>
|
|
199
|
+
Score: <strong>{score}</strong>
|
|
200
|
+
{best !== null ? <span className="muted"> · Best: {best}</span> : null}
|
|
201
|
+
</p>
|
|
202
|
+
{error ? <p className="error">{error}</p> : null}
|
|
203
|
+
|
|
204
|
+
{who?.signedIn && !who.access ? (
|
|
205
|
+
<p className="actions">
|
|
206
|
+
<span className="muted">This account cannot use this app here.</span>
|
|
207
|
+
<button type="button" onClick={() => void signOut()}>
|
|
208
|
+
Sign out
|
|
209
|
+
</button>
|
|
210
|
+
</p>
|
|
211
|
+
) : null}
|
|
212
|
+
{who?.signedIn && who.access ? (
|
|
213
|
+
<p className="actions">
|
|
214
|
+
<span className="muted">Signed in as {who.name ?? who.email}</span>
|
|
215
|
+
<button type="button" onClick={() => void signOut()}>
|
|
216
|
+
Sign out
|
|
217
|
+
</button>
|
|
218
|
+
</p>
|
|
219
|
+
) : null}
|
|
220
|
+
{who && !who.signedIn && offer ? (
|
|
221
|
+
<SignIn offer={offer} onSignedIn={() => void refresh()} />
|
|
222
|
+
) : null}
|
|
223
|
+
{who === null ? (
|
|
224
|
+
<p className="notice">
|
|
225
|
+
Sign-in works on the deployed app. Run <code>bun run deploy</code> and open the address it
|
|
226
|
+
prints.
|
|
227
|
+
</p>
|
|
228
|
+
) : null}
|
|
229
|
+
</main>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Hono, type Context } from 'hono'
|
|
2
2
|
import type { PaymentLinkLineItem, Product } from 'marcapony-sdk'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
errorResponse,
|
|
5
|
+
marcaponyFromEnv,
|
|
6
|
+
siteThemeResponse,
|
|
7
|
+
type MarcaponyBindings,
|
|
8
|
+
} from './marcapony'
|
|
4
9
|
|
|
5
10
|
/**
|
|
6
11
|
* A small public shop: the catalog, a cart kept in the browser, and a checkout
|
|
@@ -61,6 +66,7 @@ async function cartFingerprint(quantities: Map<string, number>): Promise<string>
|
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
69
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
64
70
|
|
|
65
71
|
app.get('/api/catalog', async (c) => {
|
|
66
72
|
const mp = marcaponyFromEnv(c.env)
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
errorResponse,
|
|
5
5
|
identityOf,
|
|
6
6
|
marcaponyFromEnv,
|
|
7
|
+
siteThemeResponse,
|
|
7
8
|
type Marcapony,
|
|
8
9
|
type MarcaponyBindings,
|
|
9
10
|
} from './marcapony'
|
|
@@ -51,6 +52,7 @@ async function memberOf(
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
55
|
+
app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
|
|
54
56
|
|
|
55
57
|
app.get('/api/me', async (c) => {
|
|
56
58
|
const mp = marcaponyFromEnv(c.env)
|