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

Sending bytes as query param #41

Open
kabece opened this issue Sep 7, 2022 · 2 comments
Open

Sending bytes as query param #41

kabece opened this issue Sep 7, 2022 · 2 comments

Comments

@kabece
Copy link

kabece commented Sep 7, 2022

The fix for Issue #22 works only if the bytes are in the body of the request, not in query parameters. In this case, they're being removed by flattenRequestPayload even though the type generated is Uint8Array.

Changing the flattenRequestPayload function to below (see the added third if statement) seems to solve the problem. It reuses the replacer function behaviour.

function flattenRequestPayload<T extends RequestPayload>(
  requestPayload: T,
  path: string = ''
): FlattenedRequestPayload {
  return Object.keys(requestPayload).reduce((acc: T, key: string): T => {
    const value = requestPayload[key];
    const newPath = path ? [path, key].join('.') : key;

    const isNonEmptyPrimitiveArray =
      Array.isArray(value) &&
      value.every(v => isPrimitive(v)) &&
      value.length > 0;

    const isNonZeroValuePrimitive =
      isPrimitive(value) && !isZeroValuePrimitive(value as Primitive);

    let objectToMerge = {};

    if (isPlainObject(value)) {
      objectToMerge = flattenRequestPayload(value as RequestPayload, newPath);
    } else if (isNonZeroValuePrimitive || isNonEmptyPrimitiveArray) {
      objectToMerge = { [newPath]: value };
    } else if (value && value.constructor === Uint8Array) {
      objectToMerge = { [newPath]: b64Encode(value, 0, value.length) };
    }

    return { ...acc, ...objectToMerge };
  }, {} as T) as FlattenedRequestPayload;
}
@brennanjl
Copy link

Did this ever get resolved? Looking to implement this into my project, but this would likely be a requirement

@kabece
Copy link
Author

kabece commented Feb 2, 2023

Not as far as I know, but the monkey patching above works well.

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

No branches or pull requests

2 participants