feat(stac-api): accept callable options resolved at call time#56
Closed
alukach wants to merge 4 commits into
Closed
feat(stac-api): accept callable options resolved at call time#56alukach wants to merge 4 commits into
alukach wants to merge 4 commits into
Conversation
`StacApiProvider`/`useStacApi`/`StacApi` now accept `options` as either the existing static `GenericObject` (backward compatible) or a function of type `OptionsGetter` invoked per request. The function form lets consumers rotate values (auth headers, custom headers, signal, etc.) without rebuilding the StacApi instance and re-firing the landing-page probe. Internally, `useStacApi` ref-stabilizes whatever options form was passed and excludes it from the react-query queryKey, so identity changes across renders no longer cause client rebuilds. Static options no longer need to be memoized. The library takes no opinions on auth scheme or URL scoping — `options` applies to every request `StacApi.fetch` issues. Consumers needing secret-scoping (e.g. don't send a bearer to asset hrefs) should gate at their call sites. The constructor signature stays positional `(baseUrl, searchMode, options?)` — the only change to the existing API surface is widening the `options` type to `GenericObject | OptionsGetter`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
/cc @pantierra |
Member
|
I think I'm not fully grasping the goal here. Passing a callable options that gets executed every time seem excessive. IMO it would make more sense for the query to return the search mode and url and the stac api be constructed outside. Then if the options change the stac api is updated and there's no need of a new request. Am I missing something really big here? Something like: function useStacApi(url: string, options?: GenericObject): StacApiHook {
const { data, isSuccess, isLoading, isError } = useQuery({
queryKey: generateStacApiQueryKey(url),
queryFn: async () => {
const response = await fetch(url, {
headers: {
...options?.headers,
},
});
const stacData = await handleStacResponse<{ links?: Link[] }>(response);
const doesPost = stacData.links?.find(
({ rel, method }: Link) => rel === 'search' && method === 'POST'
);
return {
mode: doesPost ? SearchMode.POST : SearchMode.GET,
url: response.url
}
},
staleTime: Infinity,
});
return useMemo(() => {
if (isSuccess) {
return {
stacApi: new StacApi(data.url, data.mode, options),
isLoading,
isError
}
}
return {
stacApi: undefined,
isLoading,
isError
}
}, [data, isSuccess, isLoading, isError, options])
}If the headers change the query won't run again. We need to make sure that's ok. |
Member
Author
|
@danielfdsilva Good suggestion. Closing in favor of #57 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What I'm changing
The PR enables dynamic options to accompany requests to a STAC API. My current use case is to pass in a callable
optionsparameter that fetches the latest authorization header (developmentseed/stac-manager#71), which allows us to maintain the same STAC API instance while the underlying auth token is refreshed within my application.How I did it
StacApiProvider/useStacApi/StacApinow acceptoptionsas either the existing staticGenericObject(backward compatible) or a function of typeOptionsGetterinvoked per request. The function form lets consumers rotate values (auth headers, custom headers, signal, etc.) without rebuilding the StacApi instance and re-firing the landing-page probe.Internally,
useStacApiref-stabilizes whatever options form was passed and excludes it from the react-query queryKey, so identity changes across renders no longer cause client rebuilds. Static options no longer need to be memoized.Note
It wasn't really clear to me why
optionswere ever part of thequeryKey. It's worth considering if they need to be there for any reason that I'm not seeing.The constructor signature stays positional
(baseUrl, searchMode, options?)— the only change to the existing API surface is widening theoptionstype toGenericObject | OptionsGetter.