Skip to content
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

fix: do not modify property names #4

Merged
merged 1 commit into from
Feb 27, 2021
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
6 changes: 3 additions & 3 deletions src/___tests___/__snapshots__/generate.test.ts.snap

Large diffs are not rendered by default.

57 changes: 10 additions & 47 deletions src/___tests___/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe("getType", () => {
itemUuid: { type: "string" }
}
} as any)
).toEqual(right("PostCartItem&{itemUuid?:string}"));
).toEqual(right('PostCartItem&{"itemUuid"?:string}'));
});
});

Expand Down Expand Up @@ -600,7 +600,7 @@ describe("getType", () => {
itemUuid: { type: "string" }
}
} as any)
).toEqual(right("PostCartItem|{itemUuid?:string}"));
).toEqual(right('PostCartItem|{"itemUuid"?:string}'));
});
});

Expand Down Expand Up @@ -683,7 +683,7 @@ describe("getType", () => {
itemUuid: { type: "string" }
}
} as any)
).toEqual(right("PostCartItem|{itemUuid?:string}"));
).toEqual(right('PostCartItem|{"itemUuid"?:string}'));
});
});

Expand Down Expand Up @@ -755,7 +755,9 @@ describe("getType", () => {
type: "object"
})
).toEqual(
right("{1?:string,names?:string,data?:{age?:number,address?:string}}")
right(
'{"1"?:string,"names"?:string,"data"?:{"age"?:number,"address"?:string}}'
)
);
});
});
Expand All @@ -779,7 +781,7 @@ describe("getType", () => {
})
).toEqual(
right(
'{|"1"?:string,names?:string,data?:{|age?:number,address?:string|}|}'
'{|"1"?:string,"names"?:string,"data"?:{|"age"?:number,"address"?:string|}|}'
)
);
});
Expand All @@ -804,7 +806,7 @@ describe("getType", () => {
})
).toEqual(
right(
"({names?:(string|null),data?:{age?:number,address?:string}}|null)"
'({"names"?:(string|null),"data"?:{"age"?:number,"address"?:string}}|null)'
)
);
});
Expand All @@ -831,7 +833,7 @@ describe("getType", () => {
})
).toEqual(
right(
"{names:string,addresses?:string,data:{age:number,address?:string}}"
'{"names":string,"addresses"?:string,"data":{"age":number,"address"?:string}}'
)
);
});
Expand Down Expand Up @@ -951,46 +953,7 @@ describe("generate", () => {
}
})
).toEqual(
right('"use strict";\nexport type ExceptionResponse={code?:string}')
);
});
});

describe("when a property contains '-'", () => {
test("it converts the name to camelCase", () => {
expect(
generate(baseOptions)({
openapi: "3.0.0",
components: {
schemas: {
response: {
properties: {
type: {
description:
"Bulk type useful for apply proper validations",
type: "string",
enum: [
"timeslot",
"daily",
"open-max-days",
"open-end-date"
]
},
"tag-name": {
description: "Name to identify a bulk",
type: "string",
nullable: true
}
},
type: "object"
}
}
}
})
).toEqual(
right(
"\"use strict\";\nexport type Response={type?:'timeslot'|'daily'|'open-max-days'|'open-end-date',tagName?:(string|null)}"
)
right('"use strict";\nexport type ExceptionResponse={"code"?:string}')
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/generators/codecGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
reduce,
suffix,
surround,
toCamelCase,
toPascalCase
} from "../services/utils";

Expand All @@ -25,7 +24,8 @@ const getIntersection = doIfElse(
flow(join(","), surround("t.intersection([", "])"))
);

const getKey = (key: string): string => pipe(key, toCamelCase, suffix(":"));
const getKey = (key: string): string =>
pipe(key, surround('"', '"'), suffix(":"));

const getType = flow(join(","), surround("t.type({", "})"));

Expand Down
6 changes: 1 addition & 5 deletions src/generators/flowGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import {
prefix,
suffix,
surround,
toCamelCase,
toPascalCase
} from "../services/utils";

const isNumberKey = flow(parseInt, not(isNaN));

export const flowGenerator: Generator = {
getTypeString: () => "string",
getTypeNumber: () => "number",
Expand All @@ -29,8 +26,7 @@ export const flowGenerator: Generator = {
getProperty: (key, isRequired) =>
pipe(
key,
toCamelCase,
doIf(constant(isNumberKey(key)), surround('"', '"')),
surround('"', '"'),
doIf(not(constant(isRequired)), suffix("?")),
suffix(":"),
prefix
Expand Down
3 changes: 1 addition & 2 deletions src/generators/typescriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
prefix,
suffix,
surround,
toCamelCase,
toPascalCase
} from "../services/utils";

Expand All @@ -27,7 +26,7 @@ export const typeScriptGenerator: Generator = {
getProperty: (key, isRequired) =>
pipe(
key,
toCamelCase,
surround('"', '"'),
doIf(not(constant(isRequired)), suffix("?")),
suffix(":"),
prefix
Expand Down
8 changes: 0 additions & 8 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ export function replace(searchValue: string, replaceValue: string) {
};
}

export const toCamelCase = flow(
split("-"),
map((token, index) =>
index === 0 ? token : token[0].toUpperCase() + token.slice(1)
),
join("")
);

export const toPascalCase = flow(
split("-"),
map(token => token[0].toUpperCase() + token.slice(1)),
Expand Down