Skip to content

Commit

Permalink
Fix compatibility with Cloudflare Workers (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Feb 25, 2024
1 parent 38ac18b commit d8ee602
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/core/Ky.ts
Expand Up @@ -118,9 +118,9 @@ export class Ky {
// eslint-disable-next-line complexity
constructor(input: Input, options: Options = {}) {
this._input = input;
const isCredentialsSupported = 'credentials' in Request.prototype;
this._options = {
// TODO: credentials can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208
credentials: (this._input as Request).credentials || 'same-origin',
credentials: isCredentialsSupported ? (this._input as Request).credentials : undefined,
...options,
headers: mergeHeaders((this._input as Request).headers, options.headers),
hooks: deepMerge<Required<Hooks>>(
Expand Down
5 changes: 3 additions & 2 deletions source/types/options.ts
Expand Up @@ -239,12 +239,13 @@ export interface Options extends KyOptions, Omit<RequestInit, 'headers'> { // es

export type InternalOptions = Required<
Omit<Options, 'hooks' | 'retry'>,
'credentials' | 'fetch' | 'prefixUrl' | 'timeout'
'fetch' | 'prefixUrl' | 'timeout'
> & {
headers: Required<Headers>;
hooks: Required<Hooks>;
retry: Required<RetryOptions>;
prefixUrl: string;
credentials?: Options['credentials']; // Allows credentials to be undefined for workers
};

/**
Expand All @@ -253,7 +254,7 @@ Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
export interface NormalizedOptions extends RequestInit { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- This must stay an interface so that it can be extended outside of Ky for use in `ky.create`.
// Extended from `RequestInit`, but ensured to be set (not optional).
method: NonNullable<RequestInit['method']>;
credentials: NonNullable<RequestInit['credentials']>;
credentials: RequestInit['credentials'];

// Extended from custom `KyOptions`, but ensured to be set (not optional).
retry: RetryOptions;
Expand Down

0 comments on commit d8ee602

Please sign in to comment.