-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: brings back --make-paths-enum option to generate ApiPaths enum (#…
…2052) * feat: brings back --make-paths-enum option to generate ApiPaths enum * chore: adds --make-paths-enum flag to cli docs * chore: adds minor changeset for * tests: adds tests for --make-paths-enum option and paths-enum.ts transformer
- Loading branch information
1 parent
d4689b1
commit d2de5c7
Showing
10 changed files
with
276 additions
and
1 deletion.
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", | ||
{ | ||
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, | ||
); | ||
} | ||
}); |