烧饼论坛
登录

😡 ChatGPT 你看看你写的代码

2282
  • 纳西妲
    UID 3952Lv.7 熟悉面孔博主nahida.im

    有你这样格式化代码的?生气

    const
      POSTHOG_TOKEN
        = 
      "__token";
    
    const
      POSTHOG_ENDPOINT
        = 
      "__url";
    
    const
      compact
        = 
      (
        data
      ) =>
        Object
          .fromEntries(
            Object
              .entries(
                data
              )
              .filter(
              (
                [
                  ,
                  value
                ]
              ) =>
                value
                  !==
                undefined
                  &&
                value
                  !==
                null
                  &&
                value
                  !==
                "",
            ),
        );
    
    export default {
      async
        fetch(
          request
        ) {
        if (
          request
            .method
              !==
          "POST"
        ) {
          return
            new Response(
              "Method Not Allowed",
              {
                status:
                  405,
              }
            );
        }
        const
          payload =
            await
          request
            .json();
        payload
          .api_key
            = 
          POSTHOG_TOKEN;
        payload
          .properties
            ??=
          {};
        const
          cf
            = 
          request.cf ?? {};
        const
          ip
            = 
        request
          .headers
          .get(
            "CF-Connecting-IP"
          );
        const
          userAgent
            = 
        request
          .headers
          .get(
            "User-Agent"
          );
        Object.assign(
          payload.properties,
          compact({
            $ip:
              ip,
            $user_agent:
              userAgent,
            $raw_user_agent:
              userAgent,
            $geoip_country_code:
              cf
                .country,
            $geoip_city_name:
              cf
                .city,
            $geoip_continent_code:
              cf
                .continent,
            $geoip_subdivision_1_name:
              cf
                .region,
            $geoip_subdivision_1_code:
              cf
                .regionCode,
            $geoip_time_zone:
              cf
                .timezone,
            $geoip_latitude:
              cf
                .latitude != null
                  ?
                    Number(
                      cf
                        .latitude
                    )
                  :
                    undefined,
            $geoip_longitude:
              cf
                .longitude != null
                  ?
                    Number(
                      cf
                        .longitude
                    )
                  :
                    undefined,
            $geoip_postal_code:
              cf
                .postalCode,
          }),
        );
    
        return fetch(
          POSTHOG_ENDPOINT,
          {
            method:
              "POST",
            headers:
              {
                "Content-Type":
                  "application/json",
                "User-Agent":
                  userAgent
                    ||
                  "",
              },
            body:
              JSON
                .stringify(
                  payload
                ),
          }
        );
      },
    };
    
  • 收藏支持反对打赏

2 条回复

  • 火星人
    #1
    该楼层已被作者或管理员删除,作者可以在「我的删帖」页面申请恢复
  • ibd818
    UID 1611Lv.3 初识社区 禁言
    #2
    该用户已被禁言,内容已隐藏
  • 泡泡Gmail
    UID 6900Lv.3 初识社区
    #3

    这排版... 绝

发表回复