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

@revas-hq/kit-endpoint

v0.0.3

Published

The `@revas-hq/kit-endpoint` package provides essential types and interfaces for defining and managing endpoints in your application. Inspired by Go-Kit endpoints, this package helps ensure consistent contracts and smooth integration with other libraries.

Downloads

18

Readme

Kit Endpoint Library

The @revas-hq/kit-endpoint package provides essential types and interfaces for defining and managing endpoints in your application. Inspired by Go-Kit endpoints, this package helps ensure consistent contracts and smooth integration with other libraries.

Installation

Install the package using npm or yarn:

npm install @revas-hq/kit-endpoint
# or
yarn add @revas-hq/kit-endpoint

Types and Interfaces

EndpointMiddleware

A middleware function that wraps an endpoint, allowing you to modify its behavior.

type EndpointMiddleware = (next: Endpoint) => Endpoint;

Endpoint

The main function signature for an endpoint. It receives a Context and a request object, and returns a promise that resolves to an EndpointResult.

type Endpoint = (context: Context, request: any) => Promise<EndpointResult>;

EndpointResult

The result returned by an endpoint. It contains the context, an optional response, and an optional error.

type EndpointResult = {
  context: Context;
  response?: any;
  error?: EndpointError;
};

EndpointError

Represents an error that can be returned by an endpoint.

type EndpointError = {
  code: number;
  message: string;
};

Usage

Defining an Endpoint

Endpoints are defined as asynchronous functions that take a Context and a request object, and return a promise with an EndpointResult.

import { Context, EndpointResult, Endpoint } from "@revas-hq/kit-endpoint";

const myEndpoint: Endpoint = async (
  context: Context,
  request: any,
): Promise<EndpointResult> => {
  try {
    // Business logic here
    const response = { data: "some data" }; // Mock response

    return {
      context,
      response,
    };
  } catch (error) {
    return {
      context,
      error: {
        code: 500,
        message: "Internal Server Error",
      },
    };
  }
};

Using Middleware

Middleware functions can wrap an endpoint to extend or modify its behavior.

import { Endpoint, EndpointMiddleware } from "@revas-hq/kit-endpoint";

const loggingMiddleware: EndpointMiddleware =
  (next: Endpoint) => async (context: Context, request: any) => {
    console.log("Request received:", request);
    const result = await next(context, request);
    console.log("Response:", result.response);
    return result;
  };

Combining Middleware and Endpoints

You can compose middleware functions to wrap an endpoint:

const endpointWithMiddleware = loggingMiddleware(myEndpoint);

endpointWithMiddleware(someContext, someRequest).then((result) => {
  // Handle result
});

License

This project is licensed under the MIT License. See the LICENSE file for details.