Skip to content

Commit 695cee7

Browse files
committed
fix(schema): fix AcceptMime decorator behavior
1 parent c3d056d commit 695cee7

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

docs/docs/controllers.md

+20
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ You have to add extra parameter to enable it:
266266

267267
:::
268268

269+
### Accept Mime
270+
271+
@@AcceptMime@@ decorator provides you quick access to the `request.accepts()` and add given mime type to the consumable data types.
272+
273+
```typescript
274+
import {BodyParams} from "@tsed/platform-params";
275+
import {Get, AcceptMime} from "@tsed/schema";
276+
import {Controller} from "@tsed/di";
277+
278+
@Controller("/")
279+
class ExampleCtrl {
280+
@Get("/")
281+
@AcceptMime("application/x-www-form-urlencoded")
282+
async get(@BodyParams() model: Model) {
283+
return {message: "Hello world"};
284+
}
285+
}
286+
```
287+
269288
### Session/Cookies/Locals/Context
270289

271290
For the session, cookies, locals or context data attached on the request, it works the same way as seen before. Use the
@@ -275,6 +294,7 @@ following decorators to get the data:
275294
- @@Cookies@@
276295
- @@Locals@@
277296
- @@Context@@
297+
-
278298

279299
#### Locals
280300

packages/specs/schema/src/decorators/operations/acceptMime.spec.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import {Get, getSpec, JsonMethodStore, SpecTypes} from "../../index.js";
1+
import {BodyParams} from "@tsed/platform-params";
2+
3+
import {Get, getSpec, JsonMethodStore, Property, SpecTypes} from "../../index.js";
24
import {AcceptMime} from "./acceptMime.js";
35

6+
class Model {
7+
@Property()
8+
id: string;
9+
}
10+
411
describe("AcceptMime", () => {
512
it("should set metadata", () => {
613
class Test {
714
@Get("/")
815
@AcceptMime("application/json")
9-
test() {}
16+
test(@BodyParams() model: Model) {}
1017
}
1118

1219
const endpoint = JsonMethodStore.get(Test, "test");
@@ -15,11 +22,33 @@ describe("AcceptMime", () => {
1522
const spec = getSpec(Test, {specType: SpecTypes.OPENAPI});
1623

1724
expect(spec).toEqual({
25+
components: {
26+
schemas: {
27+
Model: {
28+
properties: {
29+
id: {
30+
type: "string"
31+
}
32+
},
33+
type: "object"
34+
}
35+
}
36+
},
1837
paths: {
1938
"/": {
2039
get: {
2140
operationId: "testTest",
2241
parameters: [],
42+
requestBody: {
43+
content: {
44+
"application/json": {
45+
schema: {
46+
$ref: "#/components/schemas/Model"
47+
}
48+
}
49+
},
50+
required: false
51+
},
2352
responses: {
2453
"200": {
2554
description: "Success"

packages/specs/schema/src/decorators/operations/acceptMime.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {StoreSet, useDecorators} from "@tsed/core";
22

3-
import {Produces} from "./produces.js";
3+
import {Consumes} from "./consumes.js";
44

55
/**
66
* Set a mime list which are acceptable and checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.
@@ -10,7 +10,7 @@ import {Produces} from "./produces.js";
1010
* export class MyCtrl {
1111
*
1212
* @Get('/')
13-
* @AcceptMime('application/json')
13+
* @AcceptMime('application/x-www-form-urlencoded')
1414
* public getResource(){}
1515
* }
1616
* ```
@@ -21,5 +21,5 @@ import {Produces} from "./produces.js";
2121
* @response
2222
*/
2323
export function AcceptMime(...mimes: string[]): ClassDecorator & MethodDecorator {
24-
return useDecorators(Produces(...mimes), StoreSet("acceptMimes", mimes));
24+
return useDecorators(Consumes(...mimes), StoreSet("acceptMimes", mimes));
2525
}

0 commit comments

Comments
 (0)