Skip to content

Conversation

@vokrik
Copy link

@vokrik vokrik commented Nov 15, 2025

I learned the hard way that the methods need to be lowercase (and kept wondering why the requests are not retried). This change should prevent that mistake

| LiteralType
| (BaseType & {_?: never});

export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to move this to common so that there is not a circular dependency between retry.ts and options.ts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circular dependencies are fine in ESM.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I think this is just a tick from some past projects :) I'll move the HttpMethod back to options.ts

@vokrik vokrik marked this pull request as draft November 15, 2025 14:09
| LiteralType
| (BaseType & {_?: never});

export type RequestHttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' ;
Copy link
Author

@vokrik vokrik Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, in constants.ts there is an exported constant containing methods without 'options' and 'trace'

// source/core/constants.ts
...
export const requestMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'] as const;

I guess we want to keep split the methods that the user does proactively (rather than the browser doing automatically) and all available methods.

The default retryMethods however contain also options and trace as a default:

// source/utils/normalize.ts
const retryMethods: Array<HttpMethod> = ['get', 'put', 'head', 'delete', 'options', 'trace'];

@vokrik vokrik marked this pull request as ready for review November 15, 2025 14:36
@sindresorhus
Copy link
Owner

I appreciate you working to prevent the uppercase method mistake. However, hardcoding HttpMethod as a union type would break extensibility. There are almost 40 registered HTTP methods in the IANA registry (not just 8). The Fetch API also allows arbitrary custom methods, and users might want to retry them.

Instead, we should use the LiteralUnion pattern that Ky already uses elsewhere:

methods?: Array<LiteralUnion<HttpMethod, string>>;

This provides autocomplete for common methods while still accepting any string, maintaining both type safety and extensibility.

@vokrik vokrik force-pushed the add-type-to-retry-methods branch from 37ea1ac to 8340cd7 Compare November 17, 2025 14:17
@vokrik
Copy link
Author

vokrik commented Nov 17, 2025

I appreciate you working to prevent the uppercase method mistake. However, hardcoding HttpMethod as a union type would break extensibility. There are almost 40 registered HTTP methods in the IANA registry (not just 8). The Fetch API also allows arbitrary custom methods, and users might want to retry them.

Instead, we should use the LiteralUnion pattern that Ky already uses elsewhere:

methods?: Array<LiteralUnion<HttpMethod, string>>;

This provides autocomplete for common methods while still accepting any string, maintaining both type safety and extensibility.

Ok, that makes sense.. In that case, would it make sense to you if I lowercased the passed methods in normalize.ts so that it's bulletproof? (Added proposal as a last commit)

@vokrik vokrik force-pushed the add-type-to-retry-methods branch from 0ffc5c2 to 87c9848 Compare November 18, 2025 07:55

// Check if method is retriable for non-forced retries
if (!this.#options.retry.methods.includes(this.request.method.toLowerCase())) {
if (!this.#options.retry.methods.includes((this.request.method.toLowerCase()))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change meant to be included?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, sorry, was removing a typecast and my IDE left the braces

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.

4 participants