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

Add baseUrl option, rename prefixUrl, and allow leading slashes in input #606

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sholladay
Copy link
Collaborator

@sholladay sholladay commented Jul 4, 2024

This PR aims to improve the flexibility of input URLs and options for resolving them prior to the request.

  • prefixUrl is renamed to startPath
    • As before, when this option is used, it is prepended to input before any other processing takes place
    • startPath can still be a full URL if needed, however the name is meant to signal its primary use case is a simple base path without a domain, e.g. /api
  • A new baseUrl option is added
    • When this option is used, input (including any startPath) is resolved against the baseUrl according to standard URL resolution rules
    • baseUrl will respect the HTML <base> tag if present, and is similar to it, except that it only applies to Ky
  • input can now be any valid URL, with or without a leading slash
    • Slashes are automatically normalized when using startPath
    • Slashes are kept as-is and respected in accordance with standard URL resolution rules when using baseUrl
ky('books', { startPath : '/api' });  // GET /api/books
ky('books', { baseUrl : '/api' });  // GET /books
ky('books', { baseUrl : '/api/' });  // GET /api/books
ky('./books', { baseUrl : '/api/' });  // GET /api/books
ky('/books', { baseUrl : '/api/' });  // GET /books
ky('http://bar.com', { startPath : 'http://foo.com' });  // GET http://foo.com/http://bar.com
ky('http://bar.com', { baseUrl : 'http://foo.com' });  // GET http://bar.com
ky('books', { baseUrl : 'http://foo.com', startPath : '/api' });  // GET http://foo.com/api/books

Possible names for the string that is prepended to input

  • startPath (as is currently implemented here)
  • pathPrefix
  • prefixPath
  • rootPath

TODO:

  • Finalize name for startPath option
  • Documentation
  • TypeScript types

@faradaytrs
Copy link

i see that it's going to be a breaking change, maybe it's worth about discusssing about the next major? i think it's good to remove default timeout and retry settings, because many people come from fetch and axios and it's not obvious ky have these defaults.

@sholladay
Copy link
Collaborator Author

I would be okay with removing the default timeout, but not the default retries.

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