create-marcapony-app 0.2.0 → 0.2.2
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/package.json +1 -1
- package/template/base/_package.json +2 -2
- package/template/base/src/server/marcapony.ts +0 -14
- package/template/base/src/ui/api.ts +4 -1
- package/template/booking/src/server/index.ts +2 -3
- package/template/booking/src/ui/App.tsx +8 -3
- package/template/shop/src/server/index.ts +18 -13
- package/template/team-tool/src/server/index.ts +6 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-marcapony-app",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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.7.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.4.
|
|
27
|
+
"marcapony": "^0.4.2",
|
|
28
28
|
"typescript": "^5.9.0",
|
|
29
29
|
"vite": "^7.3.0"
|
|
30
30
|
}
|
|
@@ -53,20 +53,6 @@ export async function identityOf(
|
|
|
53
53
|
return mp.identity(request)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/**
|
|
57
|
-
* Whether Marcapony only simulated a write. New apps start in test mode, where
|
|
58
|
-
* writes answer with the record they would have created, marked
|
|
59
|
-
* `simulated: true`, and nothing is saved. The SDK types do not declare the
|
|
60
|
-
* marker yet, so read it defensively.
|
|
61
|
-
*/
|
|
62
|
-
export function isSimulated(result: unknown): boolean {
|
|
63
|
-
return (
|
|
64
|
-
typeof result === 'object' &&
|
|
65
|
-
result !== null &&
|
|
66
|
-
(result as { simulated?: unknown }).simulated === true
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
56
|
/**
|
|
71
57
|
* Turns a failed call into a JSON answer for the browser. A Marcapony error
|
|
72
58
|
* keeps its status (401 not signed in, 403 not allowed, 404, 422 invalid
|
|
@@ -19,7 +19,10 @@ export async function api<T>(path: string, init?: { method?: string; body?: unkn
|
|
|
19
19
|
})
|
|
20
20
|
const body = (await response.json().catch(() => ({}))) as { error?: string }
|
|
21
21
|
if (!response.ok) {
|
|
22
|
-
throw new ApiError(
|
|
22
|
+
throw new ApiError(
|
|
23
|
+
response.status,
|
|
24
|
+
body.error ?? `Request failed with status ${response.status}`,
|
|
25
|
+
)
|
|
23
26
|
}
|
|
24
27
|
return body as T
|
|
25
28
|
}
|
|
@@ -3,7 +3,6 @@ import type { Identity } from 'marcapony-sdk'
|
|
|
3
3
|
import {
|
|
4
4
|
errorResponse,
|
|
5
5
|
identityOf,
|
|
6
|
-
isSimulated,
|
|
7
6
|
marcaponyFromEnv,
|
|
8
7
|
type Marcapony,
|
|
9
8
|
type MarcaponyBindings,
|
|
@@ -97,7 +96,7 @@ app.post('/api/reservations', async (c) => {
|
|
|
97
96
|
{ idempotencyKey: `booking-${who.sub}-${requestId}` },
|
|
98
97
|
)
|
|
99
98
|
// Simulated in a preview, and in test mode for every visitor.
|
|
100
|
-
return c.json({ reservation, simulated: who.preview ||
|
|
99
|
+
return c.json({ reservation, simulated: who.preview || reservation.simulated === true }, 201)
|
|
101
100
|
})
|
|
102
101
|
|
|
103
102
|
app.post('/api/reservations/:id/cancel', async (c) => {
|
|
@@ -105,7 +104,7 @@ app.post('/api/reservations/:id/cancel', async (c) => {
|
|
|
105
104
|
const who = await customerOf(mp, c.env, c.req.raw)
|
|
106
105
|
if (who instanceof Response) return who
|
|
107
106
|
const result = await mp.forRequest(c.req.raw).me.reservations.cancel(c.req.param('id'))
|
|
108
|
-
return c.json({ reservation: result, simulated: who.preview ||
|
|
107
|
+
return c.json({ reservation: result, simulated: who.preview || result.simulated === true })
|
|
109
108
|
})
|
|
110
109
|
|
|
111
110
|
app.onError((err) => errorResponse(err))
|
|
@@ -85,8 +85,8 @@ export function App() {
|
|
|
85
85
|
<p className="muted">Signed in as {me.email}</p>
|
|
86
86
|
{me.preview ? (
|
|
87
87
|
<p className="notice">
|
|
88
|
-
Preview: you are viewing this page as a customer. Bookings and cancellations are
|
|
89
|
-
|
|
88
|
+
Preview: you are viewing this page as a customer. Bookings and cancellations are simulated
|
|
89
|
+
and nothing is saved.
|
|
90
90
|
</p>
|
|
91
91
|
) : null}
|
|
92
92
|
{error ? <p className="error">{error}</p> : null}
|
|
@@ -176,7 +176,12 @@ function BookingForm({ onBooked }: { onBooked: () => void }) {
|
|
|
176
176
|
</label>
|
|
177
177
|
<label>
|
|
178
178
|
Notes (optional)
|
|
179
|
-
<textarea
|
|
179
|
+
<textarea
|
|
180
|
+
rows={3}
|
|
181
|
+
maxLength={500}
|
|
182
|
+
value={notes}
|
|
183
|
+
onChange={(e) => edit(setNotes)(e.target.value)}
|
|
184
|
+
/>
|
|
180
185
|
</label>
|
|
181
186
|
<button type="submit" disabled={busy}>
|
|
182
187
|
{busy ? 'Booking…' : 'Book'}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hono, type Context } from 'hono'
|
|
2
|
-
import type {
|
|
3
|
-
import { errorResponse,
|
|
2
|
+
import type { PaymentLinkLineItem, Product } from 'marcapony-sdk'
|
|
3
|
+
import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marcapony'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* A small public shop: the catalog, a cart kept in the browser, and a checkout
|
|
@@ -55,7 +55,9 @@ async function cartFingerprint(quantities: Map<string, number>): Promise<string>
|
|
|
55
55
|
.map(([id, quantity]) => `${id}:${quantity}`)
|
|
56
56
|
.join(',')
|
|
57
57
|
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(canonical))
|
|
58
|
-
return [...new Uint8Array(digest).slice(0, 8)]
|
|
58
|
+
return [...new Uint8Array(digest).slice(0, 8)]
|
|
59
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
60
|
+
.join('')
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
app.get('/api/health', (c) => c.json({ ok: true }))
|
|
@@ -179,7 +181,7 @@ app.post('/api/checkout', async (c) => {
|
|
|
179
181
|
// are part of the key too: a reused attempt id with a different cart must
|
|
180
182
|
// not be answered with the earlier cart's link.
|
|
181
183
|
const idempotencyKey = `checkout-${attemptId}-${await cartFingerprint(quantities)}`
|
|
182
|
-
const link =
|
|
184
|
+
const link = await mp.paymentLinks.create(
|
|
183
185
|
{
|
|
184
186
|
title: `${site.name} order`,
|
|
185
187
|
amount,
|
|
@@ -187,20 +189,23 @@ app.post('/api/checkout', async (c) => {
|
|
|
187
189
|
lineItems,
|
|
188
190
|
},
|
|
189
191
|
{ idempotencyKey },
|
|
190
|
-
)
|
|
192
|
+
)
|
|
191
193
|
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
194
|
+
// In test mode the link is only simulated: nothing was saved and there is
|
|
195
|
+
// nothing to pay yet, so there is no payment page either.
|
|
196
|
+
if (link.simulated) {
|
|
197
|
+
return c.json(
|
|
198
|
+
{ amount, currency, payUrl: null, linkId: link.linkId ?? null, simulated: true },
|
|
199
|
+
201,
|
|
200
|
+
)
|
|
201
|
+
}
|
|
197
202
|
return c.json(
|
|
198
203
|
{
|
|
199
204
|
amount,
|
|
200
|
-
currency: link.currency
|
|
201
|
-
payUrl:
|
|
205
|
+
currency: link.currency,
|
|
206
|
+
payUrl: link.paymentUrl,
|
|
202
207
|
linkId: link.linkId,
|
|
203
|
-
simulated,
|
|
208
|
+
simulated: false,
|
|
204
209
|
},
|
|
205
210
|
201,
|
|
206
211
|
)
|
|
@@ -3,7 +3,6 @@ import type { Identity, Reservation, UpdateReservationStatusInput } from 'marcap
|
|
|
3
3
|
import {
|
|
4
4
|
errorResponse,
|
|
5
5
|
identityOf,
|
|
6
|
-
isSimulated,
|
|
7
6
|
marcaponyFromEnv,
|
|
8
7
|
type Marcapony,
|
|
9
8
|
type MarcaponyBindings,
|
|
@@ -102,13 +101,17 @@ app.post('/api/reservations/:id/status', async (c) => {
|
|
|
102
101
|
return c.json({ error: 'status must be approved, rejected, cancelled or completed.' }, 400)
|
|
103
102
|
}
|
|
104
103
|
const input: UpdateReservationStatusInput = { status }
|
|
105
|
-
if (
|
|
104
|
+
if (
|
|
105
|
+
status === 'rejected' &&
|
|
106
|
+
typeof body.rejectionReason === 'string' &&
|
|
107
|
+
body.rejectionReason.trim()
|
|
108
|
+
) {
|
|
106
109
|
input.rejectionReason = body.rejectionReason.trim().slice(0, 500)
|
|
107
110
|
}
|
|
108
111
|
|
|
109
112
|
const result = await mp.forRequest(c.req.raw).reservations.updateStatus(c.req.param('id'), input)
|
|
110
113
|
// Simulated in a preview, and in test mode for every member.
|
|
111
|
-
return c.json({ ok: true, simulated: who.preview ||
|
|
114
|
+
return c.json({ ok: true, simulated: who.preview || result.simulated === true })
|
|
112
115
|
})
|
|
113
116
|
|
|
114
117
|
app.onError((err) => errorResponse(err))
|