npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rocket-kit/edge

v1.1.22

Published

## Install

Downloads

3,098

Readme

Quick Start

Install

Note:: We recommend using a deno.json file to manage your dependencies

deno add npm:@rocket-kit/edge npm:zod jsr:@supabase/functions-js/edge-runtime.d.ts

Recommended Architecture

.
└──supabase
    ├── common ('methods that share multiple functions')
    └── functions ('functions')
        └── <function-name>
            ├── index.ts ('router')
            ├── utils ('methods that share multiple modules')
            └── modules ('modules')
                └── module ('exemple for POST method use name "createProduct" or GET "getProductById"')
                    ├──  controller.ts
                    └── service.ts

Example Usage

Sigle Example

// path: ./index.ts

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

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

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

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

Deno.serve(Controller);

Full Example

Schemas

// path: ./modules/example/schemas.ts
import { z } from "zod";

export const exampleBodySchemas = z.object({
    name: z.string().optional(),
});

export type exampleBodyType = Zod.infer<typeof exampleBodySchemas>

export const exampleSchemas = {
    params: z.object({
        id: z.string().regex(/^[0-9]+$/, {
            message: "The id must be a number",
        }),
    }),
    query: z.object({
        name: z.string().optional(),
    }),
    body: exampleBodySchemas,
}

service

// path: ./modules/example/service.ts

import { exampleBodyType } from "./schemas";
import { onSupabase, SupaError } from "@rocket-kit/edge";

interface exampleServiceParamsType {
    id: string;
    name: string;
    body: exampleBodyType;
}

type ExampleServiceFn = (p: exampleServiceParamsType) => string;

export const exampleService = async ({ id, name, body }) => {
    // method that returns a supabase client in a very simple way
    const supabase = onSupabase();

    const { data, error } = await supabase
        .from("users")
        .update(body)
        .eq("id", id)
        .eq("name", name)
        .throwOnError();

    // SupaError es para la validación de errores de la consulta.
    if (error) throw new SupaError(error);

    return "Hello from Supabase Edge Functions!";
}

Controller

// path: ./modules/example/controller.ts

import { exampleSchemas } from "./schemas";
import { exampleService } from "./service";
import { onEdge, ReasonPhrases, StatusCodes } from "@rocket-kit/edge";

export const exampleController = onEdge({
    // zod schemas
    schemas: exampleSchema,
    // 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"]);

        const message = exampleService({
            id: params.id,
            name: query.name,
            body
        });

        return reply.json({
          message,
          params,
          body,
          query,
          data,
        }, {
            status: StatusCodes.OK,
            statusText: ReasonPhrases.OK,
        });
    },
});

Router

// path: ./index.ts

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

import { onRouter } from "@rocket-kit/edge";
import { Controller } from './modules/example/controller.ts';

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

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

Deno.serve(router.listen);

Middleware

// path: ./utils/middleware.ts or ./modules/example//middleware.ts

import { exampleSchemas } from "./schemas";
import { exampleService } from "./service";
import { onEdge, ReasonPhrases, StatusCodes } from "@rocket-kit/edge";

export const exampleMiddleware = onEdge({
    // zod schemas
    schemas: exampleSchema,
    // logic that is executed with the controller
    async Handler(_req, _reply, next) {

        // Logic

        return next!();
    },
});

You can use any number of middleware, before or after the routes, remembering that all pre must return next to move on to the next step in the life cycle or reply to respond to the client and stop the cycle.

Use global middlewae in a routes

// path: ./index.ts

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

import { onRouter } from "@rocket-kit/edge";
import { exampleMiddleware } from './urils/middleware';
import { Controller } from './modules/example/controller.ts';

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


router.get("/example/pre/:id", exampleMiddleware, exampleController /* ...edge cotrollers */);

router.get("/example/pos/:id", exampleController,  exampleMiddleware /* ...edge cotrollers */);

Deno.serve(router.listen);

Use global middlewae in all routes

// path: ./index.ts

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

import { onRouter } from "@rocket-kit/edge";
import { exampleMiddleware } from './urils/middleware';
import { Controller } from './modules/example/controller.ts';

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

router.preMiddy(exampleMiddleware, /* ...edge cotrollers */);

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

router.posMiddy(exampleMiddleware, /* ...edge cotrollers */);


Deno.serve(router.listen);