Skip to content

Sending bytes as query param #41

Open
@kabece

Description

@kabece

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions