Installations
npm install @poppinss/macroable
Releases
Update dependencies
Published on 17 Sept 2024
Update dependencies
Published on 28 Mar 2024
Updating dependencies
Published on 15 Dec 2023
Bundle using TSUP and publish under latest tag
Published on 14 Oct 2023
Update dependencies
Published on 23 Sept 2023
Update dependencies
Published on 29 Jun 2023
Developer
poppinss
Developer Guide
Module System
ESM
Min. Node Version
>=18.16.0
Typescript Support
Yes
Node Version
20.17.0
NPM Version
10.8.2
Statistics
36 Stars
93 Commits
2 Forks
7 Watching
2 Branches
3 Contributors
Updated on 10 Oct 2024
Languages
TypeScript (97.82%)
Shell (1.12%)
JavaScript (1.06%)
Total Downloads
Cumulative downloads
Total Downloads
1,598,429
Last day
-1.6%
9,999
Compared to previous day
Last week
3%
57,685
Compared to previous week
Last month
8.1%
241,265
Compared to previous month
Last year
4,712%
1,565,692
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@poppinss/macroable
Extend classes from outside in using Macros and getters
Macroable offers a simple API for adding properties and getters to the class prototype. You might not even need this package, if you are happy writing Object.defineProperty
calls yourself.
Usage
Install the package from npm packages registry as follows.
1npm i @poppinss/macroable 2 3# yarn lovers 4yarn add @poppinss/macroable
And import the Macroable
class.
1import Macroable from '@poppinss/macroable' 2export class Route extends Macroable {}
Now, you can add properties to the Route class from outside-in. This is usually needed, when you want the consumer of your classes to be able to extend them by adding custom properties.
Macros
Getters are added to the class prototype directly.
1Route.macro('head', function (uri, callback) { 2 return this.route(['HEAD'], uri, callback) 3})
And now, you can will be use the head
method from an instance of the Route
class.
1const route = new Route() 2route.head('/', () => {})
Adding a macro is same as writing the following code in JavaScript.
1Route.prototype.head = function () { 2}
Getters
Getters are added to the class prototype using the Object.defineProperty
. The implementation of a getter is always a function.
1Route.getter('version', function () { 2 return 'v1' 3})
And now access the version as follows.
1const route = new Route() 2route.version // v1
Adding a getter is same as writing the following code in JavaScript.
1Object.defineProperty(Route.prototype, 'version', { 2 get() { 3 const value = callback() 4 return value 5 }, 6 configurable: false, 7 enumerable: false, 8})
Singleton getters
Singleton getters are also defined on the class prototype. However, their values are cached after the first access.
1const singleton = true 2 3Mysql.getter('version', function () { 4 return this.config.driver.split('-')[1] 5}, singleton)
Adding a singleton getter is same as writing the following code in JavaScript.
1Object.defineProperty(Mysql.prototype, 'version', {
2 get() {
3 const value = callback()
4
5 // Cache value on the class instance
6 Object.defineProperty(this, 'version', {
7 configurable: false,
8 enumerable: false,
9 value: value,
10 writable: false,
11 })
12
13 return value
14 },
15 configurable: false,
16 enumerable: false,
17})
TypeScript types
You will have to use module augmentation in order to define the types for the dynamically added properties.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/poppinss/.github/docs/SECURITY.md:1
- Info: Found linked content: github.com/poppinss/.github/docs/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/poppinss/.github/docs/SECURITY.md:1
- Info: Found text in security policy: github.com/poppinss/.github/docs/SECURITY.md:1
Reason
6 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/labels.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/poppinss/macroable/labels.yml/1.x?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/labels.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/poppinss/macroable/labels.yml/1.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/poppinss/macroable/release.yml/1.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/poppinss/macroable/release.yml/1.x?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:28
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/checks.yml:1
- Warn: topLevel 'contents' permission set to 'write': .github/workflows/release.yml:4
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch '1.x'
Score
4.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More