2.1.0 • Published 1 year ago

@propero/easy-api v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Easy API

Maintainability Test Coverage Build Status

Installing

npm i @propero/easy-api
yarn add @propero/easy-api
pnpm add @propero/easy-api

Documentation

In progress... maybe... hopefully... within the next decade or so... no guarantees though

Examples

For further examples check out the example directory.

Simple Hello World Service
import express from "express";
import { Service, createService, Get, Param } from "@propero/easy-api";

@Service("/hello")
class HelloService {
  @Get("/:name?")
  public async hello(@Param("name") name: string = "world") {
    return `Hello ${name}!`;
  }
}

const app = express();
app.use("/api/v1", createService(new HelloService()));
app.listen(3000, () => console.log("http://localhost:3000/api/v1/hello"));
Simple Custom Injection Decorator
import express from "express";
import { createInjectorMiddleware, Post, Catch, CatchError } from "@propero/easy-api";
import { validateOrReject, ValidatorOptions, IsEmail } from "class-validator";
import { plainToClass } from "class-transformer";

const Validated = (Cls: unknown, validateOptions?: ValidatorOptions) => createInjectorMiddleware(() => async (req) => {
  const casted = plainToClass(Cls, req.body);
  await validateOrReject(casted, validateOptions);
  return casted;
});

class SubmitFormBody {
  @IsEmail()
  email: string;
}

@Service("/submit-form")
class SubmitFormService {
  @Post()
  public async onSubmit(@Validated(SubmitFormBody) body: SubmitFormBody) {
    console.log(body);
  }

  // Classes is set to array since class-validator does not throw an error but an array of errors
  @Catch({ status: 400, classes: Array })
  public async onError(@CatchError errors: ValidationError[]) {
    return { errors };
  }
}


const app = express();
app.use(createService(new SubmitFormService()));
app.listen(3000, () => console.log("http://localhost:3000/"));
2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.0

2 years ago

1.2.0

3 years ago

1.1.3

3 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago