Skip to content

Commit

Permalink
take empty properties into account
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Jul 4, 2024
1 parent 7c14d94 commit 82061a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ function toTypeScriptType(property: Property) {
}
}

function propertyIsEmpty(p: Property | null) {
if (!p) return true
return !p.type && !p.$ref
}

function propertyHasComment(p: Property | null) {
if (!p) return false
return p.description || p.$ref
Expand Down Expand Up @@ -56,6 +61,7 @@ export function render() {
...getContext(),
toTypeScriptType,
propertyHasComment,
propertyIsEmpty,
exportHasComment,
formatBlockComment,
formatComment,
Expand Down
8 changes: 4 additions & 4 deletions template/src/index.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

<% schema.exports.forEach(ex => { -%>
export function <%= ex.name %>(): number {
<% if (ex.input) { -%>
<% if (!propertyIsEmpty(ex.input)) { -%>
<% if (ex.input.contentType === 'application/json') { -%>
const input: <%= toTypeScriptType(ex.input) %> = JSON.parse(Host.inputString())
<% } else if (ex.input.type === 'string') { -%>
Expand All @@ -18,20 +18,20 @@ export function <%= ex.name %>(): number {
const input: ArrayBufferLike = Host.inputBytes()
<% } -%>
<% if (ex.output) { -%>
<% if (!propertyIsEmpty(ex.output)) { -%>
const output = main.<%= ex.name %>Impl(input)
<% } else { -%>
main.<%= ex.name %>Impl(input)
<% } -%>
<% } else { -%>
<% if (ex.output) { -%>
<% if (!propertyIsEmpty(ex.output)) { -%>
const output = main.<%= ex.name %>Impl()
<% } else { -%>
main.<%= ex.name %>Impl()
<% } -%>
<% } -%>
<% if (ex.output) { -%>
<% if (!propertyIsEmpty(ex.output)) { -%>
<% if (ex.output.contentType === 'application/json') { -%>
Host.outputString(JSON.stringify(output))
<% } else if (ex.output.type === 'string') { -%>
Expand Down
2 changes: 1 addition & 1 deletion template/src/main.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
<% } -%>
*/
<% -%>
export function <%= ex.name %>Impl(<%= ex.input ? `input: ${toTypeScriptType(ex.input)}` : "" %>)<%= ex.output ? `:${toTypeScriptType(ex.output)}` : "" %> {
export function <%= ex.name %>Impl(<%= propertyIsEmpty(ex.input) ? "" : `input: ${toTypeScriptType(ex.input)}` %>)<%= propertyIsEmpty(ex.output) ? "" : `:${toTypeScriptType(ex.output)}` %> {
// TODO: fill out your implementation here
throw new Error("Function not implemented.");
}
Expand Down

0 comments on commit 82061a5

Please sign in to comment.