monetize-npm-cli
is a modular CLI that helps monetize npm packages using the Web Monetization API and different providers.
npm install -g monetize-npm-cli
To run your app while monetizing the supported npm packages
monetize-npm-cli yourFile.js
To view help page with all details
monetize-npm-cli --help
To login to your web monetization provider
monetize-npm-cli --login
This will default to coil-extension if no provider is provided. See help for more details.
To logout from your web monetization provider
monetize-npm-cli --logout
This will default to coil-extension if no provider is provided. See help for more details.
To list all packages supporting web monetization
monetize-npm-cli --list
Use help to get full list of supported commands
The aim of this CLI is to mimic the web monetization API given here as much as it could.
Instead of document.monetization
, user gets globalThis.monetization
.
globalThis.monetization
itself is a proxy of an object which contains all the information and is not accessible globally.
document.monetization.state
=> globalThis.monetization.getState(name, version)
name
and version
are defined in package.json
of each package.
There can be four listeners set up monetizationpending
, monetizationstart
, monetizationstop
, monetizationprogress
.
Let identify them by listenerIdentifier.
document.monetization.addEventListener(listenerIdentifier, foo)
=> globalThis.monetization.addEventListener(name, version, listenerIdentifier, foo)
globalThis.monetization.removeEventListener(name, version, listenerIdentifier, foo)
If foo is not passed, all the listeners for that package are removed.
Currently only coil-extension is supported as a provider. You read more about it here
For packages to support web monetization, they must add a webMonetization key in thier package.json file
{
"webMonetization": {
"wallet": "$yourWalletAddressGoesHere"
}
}
This app currently comes with only coil-extension
provider as its the only provider existing right now, but support for more can be easily added as this CLI has been made keeping modularity in mind.
To create a provider module compatible with this CLI, it must have 3 exposed methods.
login()
logout()
monetize(monetizationPackages[,timeout])
monetizationPackages
is a proxy object passed to a provider module to start monetization
// monetizationPackages
{
packages:[
{
name: "",
version: "",
webMonetization: {
wallet:""
},
state: "",
// These arrays contain event listeners
monetizationpending: [],
monetizationstart: [],
monetizationstop: [],
monetizationprogress: [],
}
],
invokeListener(data){
// data is the response argument received when any event is fired
// monetizationpending || monetizationstart || monetizationstop || monetizationprogress
// Pass args as an array
}
}
Create an issue on this repo to add support for your provider module.