1.3.0 • Published 5 months ago

@electra/web v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@electra/web

@electra/web is designed to extend the functionality of @electra/core for use in web applications. It provides classes to expose events from @electra/core via an HTTP router, while abstracting away framework-specific details. It allows for seamless integration with web frameworks like ExpressJS, and ensures that your API endpoints remain agnostic to the underlying web framework. This flexibility makes it easier to switch frameworks without a major overhaul of your API business logic.

Features:

  • AbstractEndpoint class that extends AbstractEvent from @electra/core, designed to work with @electra/web's request and response classes.
  • Provides its own classes to interact with web requests and responses, decoupling your application logic from specific web frameworks.
  • Designed to work with any web framework, allowing for easy switching between frameworks with minimal code changes.

Installation

Using npm:

npm install @electra/web

Using yarn:

yarn add @electra/web

Usage

Classes are exported as named exports from @electra/web.

import { AbstractEndpoint } from "@electra/web";
import { AbstractPayload, PublicProperties } from "@electra/core";

class ExamplePayload extends AbstractPayload
{
  public exampleProperty: string;
  
  public validate(): boolean
  {
    return typeof this.exampleProperty === 'string';
  }
}

class ExampleResponse
{
  public success: boolean;
}

// Endpoints are defined by extending the AbstractEndpoint class
// An endpoint is almost identical to an @electra/core event - the only difference is that, when constructed, an 
// endpoint requires a request and response as well as the endpoint's payload
class ExampleEndpoint extends AbstractEndpoint<ExamplePayload, ExampleResponse>
{
  protected getPayload(data: PublicProperties<ExamplePayload>): ExamplePayload
  {
    const payload = new ExamplePayload();
    payload.exampleProperty = data.exampleProperty;
    return payload;
  }
  
  protected process(payload: ExamplePayload): ExampleResponse
  {
    const response = new ExampleResponse();
    
    // The request can be accessed via the request property
    this.request.routeParams().get('exampleParam');
    
    // The response can be accessed via the response property
    this.response.setCookie('exampleCookie', 'exampleValue');
    
    response.success = true;
    return response;
  }
}

License

MIT

1.3.0

5 months ago

1.2.0

6 months ago

1.1.2

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago