Skip to content

Commit a97dffc

Browse files
committed
feat: add text/csv media type support
1 parent 9ff319c commit a97dffc

2 files changed

Lines changed: 113 additions & 2 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { describe, expect, it } from "vitest";
2+
import { findCompatibleMediaType } from "./findCompatibleMediaType";
3+
4+
describe("findCompatibleMediaType", () => {
5+
it("should return undefined if content is empty", () => {
6+
expect(findCompatibleMediaType({ content: {} })).toBeUndefined();
7+
});
8+
9+
it("should return MediaTypeObject for application/json", () => {
10+
const mediaTypeObject = { schema: { type: "object" as const } };
11+
const result = findCompatibleMediaType({
12+
content: {
13+
"application/json": mediaTypeObject,
14+
},
15+
});
16+
expect(result).toBe(mediaTypeObject);
17+
});
18+
19+
it("should return MediaTypeObject for application/json with charset", () => {
20+
const mediaTypeObject = { schema: { type: "object" as const } };
21+
const result = findCompatibleMediaType({
22+
content: {
23+
"application/json; charset=utf-8": mediaTypeObject,
24+
},
25+
});
26+
expect(result).toBe(mediaTypeObject);
27+
});
28+
29+
it("should return MediaTypeObject for */*", () => {
30+
const mediaTypeObject = { schema: { type: "string" as const } };
31+
const result = findCompatibleMediaType({
32+
content: {
33+
"*/*": mediaTypeObject,
34+
},
35+
});
36+
expect(result).toBe(mediaTypeObject);
37+
});
38+
39+
it("should return MediaTypeObject for application/octet-stream", () => {
40+
const mediaTypeObject = {
41+
schema: { type: "string" as const, format: "binary" },
42+
};
43+
const result = findCompatibleMediaType({
44+
content: {
45+
"application/octet-stream": mediaTypeObject,
46+
},
47+
});
48+
expect(result).toBe(mediaTypeObject);
49+
});
50+
51+
it("should return MediaTypeObject for multipart/form-data", () => {
52+
const mediaTypeObject = {
53+
schema: {
54+
type: "object" as const,
55+
properties: {
56+
file: { type: "string" as const, format: "binary" },
57+
},
58+
},
59+
};
60+
const result = findCompatibleMediaType({
61+
content: {
62+
"multipart/form-data": mediaTypeObject,
63+
},
64+
});
65+
expect(result).toBe(mediaTypeObject);
66+
});
67+
68+
it("should return MediaTypeObject for text/csv", () => {
69+
const mediaTypeObject = { schema: { type: "string" as const } };
70+
const result = findCompatibleMediaType({
71+
content: {
72+
"text/csv": mediaTypeObject,
73+
},
74+
});
75+
expect(result).toBe(mediaTypeObject);
76+
});
77+
78+
it("should return MediaTypeObject for text/csv with charset", () => {
79+
const mediaTypeObject = { schema: { type: "string" as const } };
80+
const result = findCompatibleMediaType({
81+
content: {
82+
"text/csv; charset=utf-8": mediaTypeObject,
83+
},
84+
});
85+
expect(result).toBe(mediaTypeObject);
86+
});
87+
88+
it("should return undefined for unsupported media type", () => {
89+
const result = findCompatibleMediaType({
90+
content: {
91+
"text/html": { schema: { type: "string" as const } },
92+
},
93+
});
94+
expect(result).toBeUndefined();
95+
});
96+
97+
it("should return the first compatible media type when multiple are present", () => {
98+
const jsonMediaType = { schema: { type: "object" as const } };
99+
const csvMediaType = { schema: { type: "string" as const } };
100+
const result = findCompatibleMediaType({
101+
content: {
102+
"text/html": { schema: { type: "string" as const } },
103+
"application/json": jsonMediaType,
104+
"text/csv": csvMediaType,
105+
},
106+
});
107+
expect(result).toBeDefined();
108+
expect([jsonMediaType, csvMediaType]).toContain(result);
109+
});
110+
});

plugins/typescript/src/core/findCompatibleMediaType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
MediaTypeObject,
33
RequestBodyObject,
44
ResponseObject,
@@ -19,7 +19,8 @@ export const findCompatibleMediaType = (
1919
contentType.startsWith("*/*") ||
2020
contentType.startsWith("application/json") ||
2121
contentType.startsWith("application/octet-stream") ||
22-
contentType.startsWith("multipart/form-data")
22+
contentType.startsWith("multipart/form-data") ||
23+
contentType.startsWith("text/csv")
2324
) {
2425
return requestBodyOrResponseObject.content[contentType];
2526
}

0 commit comments

Comments
 (0)