Skip to content

Commit

Permalink
prettify files
Browse files Browse the repository at this point in the history
  • Loading branch information
shemogumbe committed Oct 16, 2024
1 parent db831b1 commit e2856a5
Showing 1 changed file with 61 additions and 78 deletions.
139 changes: 61 additions & 78 deletions packages/abstractions/src/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,84 +93,67 @@ interface MultipartEntry {
serializationCallback?: ModelSerializerFunction<Parsable>;
}

export function serializeMultipartBody(
writer: SerializationWriter,
multipartBody: Partial<MultipartBody> = new MultipartBody(),
): void {
if (!writer) {
throw new Error("writer cannot be undefined");
}
if (!multipartBody) {
throw new Error("multipartBody cannot be undefined");
}
if (!multipartBody.listParts) {
throw new Error("multipartBody.listParts cannot be undefined");
}
if (!multipartBody.getBoundary) {
throw new Error("multipartBody.getBoundary cannot be undefined");
}
const parts = multipartBody.listParts();
if (Object.keys(parts).length === 0) {
throw new Error("multipartBody cannot be empty");
}
const boundary = multipartBody.getBoundary();
let first = true;
for (const partName in parts) {
if (first) {
first = false;
} else {
writer.writeStringValue(undefined, "\r\n");
}
writer.writeStringValue(undefined, "--" + boundary);
writer.writeStringValue(undefined, "\r\n");
const part = parts[partName];
writer.writeStringValue("Content-Type", part.contentType);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(
"Content-Disposition",
'form-data; name="' + part.originalName + '"',
);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "\r\n");
if (typeof part.content === "string") {
writer.writeStringValue(undefined, part.content);
} else if (part.content instanceof ArrayBuffer) {
writer.writeByteArrayValue(undefined, new Uint8Array(part.content));
} else if (part.content instanceof Uint8Array) {
writer.writeByteArrayValue(undefined, part.content);
} else if (part.serializationCallback) {
if (!multipartBody.requestAdapter) {
throw new Error("requestAdapter cannot be undefined");
}
const serializationWriterFactory =
multipartBody.requestAdapter.getSerializationWriterFactory();
if (!serializationWriterFactory) {
throw new Error("serializationWriterFactory cannot be undefined");
}
const partSerializationWriter =
serializationWriterFactory.getSerializationWriter(part.contentType);
if (!partSerializationWriter) {
throw new Error(
"no serialization writer factory for content type: " +
part.contentType,
);
}
partSerializationWriter.writeObjectValue(
undefined,
part.content as Parsable,
part.serializationCallback,
);
const partContent = partSerializationWriter.getSerializedContent();
writer.writeByteArrayValue(undefined, new Uint8Array(partContent));
} else {
throw new Error(
"unsupported content type for multipart body: " + typeof part.content,
);
}
}
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "--" + boundary + "--");
writer.writeStringValue(undefined, "\r\n");
export function serializeMultipartBody(writer: SerializationWriter, multipartBody: Partial<MultipartBody> = new MultipartBody()): void {
if (!writer) {
throw new Error("writer cannot be undefined");
}
if (!multipartBody) {
throw new Error("multipartBody cannot be undefined");
}
if (!multipartBody.listParts) {
throw new Error("multipartBody.listParts cannot be undefined");
}
if (!multipartBody.getBoundary) {
throw new Error("multipartBody.getBoundary cannot be undefined");
}
const parts = multipartBody.listParts();
if (Object.keys(parts).length === 0) {
throw new Error("multipartBody cannot be empty");
}
const boundary = multipartBody.getBoundary();
let first = true;
for (const partName in parts) {
if (first) {
first = false;
} else {
writer.writeStringValue(undefined, "\r\n");
}
writer.writeStringValue(undefined, "--" + boundary);
writer.writeStringValue(undefined, "\r\n");
const part = parts[partName];
writer.writeStringValue("Content-Type", part.contentType);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue("Content-Disposition", 'form-data; name="' + part.originalName + '"');
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "\r\n");
if (typeof part.content === "string") {
writer.writeStringValue(undefined, part.content);
} else if (part.content instanceof ArrayBuffer) {
writer.writeByteArrayValue(undefined, new Uint8Array(part.content));
} else if (part.content instanceof Uint8Array) {
writer.writeByteArrayValue(undefined, part.content);
} else if (part.serializationCallback) {
if (!multipartBody.requestAdapter) {
throw new Error("requestAdapter cannot be undefined");
}
const serializationWriterFactory = multipartBody.requestAdapter.getSerializationWriterFactory();
if (!serializationWriterFactory) {
throw new Error("serializationWriterFactory cannot be undefined");
}
const partSerializationWriter = serializationWriterFactory.getSerializationWriter(part.contentType);
if (!partSerializationWriter) {
throw new Error("no serialization writer factory for content type: " + part.contentType);
}
partSerializationWriter.writeObjectValue(undefined, part.content as Parsable, part.serializationCallback);
const partContent = partSerializationWriter.getSerializedContent();
writer.writeByteArrayValue(undefined, new Uint8Array(partContent));
} else {
throw new Error("unsupported content type for multipart body: " + typeof part.content);
}
}
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "--" + boundary + "--");
writer.writeStringValue(undefined, "\r\n");
}

export const deserializeIntoMultipartBody = (
Expand Down

0 comments on commit e2856a5

Please sign in to comment.