Skip to content

Commit

Permalink
[ACC-1682] Add HalFormsTemplateBuilder#fromTemplate method
Browse files Browse the repository at this point in the history
Add ability to easily create a builder from an existing hal forms template,
to modify the template when it's necessary
  • Loading branch information
vierbergenlars committed Nov 5, 2024
1 parent d45ddbe commit 2ec2022
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/hal-forms/__tests__/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import buildTemplate, { HalFormsTemplateBuilder } from "../src/builder";

describe("HalFormsTemplateBuilder", () => {
it("#fromTemplate", () => {
const original = buildTemplate("GET", "/")
.withContentType("application/json")
.addProperty("xyz", p => p.withPrompt("Test"));

const fromTemplate = HalFormsTemplateBuilder.fromTemplate(original);

expect(fromTemplate.contentType).toEqual("application/json");
expect(fromTemplate.properties).toHaveLength(1);
expect(fromTemplate.properties[0]?.name).toEqual("xyz");
expect(fromTemplate.properties[0]?.prompt).toEqual("Test");
})
});
12 changes: 12 additions & 0 deletions packages/hal-forms/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export class HalFormsTemplateBuilder<Body, Response> implements HalFormsTemplate
return new HalFormsTemplateBuilder(request.method + " "+request.url, request);
}

public static fromTemplate<B, R>(template: HalFormsTemplate<TypedRequestSpec<B, R>>): HalFormsTemplateBuilder<B, R> {
let builder = this.from(template.request)
.withName(template.name)
.withContentType(template.contentType);

for (const prop of template.properties) {
builder = builder.addProperty(prop.name, () => prop);
}

return builder;
}

public get title() {
return undefined;
}
Expand Down

0 comments on commit 2ec2022

Please sign in to comment.