
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
@contentgrid/hal-forms
Advanced tools
@contentgrid/hal-formsTypescript models for reading the HAL-FORMS format.
The typical entrypoint for this library is resolveTemplate (or resolveTemplateRequired if you want an exception when a template is missing).
import { resolveTemplateRequired } from "@contentgrid/hal-forms"
import { HalFormsTemplateShape } from "@contentgrid/hal-forms/shape"
import { HalObjectShape } from "@contentgrid/hal/shape"
namespace myLibrary {
export const response: HalObjectShape<{
_templates?: {
search?: HalFormsTemplateShape
}
}> = {
_templates: {
search: {
method: "GET",
target: "http://localhost/gifts/search",
properties: [
{
name: "name",
required: true
}
]
}
}
}
}
const template = resolveTemplateRequired(myLibrary.response, "search");
template.properties.forEach(property => {
console.log(`Parameter: ${property.name}`)
})
When you have a form, you probably want to display it to your users in some way so they can enter values in your form fields.
The @contentgrid/hal-forms/values package is a simple utility to manage the form field values entered by users.
It uses the correct datatype belonging to each form field.
import { createValues } from "@contentgrid/hal-forms/values"
const templateValues = createValues(template); // template is a HalFormsTemplate
templateValues.values.forEach(value => {
console.log(`Field ${value.property.name}: ${value.value}`)
})
templateValues = templateValues.withValue("name", "Jeff"); // Creates a new object with the value set
templateValues.values.forEach(value => {
console.log(`Field ${value.property.name}: ${value.value}`)
})
After collecting user input, you probably want to send a request to the backend to submit the values entered in the form.
The @contentgrid/hal-forms/codecs sub-package encodes the values according to the content-type required by the HAL-FORMS template.
A default set of codecs is available, but you can also create your own set of codecs using HalFormsCodecs.builder().
The default set of codecs includes:
. are mapped to nested objectsx-www-form-urlencoded)multipart/form-data)text/uri-list for forms containing only URIsimport codecs from "@contentgrid/hal-forms/codecs";
const request = codecs.requireCodecFor(template) // template is a HalFormsTemplate
.encode(templateValues); // templateValues is a HalFormValues
// The HAL-FORMS template method, target, contentType and values are encoded in request
// Put some additional headers on your request here
request.headers.set('Authorization', 'Basic ....');
// Perform the HTTP request
const response = await fetch(request);
console.log(response);
You may want to pre-fill some HAL-FORMS with values coming from a response received earlier from the backend.
The @contentgrid/hal-forms/codecs sub-package also provides decoders that can decode a response into values for a HAL-FORMS template.
This is especially useful for edit forms, where the current values of the backend are prefilled into the form before showing it to the user for modification.
import codecs from "@contentgrid/hal-forms/codecs";
const templateValues = codecs.requireCodecFor(template) // template is a HalFormsTemplate
.decode({
contentType: "application/json",
body: { // body can be a string encoding a JSON object as well (or FormData)
name: "Jeff"
}
});
// templateValues is the same as created using createValues(template), but with data filled in
templateValues.values.forEach(value => {
console.log(`Field ${value.property.name}: ${value.value}`)
})
The @contentgrid/hal-forms/shape sub-package provides POJO (plain old javascript object) types that can be used to represent the raw HAL-FORMS JSON data.
When using these shapes to define the response schema, HalFormsTemplateShape can take type parameters which determine the shape of the expected request and response bodies.
These types can be combined with @contentgrid/typed-fetch to statically type the expected request and response bodies for the form.
import { HalSlice } from "@contentgrid/hal";
import { resolveTemplateRequired } from "@contentgrid/hal-forms"
import { HalFormsTemplateShape } from "@contentgrid/hal-forms/shape"
import { HalObjectShape, HalSliceShape } from "@contentgrid/hal/shape"
import { fetch, createRequest, Representation } from "@contentgrid/typed-fetch"
namespace myLibrary {
export interface Gift {
id: number;
name: string;
}
export interface GiftSearchRequest {
name?: string;
}
export type GiftSearchResponse = HalSliceShape<Gift>;
export const response: HalObjectShape<{
_templates?: {
search?: HalFormsTemplateShape<GiftSearchRequest, GiftSearchResponse>
}
}> = {
_templates: {
search: {
method: "GET",
target: "http://localhost/gifts/search",
properties: [
{
name: "name",
required: true
}
]
}
}
}
}
const template = resolveTemplateRequired(myLibrary.response, "search");
const request = createRequest(template.request, {
body: Representation.json({
name: "My friend"
})
});
(async () => {
const response = await fetch(request);
if(response.ok) {
const jsonData = await response.json();
const page = new HalSlice(jsonData);
page.items.forEach(item => {
console.log(item.data.name);
})
}
})();
In case you want to use the HAL-FORMS models to build your forms, but the API you're using does not have a HAL-FORMS template, you can construct a template by using @contentgrid/hal-forms/builder
import buildHalForm from "@contentgrid/hal-forms/builder"
const template = buildHalForm("GET", "http://localhost/gifts/search")
.addProperty("name", property => property.withType("text").withRequired(true));
template.properties.forEach(property => {
console.log(`Parameter: ${property.name}`)
})
FAQs
HAL-FORMS models
We found that @contentgrid/hal-forms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.