Skip to content

Commit 60aa614

Browse files
committed
feat(utils): add buildEnumObject function and update imports
- Implement buildEnumObject to create an object from enum values. - Update openapi and swagger handleJson to use buildEnumObject. - Refactor inflection imports to use named imports instead of default import - Small refactors - Add TSDoc for the getTYpe util function Signed-off-by: J3m5 <[email protected]>
1 parent a90b31c commit 60aa614

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

src/core/utils/buildEnumObject.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { humanize } from "inflection";
2+
3+
/**
4+
* Builds an object from an array of enum values.
5+
* The keys of the object are the humanized versions of the enum values,
6+
* and the values are the original enum values.
7+
*
8+
* @param enumArray - An array of enum values.
9+
* @returns An object mapping humanized enum names to their original values, or null if the input is empty.
10+
*/
11+
export function buildEnumObject(
12+
enumArray: any[] | undefined,
13+
): Record<string, string | number> | null {
14+
if (!enumArray || enumArray.length === 0) {
15+
return null;
16+
}
17+
18+
return Object.fromEntries(
19+
// Object.values is used because the array is annotated: it contains the __meta symbol used by jsonref.
20+
Object.values(enumArray).map((enumValue: string | number) => [
21+
typeof enumValue === "string" ? humanize(enumValue) : enumValue,
22+
enumValue,
23+
]),
24+
);
25+
}

src/core/utils/getType.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import inflection from "inflection";
1+
import { camelize } from "inflection";
22
import type { FieldType } from "../Field.js";
33

4+
/**
5+
* Returns the corresponding FieldType for a given OpenAPI type and optional format.
6+
*
7+
* If a format is provided, it will map certain formats (e.g., "int32", "int64") to "integer".
8+
* Otherwise, it will camelize the format string. If no format is provided, it returns the OpenAPI type.
9+
*
10+
* @param openApiType - The OpenAPI type string.
11+
* @param format - An optional format string.
12+
* @returns The mapped FieldType.
13+
*/
414
export function getType(openApiType: string, format?: string): FieldType {
515
if (format) {
616
switch (format) {
717
case "int32":
818
case "int64":
919
return "integer";
1020
default:
11-
return inflection.camelize(format.replace("-", "_"), true);
21+
return camelize(format.replace("-", "_"), true);
1222
}
1323
}
1424

src/core/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { assignSealed } from "./assignSealed.js";
2+
export { buildEnumObject } from "./buildEnumObject.js";
23
export { getResourcePaths } from "./getResourcePaths.js";
34
export { getType } from "./getType.js";
45
export { parsedJsonReplacer } from "./parsedJsonReplacer.js";

src/openapi3/handleJson.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import inflection from "inflection";
1+
import { camelize, classify, pluralize } from "inflection";
22
import type { ParseOptions } from "jsonref";
33
import { parse } from "jsonref";
44
import type { OpenAPIV3 } from "openapi-types";
55
import type { OperationType } from "../core/index.js";
66
import { Field, Operation, Parameter, Resource } from "../core/index.js";
77
import {
8-
getResourcePaths,
8+
buildEnumObject,
9+
getResourcePaths,
910
getType,
1011
removeTrailingSlash,
1112
} from "../core/utils/index.js";
@@ -39,21 +40,6 @@ function mergeResources(resourceA: Resource, resourceB: Resource) {
3940
return resourceA;
4041
}
4142

42-
function buildEnumObject(enumArray: SchemaObjectDereferenced["enum"]) {
43-
if (!enumArray) {
44-
return null;
45-
}
46-
return Object.fromEntries(
47-
// Object.values is used because the array is annotated: it contains the __meta symbol used by jsonref.
48-
Object.values(enumArray).map((enumValue) => [
49-
typeof enumValue === "string"
50-
? inflection.humanize(enumValue)
51-
: enumValue,
52-
enumValue,
53-
]),
54-
);
55-
}
56-
5743
function getArrayType(property: SchemaObjectDereferenced) {
5844
if (property.type !== "array") {
5945
return null;
@@ -159,14 +145,14 @@ export default async function handleJson(
159145
throw new Error("Invalid path: " + path);
160146
}
161147

162-
const name = inflection.pluralize(baseName);
148+
const name = pluralize(baseName);
163149
const url = `${removeTrailingSlash(serverUrl)}/${name}`;
164150
const pathItem = document.paths[path];
165151
if (!pathItem) {
166152
throw new Error();
167153
}
168154

169-
const title = inflection.classify(baseName);
155+
const title = classify(baseName);
170156

171157
const showOperation = pathItem.get;
172158
const editOperation = pathItem.put || pathItem.patch;
@@ -198,12 +184,15 @@ export default async function handleJson(
198184
resource = mergeResources(showResource, editResource);
199185
}
200186

201-
const putOperation = pathItem.put;
202-
const patchOperation = pathItem.patch;
203-
const deleteOperation = pathItem.delete;
187+
const {
188+
put: putOperation,
189+
patch: patchOperation,
190+
delete: deleteOperation,
191+
} = pathItem;
204192
const pathCollection = document.paths[`/${name}`];
205193
const listOperation = pathCollection && pathCollection.get;
206194
const createOperation = pathCollection && pathCollection.post;
195+
207196
resource.operations = [
208197
...(showOperation
209198
? [buildOperationFromPathItem("get", "show", showOperation)]
@@ -244,10 +233,10 @@ export default async function handleJson(
244233
// Guess embeddeds and references from property names
245234
for (const resource of resources) {
246235
for (const field of resource.fields ?? []) {
247-
const name = inflection.camelize(field.name).replace(/Ids?$/, "");
236+
const name = camelize(field.name).replace(/Ids?$/, "");
248237

249238
const guessedResource = resources.find(
250-
(res) => res.title === inflection.classify(name),
239+
(res) => res.title === classify(name),
251240
);
252241
if (!guessedResource) {
253242
continue;

src/swagger/handleJson.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import inflection from "inflection";
1+
import { classify, pluralize } from "inflection";
22
import type { OpenAPIV2 } from "openapi-types";
33
import { Field, Resource } from "../core/index.js";
44
import {
5+
buildEnumObject,
56
getResourcePaths,
67
getType,
78
removeTrailingSlash,
@@ -20,10 +21,10 @@ export default function handleJson(
2021
throw new Error("Invalid path: " + path);
2122
}
2223

23-
const name = inflection.pluralize(baseName);
24+
const name = pluralize(baseName);
2425
const url = `${removeTrailingSlash(entrypointUrl)}/${name}`;
2526

26-
const title = inflection.classify(baseName);
27+
const title = classify(baseName);
2728

2829
if (!response.definitions) {
2930
throw new Error(); // @TODO
@@ -52,16 +53,7 @@ export default function handleJson(
5253
typeof property?.type === "string" ? property.type : "",
5354
property?.["format"] ?? "",
5455
),
55-
enum: property.enum
56-
? Object.fromEntries(
57-
property.enum.map((enumValue: string | number) => [
58-
typeof enumValue === "string"
59-
? inflection.humanize(enumValue)
60-
: enumValue,
61-
enumValue,
62-
]),
63-
)
64-
: null,
56+
enum: buildEnumObject(property.enum),
6557
reference: null,
6658
embedded: null,
6759
required: requiredFields.some((value) => value === fieldName),

0 commit comments

Comments
 (0)