import base64


def encode(data: bytes) -> str:
    """Encode data to base64url (RFC 4648) text

    :param data: Raw data
    :returns: Base64-encoded data
    :raises TypeError: if `data` is not a bytes instance

    >>> encode(b'test')
    'dGVzdA'
    """

    if not isinstance(data, bytes):
        raise TypeError("Can only encode() bytes.")

    return base64.b64encode(data).decode("ascii").rstrip("=")