@kirby-tools/licensing 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.js +62 -0
- package/dist/index.d.ts +3 -37
- package/dist/index.js +2 -169
- package/dist/license.d.ts +26 -0
- package/dist/license.js +84 -0
- package/dist/types.d.ts +13 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +30 -0
- package/package.json +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
const TRANSLATIONS = {
|
|
3
|
+
en: {
|
|
4
|
+
"modal.fields.info": "Thanks for purchasing {label}! Please enter your email address and order ID to activate your license.",
|
|
5
|
+
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Find your order number</a> on Lemon Squeezy or <a href=\"mailto:hello@kirby.tools\">contact us</a> if you cannot find it.",
|
|
6
|
+
"errors.validation.missingFields": "Email address and order ID are required",
|
|
7
|
+
"errors.validation.invalidCredentials": "Email address or order ID is incorrect",
|
|
8
|
+
"errors.license.invalidPlugin": "License key invalid for this plugin",
|
|
9
|
+
"errors.license.versionMismatch": "License key invalid for this plugin version",
|
|
10
|
+
"errors.license.needsUpgrade": "License key invalid for this plugin version. Upgrade now on https://hub.kirby.tools.",
|
|
11
|
+
"errors.activation.alreadyUsed": "License key already activated",
|
|
12
|
+
"activate": "Activate",
|
|
13
|
+
"activated": "Plugin activated",
|
|
14
|
+
"buy": "Buy a license",
|
|
15
|
+
"upgrade": "Upgrade"
|
|
16
|
+
},
|
|
17
|
+
de: {
|
|
18
|
+
"modal.fields.info": "Dankeschön für den Kauf von {label}! Bitte gib deine E-Mail-Adresse und Bestellnummer ein, um deine Lizenz zu aktivieren.",
|
|
19
|
+
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Finde deine Bestellnummer</a> bei Lemon Squeezy oder <a href=\"mailto:hello@kirby.tools\">kontaktiere uns</a>, wenn du sie nicht finden kannst.",
|
|
20
|
+
"errors.validation.missingFields": "E-Mail-Adresse und Bestellnummer sind notwendig",
|
|
21
|
+
"errors.validation.invalidCredentials": "E-Mail-Adresse oder Bestellnummer ist falsch",
|
|
22
|
+
"errors.license.invalidPlugin": "Lizenzschlüssel ungültig für dieses Plugin",
|
|
23
|
+
"errors.license.versionMismatch": "Lizenzschlüssel ungültig für diese Plugin-Version",
|
|
24
|
+
"errors.license.needsUpgrade": "Lizenzschlüssel ungültig für diese Plugin-Version. Aktualisiere jetzt auf https://hub.kirby.tools.",
|
|
25
|
+
"errors.activation.alreadyUsed": "Lizenzschlüssel bereits aktiviert",
|
|
26
|
+
"activate": "Aktivieren",
|
|
27
|
+
"activated": "Plugin aktiviert",
|
|
28
|
+
"buy": "Lizenz kaufen",
|
|
29
|
+
"upgrade": "Upgrade"
|
|
30
|
+
},
|
|
31
|
+
fr: {
|
|
32
|
+
"modal.fields.info": "Merci d'avoir acheté {label} ! Veuillez saisir votre adresse e-mail et votre numéro de commande pour activer votre licence.",
|
|
33
|
+
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Trouvez votre numéro de commande</a> sur Lemon Squeezy ou <a href=\"mailto:hello@kirby.tools\">contactez-nous</a> si vous ne le trouvez pas.",
|
|
34
|
+
"errors.validation.missingFields": "Adresse e-mail et numéro de commande requis",
|
|
35
|
+
"errors.validation.invalidCredentials": "Adresse e-mail ou numéro de commande incorrect",
|
|
36
|
+
"errors.license.invalidPlugin": "Clé de licence invalide pour ce plugin",
|
|
37
|
+
"errors.license.versionMismatch": "Clé de licence invalide pour cette version du plugin",
|
|
38
|
+
"errors.license.needsUpgrade": "Clé de licence invalide pour cette version du plugin. Mettez à niveau maintenant sur https://hub.kirby.tools.",
|
|
39
|
+
"errors.activation.alreadyUsed": "Clé de licence déjà activée",
|
|
40
|
+
"activate": "Activer",
|
|
41
|
+
"activated": "Plugin activé",
|
|
42
|
+
"buy": "Acheter une licence",
|
|
43
|
+
"upgrade": "Upgrade"
|
|
44
|
+
},
|
|
45
|
+
nl: {
|
|
46
|
+
"modal.fields.info": "Bedankt voor het kopen van {label}! Voer je e-mailadres en bestelnummer in om je licentie te activeren.",
|
|
47
|
+
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Vind je bestelnummer</a> op Lemon Squeezy of <a href=\"mailto:hello@kirby.tools\">neem contact met ons op</a> als je het niet kunt vinden.",
|
|
48
|
+
"errors.validation.missingFields": "E-mailadres en bestelnummer zijn verplicht",
|
|
49
|
+
"errors.validation.invalidCredentials": "E-mailadres of bestelnummer is onjuist",
|
|
50
|
+
"errors.license.invalidPlugin": "Licentiesleutel ongeldig voor dit plug-in",
|
|
51
|
+
"errors.license.versionMismatch": "Licentiesleutel ongeldig voor deze plug-inversie",
|
|
52
|
+
"errors.license.needsUpgrade": "Licentiesleutel ongeldig voor deze plug-inversie. Upgrade nu op https://hub.kirby.tools.",
|
|
53
|
+
"errors.activation.alreadyUsed": "Licentiesleutel al geactiveerd",
|
|
54
|
+
"activate": "Activeren",
|
|
55
|
+
"activated": "Plugin geactiveerd",
|
|
56
|
+
"buy": "Koop een licentie",
|
|
57
|
+
"upgrade": "Upgrade"
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { TRANSLATIONS };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type MaybeRef<T = any> = T | Ref<T>;
|
|
5
|
-
type LicenseKey = string;
|
|
6
|
-
type LicenseStatus = 'active' | 'inactive' | 'invalid' | 'incompatible' | 'upgradeable';
|
|
7
|
-
interface License {
|
|
8
|
-
key: LicenseKey;
|
|
9
|
-
version: number;
|
|
10
|
-
compatibility: string;
|
|
11
|
-
}
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/license.d.ts
|
|
14
|
-
interface LicenseOptions {
|
|
15
|
-
label: string;
|
|
16
|
-
apiNamespace: string;
|
|
17
|
-
}
|
|
18
|
-
interface LicenseModalResult {
|
|
19
|
-
isLicenseActive: boolean;
|
|
20
|
-
}
|
|
21
|
-
declare function useLicense(licenseOptions: LicenseOptions): {
|
|
22
|
-
isLocalhost: boolean;
|
|
23
|
-
assertActivationIntegrity: ({
|
|
24
|
-
component,
|
|
25
|
-
licenseStatus
|
|
26
|
-
}: {
|
|
27
|
-
component: MaybeRef<ComponentPublicInstance | null | undefined>;
|
|
28
|
-
licenseStatus: MaybeRef<LicenseStatus>;
|
|
29
|
-
}) => void;
|
|
30
|
-
openLicenseModal: () => Promise<{
|
|
31
|
-
isLicenseActive: boolean;
|
|
32
|
-
}>;
|
|
33
|
-
};
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region src/utils.d.ts
|
|
36
|
-
declare function isLocal(): boolean;
|
|
37
|
-
//#endregion
|
|
1
|
+
import { License, LicenseKey, LicenseStatus } from "./types.js";
|
|
2
|
+
import { LicenseModalResult, LicenseOptions, useLicense } from "./license.js";
|
|
3
|
+
import { isLocal } from "./utils.js";
|
|
38
4
|
export { type License, type LicenseKey, LicenseModalResult, LicenseOptions, type LicenseStatus, isLocal, useLicense };
|
package/dist/index.js
CHANGED
|
@@ -1,171 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isLocal } from "./utils.js";
|
|
2
|
+
import { useLicense } from "./license.js";
|
|
2
3
|
|
|
3
|
-
//#region src/constants.ts
|
|
4
|
-
const TRANSLATIONS = {
|
|
5
|
-
en: {
|
|
6
|
-
"modal.fields.info": "Thanks for purchasing {label}! Please enter your email address and order ID to activate your license.",
|
|
7
|
-
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Find your order number</a> on Lemon Squeezy or <a href=\"mailto:hello@kirby.tools\">contact us</a> if you cannot find it.",
|
|
8
|
-
"errors.validation.missingFields": "Email address and order ID are required",
|
|
9
|
-
"errors.validation.invalidCredentials": "Email address or order ID is incorrect",
|
|
10
|
-
"errors.license.invalidPlugin": "License key invalid for this plugin",
|
|
11
|
-
"errors.license.versionMismatch": "License key invalid for this plugin version",
|
|
12
|
-
"errors.license.needsUpgrade": "License key invalid for this plugin version. Upgrade now on https://hub.kirby.tools.",
|
|
13
|
-
"errors.activation.alreadyUsed": "License key already activated",
|
|
14
|
-
"activate": "Activate",
|
|
15
|
-
"activated": "Plugin activated",
|
|
16
|
-
"buy": "Buy a license",
|
|
17
|
-
"upgrade": "Upgrade"
|
|
18
|
-
},
|
|
19
|
-
de: {
|
|
20
|
-
"modal.fields.info": "Dankeschön für den Kauf von {label}! Bitte gib deine E-Mail-Adresse und Bestellnummer ein, um deine Lizenz zu aktivieren.",
|
|
21
|
-
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Finde deine Bestellnummer</a> bei Lemon Squeezy oder <a href=\"mailto:hello@kirby.tools\">kontaktiere uns</a>, wenn du sie nicht finden kannst.",
|
|
22
|
-
"errors.validation.missingFields": "E-Mail-Adresse und Bestellnummer sind notwendig",
|
|
23
|
-
"errors.validation.invalidCredentials": "E-Mail-Adresse oder Bestellnummer ist falsch",
|
|
24
|
-
"errors.license.invalidPlugin": "Lizenzschlüssel ungültig für dieses Plugin",
|
|
25
|
-
"errors.license.versionMismatch": "Lizenzschlüssel ungültig für diese Plugin-Version",
|
|
26
|
-
"errors.license.needsUpgrade": "Lizenzschlüssel ungültig für diese Plugin-Version. Aktualisiere jetzt auf https://hub.kirby.tools.",
|
|
27
|
-
"errors.activation.alreadyUsed": "Lizenzschlüssel bereits aktiviert",
|
|
28
|
-
"activate": "Aktivieren",
|
|
29
|
-
"activated": "Plugin aktiviert",
|
|
30
|
-
"buy": "Lizenz kaufen",
|
|
31
|
-
"upgrade": "Upgrade"
|
|
32
|
-
},
|
|
33
|
-
fr: {
|
|
34
|
-
"modal.fields.info": "Merci d'avoir acheté {label} ! Veuillez saisir votre adresse e-mail et votre numéro de commande pour activer votre licence.",
|
|
35
|
-
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Trouvez votre numéro de commande</a> sur Lemon Squeezy ou <a href=\"mailto:hello@kirby.tools\">contactez-nous</a> si vous ne le trouvez pas.",
|
|
36
|
-
"errors.validation.missingFields": "Adresse e-mail et numéro de commande requis",
|
|
37
|
-
"errors.validation.invalidCredentials": "Adresse e-mail ou numéro de commande incorrect",
|
|
38
|
-
"errors.license.invalidPlugin": "Clé de licence invalide pour ce plugin",
|
|
39
|
-
"errors.license.versionMismatch": "Clé de licence invalide pour cette version du plugin",
|
|
40
|
-
"errors.license.needsUpgrade": "Clé de licence invalide pour cette version du plugin. Mettez à niveau maintenant sur https://hub.kirby.tools.",
|
|
41
|
-
"errors.activation.alreadyUsed": "Clé de licence déjà activée",
|
|
42
|
-
"activate": "Activer",
|
|
43
|
-
"activated": "Plugin activé",
|
|
44
|
-
"buy": "Acheter une licence",
|
|
45
|
-
"upgrade": "Upgrade"
|
|
46
|
-
},
|
|
47
|
-
nl: {
|
|
48
|
-
"modal.fields.info": "Bedankt voor het kopen van {label}! Voer je e-mailadres en bestelnummer in om je licentie te activeren.",
|
|
49
|
-
"modal.fields.orderId.help": "<a href=\"https://app.lemonsqueezy.com/my-orders\" target=\"_blank\">Vind je bestelnummer</a> op Lemon Squeezy of <a href=\"mailto:hello@kirby.tools\">neem contact met ons op</a> als je het niet kunt vinden.",
|
|
50
|
-
"errors.validation.missingFields": "E-mailadres en bestelnummer zijn verplicht",
|
|
51
|
-
"errors.validation.invalidCredentials": "E-mailadres of bestelnummer is onjuist",
|
|
52
|
-
"errors.license.invalidPlugin": "Licentiesleutel ongeldig voor dit plug-in",
|
|
53
|
-
"errors.license.versionMismatch": "Licentiesleutel ongeldig voor deze plug-inversie",
|
|
54
|
-
"errors.license.needsUpgrade": "Licentiesleutel ongeldig voor deze plug-inversie. Upgrade nu op https://hub.kirby.tools.",
|
|
55
|
-
"errors.activation.alreadyUsed": "Licentiesleutel al geactiveerd",
|
|
56
|
-
"activate": "Activeren",
|
|
57
|
-
"activated": "Plugin geactiveerd",
|
|
58
|
-
"buy": "Koop een licentie",
|
|
59
|
-
"upgrade": "Upgrade"
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/utils.ts
|
|
65
|
-
const LOCALHOST_HOSTNAMES = [
|
|
66
|
-
"localhost",
|
|
67
|
-
"127.0.0.1",
|
|
68
|
-
"[::1]"
|
|
69
|
-
];
|
|
70
|
-
const LOCAL_DOMAINS = [
|
|
71
|
-
"local",
|
|
72
|
-
"test",
|
|
73
|
-
"ddev.site"
|
|
74
|
-
];
|
|
75
|
-
function t(key = "", data) {
|
|
76
|
-
const languageCode = window.panel.translation.code;
|
|
77
|
-
const translation = TRANSLATIONS?.[languageCode]?.[key] ?? key;
|
|
78
|
-
return data ? template(translation, data) : translation;
|
|
79
|
-
}
|
|
80
|
-
function template(input, values, fallback) {
|
|
81
|
-
return input.replace(/\{(\w+)\}/g, (_, key) => values[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key));
|
|
82
|
-
}
|
|
83
|
-
function isLocal() {
|
|
84
|
-
const { hostname } = window.location;
|
|
85
|
-
const isLocalhost = LOCALHOST_HOSTNAMES.includes(hostname);
|
|
86
|
-
const isLocalDomain = LOCAL_DOMAINS.some((domain) => hostname.endsWith(`.${domain}`));
|
|
87
|
-
return isLocalhost || isLocalDomain;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/license.ts
|
|
92
|
-
const ERROR_MESSAGE_TRANSLATIONS = {
|
|
93
|
-
"Unauthorized": "errors.validation.invalidCredentials",
|
|
94
|
-
"License key not valid for this plugin": "errors.license.invalidPlugin",
|
|
95
|
-
"License key not valid for this plugin version": "errors.license.versionMismatch",
|
|
96
|
-
"License key not valid for this plugin version, please upgrade your license": "errors.license.needsUpgrade",
|
|
97
|
-
"License key already activated": "errors.activation.alreadyUsed"
|
|
98
|
-
};
|
|
99
|
-
const INTEGRITY_ERROR = "The activation buttons appear to be hidden. Please purchase a license.";
|
|
100
|
-
function useLicense(licenseOptions) {
|
|
101
|
-
const panel = usePanel();
|
|
102
|
-
const { openFieldsDialog } = useDialog();
|
|
103
|
-
const isLocalhost = isLocal();
|
|
104
|
-
const openLicenseModal = async () => {
|
|
105
|
-
const { label } = licenseOptions;
|
|
106
|
-
return { isLicenseActive: (await openFieldsDialog({
|
|
107
|
-
submitButton: {
|
|
108
|
-
icon: "check",
|
|
109
|
-
theme: "love",
|
|
110
|
-
text: t("activate", { label })
|
|
111
|
-
},
|
|
112
|
-
fields: {
|
|
113
|
-
info: {
|
|
114
|
-
type: "info",
|
|
115
|
-
text: t("modal.fields.info", { label })
|
|
116
|
-
},
|
|
117
|
-
email: {
|
|
118
|
-
label: panel.t("email"),
|
|
119
|
-
type: "email"
|
|
120
|
-
},
|
|
121
|
-
orderId: {
|
|
122
|
-
label: "Order ID",
|
|
123
|
-
type: "text",
|
|
124
|
-
help: t("modal.fields.orderId.help", { label })
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
onSubmit: async (value) => {
|
|
128
|
-
const isLicenseActive = await activateLicense(value, licenseOptions);
|
|
129
|
-
if (!isLicenseActive) return false;
|
|
130
|
-
panel.notification.success(t("activated"));
|
|
131
|
-
return { isLicenseActive };
|
|
132
|
-
}
|
|
133
|
-
}))?.isLicenseActive ?? false };
|
|
134
|
-
};
|
|
135
|
-
const assertActivationIntegrity = ({ component, licenseStatus }) => {
|
|
136
|
-
if (unref(licenseStatus) === "active") return;
|
|
137
|
-
const element = unref(component)?.$el;
|
|
138
|
-
if (!element) throw new Error(INTEGRITY_ERROR);
|
|
139
|
-
const style = window.getComputedStyle(element);
|
|
140
|
-
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0" || style.clipPath === "inset(100%)" || style.transform.includes("scale(0")) throw new Error(INTEGRITY_ERROR);
|
|
141
|
-
const rect = element.getBoundingClientRect();
|
|
142
|
-
if (rect.width === 0 || rect.height === 0 || rect.right < 0 || rect.bottom < 0 || rect.left > window.innerWidth || rect.top > window.innerHeight) throw new Error(INTEGRITY_ERROR);
|
|
143
|
-
};
|
|
144
|
-
return {
|
|
145
|
-
isLocalhost,
|
|
146
|
-
assertActivationIntegrity,
|
|
147
|
-
openLicenseModal
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
async function activateLicense(event, licenseOptions) {
|
|
151
|
-
const panel = usePanel();
|
|
152
|
-
const { email, orderId } = event;
|
|
153
|
-
if (!email || !orderId) {
|
|
154
|
-
panel.notification.error(t("errors.validation.missingFields"));
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
try {
|
|
158
|
-
if ((await panel.api.post(`${licenseOptions.apiNamespace}/activate`, {
|
|
159
|
-
email,
|
|
160
|
-
orderId: Number(orderId)
|
|
161
|
-
}))?.status !== "ok") throw new Error("Failed to activate license key");
|
|
162
|
-
return true;
|
|
163
|
-
} catch (error) {
|
|
164
|
-
const message = error.message;
|
|
165
|
-
panel.notification.error(ERROR_MESSAGE_TRANSLATIONS[message] ? t(ERROR_MESSAGE_TRANSLATIONS[message]) : message);
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
//#endregion
|
|
171
4
|
export { isLocal, useLicense };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { LicenseStatus, MaybeRef } from "./types.js";
|
|
2
|
+
import { ComponentPublicInstance } from "vue";
|
|
3
|
+
|
|
4
|
+
//#region src/license.d.ts
|
|
5
|
+
interface LicenseOptions {
|
|
6
|
+
label: string;
|
|
7
|
+
apiNamespace: string;
|
|
8
|
+
}
|
|
9
|
+
interface LicenseModalResult {
|
|
10
|
+
isLicenseActive: boolean;
|
|
11
|
+
}
|
|
12
|
+
declare function useLicense(licenseOptions: LicenseOptions): {
|
|
13
|
+
isLocalhost: boolean;
|
|
14
|
+
assertActivationIntegrity: ({
|
|
15
|
+
component,
|
|
16
|
+
licenseStatus
|
|
17
|
+
}: {
|
|
18
|
+
component: MaybeRef<ComponentPublicInstance | null | undefined>;
|
|
19
|
+
licenseStatus: MaybeRef<LicenseStatus>;
|
|
20
|
+
}) => void;
|
|
21
|
+
openLicenseModal: () => Promise<{
|
|
22
|
+
isLicenseActive: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { LicenseModalResult, LicenseOptions, useLicense };
|
package/dist/license.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { isLocal, t } from "./utils.js";
|
|
2
|
+
import { unref, useDialog, usePanel } from "kirbyuse";
|
|
3
|
+
|
|
4
|
+
//#region src/license.ts
|
|
5
|
+
const ERROR_MESSAGE_TRANSLATIONS = {
|
|
6
|
+
"Unauthorized": "errors.validation.invalidCredentials",
|
|
7
|
+
"License key not valid for this plugin": "errors.license.invalidPlugin",
|
|
8
|
+
"License key not valid for this plugin version": "errors.license.versionMismatch",
|
|
9
|
+
"License key not valid for this plugin version, please upgrade your license": "errors.license.needsUpgrade",
|
|
10
|
+
"License key already activated": "errors.activation.alreadyUsed"
|
|
11
|
+
};
|
|
12
|
+
const INTEGRITY_ERROR = "The activation buttons appear to be hidden. Please purchase a license.";
|
|
13
|
+
function useLicense(licenseOptions) {
|
|
14
|
+
const panel = usePanel();
|
|
15
|
+
const { openFieldsDialog } = useDialog();
|
|
16
|
+
const isLocalhost = isLocal();
|
|
17
|
+
const openLicenseModal = async () => {
|
|
18
|
+
const { label } = licenseOptions;
|
|
19
|
+
return { isLicenseActive: (await openFieldsDialog({
|
|
20
|
+
submitButton: {
|
|
21
|
+
icon: "check",
|
|
22
|
+
theme: "love",
|
|
23
|
+
text: t("activate", { label })
|
|
24
|
+
},
|
|
25
|
+
fields: {
|
|
26
|
+
info: {
|
|
27
|
+
type: "info",
|
|
28
|
+
text: t("modal.fields.info", { label })
|
|
29
|
+
},
|
|
30
|
+
email: {
|
|
31
|
+
label: panel.t("email"),
|
|
32
|
+
type: "email"
|
|
33
|
+
},
|
|
34
|
+
orderId: {
|
|
35
|
+
label: "Order ID",
|
|
36
|
+
type: "text",
|
|
37
|
+
help: t("modal.fields.orderId.help", { label })
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
onSubmit: async (value) => {
|
|
41
|
+
const isLicenseActive = await activateLicense(value, licenseOptions);
|
|
42
|
+
if (!isLicenseActive) return false;
|
|
43
|
+
panel.notification.success(t("activated"));
|
|
44
|
+
return { isLicenseActive };
|
|
45
|
+
}
|
|
46
|
+
}))?.isLicenseActive ?? false };
|
|
47
|
+
};
|
|
48
|
+
const assertActivationIntegrity = ({ component, licenseStatus }) => {
|
|
49
|
+
if (unref(licenseStatus) === "active") return;
|
|
50
|
+
const element = unref(component)?.$el;
|
|
51
|
+
if (!element) throw new Error(INTEGRITY_ERROR);
|
|
52
|
+
const style = window.getComputedStyle(element);
|
|
53
|
+
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0" || style.clipPath === "inset(100%)" || style.transform.includes("scale(0")) throw new Error(INTEGRITY_ERROR);
|
|
54
|
+
const rect = element.getBoundingClientRect();
|
|
55
|
+
if (rect.width === 0 || rect.height === 0 || rect.right < 0 || rect.bottom < 0 || rect.left > window.innerWidth || rect.top > window.innerHeight) throw new Error(INTEGRITY_ERROR);
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
isLocalhost,
|
|
59
|
+
assertActivationIntegrity,
|
|
60
|
+
openLicenseModal
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async function activateLicense(event, licenseOptions) {
|
|
64
|
+
const panel = usePanel();
|
|
65
|
+
const { email, orderId } = event;
|
|
66
|
+
if (!email || !orderId) {
|
|
67
|
+
panel.notification.error(t("errors.validation.missingFields"));
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
if ((await panel.api.post(`${licenseOptions.apiNamespace}/activate`, {
|
|
72
|
+
email,
|
|
73
|
+
orderId: Number(orderId)
|
|
74
|
+
}))?.status !== "ok") throw new Error("Failed to activate license key");
|
|
75
|
+
return true;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
const message = error.message;
|
|
78
|
+
panel.notification.error(ERROR_MESSAGE_TRANSLATIONS[message] ? t(ERROR_MESSAGE_TRANSLATIONS[message]) : message);
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
export { useLicense };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Ref } from "vue";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type MaybeRef<T = any> = T | Ref<T>;
|
|
5
|
+
type LicenseKey = string;
|
|
6
|
+
type LicenseStatus = 'active' | 'inactive' | 'invalid' | 'incompatible' | 'upgradeable';
|
|
7
|
+
interface License {
|
|
8
|
+
key: LicenseKey;
|
|
9
|
+
version: number;
|
|
10
|
+
compatibility: string;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { License, LicenseKey, LicenseStatus, MaybeRef };
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { TRANSLATIONS } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils.ts
|
|
4
|
+
const LOCALHOST_HOSTNAMES = [
|
|
5
|
+
"localhost",
|
|
6
|
+
"127.0.0.1",
|
|
7
|
+
"[::1]"
|
|
8
|
+
];
|
|
9
|
+
const LOCAL_DOMAINS = [
|
|
10
|
+
"local",
|
|
11
|
+
"test",
|
|
12
|
+
"ddev.site"
|
|
13
|
+
];
|
|
14
|
+
function template(input, values, fallback) {
|
|
15
|
+
return input.replace(/\{(\w+)\}/g, (_, key) => values[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key));
|
|
16
|
+
}
|
|
17
|
+
function t(key = "", data) {
|
|
18
|
+
const languageCode = window.panel.translation.code;
|
|
19
|
+
const translation = TRANSLATIONS?.[languageCode]?.[key] ?? key;
|
|
20
|
+
return data ? template(translation, data) : translation;
|
|
21
|
+
}
|
|
22
|
+
function isLocal() {
|
|
23
|
+
const { hostname } = window.location;
|
|
24
|
+
const isLocalhost = LOCALHOST_HOSTNAMES.includes(hostname);
|
|
25
|
+
const isLocalDomain = LOCAL_DOMAINS.some((domain) => hostname.endsWith(`.${domain}`));
|
|
26
|
+
return isLocalhost || isLocalDomain;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { isLocal, t };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kirby-tools/licensing",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.1",
|
|
5
5
|
"packageManager": "pnpm@10.28.0",
|
|
6
6
|
"description": "License system for Kirby Tools plugins within the Kirby Panel",
|
|
7
7
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|