Skip to content
Merged
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
1 change: 1 addition & 0 deletions .claude/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
settings.local.json
14 changes: 7 additions & 7 deletions .github/workflows/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
level: error
- run: pnpm run typecheck

biome:
name: runner / biome
oxlint:
name: runner / oxlint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -50,9 +50,9 @@ jobs:
- name: Setup reviewdog
uses: reviewdog/action-setup@v1.3.2
- run: pnpm install
- name: Run Biome with reviewdog
uses: mongolyy/reviewdog-action-biome@v2.0.0
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
- name: Run oxlint with reviewdog
run: |
pnpm exec oxlint --format=checkstyle | reviewdog -f=checkstyle -name=eslint -reporter=github-pr-review
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.github_token }}
- run: pnpm run lint
5 changes: 4 additions & 1 deletion .oxfmtrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"printWidth": 120
"printWidth": 120,
"ignorePatterns": [
"!**/*.{js,ts,jsx,tsx,json,jsonc}"
]
}
20 changes: 19 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
{
"biome.enabled": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript][typescript][javascriptreact][typescriptreact][json][jsonc][css]": {
"[javascript]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[javascriptreact]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[json]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[css]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
},
"[astro]": {
Expand Down
13 changes: 12 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ Use `@/openspec/AGENTS.md` to learn:

Keep this managed block so 'openspec update' can refresh the instructions.

<!-- OPENSPEC:END -->
<!-- OPENSPEC:END -->

## Version Control

This project uses **jj (Jujutsu)** instead of git for version control.

Common commands:
- `jj status` - Show working copy changes
- `jj log` - Show commit history
- `jj commit -m "message"` - Commit working copy with message and start new change
- `jj new` - Create a new change on top of the current one
- `jj squash` - Squash changes into parent commit
61 changes: 61 additions & 0 deletions openspec/changes/add-dynamic-params-support/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Proposal: Add Dynamic Params Support

## Summary

Add support for dynamic path parameters in oRPC routes when generating MSW handlers, including exposing path parameter values to mock handler callbacks.

## Problem Statement

oRPC uses curly brace syntax for path parameters (e.g., `/users/{id}`), while MSW uses colon syntax (e.g., `/users/:id`). This mismatch requires conversion when creating MSW handlers from oRPC routes. Additionally, oRPC supports catch-all parameters with `{+param}` syntax for matching slashes. Path parameter values need to be exposed to mock handler callbacks so developers can use them in dynamic responses.

For example, an oRPC route defined as:
```typescript
os.route({ path: "/users/{id}", method: "GET" })
```

Should generate an MSW handler that matches `/users/:id` and provides the extracted `id` parameter value to the handler callback.

## Proposed Solution

Implement a path normalization function that converts oRPC path parameter syntax to MSW-compatible syntax and ensure path parameters are properly exposed to mock handlers via the `params` object.

### Normalization Rules

| oRPC Syntax | MSW Syntax | Description |
|-------------|------------|-------------|
| `{param}` | `:param` | Named parameter (conversion required) |
| `{+param}` | `:param*` | Catch-all parameter (matches slashes) |

### Implementation Location

The conversion should happen in the `createHandler` function in [msw.ts](../../src/msw.ts) at line 102-104, where `routePath` is constructed before being passed to the MSW handler.

## Scope

### In Scope
- Convert oRPC `{param}` syntax to MSW `:param` syntax in route paths
- Convert oRPC `{+param}` catch-all syntax to MSW wildcard syntax
- Expose path parameters to the mock handler callback via `MSWProcedureInput`
- Handle edge cases (multiple params, params at different positions)

### Out of Scope
- Regex-based route matching
- Query parameter handling (already works via existing serializer)

## Benefits

- Enables full oRPC OpenAPI contract support with dynamic routes
- Maintains type safety for path parameters
- Zero breaking changes to existing API

## Risks & Mitigations

| Risk | Mitigation |
|------|------------|
| Regex edge cases in param names | Use well-tested regex pattern; add comprehensive tests |
| Catch-all `{+param}` behavior may differ between oRPC and MSW | Document the behavior; ensure MSW wildcard syntax matches oRPC semantics |

## Related

- [MSW path matching documentation](https://mswjs.io/docs/basics/matching#path-params)
- [oRPC route configuration](https://orpc.unnoq.com/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Dynamic Path Parameters

Capability for normalizing oRPC path parameters for MSW compatibility and exposing them to mock handlers.

## ADDED Requirements

### Requirement: Path parameter syntax normalization

The library MUST normalize oRPC path parameter syntax for MSW compatibility. oRPC uses curly brace syntax (`{param}`) while MSW uses colon syntax (`:param`), so conversion is required.

#### Scenario: Single path parameter

Given an oRPC route with path `/users/{id}`
When an MSW handler is created for this route
Then the handler MUST convert to `/users/:id` pattern for MSW
And a request to `/users/123` MUST be intercepted by the handler

#### Scenario: Multiple path parameters

Given an oRPC route with path `/users/{userId}/posts/{postId}`
When an MSW handler is created for this route
Then the handler MUST convert to `/users/:userId/posts/:postId` pattern for MSW
And a request to `/users/1/posts/42` MUST be intercepted by the handler

#### Scenario: Path parameter at start of path

Given an oRPC route with path `/{resource}/list`
When an MSW handler is created for this route
Then the handler MUST convert to `/:resource/list` pattern for MSW

#### Scenario: Catch-all path parameter

Given an oRPC route with path `/files/{+path}`
When an MSW handler is created for this route
Then the handler MUST convert to `/files/:path*` pattern for MSW (or equivalent wildcard)
Note: oRPC `{+param}` matches slashes, equivalent to MSW wildcard segments

### Requirement: Path parameter value exposure

The library MUST expose extracted path parameter values to the mock handler callback.

#### Scenario: Access path parameters in handler

Given an oRPC route with path `/users/{id}`
And a mock handler function is provided
When a request to `/users/456` is intercepted
Then the handler callback MUST receive `params` containing `{ id: "456" }`

#### Scenario: Access multiple path parameters

Given an oRPC route with path `/users/{userId}/posts/{postId}`
And a mock handler function is provided
When a request to `/users/1/posts/99` is intercepted
Then the handler callback MUST receive `params` containing `{ userId: "1", postId: "99" }`

### Requirement: Static path segments preserved

The library MUST preserve static path segments unchanged during normalization.

#### Scenario: Mixed static and dynamic segments

Given an oRPC route with path `/api/v1/users/{id}/profile`
When an MSW handler is created for this route
Then the handler MUST convert to `/api/v1/users/:id/profile` pattern for MSW
And static segments `/api/v1/users/` and `/profile` MUST remain unchanged

### Requirement: Path parameter merging based on inputStructure

The library MUST merge path parameters into the input object based on the route's `inputStructure` setting, matching oRPC's behavior.

#### Scenario: Compact mode merges params into input (default)

Given an oRPC route with path `/users/{id}` and default inputStructure (compact)
And a mock handler function is provided
When a request to `/users/456` is intercepted with body `{ "name": "John" }`
Then the handler callback MUST receive `input` containing `{ id: "456", name: "John" }`
Note: Path params are merged with body/query data in compact mode

#### Scenario: Compact mode with GET request

Given an oRPC route with path `/users/{id}` method GET and default inputStructure (compact)
And a mock handler function is provided
When a request to `/users/456?search=test` is intercepted
Then the handler callback MUST receive `input` containing `{ id: "456", search: "test" }`

#### Scenario: Detailed mode keeps params separate

Given an oRPC route with path `/users/{id}` and `inputStructure: 'detailed'`
And a mock handler function is provided
When a request to `/users/456` is intercepted with body `{ "name": "John" }`
Then the handler callback MUST receive `input` containing `{ params: { id: "456" }, body: { name: "John" } }`
Note: In detailed mode, params are NOT merged into the top-level input

#### Scenario: Params always available separately

Given any oRPC route with path parameters
When a request is intercepted
Then the handler callback MUST always receive `params` as a separate field in MSWProcedureInput
Note: The `params` field is always available regardless of inputStructure for cases where handlers need direct access
67 changes: 67 additions & 0 deletions openspec/changes/add-dynamic-params-support/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Tasks: Add Dynamic Params Support

## Implementation Tasks

### 1. Add path normalization utility function
- [x] Create `convertOrpcPathToMsw(path: string): string` function in `msw.ts`
- [x] Convert oRPC curly brace syntax `{param}` to MSW colon syntax `:param`
- [x] Convert catch-all syntax `{+param}` to MSW wildcard syntax `:param*`

**Validation**: Unit tests for normalization function with various path patterns

### 2. Integrate normalization in handler creation
- [x] Call `convertOrpcPathToMsw()` on `routePath` before passing to MSW handler
- [x] Location: `createHandler` function around line 104

**Validation**: Existing tests continue to pass (no regression)

### 3. Expose path params in MSWProcedureInput
- [x] Extract path parameters from MSW request info
- [x] Add `params: Record<string, string | readonly string[] | undefined>` to `MSWProcedureInput` interface
- [x] Pass params to mock handler callback

**Validation**: New tests verify params are accessible in handler

### 4. Add comprehensive tests
- [x] Test single param: `/users/{id}` -> `/users/:id` -> matches `/users/123`
- [x] Test multiple params: `/users/{userId}/posts/{postId}` -> `/users/:userId/posts/:postId`
- [x] Test param at start: `/{resource}/list` -> `/:resource/list`
- [x] Test catch-all param: `/files/{+path}` -> `/files/:path*`
- [x] Test mixed static and dynamic segments: `/api/v1/users/{id}/profile`
- [x] Test param value extraction in mock handler

**Validation**: All new tests pass

### 5. Update documentation
- [x] Add JSDoc comments to new function and interface changes
- [x] Update type definitions for `MSWProcedureInput`

**Validation**: Types compile without errors

### 6. Implement params merging based on inputStructure
- [x] Read `inputStructure` from route config (default: 'compact')
- [x] Add `normalizeParams()` function to convert MSW array params to joined strings
- [x] In compact mode: merge path params into input object alongside query/body data
- [x] In detailed mode: structure input as `{ params, query, body }`
- [x] Handle case where queryOrBody is primitive (no path params case)

**Validation**: Tests verify params are correctly merged into input

### 7. Add tests for params merging behavior
- [x] Test PUT request with path params merged into body
- [x] Test GET request with path params merged into query
- [x] Test that both `input` (merged) and `params` (raw) are available
- [x] Test catch-all params are joined as string in input

**Validation**: All new tests pass (42 total tests passing)

## Dependencies

- Task 1 must complete before Task 2
- Task 2 must complete before Task 3
- Tasks 4 and 5 can be done in parallel after Task 3

## Parallelizable Work

- Test writing (Task 4) can begin once Task 2 is complete for basic tests
- Documentation (Task 5) can be done in parallel with testing
Loading