-
Notifications
You must be signed in to change notification settings - Fork 2
feat: functional_status_codes v3.2.0
#124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39713ef
feat: add new features and fixes for version 3.2.0
tsinis aee7bc0
feat(llm): add api reference, functional patterns guide, and prompts
tsinis 75dff9b
fix(spell): correct regex for status code pattern matching
tsinis 1120102
feat: enhance api reference and patterns documentation
tsinis 2ca9fe9
refactor: update caching strategy and response handling documentation
tsinis fb5adc7
fix: update comments and tests for status code handling
tsinis ae828e8
fix: improve regex explanation for http status code pattern matching
tsinis 7a61afa
fix: clarify regex assertions in status code pattern matching
tsinis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| resources: | ||
| - title: "API Reference" | ||
| description: "Concise cheat-sheet for all StatusCode constants, getters, methods, | ||
| and functional helpers. Include in agent context when working with HTTP status codes." | ||
| path: extension/mcp/resources/api_reference.md | ||
|
|
||
| - title: "Functional Patterns Guide" | ||
| description: "Idiomatic patterns for when/map/maybeWhen/const variants, | ||
| isCacheable, isRetryable, and real-world HTTP client integration." | ||
| path: extension/mcp/resources/patterns.md | ||
|
|
||
| prompts: | ||
| - title: "Handle HTTP Response" | ||
| description: "Generate idiomatic functional_status_codes code to handle an HTTP | ||
| response. Provide http_client (http, dio, or native) and optionally status_code." | ||
| path: extension/mcp/prompts/handle_response.md | ||
| arguments: | ||
| - name: http_client | ||
| description: "HTTP client in use: 'http', 'dio', or 'native' (dart:io)" | ||
| required: false | ||
| - name: status_code | ||
| description: "Specific status code to handle (e.g. 404). Leave blank for general handling." | ||
| required: false | ||
|
|
||
| - title: "Cache Strategy" | ||
| description: "Derive a caching strategy for an HTTP response using isCacheable | ||
| and RFC 7231 semantics." | ||
| path: extension/mcp/prompts/cache_strategy.md | ||
| arguments: | ||
| - name: status_code | ||
| description: "HTTP status code to evaluate (e.g. 200)" | ||
| required: true | ||
| - name: cache_backend | ||
| description: "Cache storage backend being used, if any" | ||
| required: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| Derive a caching strategy for HTTP status code **{{status_code}}** using the `functional_status_codes` package and RFC 7231 semantics. | ||
|
|
||
| {{#cache_backend}} | ||
| The cache backend in use is: **{{cache_backend}}** | ||
| {{/cache_backend}} | ||
| {{^cache_backend}} | ||
| Use a generic `cache.store(key, value, {Duration? ttl})` / `cache.get(key)` interface in the example. The caller can adapt it to their backend. | ||
| {{/cache_backend}} | ||
|
|
||
| Follow these steps: | ||
|
|
||
| 1. **Evaluate cacheability** using `.isCacheable`: | ||
|
|
||
| The RFC 7231 cacheable set is: `200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501`. | ||
|
|
||
| ```dart | ||
| if ({{status_code}}.isCacheable) { | ||
| // safe to cache the response body | ||
| } | ||
| ``` | ||
|
|
||
| 2. **Assign a cache TTL** appropriate for code `{{status_code}}`: | ||
| - `200 OK` β moderate TTL (e.g. 5 minutes), respect `Cache-Control` headers when present | ||
| - `301 Moved Permanently` β long TTL (e.g. 1 day), permanent redirect is stable | ||
| - `404 Not Found` β short negative-cache TTL (e.g. 1-10 minutes) to avoid repeated misses | ||
| - `405 Method Not Allowed`, `410 Gone` β moderate TTL, these are stable server decisions | ||
| - `501 Not Implemented` β long TTL, unlikely to change soon | ||
| - `204`, `203`, `206`, `300`, `414` β short to moderate TTL depending on use case | ||
|
|
||
| Use `whenConstOrNull` to express this as a compile-time mapping (`whenConstOrNull` is on `StatusCode`, so convert from raw `int` first): | ||
|
|
||
| ```dart | ||
| final ttl = rawStatusCode.toRegisteredStatusCode()?.whenConstOrNull( | ||
| okHttp200: Duration(minutes: 5), | ||
| noContentHttp204: Duration(minutes: 1), | ||
| notFoundHttp404: Duration(minutes: 10), | ||
| movedPermanentlyHttp301: Duration(days: 1), | ||
| goneHttp410: Duration(hours: 1), | ||
| notImplementedHttp501: Duration(hours: 6), | ||
| ); | ||
| ``` | ||
|
|
||
| 3. **Generate the store/skip logic**: | ||
| - If `isCacheable` is `true` for `{{status_code}}`: generate a `cache.store(...)` call{{#cache_backend}} adapted to **{{cache_backend}}**{{/cache_backend}} | ||
| - If `isCacheable` is `false`: explain why this code should not be cached and suggest adding a `Cache-Control: no-store` header to the response if you control the server | ||
|
|
||
| 4. **Emit a complete, reusable function** that: | ||
| - Checks `isStatusCode` before doing anything (guards against invalid codes) | ||
| - Uses `isCacheable` to gate the write | ||
| - Uses `toRegisteredStatusCode()?.whenConstOrNull(...)` for TTL assignment β convert first since these methods are on `StatusCode`, not raw `int` (no runtime allocations) | ||
| - Falls back gracefully for non-cacheable codes (log and skip, do not throw) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| Generate idiomatic Dart code to handle an HTTP response using the `functional_status_codes` package. | ||
|
|
||
| {{#http_client}} | ||
| The HTTP client in use is: **{{http_client}}** | ||
| {{/http_client}} | ||
| {{^http_client}} | ||
| Detect the HTTP client from the existing code (look for `http.Response`, `dio.Response`, `HttpClientResponse`, or a plain `int` status code field). | ||
| {{/http_client}} | ||
|
|
||
| {{#status_code}} | ||
| Focus specifically on handling status code **{{status_code}}** (in addition to general handling). | ||
| {{/status_code}} | ||
|
|
||
| Follow these steps: | ||
|
|
||
| 1. **Extract the status code** as `int` or `num` from the response object: | ||
| - `package:http` β `response.statusCode` (already `int`) | ||
| - `package:dio` β `response.statusCode` (already `int?`) | ||
| - `dart:io HttpClientResponse` β `response.statusCode` (already `int`) | ||
| - Custom β use whatever field holds the integer status | ||
|
|
||
| 2. **Apply category-level branching** using `maybeWhenStatusCode` or `maybeMapStatusCode`: | ||
| - Use `maybeWhenStatusCode` when the callbacks do not need the code value passed in | ||
| - Use `maybeMapStatusCode` when you need `(code) =>` in the callback | ||
| - Provide `orElse` for codes outside 100-599 or unhandled categories | ||
| - Do **not** provide `isStatusCode` alongside category handlers unless you intentionally want it to catch all valid codes first | ||
|
|
||
| 3. **Drill into specific codes** when needed by converting first: | ||
|
|
||
| ```dart | ||
| final registered = rawCode.toRegisteredStatusCode(); | ||
| final result = registered?.maybeMap( | ||
| orElse: (c) => defaultHandling(c), | ||
| notFoundHttp404: (_) => handleNotFound(), | ||
| ); | ||
| ``` | ||
|
|
||
| 4. **Emit null-safe, idiomatic Dart**: | ||
| - Prefer `?.` and `??` over null-checks | ||
| - Use `whenConstStatusCode` / `whenConstOrNull` when the return values are constants β these accept **direct values**, not closures (e.g. `isSuccess: 'ok'`, never `isSuccess: () => 'ok'`); they must be called on a `StatusCode` value after `toRegisteredStatusCode()` conversion | ||
| - Avoid `if (code >= 200 && code < 300)` β use `.isSuccess` / `.isError` / `.isCacheable` etc. | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 5. **Handle the specific code** {{#status_code}}({{status_code}}){{/status_code}} with a dedicated branch inside `maybeMap` or `maybeWhen` on the `StatusCode`, using the constant name `<camelName>Http<NNN>`. | ||
|
|
||
| Produce a complete, runnable function or method β not just a snippet β with appropriate return type and error handling. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.