-
-
Notifications
You must be signed in to change notification settings - Fork 525
feat: brings back --make-paths-enum option to generate ApiPaths enum #2052
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9e125a9
feat: brings back --make-paths-enum option to generate ApiPaths enum
laurenz-glueck aeb2f5b
chore: adds --make-paths-enum flag to cli docs
laurenz-glueck 253c23b
chore: adds minor changeset for
laurenz-glueck 4affc3c
tests: adds tests for --make-paths-enum option and paths-enum.ts tran…
laurenz-glueck 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 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 @@ | ||
--- | ||
"openapi-typescript": minor | ||
--- | ||
|
||
brings back --make-paths-enum option to generate ApiPaths enum |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,47 @@ | ||
import type ts from "typescript"; | ||
import { tsEnum } from "../lib/ts.js"; | ||
import { getEntries } from "../lib/utils.js"; | ||
import type { PathsObject } from "../types.js"; | ||
|
||
export default function makeApiPathsEnum(pathsObject: PathsObject): ts.EnumDeclaration { | ||
const enumKeys = []; | ||
const enumMetaData = []; | ||
|
||
for (const [url, pathItemObject] of getEntries(pathsObject)) { | ||
for (const [method, operation] of Object.entries(pathItemObject)) { | ||
if (!["get", "put", "post", "delete", "options", "head", "patch", "trace"].includes(method)) { | ||
continue; | ||
} | ||
|
||
// Generate a name from the operation ID | ||
let pathName: string; | ||
if (operation.operationId) { | ||
pathName = operation.operationId; | ||
} else { | ||
// If the operation ID is not present, construct a name from the method and path | ||
pathName = (method + url) | ||
.split("/") | ||
.map((part) => { | ||
const capitalised = part.charAt(0).toUpperCase() + part.slice(1); | ||
|
||
// Remove any characters not allowed as enum keys, and attempt to remove | ||
// named parameters. | ||
return capitalised.replace(/{.*}|:.*|[^a-zA-Z\d_]+/, ""); | ||
}) | ||
.join(""); | ||
} | ||
|
||
// Replace {parameters} with :parameters | ||
const adaptedUrl = url.replace(/{(\w+)}/g, ":$1"); | ||
|
||
enumKeys.push(adaptedUrl); | ||
enumMetaData.push({ | ||
name: pathName, | ||
}); | ||
} | ||
} | ||
|
||
return tsEnum("ApiPaths", enumKeys, enumMetaData, { | ||
export: true, | ||
}); | ||
} |
This file contains 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
This file contains 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
This file contains 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
125 changes: 125 additions & 0 deletions
125
packages/openapi-typescript/test/transform/paths-enum.test.ts
This file contains 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,125 @@ | ||
import { fileURLToPath } from "node:url"; | ||
import { astToString } from "../../src/lib/ts.js"; | ||
import makeApiPathsEnum from "../../src/transform/paths-enum.js"; | ||
import type { GlobalContext } from "../../src/types.js"; | ||
import type { TestCase } from "../test-helpers.js"; | ||
|
||
describe("transformPathsObjectToEnum", () => { | ||
const tests: TestCase<any, GlobalContext>[] = [ | ||
[ | ||
"basic", | ||
{ | ||
given: { | ||
"/api/v1/user": { | ||
get: {}, | ||
}, | ||
}, | ||
want: `export enum ApiPaths { | ||
GetApiV1User = "/api/v1/user" | ||
}`, | ||
}, | ||
], | ||
[ | ||
"basic with path parameter", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great tests! 🎉 |
||
{ | ||
given: { | ||
"/api/v1/user/{user_id}": { | ||
parameters: [ | ||
{ | ||
name: "page", | ||
in: "query", | ||
schema: { type: "number" }, | ||
description: "Page number.", | ||
}, | ||
], | ||
get: { | ||
parameters: [{ name: "user_id", in: "path", description: "User ID." }], | ||
}, | ||
}, | ||
}, | ||
want: `export enum ApiPaths { | ||
GetApiV1User = "/api/v1/user/:user_id" | ||
}`, | ||
}, | ||
], | ||
[ | ||
"with operationId", | ||
{ | ||
given: { | ||
"/api/v1/user/{user_id}": { | ||
parameters: [ | ||
{ | ||
name: "page", | ||
in: "query", | ||
schema: { type: "number" }, | ||
description: "Page number.", | ||
}, | ||
], | ||
get: { | ||
operationId: "GetUserById", | ||
parameters: [{ name: "user_id", in: "path", description: "User ID." }], | ||
}, | ||
}, | ||
}, | ||
want: `export enum ApiPaths { | ||
GetUserById = "/api/v1/user/:user_id" | ||
}`, | ||
}, | ||
], | ||
[ | ||
"with and without operationId", | ||
{ | ||
given: { | ||
"/api/v1/user/{user_id}": { | ||
parameters: [ | ||
{ | ||
name: "page", | ||
in: "query", | ||
schema: { type: "number" }, | ||
description: "Page number.", | ||
}, | ||
], | ||
get: { | ||
operationId: "GetUserById", | ||
parameters: [{ name: "user_id", in: "path", description: "User ID." }], | ||
}, | ||
post: { | ||
parameters: [{ name: "user_id", in: "path", description: "User ID." }], | ||
}, | ||
}, | ||
}, | ||
want: `export enum ApiPaths { | ||
GetUserById = "/api/v1/user/:user_id", | ||
PostApiV1User = "/api/v1/user/:user_id" | ||
}`, | ||
}, | ||
], | ||
[ | ||
"invalid method", | ||
{ | ||
given: { | ||
"/api/v1/user": { | ||
invalidMethod: {}, | ||
}, | ||
}, | ||
want: `export enum ApiPaths { | ||
}`, | ||
}, | ||
], | ||
]; | ||
|
||
for (const [testName, { given, want, ci }] of tests) { | ||
test.skipIf(ci?.skipIf)( | ||
testName, | ||
async () => { | ||
const result = astToString(makeApiPathsEnum(given)); | ||
if (want instanceof URL) { | ||
expect(result).toMatchFileSnapshot(fileURLToPath(want)); | ||
} else { | ||
expect(result).toBe(`${want}\n`); | ||
} | ||
}, | ||
ci?.timeout, | ||
); | ||
} | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great docs, thank you!