|
| 1 | +import { CodeBlock } from '@/components/code-block'; |
| 2 | +import { EditPageLink } from '@/components/edit-page-link'; |
| 3 | + |
| 4 | +export const metadata = { |
| 5 | + title: 'API Design (Typed vs. Untyped)', |
| 6 | + description: |
| 7 | + 'How LogStruct balances an idiomatic Rails logging API with an optional typed path for teams using sorbet-runtime.', |
| 8 | +}; |
| 9 | + |
| 10 | +export default function ApiDesignPage() { |
| 11 | + const untypedExample = `# Untyped, idiomatic Rails logging (works out of the box) |
| 12 | +Rails.logger.info({ |
| 13 | + msg: "User signed in", |
| 14 | + user_id: current_user.id, |
| 15 | + feature: "onboarding" |
| 16 | +})`; |
| 17 | + |
| 18 | + const typedExample = `# Optional typed API (sorbet-runtime) |
| 19 | +log = LogStruct::Log::Request.new( |
| 20 | + message: "GET /projects", |
| 21 | + event: LogStruct::Event::Request, |
| 22 | + source: LogStruct::Source::Rails, |
| 23 | + controller: "ProjectsController", |
| 24 | + action: "index" |
| 25 | +) |
| 26 | +
|
| 27 | +LogStruct.info(log)`; |
| 28 | + |
| 29 | + const customStructSketch = `# Sketch: defining a custom typed log struct |
| 30 | +class MyApp::Logs::Checkout < T::Struct |
| 31 | + include LogStruct::Log::Interfaces::CommonFields |
| 32 | + include LogStruct::Log::Interfaces::AdditionalDataField |
| 33 | +
|
| 34 | + const :event, LogStruct::Event::Log |
| 35 | + const :source, LogStruct::Source, default: T.let(LogStruct::Source::App, LogStruct::Source) |
| 36 | + const :message, String |
| 37 | + const :cart_id, String |
| 38 | + const :amount_cents, Integer |
| 39 | +end |
| 40 | +
|
| 41 | +# Then log it with |
| 42 | +LogStruct.info( |
| 43 | + MyApp::Logs::Checkout.new(message: "checkout_completed", cart_id: cart.id, amount_cents: 1299) |
| 44 | +)`; |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="space-y-8"> |
| 48 | + <h1 className="text-3xl font-bold">API Design: Typed vs. Untyped</h1> |
| 49 | + <p className="text-neutral-600 dark:text-neutral-400"> |
| 50 | + LogStruct is designed for an idiomatic Rails experience first, with an |
| 51 | + optional typed path for teams that use sorbet-runtime. Most Rails |
| 52 | + developers can adopt LogStruct without learning Sorbet. Teams wanting |
| 53 | + stronger guarantees can progressively introduce typed log structs. |
| 54 | + </p> |
| 55 | + |
| 56 | + <h2 className="text-2xl font-semibold">Untyped, Idiomatic Rails</h2> |
| 57 | + <p> |
| 58 | + You can continue using <code>Rails.logger</code> with hashes and |
| 59 | + strings. LogStruct's formatter scrubs sensitive values and keeps |
| 60 | + output JSON-friendly. |
| 61 | + </p> |
| 62 | + <CodeBlock language="ruby">{untypedExample}</CodeBlock> |
| 63 | + |
| 64 | + <h2 className="text-2xl font-semibold">Optional Typed Path</h2> |
| 65 | + <p> |
| 66 | + For teams that want stricter contracts, use LogStruct's typed |
| 67 | + structs. These are runtime-checked via sorbet-runtime and integrate with |
| 68 | + our formatter seamlessly. |
| 69 | + </p> |
| 70 | + <CodeBlock language="ruby">{typedExample}</CodeBlock> |
| 71 | + |
| 72 | + <h2 className="text-2xl font-semibold"> |
| 73 | + Custom Typed Structures (Sketch) |
| 74 | + </h2> |
| 75 | + <p> |
| 76 | + You can define app-specific typed logs by composing LogStruct |
| 77 | + interfaces. Keep this ergonomic and discoverable; the untyped path |
| 78 | + remains first-class. |
| 79 | + </p> |
| 80 | + <CodeBlock language="ruby">{customStructSketch}</CodeBlock> |
| 81 | + |
| 82 | + <EditPageLink path="app/docs/api-design/page.tsx" /> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
0 commit comments