fix: normalize bare-array list responses into { Data, TotalRecords } - #78
Merged
Merged
Conversation
The live ConnectWise Automate REST API returns list endpoints
(/Computers, /Clients, /Locations, /Alerts, /Contacts, /Groups,
/Groups/:id/Members, /Patches, /Patches/ComputerPatches, /Scripts) as a
bare JSON array, but this library's types model them as
{ Data, TotalRecords }. Because HttpClient.handleResponse does an
unchecked `response.json() as T` cast, the mismatch compiled cleanly
and only surfaced at runtime as `response.Data` being undefined for
every consumer.
Add normalizeListResponse<T>() (types/common.ts) and apply it in every
resource's list()-shaped method plus PaginatedIterable, so either shape
is accepted and callers always get a real { Data, TotalRecords }
object back.
Fixes #38
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
The live ConnectWise Automate REST API returns list endpoints (
/Computers,/Clients,/Locations,/Alerts,/Contacts,/Groups,/Groups/:id/Members,/Patches,/Patches/ComputerPatches,/Scripts) as a bare JSON array, but this library's types model them as{ Data, TotalRecords }.HttpClient.handleResponsedoes an uncheckedresponse.json() as Tcast, so the mismatch compiled cleanly and only surfaced at runtime — any consumer readingresponse.Datagotundefined(Cannot read properties of undefined (reading 'length'), etc).This was already hit downstream in
connectwise-automate-mcp(#35), which now compensates by normalizing both shapes at its own boundary (src/utils/results.ts) — this PR fixes it at the source so every other consumer doesn't have to re-implement the same shim.Changes
normalizeListResponse<T>()helper insrc/types/common.ts: accepts either a bareT[]or the documented{ Data, TotalRecords }envelope and always returns the envelope shape.alerts.list,clients.list,clients(Locations).list,computers.list,contacts.list,groups.list,groups.members,patches.list,patches.computerPatches,scripts.list.PaginatedIterableinpagination.ts(backs every resource'slistAll()), which had the identicalresponse.Dataassumption called out in the issue.Test plan
npm run typecheck— cleannpm test— 95/95 pass (added 4: a unit suite fornormalizeListResponsecovering bare-array, empty-array, and pass-through-envelope cases, plus an integration test assertingcomputers.list()correctly unwraps a mocked bare-array/Computersresponse)npm run build— cleannpm run lint— 0 errors/warningsFixes #38
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.