|
1 | | -import inflection from "inflection"; |
| 1 | +import { camelize, classify, pluralize } from "inflection"; |
2 | 2 | import type { ParseOptions } from "jsonref"; |
3 | 3 | import { parse } from "jsonref"; |
4 | 4 | import type { OpenAPIV3 } from "openapi-types"; |
5 | 5 | import type { OperationType } from "../core/index.js"; |
6 | 6 | import { Field, Operation, Parameter, Resource } from "../core/index.js"; |
7 | 7 | import { |
8 | | -getResourcePaths, |
| 8 | + buildEnumObject, |
| 9 | + getResourcePaths, |
9 | 10 | getType, |
10 | 11 | removeTrailingSlash, |
11 | 12 | } from "../core/utils/index.js"; |
@@ -39,21 +40,6 @@ function mergeResources(resourceA: Resource, resourceB: Resource) { |
39 | 40 | return resourceA; |
40 | 41 | } |
41 | 42 |
|
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 | | - |
57 | 43 | function getArrayType(property: SchemaObjectDereferenced) { |
58 | 44 | if (property.type !== "array") { |
59 | 45 | return null; |
@@ -159,14 +145,14 @@ export default async function handleJson( |
159 | 145 | throw new Error("Invalid path: " + path); |
160 | 146 | } |
161 | 147 |
|
162 | | - const name = inflection.pluralize(baseName); |
| 148 | + const name = pluralize(baseName); |
163 | 149 | const url = `${removeTrailingSlash(serverUrl)}/${name}`; |
164 | 150 | const pathItem = document.paths[path]; |
165 | 151 | if (!pathItem) { |
166 | 152 | throw new Error(); |
167 | 153 | } |
168 | 154 |
|
169 | | - const title = inflection.classify(baseName); |
| 155 | + const title = classify(baseName); |
170 | 156 |
|
171 | 157 | const showOperation = pathItem.get; |
172 | 158 | const editOperation = pathItem.put || pathItem.patch; |
@@ -198,12 +184,15 @@ export default async function handleJson( |
198 | 184 | resource = mergeResources(showResource, editResource); |
199 | 185 | } |
200 | 186 |
|
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; |
204 | 192 | const pathCollection = document.paths[`/${name}`]; |
205 | 193 | const listOperation = pathCollection && pathCollection.get; |
206 | 194 | const createOperation = pathCollection && pathCollection.post; |
| 195 | + |
207 | 196 | resource.operations = [ |
208 | 197 | ...(showOperation |
209 | 198 | ? [buildOperationFromPathItem("get", "show", showOperation)] |
@@ -244,10 +233,10 @@ export default async function handleJson( |
244 | 233 | // Guess embeddeds and references from property names |
245 | 234 | for (const resource of resources) { |
246 | 235 | for (const field of resource.fields ?? []) { |
247 | | - const name = inflection.camelize(field.name).replace(/Ids?$/, ""); |
| 236 | + const name = camelize(field.name).replace(/Ids?$/, ""); |
248 | 237 |
|
249 | 238 | const guessedResource = resources.find( |
250 | | - (res) => res.title === inflection.classify(name), |
| 239 | + (res) => res.title === classify(name), |
251 | 240 | ); |
252 | 241 | if (!guessedResource) { |
253 | 242 | continue; |
|
0 commit comments