Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
feat(core/web-monetization): add object configuration (speced#3733)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres authored Aug 9, 2021
1 parent a7ca2b9 commit 6a4e995
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/core/web-monetization.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,44 @@ import { html } from "./import-maps.js";

export const name = "core/web-monetization";

const DEFAULT_PAYMENT_POINTER = "$respec.org";

export function run(conf) {
const { monetization } = conf;
if (monetization === false) {
if (conf.monetization === false) {
return;
}
const { monetization } = conf;

const paymentPointer =
typeof monetization === "string" ? monetization : DEFAULT_PAYMENT_POINTER;
const { removeOnSave, paymentPointer } = canonicalizeConfig(monetization);

if (paymentPointer === DEFAULT_PAYMENT_POINTER) {
document.head.append(
html`<!-- Support ReSpec's development - https://opencollective.com/respec --> `
);
}
const cssClass = removeOnSave ? "removeOnSave" : null;
document.head.append(html`<meta
name="monetization"
content="${paymentPointer}"
class="removeOnSave"
class="${cssClass}"
/>`);
}

/**
* @param {object|string} rawConfig
* - {string} paymentPointer - The payment pointer to use.
* - {boolean} removeOnSave - Whether to remove the meta tag when the document is saved.
*/
function canonicalizeConfig(rawConfig) {
const config = {
paymentPointer: "$respec.org",
removeOnSave: true,
};
switch (typeof rawConfig) {
case "string":
config.paymentPointer = rawConfig;
break;
case "object":
if (rawConfig.paymentPointer) {
config.paymentPointer = String(rawConfig.paymentPointer);
}
if (rawConfig.removeOnSave === false) {
config.removeOnSave = false;
}
break;
}
return config;
}
34 changes: 34 additions & 0 deletions tests/spec/core/web-monetization-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,38 @@ describe("Core - Web Monetization", () => {
expect(metaTag.content).toBe("$wallet.example.org");
expect(metaTag.classList).toContain("removeOnSave");
});

describe("object-based configuration", () => {
it("uses default payment pointer when using an empty object", async () => {
const ops = makeStandardOps({ monetization: {} });
const doc = await makeRSDoc(ops);

const metaTag = doc.querySelector("meta[name='monetization']");
expect(metaTag.content).toBe("$respec.org");
expect(metaTag.classList).toContain("removeOnSave");
});

it("allows just setting removeOnSave", async () => {
const ops = makeStandardOps({ monetization: { removeOnSave: false } });
const doc = await makeRSDoc(ops);

const metaTag = doc.querySelector("meta[name='monetization']");
expect(metaTag.content).toBe("$respec.org");
expect(metaTag.classList).not.toContain("removeOnSave");
});

it("allows changing payment pointer", async () => {
const ops = makeStandardOps({
monetization: {
paymentPointer: "$wallet.example.org",
removeOnSave: false,
},
});
const doc = await makeRSDoc(ops);

const metaTag = doc.querySelector("meta[name='monetization']");
expect(metaTag.content).toBe("$wallet.example.org");
expect(metaTag.classList).not.toContain("removeOnSave");
});
});
});

0 comments on commit 6a4e995

Please sign in to comment.