Skip to content
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

Fix Axios adapter binding for Cloudflare Workers environment #257

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

tharropoulos
Copy link
Contributor

Change Summary

What is this?

This PR addresses a critical issue where Typesense library users encounter "Illegal invocation" errors when using custom Axios adapters in Cloudflare Workers environment. The error occurs because the fetch adapter loses its this context when used in Workers. This implementation ensures proper binding of the adapter to the correct context, making the library usable on edge computing environments.

Changes

Added Features:

  1. New Method in ApiCall.ts:
    • getAdapter(): A private method that intelligently handles Axios adapter configuration:
      • Safely returns undefined when no adapter is configured
      • Preserves function adapters as-is when directly provided
      • Automatically detects Cloudflare Workers environment
      • Binds the adapter to globalThis specifically in Workers environment

Code Changes:

  1. In ApiCall.ts:
    • Import AxiosAdapter type for better type safety
    • Replace direct adapter usage with the new getAdapter() method in performRequest
    • Add environment detection logic for Cloudflare Workers
    • Implement proper this binding for the adapter in Workers context

Context

On issue #254 @isaacrowntree encountered that behavior on an edge runtime on Cloudflare Workers.

PR Checklist

- add `getAdapter` method to handle custom axios adapters
- fix `this` binding issue in cloudflare workers environment
- add type checking for function adapters
- replace direct adapter usage with `getAdapter` method
@jasonbosco jasonbosco merged commit f640cdd into typesense:master Jan 17, 2025
1 check 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.

2 participants