Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/common-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,8 @@ export const hasCoercion = (schema: z.ZodTypeAny): boolean =>
? schema._def.coerce
: false;

export const makeCleanId = (path: string, method: string, suffix?: string) => {
return [method] // collect the assets in the specified order
.concat(path.split("/"))
.concat(suffix || [])
export const makeCleanId = (...args: string[]) =>
args
.flatMap((entry) => entry.split(/[^A-Z0-9]/gi)) // split by non-alphanumeric characters
.flatMap((entry) =>
// split by sequences of capitalized letters
Expand All @@ -292,7 +290,6 @@ export const makeCleanId = (path: string, method: string, suffix?: string) => {
(entry) => entry.charAt(0).toUpperCase() + entry.slice(1).toLowerCase(),
)
.join("");
};

export const defaultSerializer = (schema: z.ZodTypeAny): string =>
createHash("sha1").update(JSON.stringify(schema), "utf8").digest("hex");
Expand Down
6 changes: 3 additions & 3 deletions src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ export const depictRequestParams = ({
});
const result =
composition === "components"
? makeRef(makeCleanId(path, method, `${clue} ${name}`), depicted)
? makeRef(makeCleanId(method, path, `${clue} ${name}`), depicted)
: depicted;
return {
name,
Expand Down Expand Up @@ -898,7 +898,7 @@ export const depictResponse = ({
const examples = depictExamples(schema, true);
const result =
composition === "components"
? makeRef(makeCleanId(path, method, clue), depictedSchema)
? makeRef(makeCleanId(method, path, clue), depictedSchema)
: depictedSchema;

return {
Expand Down Expand Up @@ -1047,7 +1047,7 @@ export const depictRequest = ({
);
const result =
composition === "components"
? makeRef(makeCleanId(path, method, clue), bodyDepiction)
? makeRef(makeCleanId(method, path, clue), bodyDepiction)
: bodyDepiction;

return {
Expand Down
2 changes: 1 addition & 1 deletion src/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Documentation extends OpenApiBuilder {
this.lastOperationIdSuffixes[userDefinedOperationId] = 1;
return userDefinedOperationId;
}
const operationId = makeCleanId(path, method);
const operationId = makeCleanId(method, path);
if (operationId in this.lastOperationIdSuffixes) {
this.lastOperationIdSuffixes[operationId]++;
return `${operationId}${this.lastOperationIdSuffixes[operationId]}`;
Expand Down
8 changes: 4 additions & 4 deletions src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ export class Integration {
optionalPropStyle,
};
const inputSchema = endpoint.getSchema("input");
const inputId = makeCleanId(path, method, "input");
const inputId = makeCleanId(method, path, "input");
const input = zodToTs({
...commons,
schema: hasRaw(inputSchema) ? ZodFile.create().buffer() : inputSchema,
isResponse: false,
});
const positiveResponseId = splitResponse
? makeCleanId(path, method, "positive.response")
? makeCleanId(method, path, "positive.response")
: undefined;
const positiveSchema = endpoint.getSchema("positive");
const positiveResponse = splitResponse
Expand All @@ -174,7 +174,7 @@ export class Integration {
})
: undefined;
const negativeResponseId = splitResponse
? makeCleanId(path, method, "negative.response")
? makeCleanId(method, path, "negative.response")
: undefined;
const negativeSchema = endpoint.getSchema("negative");
const negativeResponse = splitResponse
Expand All @@ -184,7 +184,7 @@ export class Integration {
schema: negativeSchema,
})
: undefined;
const genericResponseId = makeCleanId(path, method, "response");
const genericResponseId = makeCleanId(method, path, "response");
const genericResponse =
positiveResponseId && negativeResponseId
? f.createUnionTypeNode([
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/__snapshots__/common-helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ exports[`Common Helpers > getMessageFromError() > should pass message from other

exports[`Common Helpers > getMessageFromError() > should pass message from other error types 2`] = `"something went wrong"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 0 1`] = `"Get"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 0 1`] = `"Get"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 1 1`] = `"PostSomething"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 1 1`] = `"PostSomething"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 2 1`] = `"DeleteUserPermanently"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 2 1`] = `"DeleteUserPermanently"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 3 1`] = `"PatchUserAffiliatedAccount"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 3 1`] = `"PatchUserAffiliatedAccount"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 4 1`] = `"PutAssetsIntoStorageIdentifier"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 4 1`] = `"PutAssetsIntoStorageIdentifier"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 5 1`] = `"GetFlightDetailsFromToSeatId"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 5 1`] = `"GetFlightDetailsFromToSeatId"`;

exports[`Common Helpers > makeCleanId() > should generate valid identifier from method, path and suffix 6 1`] = `"GetCompanysCompanyIdUsersUserId"`;
exports[`Common Helpers > makeCleanId() > should generate valid identifier from the supplied strings 6 1`] = `"GetCompanysCompanyIdUsersUserId"`;
20 changes: 10 additions & 10 deletions tests/unit/common-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,17 @@ describe("Common Helpers", () => {

describe("makeCleanId()", () => {
test.each([
{ method: "get", path: "" },
{ method: "post", path: "/", suffix: "something" },
{ method: "delete", path: "/user", suffix: "permanently" },
{ method: "patch", path: "/user/affiliated/account" },
{ method: "put", path: "/assets/into/:storageIdentifier" },
{ method: "get", path: "/flightDetails/:from-:to/:seatID" },
{ method: "get", path: "/companys/:companyId/users/:userId" },
["get"],
["post", "/", "something"],
["delete", "/user", "permanently"],
["patch", "/user/affiliated/account"],
["put", "/assets/into/:storageIdentifier"],
["get", "/flightDetails/:from-:to/:seatID"],
["get", "/companys/:companyId/users/:userId"],
])(
"should generate valid identifier from method, path and suffix %#",
({ method, path, suffix }) => {
expect(makeCleanId(path, method, suffix)).toMatchSnapshot();
"should generate valid identifier from the supplied strings %#",
(...args) => {
expect(makeCleanId(...args)).toMatchSnapshot();
},
);
});
Expand Down