Repository: awslabs/llrt
File: modules/llrt_fetch/src/response.rs (Rust)

Code before:
        let mut content_type: Option<String> = None;

        let body = body
            .0
            .and_then(|body| {
                if body.is_null() || body.is_undefined() {
                    None
                } else if let Some(blob) = body.as_object().and_then(Class::<Blob>::from_object) {
                    let blob = blob.borrow();
                    if !blob.mime_type().is_empty() {
                        content_type = Some(blob.mime_type());
                    }
                    Some(BodyVariant::Provided(Some(body)))
                } else if body
                    .as_object()
                    .and_then(Class::<URLSearchParams>::from_object)
                    .is_some()
                {
                    content_type = Some("application/x-www-form-urlencoded;charset=UTF-8".into());
                    Some(BodyVariant::Provided(Some(body)))
                } else if body.as_string().is_some() {
                    content_type = Some("text/plain;charset=UTF-8".into());
                    Some(BodyVariant::Provided(Some(body)))
                } else {
                    Some(BodyVariant::Provided(Some(body)))
                }
            })
            .unwrap_or_else(|| BodyVariant::Empty);

        let mut headers = headers.unwrap_or_default();
        if !headers.has(ctx.clone(), "content-type".into())? {
            if let Some(value) = content_type {
                headers.set(ctx.clone(), "content-type".into(), value.into_js(&ctx)?)?;
            }
        }
        let headers = Class::instance(ctx.clone(), headers)?;

        let content_encoding = headers.get("content-encoding")?;

        Ok(Self {
            body: RwLock::new(body),
            method: "GET".into(),
            url,
            start: Instant::now(),
            status,
            status_text,
            redirected: false,
            headers,
            content_encoding,
            abort_receiver,
        })

Review comment (suggestion):
We can be a bit more optimal here by taking the `type` from body and then matching on that. If body is object we check for classes.

Code after:

        let body = body
            .0
            .and_then(|body| {
                if body.is_null() || body.is_undefined() {
                    None
                } else if body.is_string() {
                    content_type = Some(MIME_TYPE_TEXT.into());
                    Some(BodyVariant::Provided(Some(body)))
                } else if let Some(obj) = body.as_object() {
                    if let Some(blob) = Class::<Blob>::from_object(obj) {
                        let blob = blob.borrow();
                        if !blob.mime_type().is_empty() {
                            content_type = Some(blob.mime_type());
                        }
                        Some(BodyVariant::Provided(Some(body)))
                    } else if obj.instance_of::<URLSearchParams>() {
                        content_type = Some(MIME_TYPE_APPLICATION.into());
                        Some(BodyVariant::Provided(Some(body)))
                    } else {
                        Some(BodyVariant::Provided(Some(body)))
                    }
                } else {
                    Some(BodyVariant::Provided(Some(body)))
                }
            })
            .unwrap_or_else(|| BodyVariant::Empty);

        let mut headers = headers.unwrap_or_default();
        if !headers.has(ctx.clone(), HEADERS_KEY_CONTENT_TYPE.into())? {
            if let Some(value) = content_type {
                headers.set(
                    ctx.clone(),
                    HEADERS_KEY_CONTENT_TYPE.into(),
                    value.into_js(&ctx)?,
                )?;
            }
        }
        let headers = Class::instance(ctx.clone(), headers)?;

        let content_encoding = headers.get("content-encoding")?;

        Ok(Self {
            body: RwLock::new(body),
            method: "GET".into(),
            url,
            start: Instant::now(),
            status,
            status_text,
            redirected: false,
            headers,