#How can I add dependencies directly from the quick editor

1 messages · Page 1 of 1 (latest)

buoyant ivy
#

I want to install npm install uuid for session tokens, without using wrangler is there something like dependencies tab or so.

This is what i am trying to achieve:

import { uuidv4 } from 'uuid';

export default {
  async fetch(request, env) {
    try {
      const { pathname, search } = new URL(request.url);
      const [, key, value] = pathname.split("/");
      const params = new URLSearchParams(search);

      // Check for the api_key query parameter
      const apiKey = params.get("api_key");
      if (!apiKey || apiKey !== "***********************") {
        return new Response("Invalid API key", { status: 500 });
      }

      if (!key) {
        return new Response("A key is required in the URL path", {
          status: 400,
        });
      }

      if (request.method === "GET") {
        // Check if the response has been saved in the past
        const savedValue = await env.MAPS.get(key, {
          namespace: env.NAMESPACE_ID,
        });

        if (savedValue) {
          // If it has been saved, return the saved value
          return new Response(savedValue, { status: 200 });
        } else {
          // If it has not been saved, forward the request to Google Maps API
          const sessionToken = uuidv4();
          const mapsApiUrl = `https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${key}&key=${env.API_KEY}&sessiontoken=${sessionToken}`;
          const response = await fetch(mapsApiUrl);

          if (!response.ok) {
            return new Response(
              `Request to Google Maps API failed with status code ${response.status}`,
              { status: response.status }
            );
          }

  .....
#
.....
        const responseText = await response.text();

          // Save the response in the Key-Value store
          await env.MAPS.put(key, responseText, {
            namespace: env.NAMESPACE_ID,
          });

          return new Response(responseText, { status: 200 });
        }
      } else if (request.method === "PUT") {
        // Store the value in the Key-Value store
        if (!value) {
          return new Response("A value is required in the URL path", {
            status: 400,
          });
        }
        await env.MAPS.put(key, value, { namespace: env.NAMESPACE_ID });
        return new Response(`Saved in KV: ${key} = ${value}`);
      } else {
        return new Response("Unsupported method", { status: 405 });
      }
    } catch (e) {
      return new Response(e.stack, { status: 500 });
    }
  },
};
long grotto
#

You can't with Quick Edit

#

You'll need Wrangler or your own bundling