Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"routup": "^6.3.0"
},
"devDependencies": {
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@routup/query": "^4.0.3"
},
"devDependencies": {
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b",
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/body/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"routup": "^6.3.0"
},
"devDependencies": {
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/cookie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"cookie-es": "^3.1.1"
},
"devDependencies": {
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"gitHead": "94d729e309c1eec0401afb4d8083f65ce3aa8e0b",
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"routup": "^6.3.0"
},
"devDependencies": {
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/decorators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Functional handlers need no compiler flags and no class instances; decorators ne
npm install @routup/decorators
```

ESM only, Node.js >=24. Peer dependency: `routup@^6.3.0`. The decorators are legacy TypeScript decorators: set `"experimentalDecorators": true` in your `tsconfig.json`, or the legacy decorator mode of your compiler ([TypeScript settings](https://plugins.routup.dev/decorators/#typescript-settings)). `@routup/body`, `@routup/cookie` and `@routup/query` are installed as dependencies; register their plugins for `@DBody()`, `@DQuery()`, `@DCookie()` and `@DCookies()`. For OpenAPI generation, also install the optional peers `@trapi/core`, `@trapi/metadata` and `@trapi/swagger`.
ESM only, Node.js >=24. Peer dependency: `routup@^6.5.0`. The decorators are legacy TypeScript decorators: set `"experimentalDecorators": true` in your `tsconfig.json`, or the legacy decorator mode of your compiler ([TypeScript settings](https://plugins.routup.dev/decorators/#typescript-settings)). `@routup/body`, `@routup/cookie` and `@routup/query` are installed as dependencies; register their plugins for `@DBody()`, `@DQuery()`, `@DCookie()` and `@DCookies()`. For OpenAPI generation, also install the optional peers `@trapi/core`, `@trapi/metadata` and `@trapi/swagger`.

## Quick Start

Expand Down
4 changes: 2 additions & 2 deletions packages/decorators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@trapi/core": "^2.3.0",
"@trapi/metadata": "^2.3.0",
"@trapi/swagger": "^2.3.0",
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"peerDependenciesMeta": {
"@trapi/core": {
Expand All @@ -81,7 +81,7 @@
"@trapi/metadata": "^2.3.0",
"@trapi/swagger": "^2.3.0",
"jsonata": "^2.2.2",
"routup": "^6.3.0"
"routup": "^6.5.0"
},
"publishConfig": {
"access": "public"
Expand Down
31 changes: 29 additions & 2 deletions packages/decorators/src/mixed/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,37 @@ export function DTags(...names: string[]) {
}

/**
* Declare the media types a request body may have (OpenAPI only).
* Declare the media types a request body may have (method or class).
*
* At runtime a request with a body whose `content-type` matches none of the
* class-level and method-level media types is answered with `415` (`400` for
* a malformed header) before the method runs. A request without a body is
* not checked.
*
* @param mediaTypes - e.g. `'application/json'`, `'application/*+json'` or `'text/*'`.
*/
export function DConsumes(...mediaTypes: string[]) {
return (...args: any) => { };
return (
target: any,
propertyKey?: string | symbol,
) : void => {
if (typeof propertyKey === 'undefined') {
const meta = useDecoratorMeta(target.prototype);

meta.consumes = [...mediaTypes, ...meta.consumes];

return;
}

/* istanbul ignore next */
if (typeof propertyKey !== 'string') {
return;
}

const options = useDecoratorResponseOptions(target, propertyKey);

options.consumes = [...mediaTypes, ...(options.consumes ?? [])];
};
}

/**
Expand Down
18 changes: 17 additions & 1 deletion packages/decorators/src/mount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { IApp, MethodName } from 'routup';
import { App, defineCoreHandler, toResponse } from 'routup';
import {
App,
assertRequestContentType,
defineCoreHandler,
toResponse,
} from 'routup';
import { buildDecoratorMethodArguments } from './method';
import type { ClassType, ControllerRoute, DecoratorMeta } from './types';
import {
Expand Down Expand Up @@ -58,6 +63,17 @@ function buildControllerApp(
method: method.method as MethodName,
path: method.url,
fn: async (event) => {
// The OpenAPI document lists the class and the method media
// types together, so a request may have any of them.
const consumes = [
...meta.consumes,
...(meta.responses[propertyKey]?.consumes ?? []),
];

if (consumes.length > 0 && event.request.body !== null) {
assertRequestContentType(event, consumes);
}

const args = await buildDecoratorMethodArguments(
{ event },
meta.parameters[propertyKey] ?? [],
Expand Down
12 changes: 11 additions & 1 deletion packages/decorators/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type DecoratorResponseOptions = {
* Media types declared with a method-level `@DProduces()`.
*/
produces?: string[],

/**
* Request body media types declared with a method-level `@DConsumes()`.
*/
consumes?: string[],
};

export type DecoratorMeta = {
Expand All @@ -34,7 +39,12 @@ export type DecoratorMeta = {
produces: string[],

/**
* Response options (`@DStatus()`, method-level `@DProduces()`) per
* Request body media types declared with a class-level `@DConsumes()`.
*/
consumes: string[],

/**
* Response options (`@DStatus()`, method-level `@DProduces()` and `@DConsumes()`) per
* method. Kept apart from `methods`, since these decorators may be
* applied before the verb decorator.
*/
Expand Down
11 changes: 8 additions & 3 deletions packages/decorators/src/utils/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function useResolvedDecoratorMeta(target: Record<string, any>) : Decorato
meta.middlewares ??= [];
meta.methods ??= Object.create(null);
meta.produces ??= [];
meta.consumes ??= [];
meta.responses ??= Object.create(null);
meta.parameters ??= Object.create(null);

Expand All @@ -52,6 +53,7 @@ export function useResolvedDecoratorMeta(target: Record<string, any>) : Decorato
middlewares: [],
methods: Object.create(null),
produces: [],
consumes: [],
responses: Object.create(null),
parameters: Object.create(null),
} satisfies DecoratorMeta;
Expand Down Expand Up @@ -80,9 +82,11 @@ function copyDecoratorMeta(meta: DecoratorMeta) : DecoratorMeta {
const inputResponses : LooseEntries<DecoratorResponseOptions> = meta.responses;

for (const [key, response] of Object.entries(inputResponses)) {
responses[key] = response?.produces ?
{ ...response, produces: [...response.produces] } :
response && { ...response };
responses[key] = response && {
...response,
...(response.produces ? { produces: [...response.produces] } : {}),
...(response.consumes ? { consumes: [...response.consumes] } : {}),
};
}

const parameters : Record<string, DecoratorMeta['parameters'][string] | undefined> = Object.create(null);
Expand All @@ -102,6 +106,7 @@ function copyDecoratorMeta(meta: DecoratorMeta) : DecoratorMeta {
middlewares: Array.isArray(meta.middlewares) ? [...meta.middlewares] : meta.middlewares,
methods: methods as DecoratorMeta['methods'],
produces: [...meta.produces],
consumes: [...meta.consumes],
responses: responses as DecoratorMeta['responses'],
parameters: parameters as DecoratorMeta['parameters'],
};
Expand Down
Loading
Loading