New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@greenweb/gaw-plugin-netlify-edge

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@greenweb/gaw-plugin-netlify-edge

This plugin provides some useful functions that can be used when setting up the Grid-aware Websites library using Netlify Edge Functions.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

Grid-aware Websites - Netlify Edge Plugin

This plugin provides some useful functions that can be used when setting up the @greenweb/grid-aware websites library using Netlify Edge Functions.

After you have installed the @greenweb/grid-aware-websites package (see steps), you can use this plugin to:

  • Fetch the location of a user that is exposed by the Netlify Edge platform.

Fetching request country (getLocation())

The code below is a simplified demonstation of how to use this plugin to fetch the request location, and then use it with the gridAwarePower function.

The code below shows the

import { getLocation } from "https://esm.sh/@greenweb/gaw-plugin-netlify-edge@latest";

export default async (request, context) => {
  // Use the getLocation function to check for the user's country in the request object
  const location = getLocation(context);

  // If there's an error, process the request as normal
  if (location.status === "error") {
      return new Response('There was an error');
  }

  // Otherwise we can get the "country" variable 
  const { country } = location;
  return new Response(`The country is ${country}.`)
}

Fetch request latlon

By default, the getLocation() function returns the geo.country.code value. However, it can also be used to return the geo.latitude and geo.longitude values if desired.

import { getLocation } from "https://esm.sh/@greenweb/gaw-plugin-netlify-edge@latest";

export default async (request, context) => {
  // Use the getLocation function to check for the user's latlon in the request object
  const location = getLocation(context, {
    mode: "latlon"
  });

  // If there's an error, process the request as normal
  if (location.status === "error") {
      return new Response('There was an error');
  }

  // Otherwise we can get the "country" variable 
  const { lat, lon } = location;
  return new Response(`The country is ${JSON.stringify({lat, lon})}.`)
}

[!NOTE] Using latitude and longitude values is not yet supported in the @greenweb/grid-aware-websites package.

Using this with the Grid-aware Websites library

The code below shows a simple implementation of this plugin alongside the Grid-aware Websites core library in a Netlify Edge Function.

import { gridAwarePower } from "https://esm.sh/@greenweb/grid-aware-websites@latest";
import { getLocation } from "https://esm.sh/@greenweb/gaw-plugin-netlify-edge@latest";

const apiKey = "you_api_key"; // Set your own Electricity Maps API key here.

export default async (request, context) => {
  // Use the getLocation function to check for the user's country in the request object
  const location = getLocation(context);

  // If there's an error, process the request as normal
  if (location.status === "error") {
      return new Response('There was an error');
  }

  // Otherwise we can get the "country" variable 
  const { country } = location;

  // Use the Grid-aware Websites library to fetch data for Taiwan, and check if grid-aware website changes should be applied.
    const gridData = await gridAwarePower(country, apiKey);

    // If we get an error, we can check for that and return nothing.
    if (gridData.status === "error") {
        return new Response('There was an error')
    }

    // If we've got data back using the Grid-aware Websites library, let's return that to the browser
    return new Response(gridData)
}

Keywords

FAQs

Package last updated on 08 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc