create-marcapony-app 0.1.0 → 0.2.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.
Files changed (35) hide show
  1. package/README.md +29 -27
  2. package/dist/index.d.ts +37 -3
  3. package/dist/index.js +138 -23
  4. package/package.json +5 -4
  5. package/template/base/.dev.vars.example +7 -0
  6. package/template/{_package.json → base/_package.json} +14 -10
  7. package/template/{marcapony.json → base/marcapony.json} +1 -1
  8. package/template/{scripts → base/scripts}/dev.ts +19 -2
  9. package/template/base/src/server/marcapony.ts +85 -0
  10. package/template/base/src/ui/api.ts +34 -0
  11. package/template/base/src/ui/styles.css +115 -0
  12. package/template/blank/README.md +48 -0
  13. package/template/{src → blank/src}/server/index.ts +9 -7
  14. package/template/booking/README.md +58 -0
  15. package/template/booking/marcapony.json +26 -0
  16. package/template/booking/src/server/index.ts +113 -0
  17. package/template/booking/src/ui/App.tsx +188 -0
  18. package/template/shop/README.md +66 -0
  19. package/template/shop/marcapony.json +12 -0
  20. package/template/shop/src/server/index.ts +211 -0
  21. package/template/shop/src/ui/App.tsx +214 -0
  22. package/template/team-tool/README.md +59 -0
  23. package/template/team-tool/marcapony.json +25 -0
  24. package/template/team-tool/src/server/index.ts +116 -0
  25. package/template/team-tool/src/ui/App.tsx +179 -0
  26. package/template/.dev.vars.example +0 -5
  27. package/template/README.md +0 -59
  28. package/template/src/server/marcapony.ts +0 -28
  29. package/template/src/ui/styles.css +0 -45
  30. /package/template/{_gitignore → base/_gitignore} +0 -0
  31. /package/template/{index.html → base/index.html} +0 -0
  32. /package/template/{src → base/src}/ui/main.tsx +0 -0
  33. /package/template/{tsconfig.json → base/tsconfig.json} +0 -0
  34. /package/template/{vite.config.ts → base/vite.config.ts} +0 -0
  35. /package/template/{src → blank/src}/ui/App.tsx +0 -0
