Skip to content

Commit

Permalink
add api support for resistors list
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Nov 11, 2024
1 parent d06b3fd commit 6bde1b4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
12 changes: 12 additions & 0 deletions lib/middlewares/with-is-api-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Middleware } from "winterspec"

export const withIsApiRequest: Middleware<{}, { isApiRequest: boolean }> = (
req,
ctx,
next,
) => {
ctx.isApiRequest =
req.url.includes("json=") ||
req.headers.get("content-type") === "application/json"
return next(req, ctx)
}
3 changes: 2 additions & 1 deletion lib/util/format-price.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const formatPrice = (price: number) => {
export const formatPrice = (price: number | null) => {
if (!price) return ""
return price.toLocaleString("en-US", {
style: "currency",
currency: "USD",
Expand Down
2 changes: 2 additions & 0 deletions lib/with-winter-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withCtxReact } from "./middlewares/with-ctx-react"
import { createWithWinterSpec } from "winterspec"
import { withRequestLogging } from "./middlewares/with-request-logging"
import { withCacheHeaders } from "./middlewares/with-cache-headers"
import { withIsApiRequest } from "./middlewares/with-is-api-request"

export const withWinterSpec = createWithWinterSpec({
authMiddleware: {},
Expand All @@ -16,6 +17,7 @@ export const withWinterSpec = createWithWinterSpec({
withDb,
withRequestLogging,
withCacheHeaders,
withIsApiRequest,
],

apiName: "tscircuit JLC Search",
Expand Down
52 changes: 43 additions & 9 deletions routes/resistors/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { formatPrice } from "lib/util/format-price"

export default withWinterSpec({
auth: "none",
methods: ["GET"],
queryParams: z.object({
methods: ["GET", "POST"],
commonParams: z.object({
json: z.boolean().optional(),
package: z.string().optional(),
resistance: z
.string()
Expand All @@ -20,7 +21,22 @@ export default withWinterSpec({
return parsed.value
}),
}),
jsonResponse: z.any(),
jsonResponse: z.string().or(
z.object({
resistors: z.array(
z.object({
lcsc: z.number().int(),
mfr: z.string(),
package: z.string(),
resistance: z.number(),
tolerance_fraction: z.number().optional(),
power_watts: z.number().optional(),
stock: z.number().optional(),
price1: z.number().optional(),
}),
),
}),
),
} as const)(async (req, ctx) => {
// Start with base query
let query = ctx.db
Expand All @@ -29,14 +45,16 @@ export default withWinterSpec({
.limit(100)
.orderBy("stock", "desc")

const params = req.commonParams

// Apply package filter
if (req.query.package) {
query = query.where("package", "=", req.query.package)
if (params.package) {
query = query.where("package", "=", params.package)
}

// Apply exact resistance filter
if (req.query.resistance !== undefined) {
query = query.where("resistance", "=", req.query.resistance)
if (params.resistance !== undefined) {
query = query.where("resistance", "=", params.resistance)
}

// Get unique packages for dropdown
Expand All @@ -47,6 +65,22 @@ export default withWinterSpec({
.execute()

const resistors = await query.execute()
if (ctx.isApiRequest) {
return ctx.json({
resistors: resistors
.map((r) => ({
lcsc: r.lcsc ?? 0,
mfr: r.mfr ?? "",
package: r.package ?? "",
resistance: r.resistance ?? 0,
tolerance_fraction: r.tolerance_fraction ?? undefined,
power_watts: r.power_watts ?? undefined,
stock: r.stock ?? undefined,
price1: r.price1 ?? undefined,
}))
.filter((r) => r.lcsc !== 0 && r.package !== ""),
})
}

return ctx.react(
<div>
Expand All @@ -61,7 +95,7 @@ export default withWinterSpec({
<option
key={p.package}
value={p.package ?? ""}
selected={p.package === req.query.package}
selected={p.package === params.package}
>
{p.package}
</option>
Expand All @@ -75,7 +109,7 @@ export default withWinterSpec({
type="text"
name="resistance"
placeholder="e.g. 10kΩ"
defaultValue={formatSiUnit(req.query.resistance)}
defaultValue={formatSiUnit(params.resistance)}
/>
</div>

Expand Down

0 comments on commit 6bde1b4

Please sign in to comment.