Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ This is a template project with best-practice modules:
- Winterspec for defining the API
- bun testing
- Zustand store with zod definition for database state

## Payments

- `POST /payments/send` creates a fake payment with recipient, amount, currency, and optional bounty issue.
- `GET /payments/list` returns the fake payments stored in memory.
21 changes: 20 additions & 1 deletion lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { createStore, type StoreApi } from "zustand/vanilla"
import { immer } from "zustand/middleware/immer"
import { hoist, type HoistedStoreApi } from "zustand-hoist"

import { databaseSchema, type DatabaseSchema, type Thing } from "./schema.ts"
import {
databaseSchema,
type DatabaseSchema,
type Payment,
type Thing,
} from "./schema.ts"
import { combine } from "zustand/middleware"

export const createDatabase = () => {
Expand All @@ -21,4 +26,18 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
sendPayment: (payment: Omit<Payment, "payment_id" | "status">) => {
let payment_id = ""
set((state) => {
payment_id = `payment_${state.idCounter}`
return {
payments: [
...state.payments,
{ ...payment, payment_id, status: "sent" as const },
],
idCounter: state.idCounter + 1,
}
})
return payment_id
},
}))
11 changes: 11 additions & 0 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ export const thingSchema = z.object({
})
export type Thing = z.infer<typeof thingSchema>

export const paymentSchema = z.object({
payment_id: z.string(),
recipient: z.string(),
amount: z.number(),
currency: z.string(),
bounty_issue: z.number().optional(),
status: z.enum(["pending", "sent"]),
})
export type Payment = z.infer<typeof paymentSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
payments: z.array(paymentSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>
20 changes: 20 additions & 0 deletions routes/payments/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
jsonResponse: z.object({
payments: z.array(
z.object({
payment_id: z.string(),
recipient: z.string(),
amount: z.number(),
currency: z.string(),
bounty_issue: z.number().optional(),
status: z.enum(["pending", "sent"]),
}),
),
}),
})((req, ctx) => {
return ctx.json({ payments: ctx.db.payments })
})
27 changes: 27 additions & 0 deletions routes/payments/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

const paymentResponse = z.object({
payment_id: z.string(),
recipient: z.string(),
amount: z.number(),
currency: z.string(),
bounty_issue: z.number().optional(),
status: z.enum(["pending", "sent"]),
})

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
recipient: z.string(),
amount: z.number().positive(),
currency: z.string().default("USD"),
bounty_issue: z.number().optional(),
}),
jsonResponse: paymentResponse,
})(async (req, ctx) => {
const body = await req.json()
const payment_id = ctx.db.sendPayment(body)
const payment = ctx.db.payments.find((p) => p.payment_id === payment_id)!
return ctx.json(payment)
})
26 changes: 26 additions & 0 deletions tests/routes/payments/send.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("send and list payments", async () => {
const { axios } = await getTestServer()

const { data: payment } = await axios.post("/payments/send", {
recipient: "maintainer@example.com",
amount: 10,
currency: "USD",
bounty_issue: 1,
})

expect(payment).toMatchObject({
payment_id: "payment_0",
recipient: "maintainer@example.com",
amount: 10,
currency: "USD",
bounty_issue: 1,
status: "sent",
})

const { data } = await axios.get("/payments/list")
expect(data.payments).toHaveLength(1)
expect(data.payments[0]).toEqual(payment)
})