dryvPacks

@dryv/nestjsv0.6.0

NestJS feature controllers, services and modules for HTTP operations.

NestJS Backend AdapterBackendInjectPublished 25 days ago2 downloads

dryv pack add @dryv/nestjs

You are viewing version 0.6.0. The latest release is 0.9.0.

3.2 KBRaw ↗
jinja
{%- macro shared(template) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.slot is none and dependency.template == template -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro shared_path(template) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.slot is none and dependency.template == template -%}{{ (dependency.path.prefixed or ('./' ~ dependency.path.relative)) }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro has_slot(slot) -%}
{%- for dependency in file.dependencies -%}{%- if dependency.slot == slot -%}x{%- endif -%}{%- endfor -%}
{%- endmacro -%}
{%- macro scalar_sample(value) -%}
{%- if value.is.integer -%}1
{%- elif value.is.number -%}1.5
{%- elif value.is.boolean -%}true
{%- elif value.is.uuid -%}"7f1c1f52-2f58-4a4e-9b43-0c1a3c1b0a11"
{%- else -%}"value"
{%- endif -%}
{%- endmacro -%}
{%- set persistence = has_slot('schema.persistence') != '' -%}
import "reflect-metadata";

import { Global, type INestApplication, Module } from "@nestjs/common";
import { Test } from "@nestjs/testing";
{%- if persistence %}
import { getDataSourceToken } from "@nestjs/typeorm";
{%- endif %}
import { afterAll, beforeAll, describe, expect, it } from "vitest";

import { {{ shared('app-module') }} } from "{{ shared_path('app-module') }}";
{%- if persistence %}
import { DatabaseModule } from "./database/database.module.ts";

// Stands in for the project's DatabaseModule: repositories resolve without a database.
@Global()
@Module({
  providers: [
    {
      provide: getDataSourceToken(),
      useValue: { options: { type: "postgres" }, entityMetadatas: [], getRepository: () => ({}) },
    },
  ],
  exports: [getDataSourceToken()],
})
class InMemoryDatabaseModule {}
{%- endif %}

/**
 * Managed by Dryv: the assembled application (AppModule -> group modules -> feature modules ->
 * the project's implementations) boots and serves every route its contracts declare. A route
 * may answer 4xx/5xx while its use case is unimplemented, but it must never be missing (404).
 */
const routes: Array<[method: string, path: string]> = [
{%- for operation in operations %}
{%- set http = operation.facets.http %}
  ["{{ http.method }}", "{{ http.path.effective }}"{% for binding in http.inputs %}{% if binding.is.path %}.replace("{{ '{' ~ binding.name ~ '}' }}", encodeURIComponent(String({{ scalar_sample(binding.port.value) }}))){% endif %}{% endfor %}],
{%- endfor %}
];

let app: INestApplication;
let base: string;

beforeAll(async () => {
  const moduleRef = await Test.createTestingModule({ imports: [{{ shared('app-module') }}] })
{%- if persistence %}
    .overrideModule(DatabaseModule)
    .useModule(InMemoryDatabaseModule)
{%- endif %}
    .compile();
  app = moduleRef.createNestApplication();
  await app.listen(0);
  base = (await app.getUrl()).replace("[::1]", "localhost");
});

afterAll(async () => {
  await app?.close();
});

describe("application assembly", () => {
  it.each(routes)("serves %s %s", async (method, path) => {
    const response = await fetch(base + path, {
      method,
      headers: { "content-type": "application/json" },
      body: method === "GET" || method === "DELETE" ? undefined : "{}",
    });
    expect(response.status).not.toBe(404);
  });
});