create-marcapony-app 0.1.1 → 0.2.1

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 +27 -23
  2. package/dist/index.d.ts +28 -3
  3. package/dist/index.js +102 -20
  4. package/package.json +5 -4
  5. package/template/base/.dev.vars.example +7 -0
  6. package/template/{_package.json → base/_package.json} +14 -11
  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 +71 -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 +112 -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 +205 -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 +115 -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 -57
  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,57 +0,0 @@
1
- # Marcapony app
2
-
3
- A small web app with its own backend, connected to your Marcapony account.
4
-
5
- ## Get a key
6
-
7
- ```sh
8
- npx marcapony app link
9
- ```
10
-
11
- Create the app under **My Apps** in your Marcapony dashboard, generate a
12
- connection code there, and paste it when asked. The key is written into
13
- `.dev.vars` for you — it is never shown and never goes through your clipboard.
14
- An `AGENTS.md` is written too, listing exactly what this app is allowed to do, so
15
- your AI assistant knows what it can build.
16
-
17
- ## Develop
18
-
19
- ```sh
20
- bun install
21
- bun run dev
22
- ```
23
-
24
- Your interface and your `/api` routes both run locally.
25
-
26
- One thing to know: local development and the published app run in slightly
27
- different environments. Stick to standard web APIs in `src/server` — `fetch`,
28
- `Request`, `Response`, `crypto` — since those behave identically in both, and
29
- check the published app after you change something on the backend.
30
-
31
- ## Publish
32
-
33
- ```sh
34
- npx marcapony app deploy
35
- ```
36
-
37
- That builds your app, publishes it, and prints its address. Marcapony hosts it
38
- and supplies the key at runtime; there is nothing for you to configure.
39
-
40
- ## Layout
41
-
42
- | Path | What it is |
43
- | ------------------------- | ------------------------------------------- |
44
- | `src/server/index.ts` | Your backend. Handles `/api/*` |
45
- | `src/server/marcapony.ts` | The only place that talks to Marcapony |
46
- | `src/ui/` | Your interface |
47
- | `marcapony.json` | How Marcapony builds and publishes your app |
48
- | `.dev.vars` | Your key. Never commit it |
49
-
50
- ## Rules that matter
51
-
52
- - **The key stays on the backend.** Your interface calls your own `/api` routes,
53
- and those routes call Marcapony. A key sent to the browser is a public key.
54
- - **Pass an `idempotencyKey` to every create call.** Retries then cannot create
55
- the same order, reservation, or payment link twice.
56
- - **Apps keep no storage of their own.** Anything that has to persist lives in
57
- Marcapony, 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