Skip to content

Commit

Permalink
handle for all objects, not just schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Apr 9, 2024
1 parent 2f52f5c commit 91cb87e
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/typescript-generator/coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export class Coder {
return "";
}

write() {
// This method should be overridden by a subclass.
write(script) {
if (this.requirement?.isReference) {
return script.importType(this);
}

return `/* ${this.id} */`;
return this.writeCode(script);
}

async delegate() {
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-generator/operation-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class OperationCoder extends Coder {
return super.names(this.requestMethod());
}

write() {
writeCode() {
const responses = this.requirement.get("responses");

const [firstStatusCode] = responses.map(
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class OperationTypeCoder extends Coder {
}

// eslint-disable-next-line max-statements
write(script) {
writeCode(script) {
// eslint-disable-next-line no-param-reassign
script.comments = READ_ONLY_COMMENTS;

Expand Down
2 changes: 1 addition & 1 deletion src/typescript-generator/parameters-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ParametersTypeCoder extends Coder {
return super.names("parameters");
}

write(script) {
writeCode(script) {
const typeDefinitions = (this.requirement?.data ?? [])
.filter((parameter) => parameter.in === this.placement)
.map((parameter, index) => {
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-generator/response-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ResponseTypeCoder extends Coder {
);
}

write(script) {
writeCode(script) {
script.importSharedType("ResponseBuilderFactory");

const text = `ResponseBuilderFactory<${this.buildResponseObjectType(
Expand Down
6 changes: 5 additions & 1 deletion src/typescript-generator/schema-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ export class SchemaCoder extends Coder {
}

write(script) {
if (this.requirement.isReference) {
if (this.requirement?.isReference) {
return script.import(this);
}

return this.writeCode(script);
}

writeCode(script) {
const { type } = this.requirement.data;

if (type === "object") {
Expand Down
8 changes: 1 addition & 7 deletions src/typescript-generator/schema-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,7 @@ export class SchemaTypeCoder extends Coder {
return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
}

write(script) {
// script.comments = READ_ONLY_COMMENTS;

if (this.requirement.isReference) {
return script.importType(this);
}

writeCode(script) {
const { allOf, anyOf, oneOf, type } = this.requirement.data;

if (allOf ?? anyOf ?? oneOf) {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript-generator/coder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("a Coder", () => {

it("writes code synchronously given a requirement that is loaded", () => {
class JsonCoder extends Coder {
write() {
writeCode() {
return JSON.stringify(this.requirement.data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/typescript-generator/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("integration Test", () => {
yield `HTTP_${this.requirement.url.split("/").at(-1).toUpperCase()}`;
}

write() {
writeCode() {
return "() => {}";
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/typescript-generator/schema-coder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe("a SchemaCoder", () => {

return "aVariable";
},

importType() {
return "aType";
},
};

const result = coder.write(script);
Expand Down
2 changes: 1 addition & 1 deletion test/typescript-generator/script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("a Script", () => {
}
}

write() {
writeCode() {
return "{ }";
}
}
Expand Down

0 comments on commit 91cb87e

Please sign in to comment.