Edits history of script submission #12177 for ' Get the full information of a country (greip)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Greip = {
      apiKey: string;
    };
    /**
     * Get the full information of a country
     * This method allows you to retrieve detailed information about a country by providing its country code in the request.
    
    The API returns valuable data, such as the country’s name, capital, population, region, and other relevant attributes, enabling you to enrich your applications with geographic context and enhance user experiences.
     */
    export async function main(
      auth: Greip,
      CountryCode: string,
      params: string | undefined,
      format: string | undefined,
      mode: string | undefined,
      callback: string | undefined,
    ) {
      const url = new URL(`https://greipapi.com/lookup/country`);
      for (const [k, v] of [
        ["CountryCode", CountryCode],
        ["params", params],
        ["format", format],
        ["mode", mode],
        ["callback", callback],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 428 days ago