
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
builder-pattern
Advanced tools
Create a builder pattern for Typescript using ES6 proxy.
yarn add builder-pattern
interface UserInfo {
id: number;
userName: string;
email: string;
}
const userInfo = Builder<UserInfo>()
.id(1)
.userName('foo')
.email('foo@bar.baz')
.build();
To get value from existing builder, just call the method without parameter, current value will be returned.
const builder = Builder<UserInfo>();
builder.id(1);
console.log(builder.id()); // 1
console.log(builder.userName()); // undefined
builder.userName('foo');
console.log(builder.userName()); // foo
const userInfo = builder.build();
A note of caution: when building objects from scratch, the builder currently cannot ensure that all mandatory fields have been set. The built object might thus violate the contract of the given interface. For example, the following will compile (see also the example in the tests):
const brokenUserInfo = Builder<UserInfo>()
.build();
A way around this is to use template objects (see Usage with template objects).
Another way is to use StrictBuilder (see Usage with StrictBuilder).
You can also specify a template object, which allows easy creation of variation of objects. This is especially useful for making test data setup more readable:
const defaultUserInfo: UserInfo = {
id: 1,
userName: 'foo',
email: 'foo@bar.baz'
};
const modifiedUserInfo = Builder(defaultUserInfo)
.id(2)
.build();
Notes:
You can also specify a class object.
class UserInfo {
id!: number;
userName!: string;
email!: string;
}
const userInfo = Builder(UserInfo) // note that ( ) is used instead of < > here
.id(1)
.userName('foo')
.email('foo@bar.baz')
.build();
Moreover, you can also specify a class object with a template object.
class UserInfo {
id!: number;
userName!: string;
email!: string;
}
const userInfo = Builder(UserInfo, {id: 1, userName: 'foo'})
.userName:('foo bar')
.email('foo@bar.baz')
.build();
StrictBuilder is used to make sure all variables are initialized.
interface UserInfo {
id: number;
userName: string;
email: string;
}
const userInfo = StrictBuilder<UserInfo>()
.id(1)
.build(); // This expression is not callable.
// Type 'never' has no call signatures.ts(2349)
All variables must be initialized before calling build().
const userInfo = StrictBuilder<UserInfo>()
.id(1)
.userName('foo')
.email('foo@bar.baz')
.build(); // build() is called successfully
Notes:
StrictBuilder does not support template object nor class.
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featureThe idea is by unional and jcalz. Please refer to the stackoverflow question.
This project is licensed under the MIT License - see the LICENSE file for details
FAQs
Create a builder pattern for Typescript using ES6 proxy.
The npm package builder-pattern receives a total of 47,294 weekly downloads. As such, builder-pattern popularity was classified as popular.
We found that builder-pattern 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.