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

@codeperate/foalts-apischema-builder

v1.0.8

Published

A small library that helps you build ApiSchema for foalTS.

Downloads

22

Readme

foalts-apischema-builder

A small library that helps you to build ApiSchema for foalTS with builder pattern.

Getting Started

  1. This library use class validator metadata storage to convert your database entity to json schema, therefore you need validation decorators in your entity. Although this lib is agnostic between orms, the examples below are using foalts with Mikro-orm.
import { IsDate, IsString } from "class-validator";

export class SomeEntity {
  @PrimaryKey()
  @IsString()
  id: string = v4();

  @Property()
  @IsDate()
  createdAt: Date = new Date();

  @Property({ onUpdate: () => new Date() })
  @IsDate()
  updatedAt: Date = new Date();
}
  1. Setup your the schema service before bootstrap your app in index.ts
import { SchemaService } from '@codeperate/foalts-apischema-builder';
...
async function main() {
  const schemaService = new SchemaService()
  serviceManager.set("schema", schemaService, { boot: true })
  await serviceManager.boot("schema");
  const app = await createApp(AppController, { serviceManager });
  ...
}
  1. Use it in your controller example.controller.ts
export class SomeController{
  @depdency('schema')
  ss:SchemaService
  @Get('/:id')
  @ValidatePathParam("id", { type: "string" })
  @JSONResponse(200, (c: SomeController) => (c.ss.get(SomeEntity)))
  async findById(ctx: Context): Promise<HttpResponse> {
      ...
  }
}

json-response.hook.ts:

import {
  ApiResponse,
  HookDecorator,
  IApiReference,
  IApiResponse,
  IApiSchema,
} from "@foal/core";
export function JSONResponse(
  key: number | "default" | "1XX" | "2XX" | "3XX" | "4XX" | "5XX",
  schema:
    | IApiSchema
    | IApiReference
    | ((controller: any) => IApiSchema | IApiReference),
  description: string = ""
): HookDecorator {
  return (target, propertyKey?) => {
    function makeResponse(schema: IApiSchema | IApiReference): IApiResponse {
      return {
        description: description,
        content: {
          "application/json": {
            schema: schema,
          },
        },
      };
    }
    const responseBody =
      typeof schema == "function"
        ? (c: any) => makeResponse(schema(c))
        : makeResponse(schema);
    return ApiResponse(key, responseBody)(target, propertyKey);
  };
}
  1. Modify your APISchema Object easily with builder pattern:
  @JSONResponse(200, (c: SomeController) => c.ss.get(SomeEntity).pick(["prop1","prop2"]))

More

You can add more predefined schema as you like, for example:

schemaService.transform((key, value) => {
  if (orm.getMetadata().has(key)) {
    const primaryKeys = orm.getMetadata().get(key).primaryKeys;
    return value.omit(primaryKeys);
  }
  return value;
}, "post");

A schema for post body without primary key will be created and you can access it with:

schemaService.get(SomeEntity, "post");