@@ -0,0 +1,179 @@
1
+ import { useCallback, useEffect, useState } from 'react'
2
+ import { api, ApiError, errorMessage } from './api'
3
+
4
+ interface Me {
5
+ email: string
6
+ preview: boolean
7
+ canManage: boolean
8
+ }
9
+
10
+ interface Reservation {
11
+ id: string
12
+ requestedDate: string
13
+ requestedTime: string
14
+ duration: number | null
15
+ status: string | number
16
+ notes: string | null
17
+ customerName: string | null
18
+ customerPhone: string | null
19
+ productName: string | null
20
+ }
21
+
22
+ type NextStatus = 'approved' | 'rejected' | 'cancelled' | 'completed'
23
+
24
+ /** Which buttons a reservation in each status offers. */
25
+ const ACTIONS: Record<string, Array<{ status: NextStatus; label: string }>> = {
26
+ pending: [
27
+ { status: 'approved', label: 'Approve' },
28
+ { status: 'rejected', label: 'Reject' },
29
+ ],
30
+ approved: [
31
+ { status: 'completed', label: 'Complete' },
32
+ { status: 'cancelled', label: 'Cancel' },
33
+ ],
34
+ }
35
+
36
+ /** The member's local day, as YYYY-MM-DD. */
37
+ function today(): string {
38
+ const now = new Date()
39
+ const month = String(now.getMonth() + 1).padStart(2, '0')
40
+ const day = String(now.getDate()).padStart(2, '0')
41
+ return `${now.getFullYear()}-${month}-${day}`
42
+ }
43
+
44
+ export function App() {
45
+ const [me, setMe] = useState<Me | null>(null)
46
+ const [denied, setDenied] = useState<string | null>(null)
47
+ const [date, setDate] = useState(today)
48
+ const [reservations, setReservations] = useState<Reservation[] | null>(null)
49
+ const [truncated, setTruncated] = useState(false)
50
+ const [busyId, setBusyId] = useState<string | null>(null)
51
+ const [error, setError] = useState<string | null>(null)
52
+ const [message, setMessage] = useState<string | null>(null)
53
+
54
+ const load = useCallback((day: string) => {
55
+ setReservations(null)
56
+ api<{ reservations: Reservation[]; truncated: boolean }>(
57
+ `/api/reservations?date=${encodeURIComponent(day)}`,
58
+ )
59
+ .then((body) => {
60
+ setReservations(body.reservations)
61
+ setTruncated(body.truncated)
62
+ })
63
+ .catch((err: unknown) => setError(errorMessage(err)))
64
+ }, [])
65
+
66
+ useEffect(() => {
67
+ api<Me>('/api/me')
68
+ .then(setMe)
69
+ .catch((err: unknown) => {
70
+ if (err instanceof ApiError && (err.status === 401 || err.status === 403)) {
71
+ setDenied(err.message)
72
+ } else {
73
+ setError(errorMessage(err))
74
+ }
75
+ })
76
+ }, [])
77
+
78
+ useEffect(() => {
79
+ if (me) load(date)
80
+ }, [me, date, load])
81
+
82
+ async function setStatus(reservation: Reservation, status: NextStatus) {
83
+ let rejectionReason: string | undefined
84
+ if (status === 'rejected') {
85
+ const answer = window.prompt('Reason for rejecting (optional)')
86
+ if (answer === null) return
87
+ rejectionReason = answer
88
+ }
89
+ setBusyId(reservation.id)
90
+ setError(null)
91
+ setMessage(null)
92
+ try {
93
+ const body = await api<{ simulated: boolean }>(
94
+ `/api/reservations/${encodeURIComponent(reservation.id)}/status`,
95
+ { body: { status, rejectionReason } },
96
+ )
97
+ setMessage(
98
+ body.simulated ? 'Change simulated (test mode or preview). Nothing was saved.' : null,
99
+ )
100
+ load(date)
101
+ } catch (err) {
102
+ setError(errorMessage(err))
103
+ } finally {
104
+ setBusyId(null)
105
+ }
106
+ }
107
+
108
+ if (denied) {
109
+ return (
110
+ <main>
111
+ <h1>Reservations</h1>
112
+ <p>{denied}</p>
113
+ </main>
114
+ )
115
+ }
116
+
117
+ if (!me) {
118
+ return (
119
+ <main>
120
+ <h1>Reservations</h1>
121
+ {error ? <p className="error">{error}</p> : <p>Loading…</p>}
122
+ </main>
123
+ )
124
+ }
125
+
126
+ return (
127
+ <main>
128
+ <h1>Reservations</h1>
129
+ <p className="muted">Signed in as {me.email}</p>
130
+ {me.preview ? (
131
+ <p className="notice">Preview: status changes are simulated and nothing is saved.</p>
132
+ ) : null}
133
+ {error ? <p className="error">{error}</p> : null}
134
+ {message ? <p>{message}</p> : null}
135
+
136
+ <label>
137
+ Day
138
+ <input type="date" value={date} onChange={(e) => setDate(e.target.value || today())} />
139
+ </label>
140
+
141
+ {reservations === null ? <p>Loading…</p> : null}
142
+ {reservations?.length === 0 ? <p>No reservations on this day.</p> : null}
143
+ {truncated ? (
144
+ <p className="muted">Only the most recent reservations were searched for this day.</p>
145
+ ) : null}
146
+ <ul className="rows">
147
+ {reservations?.map((reservation) => {
148
+ const actions = me.canManage ? (ACTIONS[String(reservation.status)] ?? []) : []
149
+ return (
150
+ <li key={reservation.id}>
151
+ <span>
152
+ <strong>{reservation.requestedTime}</strong>{' '}
153
+ {reservation.customerName ?? 'Unknown customer'}
154
+ {reservation.productName ? `, ${reservation.productName}` : ''}
155
+ {reservation.duration ? `, ${reservation.duration} min` : ''}{' '}
156
+ <span className="muted">({String(reservation.status)})</span>
157
+ {reservation.notes ? <span className="muted"> {reservation.notes}</span> : null}
158
+ </span>
159
+ {actions.length > 0 ? (
160
+ <span className="actions">
161
+ {actions.map((action) => (
162
+ <button
163
+ key={action.status}
164
+ type="button"
165
+ disabled={busyId === reservation.id}
166
+ onClick={() => void setStatus(reservation, action.status)}
167
+ >
168
+ {action.label}
169
+ </button>
170
+ ))}
171
+ </span>
172
+ ) : null}
173
+ </li>
174
+ )
175
+ })}
176
+ </ul>
177
+ </main>
178
+ )
179
+ }
@@ -1,5 +0,0 @@
1
- # `bunx marcapony app link` writes the real values into `.dev.vars` for you —
2
- # the key is minted fresh and never passes through a clipboard. This file is
3
- # only here to document what ends up there.
4
- MARCAPONY_API_KEY=mpk_...
5
- MARCAPONY_API_URL=https://api.marcapony.com/ext/v1
@@ -1,59 +0,0 @@
1
- # Marcapony app
2
-
3
- A Worker that serves this app's API and talks to Marcapony, plus a React UI
4
- served as static assets.
5
-
6
- ## Get a key
7
-
8
- ```sh
9
- bunx marcapony app link
10
- ```
11
-
12
- Create the app under **My Apps** in your Marcapony dashboard, generate a pairing
13
- code there, and paste it when asked. The CLI writes `MARCAPONY_API_KEY` into
14
- `.dev.vars` itself — the key is never shown and never goes through a clipboard.
15
- It also fills in `AGENTS.md` with the exact operations your key can perform, so
16
- your AI assistant knows what this app is allowed to do.
17
-
18
- ## Develop
19
-
20
- ```sh
21
- bun install
22
- bun run dev
23
- ```
24
-
25
- Vite serves the UI and proxies `/api` to the Hono app in `src/server`.
26
-
27
- One thing to know: `bun run dev` runs the server under Bun, while the deployed
28
- app runs on the Workers runtime. They are close but not identical, so prefer
29
- plain web APIs (`fetch`, `Request`, `Response`, `crypto`) over Node built-ins in
30
- `src/server`, and check the deployed app after shipping a server change.
31
-
32
- ## Deploy
33
-
34
- ```sh
35
- bunx marcapony app deploy
36
- ```
37
-
38
- That builds the project, uploads it, and prints the hostname when it is live.
39
- There is no wrangler and no Cloudflare account involved — the platform runs it
40
- for you, injecting the key as a secret.
41
-
42
- ## Layout
43
-
44
- | Path | What it is |
45
- | ------------------------- | ------------------------------------------------ |
46
- | `src/server/index.ts` | The Worker. Only `/api/*` reaches it |
47
- | `src/server/marcapony.ts` | The only place that builds the Marcapony client |
48
- | `src/ui/` | React app, built to `dist/client` |
49
- | `marcapony.json` | How the platform builds and uploads this project |
50
- | `.dev.vars` | Your key. Gitignored — never commit it |
51
-
52
- ## Rules that matter
53
-
54
- - **The app key stays on the server.** The browser calls your `/api` routes;
55
- those routes call Marcapony. A key in a client bundle is a public key.
56
- - **Pass an `idempotencyKey` to every create call.** Retries then cannot create
57
- the same order, reservation, or payment link twice.
58
- - **Deployed apps are stateless.** No KV, Durable Objects, D1 or R2 — durable
59
- state lives behind the Marcapony API, which your key already reaches.
@@ -1,28 +0,0 @@
1
- import { createClient } from 'marcapony-sdk'
2
-
3
- /**
4
- * The one place that owns the Marcapony env contract and builds the client.
5
- * Keep it that way: a second `createClient` call somewhere else is how a key
6
- * ends up read from the wrong place, or bundled into the browser.
7
- */
8
- export interface MarcaponyBindings {
9
- /** Scoped app key (`mpk_...`). Written into `.dev.vars` by `marcapony app link`. */
10
- MARCAPONY_API_KEY?: string
11
- /** API root. Also written by `app link`; the SDK defaults to production. */
12
- MARCAPONY_API_URL?: string
13
- }
14
-
15
- export type Marcapony = ReturnType<typeof createClient>
16
-
17
- /**
18
- * Workers env is request-scoped, so build the client per request. The SDK is
19
- * zero-dependency and holds no connections, so construction is effectively free.
20
- */
21
- export function marcaponyFromEnv(env: MarcaponyBindings): Marcapony {
22
- if (!env.MARCAPONY_API_KEY) {
23
- throw new Error(
24
- 'MARCAPONY_API_KEY is missing. Run `bunx marcapony app link` in this project — it writes the key into .dev.vars for you.',
25
- )
26
- }
27
- return createClient({ apiKey: env.MARCAPONY_API_KEY, baseUrl: env.MARCAPONY_API_URL })
28
- }
@@ -1,45 +0,0 @@
1
- :root {
2
- color-scheme: light dark;
3
- font-family:
4
- ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
5
- line-height: 1.5;
6
- }
7
-
8
- body {
9
- margin: 0;
10
- }
11
-
12
- main {
13
- max-width: 42rem;
14
- margin: 0 auto;
15
- padding: 3rem 1.5rem;
16
- }
17
-
18
- h1 {
19
- font-size: 1.5rem;
20
- margin-bottom: 0.5rem;
21
- }
22
-
23
- h2 {
24
- font-size: 1.125rem;
25
- margin-top: 2rem;
26
- }
27
-
28
- code {
29
- font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
30
- font-size: 0.875em;
31
- }
32
-
33
- ul {
34
- padding-left: 1.25rem;
35
- }
36
-
37
- .error {
38
- color: #b91c1c;
39
- }
40
-
41
- @media (prefers-color-scheme: dark) {
42
- .error {
43
- color: #fca5a5;
44
- }
45
- }
File without changes
File without changes
File without changes
File without changes
File without changes