Skip to content

[Refactor] sdk/src/client.ts: centralize registry API error handling into apiGet #15

@shwetank-dev

Description

@shwetank-dev

Problem

Every public API method in MpakClient (8 in total) repeats the same two-block error check after every fetchWithTimeout call:

if (response.status === 404) {
  throw new MpakNotFoundError(...);
}
if (!response.ok) {
  throw new MpakNetworkError(`Failed to ...: HTTP ${response.status}`);
}

This means adding a new error type (e.g. 429 rate limit, 401 unauthorized) requires touching every single method individually.

Proposed fix

Replace fetchWithTimeout with a private apiGet method that owns the full lifecycle of a registry API call — timeout, headers, error handling, and JSON parsing — and returns Promise<unknown>:

private async apiGet(
  url: string,
  init?: RequestInit & { notFoundKey?: string },
): Promise<unknown>
  • If notFoundKey is provided and the response is 404, throw MpakNotFoundError(notFoundKey)
  • If response is not ok, throw MpakNetworkError
  • If response is ok, return response.json()

Callers then become a single line each:

return this.apiGet(url, { notFoundKey: name }) as Promise<BundleDetailResponse>;

Future error types are added in one place only.

Out of scope

Content downloads (downloadSkillContent, skill resolution) fetch arbitrary external URLs and return raw text — they are a different concern and not covered by this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions