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.6 KBRaw ↗
jinja
{#- 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) -%}
{%- if operation.input -%}
{%- 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') != '' -%}
{%- if zod %}
import type { z } from "zod";
{%- endif %}
{%- for dependency in file.dependencies %}{% if dependency.symbol and dependency.slot == 'schema.validation' %}
import type { {{ dependency.symbol }} } from "{{ (dependency.path.prefixed or ('./' ~ dependency.path.relative)) }}";
{%- endif %}{% endfor %}

/**
 * The {{ feature.name.title }} contract the controller calls. Regenerated by Dryv; the project
 * implements it in its own class, which the feature module binds to this token.
 */
export abstract class {{ file.symbol }} {
{%- for operation in operations %}
  abstract {{ operation.name.camel }}({{ signature(operation) }}): {{ result(operation) }};
{%- endfor %}
}