Skip to content

Conversation

@AaronDDM
Copy link
Collaborator

Changelog

Fixed

  • Broken CJS build outputs resulted in a "TypeError: Nylas is not a constructor" error

License

I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

@codecov
Copy link

codecov bot commented Sep 18, 2025

Codecov Report

❌ Patch coverage is 61.11111% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.34%. Comparing base (cd02642) to head (7eef782).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/utils/fetchWrapper.ts 36.36% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #668      +/-   ##
==========================================
- Coverage   94.87%   93.34%   -1.53%     
==========================================
  Files          35       37       +2     
  Lines         781      812      +31     
  Branches       66       72       +6     
==========================================
+ Hits          741      758      +17     
- Misses         33       47      +14     
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AaronDDM AaronDDM changed the title V7.13.1 Release v7.13.1 Sep 18, 2025
@playerzero-ai
Copy link

playerzero-ai bot commented Sep 18, 2025

Pull Request Summary

Summary of what this PR changes and why (product/functionality/UX)

  • Runtime fetch resolution

    • The client no longer hard-codes node-fetch. fetch, Request and Response are resolved dynamically at runtime via new async helpers (getFetch/getRequest/getResponse).
    • This makes the SDK compatible with environments that provide a native fetch (browsers, newer Node) as well as Node CJS/Esm environments that need node-fetch — it prefers globals when present and otherwise dynamically imports node-fetch.
    • The dynamic import is cached to avoid repeated loads.
  • apiClient behavior

    • newRequest() is now async (returns a Promise) and sendRequest() awaits it and awaits the runtime fetch. Tests were updated to await newRequest.
    • Existing request building, timeout handling, error parsing and response processing logic remain unchanged.
    • Practical UX effect: most high-level APIs (which call sendRequest) are unaffected, but any code calling newRequest() directly must now await it.
  • Multiple fetch-wrapper implementations + build orchestration

    • Added two implementations: an ESM wrapper (fetchWrapper-esm.ts) and a CJS wrapper (fetchWrapper-cjs.ts). Both expose async getters but use static import (ESM) vs dynamic import (CJS) as appropriate.
    • Added a build helper script (scripts/setupFetchWrapper.js) to copy the correct implementation into src/utils/fetchWrapper.ts before building.
    • tsconfig files were adjusted to exclude the opposite-format wrapper from each build (avoid emitting ESM code into CJS output and vice versa).
  • Tests and coverage

    • Existing test updated to treat newRequest as async to avoid flakes.
    • Many new tests added covering both ESM and CJS fetch wrappers and overall fetch-wrapper behavior:
      • Verify preference for global fetch/Request/Response when present.
      • Verify fallback to dynamic import of node-fetch when globals are missing.
      • Verify Request/Response behavior (headers, methods, status, .json/.text parsing) without network calls.
      • Simulate environment permutations and ensure module isolation and cleanup in tests.
    • These tests increase confidence across runtime environments and prevent regressions.
  • Developer ergonomics and packaging

    • tsconfig.json now sets baseUrl to src to allow non-relative imports from src (developer convenience).
    • Version bumped from 7.13.0 → 7.13.1.

Risk / user-facing impact (concise)

  • No changes to request semantics, error handling, or high-level APIs that use sendRequest.
  • Callers that directly used client.newRequest synchronously must now await it (the tests were updated to reflect this).
  • Build/publishing pipeline must run the setupFetchWrapper step (or otherwise ensure the correct wrapper is in place). tsconfig path changes may require downstream tooling configuration (e.g., ts-node, jest, bundlers) to pick up baseUrl if they rely on TypeScript path resolution.

Overall intent

  • Make fetch usage environment-agnostic and testable by resolving fetch/Request/Response at runtime, add robust tests for the new behavior, and ensure correct ESM/CJS build outputs via per-format wrappers and a small build script — all while keeping request semantics unchanged.

Files Changed

File Name Summary
src/apiClient.ts Removed hard-coded node-fetch usage; newRequest() and sendRequest() now resolve Request/fetch at runtime via async getRequest()/getFetch() from utils/fetchWrapper; request building, timeout, error parsing and response handling unchanged.
tests/apiClient.spec.ts Test updated so newRequest() is awaited: test made async and calls await client.newRequest(...); prevents flakes by treating API as async; no product behavior change.
tsconfig.esm.json Added "exclude": ["src/utils/fetchWrapper-cjs.ts"] to keep the CJS fetch wrapper out of ESM output.
tests/utils/fetchWrapper-esm.spec.ts New ESM-wrapper unit tests: verify getFetch/getRequest/getResponse return stable constructors/functions and correctly handle methods, headers, status and body parsing without network calls.
tests/utils/fetchWrapper.spec.ts New tests for main fetchWrapper: preferring globals, falling back to dynamic node-fetch import (mocked), and integration-style checks with apiClient; ensures environment detection and cleanup.
src/utils/fetchWrapper.ts New async helpers (getFetch/getRequest/getResponse) that prefer globals and dynamically import node-fetch via Function-based import; caches module and exports compatible types for CJS usage.
tsconfig.json Added "baseUrl": "./src" to enable non-relative imports from src; minor trailing-comma formatting change.
src/utils/fetchWrapper-esm.ts ESM-specific wrapper: statically imports node-fetch and exports async getters (getFetch/getRequest/getResponse) plus types; intended for ESM builds.
tests/utils/fetchWrapper-cjs.spec.ts New CJS-wrapper tests: verify fallback dynamic-import path (Function-based), prefer globals when present, mock node-fetch/dynamic import, and ensure isolation/cleanup.
src/version.ts Bumped SDK version from 7.13.0 to 7.13.1 (patch release, no logic changes).
src/utils/fetchWrapper-cjs.ts CJS-targeted fetch wrapper: async getFetch/getRequest/getResponse that prefer globals and dynamically import node-fetch (Function-based), caching the import; exports types as any for CJS.
scripts/setupFetchWrapper.js New script to copy the correct wrapper (esm
tsconfig.cjs.json Added "exclude": ["src/utils/fetchWrapper-esm.ts"] to prevent ESM-specific file from being compiled into the CJS build.

View more in PlayerZero
updated: Sep 22 @ 08:51 PM UTC

- Add fetchWrapper system with separate CJS/ESM implementations
- Add setupFetchWrapper.js script for build-time wrapper selection
- Update TypeScript configs to support dual module builds
- Modify apiClient to use new fetch wrapper system
- Update tests for new fetch handling approach

This improves compatibility between CommonJS and ES modules by using
dynamic imports for node-fetch in CJS builds while maintaining
native fetch support in ESM builds.
@AaronDDM AaronDDM merged commit 9b8d8b8 into main Sep 23, 2025
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants