0

Request Trace

by
Published Nov 16, 2023
Script cloudflare
  • Submitted by hugo697 Typescript (fetch-only)
    Created 536 days ago
    1
    type Cloudflare = {
    2
      token: string;
    3
      email: string;
    4
      key: string;
    5
    };
    6
    /**
    7
     * Request Trace
    8
     *
    9
     */
    10
    export async function main(
    11
      auth: Cloudflare,
    12
      account_identifier: string,
    13
      body: {
    14
        body?: {
    15
          base64?: string;
    16
          json?: { [k: string]: unknown };
    17
          plain_text?: string;
    18
          [k: string]: unknown;
    19
        };
    20
        context?: {
    21
          bot_score?: number;
    22
          geoloc?: {
    23
            city?: string;
    24
            continent?: string;
    25
            is_eu_country?: boolean;
    26
            iso_code?: string;
    27
            latitude?: number;
    28
            longitude?: number;
    29
            postal_code?: string;
    30
            region_code?: string;
    31
            subdivision_2_iso_code?: string;
    32
            timezone?: string;
    33
            [k: string]: unknown;
    34
          };
    35
          skip_challenge?: boolean;
    36
          threat_score?: number;
    37
          [k: string]: unknown;
    38
        };
    39
        cookies?: { [k: string]: string };
    40
        headers?: { [k: string]: string };
    41
        method: string;
    42
        protocol?: string;
    43
        skip_response?: boolean;
    44
        url: string;
    45
        [k: string]: unknown;
    46
      }
    47
    ) {
    48
      const url = new URL(
    49
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/request-tracer/trace`
    50
      );
    51
    
    
    52
      const response = await fetch(url, {
    53
        method: "POST",
    54
        headers: {
    55
          "X-AUTH-EMAIL": auth.email,
    56
          "X-AUTH-KEY": auth.key,
    57
          "Content-Type": "application/json",
    58
          Authorization: "Bearer " + auth.token,
    59
        },
    60
        body: JSON.stringify(body),
    61
      });
    62
      if (!response.ok) {
    63
        const text = await response.text();
    64
        throw new Error(`${response.status} ${text}`);
    65
      }
    66
      return await response.json();
    67
    }
    68