-
Notifications
You must be signed in to change notification settings - Fork 0
Partition canonical api spec output #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17d9968
feature: sha256 ν΄μ μμ± λ° stableStringify ν¨μ μΆκ°
toothlessdev 59e0522
chore: @types/node ν¨ν€μ§μμ‘΄μ± μΆκ°
toothlessdev 84b7030
feature: tag[0] κΈ°λ° νν°μ
λ μ λ΅ λμ
toothlessdev d9d8948
feature: HttpMethod κΈ°λ° νν°μ
λ μ λ΅ λμ
toothlessdev 0ab4958
fix: μ€μ²©λ κ°μ²΄ λ° λ°°μ΄λ μ λ ¬λλλ‘ μμ
toothlessdev 5c08992
test: λ¬Έμμ΄ λμ JSON.stringify κ²°κ³Όλ‘ λ체νμ¬ ν
μ€νΈ κ°λ
μ± ν₯μ
toothlessdev ff37785
test: μλͺ»λ ν
μ€νΈλͺ
μμ
toothlessdev f3bffb1
test: tag κ° μλ μν©μΌλ ν
μ€νΈ μΌμ΄μ€ μΆκ°
toothlessdev dc6ab81
test: sha256 ν΄μ±μ λν deterministic ν
μ€νΈ
toothlessdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/patchlogr-core/src/partition/__tests__/partitionByMethod.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { CanonicalSpec } from "@patchlogr/types"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import { partitionByMethod } from "../partitionByMethod"; | ||
|
|
||
| describe("partitionByMethod", () => { | ||
| test("should group by first tag", () => { | ||
|
toothlessdev marked this conversation as resolved.
Outdated
toothlessdev marked this conversation as resolved.
Outdated
|
||
| const spec: CanonicalSpec = { | ||
| operations: { | ||
| "GET /user": { | ||
| key: "GET /user", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| "GET /user/{userId}": { | ||
| key: "GET /user/{userId}", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user/{userId}", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const partitions = partitionByMethod(spec).partitions; | ||
|
toothlessdev marked this conversation as resolved.
|
||
| expect(partitions).toHaveLength(1); | ||
| expect(partitions.get("GET")).toHaveLength(2); | ||
| expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user"); | ||
| expect(partitions.get("GET")?.[1]?.operationKey).toBe( | ||
| "GET /user/{userId}", | ||
| ); | ||
| }); | ||
|
toothlessdev marked this conversation as resolved.
Outdated
|
||
|
|
||
| test("should group by multiple tags", () => { | ||
|
toothlessdev marked this conversation as resolved.
Outdated
toothlessdev marked this conversation as resolved.
Outdated
|
||
| const spec: CanonicalSpec = { | ||
| operations: { | ||
| "GET /user": { | ||
| key: "GET /user", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| "POST /auth/login": { | ||
| key: "POST /auth/login", | ||
| doc: { tags: ["auth"] }, | ||
| method: "POST", | ||
| path: "/auth/login", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const partitions = partitionByMethod(spec).partitions; | ||
|
|
||
| expect(partitions).toHaveLength(2); | ||
| expect(partitions.get("GET")).toHaveLength(1); | ||
| expect(partitions.get("POST")).toHaveLength(1); | ||
| expect(partitions.get("GET")?.[0]?.operationKey).toBe("GET /user"); | ||
| expect(partitions.get("POST")?.[0]?.operationKey).toBe( | ||
| "POST /auth/login", | ||
| ); | ||
| }); | ||
| }); | ||
69 changes: 69 additions & 0 deletions
69
packages/patchlogr-core/src/partition/__tests__/partitionByTag.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { CanonicalSpec } from "@patchlogr/types"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import { partitionByTag } from "../partitionByTag"; | ||
|
|
||
| describe("partitionByTag", () => { | ||
| test("should group by first tag", () => { | ||
| const spec: CanonicalSpec = { | ||
| operations: { | ||
| "GET /user": { | ||
| key: "GET /user", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| "GET /user/{userId}": { | ||
| key: "GET /user/{userId}", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user/{userId}", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const partitions = partitionByTag(spec).partitions; | ||
|
toothlessdev marked this conversation as resolved.
|
||
| expect(partitions).toHaveLength(1); | ||
| expect(partitions.get("user")).toHaveLength(2); | ||
| expect(partitions.get("user")?.[0]?.operationKey).toBe("GET /user"); | ||
| expect(partitions.get("user")?.[1]?.operationKey).toBe( | ||
| "GET /user/{userId}", | ||
| ); | ||
| }); | ||
|
toothlessdev marked this conversation as resolved.
|
||
|
|
||
| test("should group by multiple tags", () => { | ||
| const spec: CanonicalSpec = { | ||
| operations: { | ||
| "GET /user": { | ||
| key: "GET /user", | ||
| doc: { tags: ["user"] }, | ||
| method: "GET", | ||
| path: "/user", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| "POST /auth/login": { | ||
| key: "POST /auth/login", | ||
| doc: { tags: ["auth"] }, | ||
| method: "POST", | ||
| path: "/auth/login", | ||
| request: { params: [] }, | ||
| responses: {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const partitions = partitionByTag(spec).partitions; | ||
|
|
||
| expect(partitions).toHaveLength(2); | ||
| expect(partitions.get("user")).toHaveLength(1); | ||
| expect(partitions.get("auth")).toHaveLength(1); | ||
| expect(partitions.get("user")?.[0]?.operationKey).toBe("GET /user"); | ||
| expect(partitions.get("auth")?.[0]?.operationKey).toBe( | ||
| "POST /auth/login", | ||
| ); | ||
| }); | ||
| }); | ||
|
toothlessdev marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export type PartitionManifest = { | ||
| key: string; | ||
| hash: string; | ||
| }; | ||
|
toothlessdev marked this conversation as resolved.
|
||
|
|
||
| export type Partition = { | ||
| hash: string; | ||
| operationKey: string; | ||
| }; | ||
|
|
||
| export type PartitionedSpec = { | ||
| metadata: Record<string, unknown>; | ||
| partitions: Map<string, Partition[]>; | ||
| }; | ||
|
toothlessdev marked this conversation as resolved.
|
||
26 changes: 26 additions & 0 deletions
26
packages/patchlogr-core/src/partition/partitionByMethod.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { CanonicalSpec, HTTPMethod } from "@patchlogr/types"; | ||
| import type { Partition, PartitionedSpec } from "./partition"; | ||
|
|
||
| import { createSHA256Hash } from "../utils/createHash"; | ||
| import { stableStringify } from "../utils/stableStringify"; | ||
|
|
||
|
toothlessdev marked this conversation as resolved.
|
||
| export function partitionByMethod(spec: CanonicalSpec): PartitionedSpec { | ||
| const partitions = new Map<HTTPMethod, Partition[]>(); | ||
|
|
||
| Object.entries(spec.operations).forEach(([key, operation]) => { | ||
| const hash = createSHA256Hash(stableStringify(operation)); | ||
|
|
||
| if (!partitions.has(operation.method)) | ||
| partitions.set(operation.method, [{ hash, operationKey: key }]); | ||
| else | ||
| partitions.get(operation.method)?.push({ hash, operationKey: key }); | ||
|
toothlessdev marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| return { | ||
| metadata: { | ||
| ...spec.info, | ||
| ...spec.security, | ||
| }, | ||
| partitions, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { CanonicalSpec } from "@patchlogr/types"; | ||
| import type { Partition, PartitionedSpec } from "./partition"; | ||
|
|
||
| import { createSHA256Hash } from "../utils/createHash"; | ||
| import { stableStringify } from "../utils/stableStringify"; | ||
|
|
||
|
toothlessdev marked this conversation as resolved.
|
||
| const DEFAULT_TAG = "__DEFAULT__"; | ||
|
|
||
|
toothlessdev marked this conversation as resolved.
|
||
| export function partitionByTag(spec: CanonicalSpec): PartitionedSpec { | ||
| const partitions = new Map<string, Partition[]>(); | ||
|
|
||
| Object.entries(spec.operations).forEach(([key, operation]) => { | ||
| const tag = operation.doc?.tags?.[0] || DEFAULT_TAG; | ||
|
toothlessdev marked this conversation as resolved.
|
||
| const hash = createSHA256Hash(stableStringify(operation)); | ||
|
|
||
|
toothlessdev marked this conversation as resolved.
|
||
| if (!partitions.has(tag)) | ||
| partitions.set(tag, [{ hash, operationKey: key }]); | ||
| else partitions.get(tag)?.push({ hash, operationKey: key }); | ||
| }); | ||
|
|
||
| return { | ||
| metadata: { | ||
| ...spec.info, | ||
| ...spec.security, | ||
| }, | ||
| partitions, | ||
| }; | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
packages/patchlogr-core/src/utils/__tests__/stableStringify.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import { stableStringify } from "../stableStringify"; | ||
|
|
||
| describe("stableStringify", () => { | ||
| test("should stringify json", () => { | ||
| expect(stableStringify({ a: 1, b: 2, c: 3 })).toBe( | ||
| '{"a":1,"b":2,"c":3}', | ||
| ); | ||
| }); | ||
|
|
||
| test("should stringify json in a stable order", () => { | ||
| const obj1 = { a: 1, b: 2 }; | ||
| const obj2 = { b: 2, a: 1 }; | ||
|
|
||
| expect(stableStringify(obj1)).toBe(stableStringify(obj2)); | ||
| }); | ||
| }); | ||
|
toothlessdev marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import crypto from "crypto"; | ||
|
|
||
|
toothlessdev marked this conversation as resolved.
|
||
| export function createSHA256Hash(data: string) { | ||
| return crypto.createHash("sha256").update(data).digest("hex"); | ||
| } | ||
|
toothlessdev marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function stableStringify(obj: Record<string, unknown>) { | ||
| return JSON.stringify(obj, Object.keys(obj).sort()); | ||
|
toothlessdev marked this conversation as resolved.
Outdated
toothlessdev marked this conversation as resolved.
Outdated
|
||
| } | ||
|
toothlessdev marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.