JSPM

  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q79337F
  • License MIT

Package Exports

  • @rocket-kit/edge
  • @rocket-kit/edge/dist/edge.esm.js
  • @rocket-kit/edge/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rocket-kit/edge) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Quick Start

Install

import { onEdge, onRouter, onSupabase } from "npm:@rocket-kit/edge";

Example Usage

Sigle Example

//  file: ./index.ts

import "jsr:@supabase/functions-js/edge-runtime.d.ts";

import { onEdge } from "npm:@rocket-kit/edge";
import { Middleware } from './middleware';
import { Controller } from './controller';

const Controller = onEdge({
    Handler(_req, reply) {

        return reply.json({
            message: "Hello from Supabase Edge Functions!",
        });
    },
});

Deno.serve(Controller);

Full Example

Middleware

//  file: ./example.middleware.ts

import { onEdge } from "npm:@rocket-kit/edge";

export const Middleware = onEdge({
    Handler(_req, _reply, _info, next) {
        console.log("Middleware")

        return next!();
    })
});

Controller

//  file: ./example.controller.ts

import { z } from "npm:zod";
import { onEdge, onSupabase, SupaError, ReasonPhrases, StatusCodes } from "npm:@rocket-kit/edge";

export const Controller = onEdge({
    // zod schemas
    schemas: {
        params: z.object({
            id: z.string().regex(/^[0-9]+$/, {
                message: "The id must be a number",
            }),
        }),
        query: z.object({
            name: z.string().optional(),
        }),
        body: z.object({
            name: z.string().optional(),
        }),
    },
    // logic that is executed with the controller
    async Handler(req, reply) {
        // custom method for getting and validation information
        const params = req.getParams();

        const body = req.getBody();

        const query = req.getQuery(["name"]);

        // method that returns a supabase client in a very simple way
        const supabase = onSupabase({ req });

        const { data, error } = await supabase.from("users").select("*")
          .throwOnError();

        if (error) throw new SupaError(error);

        return reply.json({
          message: "Hello from Supabase Edge Functions!",
          params,
          body,
          query,
          data,
        }, {
            status: StatusCodes.OK,
            statusText: ReasonPhrases.OK,
        });
    },
});

Router

//  file: ./index.ts

import "jsr:@supabase/functions-js/edge-runtime.d.ts";

import { onRouter } from "npm:@rocket-kit/edge";
import { Middleware } from './middleware';
import { Controller } from './controller';

// method for router
const router = onRouter();

router.get("/example/:id", Middleware, Controller);

Deno.serve(router.listen);