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

Headers from context are overwritten from mutate headers #226

Open
dan-cooke opened this issue Mar 11, 2024 · 0 comments · May be fixed by #227
Open

Headers from context are overwritten from mutate headers #226

dan-cooke opened this issue Mar 11, 2024 · 0 comments · May be fixed by #227

Comments

@dan-cooke
Copy link

dan-cooke commented Mar 11, 2024

The components file generates react query mutations like this

export const useDocumentsControllerCreate = (
  options?: Omit<
    reactQuery.UseMutationOptions<
      Schemas.Document,
      DocumentsControllerCreateError,
      DocumentsControllerCreateVariables
    >,
    'mutationFn'
  >
) => {
  const { fetcherOptions } = useTempliContext();
  return reactQuery.useMutation<
    Schemas.Document,
    DocumentsControllerCreateError,
    DocumentsControllerCreateVariables
  >({
    mutationFn: (variables: DocumentsControllerCreateVariables) =>
      fetchDocumentsControllerCreate({ ...fetcherOptions, ...variables }),
    ...options,
  });
};

Current Behaviour

With this Context

export function use<namespace>Context...
 // -- snip
  return {
    fetcherOptions: {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    },
    queryOptions: {
      enabled: token !== null && _queryOptions?.enabled,
    },
    queryKeyFn,
  };
}

And using a mutation somewhere

 const { mutate: create } = useDocumentsControllerCreate({});


// -- snip
    create({
      headers: {
        // 1. Headers set here will overwrite any headers set by the context
        'Content-Type': 'multipart/form-data',
      },
    });

This results in the context headers being overwritten (and any other fetcherOptions)
Ie. my mutation is sent without Authorisation header

Desired behaviour

The fetcherOptions should be merged with the mutation variables, this can be achieved with lodash merge or something similar

Desired change in generated code

export const useTemplatesControllerRemoveInput = (
  options?: Omit<
    reactQuery.UseMutationOptions<
      undefined,
      TemplatesControllerRemoveInputError,
      TemplatesControllerRemoveInputVariables
    >,
    'mutationFn'
  >
) => {
  const { fetcherOptions } = useTempliContext();
  return reactQuery.useMutation<
    undefined,
    TemplatesControllerRemoveInputError,
    TemplatesControllerRemoveInputVariables
  >({
    mutationFn: (variables: TemplatesControllerRemoveInputVariables) =>
---       fetchTemplatesControllerRemoveInput({ ...fetcherOptions, ...variables }),
+++       fetchTemplatesControllerRemoveInput({ merge(fetcherOptions, variables) }),
    ...options,
  });
};
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 a pull request may close this issue.

1 participant