lukemorales / query-key-factory

A library for creating typesafe standardized query keys, useful for cache management in @tanstack/query
https://www.npmjs.com/package/@lukemorales/query-key-factory
MIT License
1.16k stars 32 forks source link

Expose Typescript types #72

Closed bluehat974 closed 1 year ago

bluehat974 commented 1 year ago

Is it possible to expose all the type defined in this project. I try to create a component which accept a DynamicFactoryOutput, but I can't import it at all as the type is defined privately

lukemorales commented 1 year ago

@bluehat974 would you care to share your use-case?

bluehat974 commented 1 year ago

I'm trying to create a Monitoring component which fetch Task type from different possible api

// 

type Props = {
  monitoringQueryOptions:  DynamicFactoryOutput<
    any[], 
    (startDate: ISODate) => {queryKey: [string], queryFn: () => Promise<Task[]>}, 
    {queryKey: [string], queryFn: () => Promise<Task[]>}
>
}
const Monitoring: React.FC<Props> = ({ tasksQueryOptions, taskGroupQueryOptions, retryTask }) => {

  const query = useQuery(tasksQueryOptions('2000-01-01'))

  ...
}

const Type1Monitoring = () => {
  return (
    <Monitoring monitoringQueryOptions={queries.monitoring1.getTask}/>
  )
}

const Type2Monitoring = () => {
  return (
    <Monitoring monitoringQueryOptions={queries.monitoring2.getTask}/>
  )
}
lukemorales commented 1 year ago

@bluehat974 not sure I follow because your props don't match the component example. Can you share a reproduction on a codepen/stackblitz? But on a superficial look, I don't think what you're trying to do is gonna work with the DynamicFactoryOutput type. With a example I can edit on codepen I can help you achieve what you're trying to do

Tinoooo commented 8 months ago

I also like to see exposed types. My use case on this is a helper function that creates query keys like so:

export const createAllQueryKey = <TParametersGetAll>(): FactoryProperty /* is not available */ => ({
  workspaceId,
  fieldsToInclude = [],
  params,
}:
{
  workspaceId: MaybeRef<string>
  fieldsToInclude?: string[]
  params?: MaybeRef<TParametersGetAll>
},
) => ({
  queryKey: [
    workspaceId,
    ...fieldsToInclude,
    ...(params ? [params] : []),
  ],
  enabled: !!unref(workspaceId),
})

usage:

export const queryKeys = createQueryKeys('my-key', {
  all: createAllQueryKey<ParametersGetAll>()
})

I have several keys in my project that have a similar structure. So such a helper would be handy.

@lukemorales Is this a valid use case for exposing the types or would you recommend a different solution?