@dryv/nestjsv0.6.0
NestJS feature controllers, services and modules for HTTP operations.
dryv pack add @dryv/nestjs{#- Dependency helpers. -#}
{%- macro symbol_for(link, slot) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.slot == slot -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro pack_for(link, slot) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.slot == slot -%}{{ dependency.pack }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro has_slot(slot) -%}
{%- for dependency in file.dependencies -%}{%- if dependency.slot == slot -%}x{%- endif -%}{%- endfor -%}
{%- endmacro -%}
{%- macro uses_pack(key) -%}
{%- for dependency in file.dependencies -%}{%- if dependency.slot == 'schema.validation' and dependency.pack == key -%}x{%- endif -%}{%- endfor -%}
{%- endmacro -%}
{%- macro ts_type(value) -%}
{%- if value.is.schema -%}
{%- set pack = pack_for(value.schema, 'schema.validation') -%}
{%- if pack == 'inject.validation.zod' -%}z.infer<typeof {{ symbol_for(value.schema, 'schema.validation') }}>
{%- elif pack == 'inject.validation.joi' -%}Record<string, unknown>
{%- else -%}{{ symbol_for(value.schema, 'schema.validation') }}
{%- endif -%}
{%- elif value.is.property and value.is.primitive -%}{{ value.property.primitive.type }}
{%- elif value.is.property and value.is.enum -%}string
{%- else -%}unknown
{%- endif -%}
{%- endmacro -%}
{%- macro body_pipe(value) -%}
{%- if value.is.schema -%}
{%- set pack = pack_for(value.schema, 'schema.validation') -%}
{%- if pack == 'inject.validation.zod' or pack == 'inject.validation.joi' -%}new SchemaValidationPipe({{ symbol_for(value.schema, 'schema.validation') }})
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- macro input_type(operation) -%}
{%- if operation.input.kind == 'schema' -%}{{ ts_type(operation.input.value) }}
{%- else -%}{ {% for field in operation.input.fields %}{{ field.name.camel }}{% if field.optional %}?{% endif %}: {{ ts_type(field.value) }}{% if not loop.last %}; {% endif %}{% endfor %} }
{%- endif -%}
{%- endmacro -%}
{%- macro signature(operation) -%}
{%- if operation.input %}input: {{ input_type(operation) }}{% endif -%}
{%- endmacro -%}
{%- macro result(operation) -%}
Promise<{% if operation.output %}{{ ts_type(operation.output.value) }}{% else %}void{% endif %}>
{%- endmacro -%}
{%- macro scalar_pipe(value) -%}
{%- if value.is.integer -%}, ParseIntPipe
{%- elif value.is.number -%}, ParseFloatPipe
{%- elif value.is.boolean -%}, ParseBoolPipe
{%- endif -%}
{%- endmacro -%}
{%- macro binds_whole(http) -%}
{%- for binding in http.inputs %}{% if binding.port.field is none %}x{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro call_input(operation, http) -%}
{#- Read a field of the input so demand-driven context materializes it. -#}
{%- if operation.input is not none and operation.input.kind -%}
{%- if binds_whole(http) -%}input
{%- else -%}{ {% for binding in http.inputs %}{{ binding.port.name.camel }}{% if not loop.last %}, {% endif %}{% endfor %} }
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- set zod = uses_pack('inject.validation.zod') != '' -%}
{%- set joi = uses_pack('inject.validation.joi') != '' -%}
import {
Body,
Controller,
Delete,
Get,
Headers,
HttpCode,
Param,
ParseBoolPipe,
ParseFloatPipe,
ParseIntPipe,
Patch,
Post,
Put,
Query,
} from "@nestjs/common";
{%- if zod %}
import type { z } from "zod";
{%- endif %}
import { Cookie } from "{{ (file.path.import_base ~ 'common/server/http/cookie.decorator.ts') if file.path.import_base else '../../common/server/http/cookie.decorator.ts' }}";
{%- if zod or joi %}
import { SchemaValidationPipe } from "{{ (file.path.import_base ~ 'common/server/validation/schema-validation.pipe.ts') if file.path.import_base else '../../common/server/validation/schema-validation.pipe.ts' }}";
{%- endif %}
{%- for dependency in file.dependencies %}{% if dependency.symbol and dependency.slot == 'schema.validation' %}
import { {{ dependency.symbol }} } from "{{ (dependency.path.prefixed or ('./' ~ dependency.path.relative)) }}";
{%- endif %}{% endfor %}
import { {{ group.name.pascal }}{{ feature.name.pascal }}Service } from "./{{ feature.name.kebab }}.service.ts";
{%- macro render_operation(operation) -%}
{%- set http = operation.facets.http %}
@{{ http.method | lower | capitalize }}("{{ http.path.effective | replace('{', ':') | replace('}', '') }}")
{%- for binding in http.outputs %}
@HttpCode({{ binding.status }})
{%- endfor %}
{{ operation.name.camel }}(
{%- for binding in http.inputs %}
{% if binding.is.path %}@Param("{{ binding.name }}"{{ scalar_pipe(binding.port.value) }}){% elif binding.is.query %}@Query("{{ binding.name }}"{{ scalar_pipe(binding.port.value) }}){% elif binding.is.query_object %}@Query({{ body_pipe(binding.port.value) }}){% elif binding.is.header %}@Headers("{{ binding.name }}"){% elif binding.is.cookie %}@Cookie("{{ binding.name }}"){% else %}@Body({{ body_pipe(binding.port.value) }}){% endif %} {{ binding.port.name.camel }}: {{ ts_type(binding.port.value) }},
{%- endfor %}
): {{ result(operation) }} {
return this.service.{{ operation.name.camel }}({{ call_input(operation, http) }});
}
{%- endmacro %}
@Controller()
export class {{ file.symbol }} {
constructor(private readonly service: {{ group.name.pascal }}{{ feature.name.pascal }}Service) {}
{%- for operation in operations %}
{%- if '{' not in operation.facets.http.path.effective %}
{{ render_operation(operation) }}
{%- endif %}
{%- endfor %}
{%- for operation in operations %}
{%- if '{' in operation.facets.http.path.effective %}
{{ render_operation(operation) }}
{%- endif %}
{%- endfor %}
}