Skip to content

Commit d9d8948

Browse files
committed
feature: HttpMethod 기반 파티셔닝 전략 도입
1 parent 84b7030 commit d9d8948

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { CanonicalSpec } from "@patchlogr/types";
2+
import { describe, expect, test } from "vitest";
3+
import { partitionByMethod } from "../partitionByMethod";
4+
5+
describe("partitionByMethod", () => {
6+
test("should group by first tag", () => {
7+
const spec: CanonicalSpec = {
8+
operations: {
9+
"GET /user": {
10+
key: "GET /user",
11+
doc: { tags: ["user"] },
12+
method: "GET",
13+
path: "/user",
14+
request: { params: [] },
15+
responses: {},
16+
},
17+
"GET /user/{userId}": {
18+
key: "GET /user/{userId}",
19+
doc: { tags: ["user"] },
20+
method: "GET",
21+
path: "/user/{userId}",
22+
request: { params: [] },
23+
responses: {},
24+
},
25+
},
26+
};
27+
28+
const partitions = partitionByMethod(spec).partitions;
29+
expect(partitions).toHaveLength(1);
30+
expect(partitions.get("GET")).toHaveLength(2);
31+
expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user");
32+
expect(partitions.get("GET")?.[1]?.operationKey).toBe(
33+
"GET /user/{userId}",
34+
);
35+
});
36+
37+
test("should group by multiple tags", () => {
38+
const spec: CanonicalSpec = {
39+
operations: {
40+
"GET /user": {
41+
key: "GET /user",
42+
doc: { tags: ["user"] },
43+
method: "GET",
44+
path: "/user",
45+
request: { params: [] },
46+
responses: {},
47+
},
48+
"POST /auth/login": {
49+
key: "POST /auth/login",
50+
doc: { tags: ["auth"] },
51+
method: "POST",
52+
path: "/auth/login",
53+
request: { params: [] },
54+
responses: {},
55+
},
56+
},
57+
};
58+
59+
const partitions = partitionByMethod(spec).partitions;
60+
61+
expect(partitions).toHaveLength(2);
62+
expect(partitions.get("GET")).toHaveLength(1);
63+
expect(partitions.get("POST")).toHaveLength(1);
64+
expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user");
65+
expect(partitions.get("POST")?.[0]?.operationKey).toBe(
66+
"POST /auth/login",
67+
);
68+
});
69+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { CanonicalSpec, HTTPMethod } from "@patchlogr/types";
2+
import type { Partition, PartitionedSpec } from "./partition";
3+
4+
import { createSHA256Hash } from "../utils/createHash";
5+
import { stableStringify } from "../utils/stableStringify";
6+
7+
export function partitionByMethod(spec: CanonicalSpec): PartitionedSpec {
8+
const partitions = new Map<HTTPMethod, Partition[]>();
9+
10+
Object.entries(spec.operations).forEach(([key, operation]) => {
11+
const hash = createSHA256Hash(stableStringify(operation));
12+
13+
if (!partitions.has(operation.method))
14+
partitions.set(operation.method, [{ hash, operationKey: key }]);
15+
else
16+
partitions.get(operation.method)?.push({ hash, operationKey: key });
17+
});
18+
19+
return {
20+
metadata: {
21+
...spec.info,
22+
...spec.security,
23+
},
24+
partitions,
25+
};
26+
}

0 commit comments

Comments
 (0)