-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from pmcelhaney/refactor-path-coder
Support OpenAPI reusable responses in global components object (handle $ref properly everywhere)
- Loading branch information
Showing
12 changed files
with
190 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { printObjectWithoutQuotes } from "./printers.js"; | ||
import { ResponseTypeCoder } from "./response-type-coder.js"; | ||
import { TypeCoder } from "./type-coder.js"; | ||
|
||
export class ResponsesTypeCoder extends TypeCoder { | ||
constructor(requirement, openApi2MediaTypes = []) { | ||
super(requirement); | ||
|
||
this.openApi2MediaTypes = openApi2MediaTypes; | ||
} | ||
|
||
typeForDefaultStatusCode(listedStatusCodes) { | ||
const definedStatusCodes = listedStatusCodes.filter( | ||
(key) => key !== "default", | ||
); | ||
|
||
if (definedStatusCodes.length === 0) { | ||
return "[statusCode in HttpStatusCode]"; | ||
} | ||
|
||
return `[statusCode in Exclude<HttpStatusCode, ${definedStatusCodes.join( | ||
" | ", | ||
)}>]`; | ||
} | ||
|
||
normalizeStatusCode(statusCode) { | ||
if (statusCode === "default") { | ||
return this.typeForDefaultStatusCode(Object.keys(this.requirement.data)); | ||
} | ||
|
||
return statusCode; | ||
} | ||
|
||
buildResponseObjectType(script) { | ||
return printObjectWithoutQuotes( | ||
this.requirement.map((response, responseCode) => [ | ||
this.normalizeStatusCode(responseCode), | ||
new ResponseTypeCoder(response, this.openApi2MediaTypes).write(script), | ||
]), | ||
); | ||
} | ||
|
||
writeCode(script) { | ||
script.importSharedType("ResponseBuilderFactory"); | ||
|
||
const text = `ResponseBuilderFactory<${this.buildResponseObjectType( | ||
script, | ||
)}>`; | ||
|
||
if (text.includes("HttpStatusCode")) { | ||
script.importSharedType("HttpStatusCode"); | ||
} | ||
|
||
return text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Coder } from "./coder.js"; | ||
|
||
export class TypeCoder extends Coder { | ||
write(script) { | ||
if (this.requirement?.isReference) { | ||
return script.importType(this); | ||
} | ||
|
||
return this.writeCode(script); | ||
} | ||
} |
Oops, something went wrong.