refactor(types): replace JsonDict with type-fest JsonObject and JsonValue#228
Merged
refactor(types): replace JsonDict with type-fest JsonObject and JsonValue#228
Conversation
…alue Remove the custom `JsonDict = Record<string, unknown>` type alias in favour of type-fest's stricter `JsonObject` and `JsonValue` types. Key changes to src/types.ts: - Import and re-export `JsonObject` and `JsonValue` from type-fest - Update `JSONSchema` interface to use `JsonValue` instead of `unknown` for properties like `enum`, `const`, `default`, and `examples` - Add explicit index signature union type for JSONSchema extensibility This provides stronger type safety by ensuring values are actually JSON-serialisable rather than accepting arbitrary `unknown` values.
Migrate all source files from the removed `JsonDict` type to the type-fest `JsonObject` type. This commit updates method signatures and internal type annotations across the codebase. Files updated: - src/tool.ts: Update execute method signatures and meta tool returns - src/toolsets.ts: Update parameter and return types - src/feedback.ts: Add explicit array types for results/errors, use JsonValue for parsed responses - src/headers.ts: Update normaliseHeaders parameter type Notable implementation changes: - meta_search_tools now uses JSON.parse(JSON.stringify(...)) to ensure the return value is JSON-serialisable (removes undefined values from JSONSchema objects) - Feedback tool now explicitly types its internal arrays to ensure type safety with the stricter JsonObject constraint
…ialisation Add a dedicated `stringifyValue()` function to safely convert JsonValue types to strings for use in URLs, headers, and form data. This replaces direct `String()` calls which could produce `[object Object]` for complex values. The helper function: - Returns strings as-is - Converts numbers and booleans via String() - Returns empty string for null values - JSON-stringifies arrays and objects Also updates the dry run response to properly serialise headers and body to JSON-compatible values using `satisfies JsonObject` for type checking.
Update test files to work with the stricter JsonObject type: src/tool.test.ts: - Add isMetaToolSearchResults type guard function - Add getSearchResults helper to safely extract search results - Replace direct type assertions with type-safe helper calls src/requestBuilder.test.ts: - Update dry run test expectation: body is now `null` instead of `undefined` when no body params exist (null is JSON-serialisable) - Add explicit type casts for runtime edge case tests that intentionally pass invalid types (Date, RegExp, circular refs) src/headers.test.ts: - Remove test for undefined values (not valid in JsonObject) - Keep null value test (null is a valid JSON value)
Update example files to work with the new JsonObject type:
examples/meta-tools.ts:
- Import JsonObject from @stackone/ai
- Add explicit type annotation for intents array using
`as const satisfies Array<{ intent: string; params: JsonObject }>`
examples/tanstack-ai-integration.test.ts:
- Simplify test structure and remove redundant assertions
eab95cb to
cd11a6d
Compare
commit: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
JsonDict = Record<string, unknown>with type-fest's stricterJsonObjectandJsonValuetypesstringifyValue()helper for type-safe JSON value serialisation in URLs, headers, and form dataMotivation
The previous
JsonDicttype (Record<string, unknown>) was too permissive, allowing any value including non-JSON-serialisable types likeundefined,Date,RegExp, and functions. By using type-fest'sJsonObjectandJsonValue, we get compile-time guarantees that values are actually JSON-serialisable.Key Changes
Types (
src/types.ts)JsonObjectandJsonValuefrom type-festJSONSchemainterface to useJsonValueforenum,const,default,examplesRequest Builder (
src/requestBuilder.ts)stringifyValue()helper that properly handles all JSON value typesnullinstead ofundefinedfor empty bodyMeta Tools (
src/tool.ts)JSON.parse(JSON.stringify(...))to ensure search results are JSON-serialisableTests
undefinedvalues (not valid inJsonObject)Test plan
Summary by cubic
Replaced the custom JsonDict type with type-fest’s JsonObject and JsonValue for stricter, JSON-serializable types across the codebase. Added a stringifyValue helper for safe URL, header, and form serialization, and made dry-run responses fully JSON-safe.
Refactors
Migration
Written for commit cd11a6d. Summary will update automatically on new commits.