63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { Body, Controller, Get, Headers, HttpCode, Param, Patch, Post, Query, Req } from '@nestjs/common';
|
|||
|
|
import type { AuthenticatedRequest } from '../auth/auth.types.js';
|
||
|
|
import { ZodValidationPipe } from '../common/zod-validation.pipe.js';
|
||
|
|
import {
|
||
|
|
type GetQuery,
|
||
|
|
type ListQuery,
|
||
|
|
bodySchema,
|
||
|
|
getQuerySchema,
|
||
|
|
listQuerySchema,
|
||
|
|
pickSlHeaders,
|
||
|
|
} from './sl-request.js';
|
||
|
|
import { SlProxyService } from './sl-proxy.service.js';
|
||
|
|
|
||
|
|
type Body = Record<string, unknown>;
|
||
|
|
type IncomingHeaders = Record<string, string | string[] | undefined>;
|
||
|
|
|
||
|
|
/** Proxy del Service Layer: `/api/sl/<Recurso>[/<clave>]` con parámetros OData del frontend. */
|
||
|
|
@Controller('sl')
|
||
|
|
export class SlProxyController {
|
||
|
|
constructor(private readonly proxy: SlProxyService) {}
|
||
|
|
|
||
|
|
@Get(':resource')
|
||
|
|
list(
|
||
|
|
@Req() req: AuthenticatedRequest,
|
||
|
|
@Param('resource') resource: string,
|
||
|
|
@Query(new ZodValidationPipe(listQuerySchema)) query: ListQuery,
|
||
|
|
@Headers() headers: IncomingHeaders,
|
||
|
|
) {
|
||
|
|
return this.proxy.list(req.user, resource, query, pickSlHeaders(headers, 'GET'));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Get(':resource/:key')
|
||
|
|
get(
|
||
|
|
@Req() req: AuthenticatedRequest,
|
||
|
|
@Param('resource') resource: string,
|
||
|
|
@Param('key') key: string,
|
||
|
|
@Query(new ZodValidationPipe(getQuerySchema)) query: GetQuery,
|
||
|
|
) {
|
||
|
|
return this.proxy.get(req.user, resource, key, query);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Post(':resource')
|
||
|
|
create(
|
||
|
|
@Req() req: AuthenticatedRequest,
|
||
|
|
@Param('resource') resource: string,
|
||
|
|
@Body(new ZodValidationPipe(bodySchema)) body: Body,
|
||
|
|
@Headers() headers: IncomingHeaders,
|
||
|
|
) {
|
||
|
|
return this.proxy.create(req.user, resource, body, pickSlHeaders(headers, 'POST'));
|
||
|
|
}
|
||
|
|
|
||
|
|
@Patch(':resource/:key')
|
||
|
|
@HttpCode(204)
|
||
|
|
async update(
|
||
|
|
@Req() req: AuthenticatedRequest,
|
||
|
|
@Param('resource') resource: string,
|
||
|
|
@Param('key') key: string,
|
||
|
|
@Body(new ZodValidationPipe(bodySchema)) body: Body,
|
||
|
|
@Headers() headers: IncomingHeaders,
|
||
|
|
): Promise<void> {
|
||
|
|
await this.proxy.update(req.user, resource, key, body, pickSlHeaders(headers, 'PATCH'));
|
||
|
|
}
|
||
|
|
}
|