@kirby-tools/licensing 0.1.1 → 0.1.2
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/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.mjs +113 -0
- package/package.json +11 -4
- package/src/constants.mjs +0 -20
- package/src/index.mjs +0 -1
- package/src/license.mjs +0 -92
- package/src/utils.mjs +0 -19
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { usePanel, useApi } from 'kirbyuse';
|
|
2
|
+
|
|
3
|
+
const TRANSLATIONS = {
|
|
4
|
+
en: {
|
|
5
|
+
"modal.info": "Thanks for purchasing {plugin}! Please enter your email address and order ID to activate your license.",
|
|
6
|
+
"modal.help.orderId": '<a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Get your order number</a> from Lemon Squeezy or <a href="mailto:hello@kirby.tools">contact us</a> if you cannot find it.',
|
|
7
|
+
"activate": "Activate",
|
|
8
|
+
"activated": "Plugin activated"
|
|
9
|
+
},
|
|
10
|
+
de: {
|
|
11
|
+
"modal.info": "Dankesch\xF6n f\xFCr den Kauf vom {plugin}! Bitte gib deine E-Mail-Adresse und Bestellnummer ein, um deine Lizenz zu aktivieren.",
|
|
12
|
+
"modal.help.orderId": 'Rufe die <a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Bestellnummer von Lemon Squeezy ab</a> oder <a href="mailto:hello@kirby.tools">kontaktiere uns</a>, wenn du sie nicht finden kannst.',
|
|
13
|
+
"activate": "Aktivieren",
|
|
14
|
+
"activated": "Plugin aktiviert"
|
|
15
|
+
},
|
|
16
|
+
fr: {
|
|
17
|
+
"modal.info": "Merci d'avoir achet\xE9 {plugin} ! Veuillez saisir votre adresse e-mail et votre num\xE9ro de commande pour activer votre licence.",
|
|
18
|
+
"modal.help.orderId": 'Obtenez votre num\xE9ro de commande sur <a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Lemon Squeezy</a> ou <a href="mailto:hello@kirby.tools">contactez-nous</a> si vous ne le trouvez pas.',
|
|
19
|
+
"activate": "Activer",
|
|
20
|
+
"activated": "Plugin activ\xE9"
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function t(key, data, fallback) {
|
|
25
|
+
const panel = usePanel();
|
|
26
|
+
const languageCode = panel.translation.code;
|
|
27
|
+
const translation = TRANSLATIONS?.[languageCode]?.[key] ?? fallback;
|
|
28
|
+
if (!translation)
|
|
29
|
+
return;
|
|
30
|
+
return data ? template(translation, data) : translation;
|
|
31
|
+
}
|
|
32
|
+
function template(input, values, fallback) {
|
|
33
|
+
return input.replace(
|
|
34
|
+
/\{(\w+)\}/g,
|
|
35
|
+
(_, key) => values[key] || (key)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function useLicense({
|
|
40
|
+
label,
|
|
41
|
+
apiNamespace
|
|
42
|
+
}) {
|
|
43
|
+
const panel = usePanel();
|
|
44
|
+
const api = useApi();
|
|
45
|
+
const isLocalhost = _isLocalhost();
|
|
46
|
+
const register = async (email, orderId) => {
|
|
47
|
+
if (!email || !orderId) {
|
|
48
|
+
throw new Error("Email and order ID are required");
|
|
49
|
+
}
|
|
50
|
+
const response = await api.post(`${apiNamespace}/register`, { email, orderId });
|
|
51
|
+
if (!response?.ok) {
|
|
52
|
+
throw new Error("Registration failed");
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
};
|
|
56
|
+
const openLicenseModal = () => {
|
|
57
|
+
panel.dialog.open({
|
|
58
|
+
component: "k-form-dialog",
|
|
59
|
+
props: {
|
|
60
|
+
submitButton: {
|
|
61
|
+
icon: "check",
|
|
62
|
+
theme: "love",
|
|
63
|
+
text: t("activate", { plugin: label })
|
|
64
|
+
},
|
|
65
|
+
fields: {
|
|
66
|
+
info: {
|
|
67
|
+
type: "info",
|
|
68
|
+
text: t("modal.info", { plugin: label })
|
|
69
|
+
},
|
|
70
|
+
email: {
|
|
71
|
+
label: panel.t("email"),
|
|
72
|
+
type: "email"
|
|
73
|
+
},
|
|
74
|
+
orderId: {
|
|
75
|
+
label: "Order ID",
|
|
76
|
+
type: "text",
|
|
77
|
+
help: t("modal.help.orderId", { plugin: label })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
on: {
|
|
82
|
+
submit: async (event) => {
|
|
83
|
+
const { email, orderId } = event;
|
|
84
|
+
if (!email || !orderId) {
|
|
85
|
+
panel.notification.error("Email and order ID are required");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await register(email, Number(orderId));
|
|
90
|
+
} catch (error) {
|
|
91
|
+
panel.notification.error(error.message);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
panel.dialog.close();
|
|
95
|
+
await panel.view.reload();
|
|
96
|
+
panel.notification.success(t("activated"), { plugin: label });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
isLocalhost,
|
|
103
|
+
openLicenseModal
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function _isLocalhost() {
|
|
107
|
+
const { hostname } = window.location;
|
|
108
|
+
const isLocalhost = ["localhost", "127.0.0.1", "::1"].includes(hostname);
|
|
109
|
+
const isTestDomain = hostname.endsWith(".test");
|
|
110
|
+
return isLocalhost || isTestDomain;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { useLicense };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kirby-tools/licensing",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"packageManager": "pnpm@9.3.0",
|
|
6
6
|
"description": "Shared tooling for licensing in the Panel",
|
|
7
7
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
@@ -16,12 +16,17 @@
|
|
|
16
16
|
},
|
|
17
17
|
"sideEffects": false,
|
|
18
18
|
"exports": {
|
|
19
|
-
".":
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"default": "./dist/index.mjs"
|
|
22
|
+
}
|
|
20
23
|
},
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
21
25
|
"files": [
|
|
22
|
-
"
|
|
26
|
+
"dist"
|
|
23
27
|
],
|
|
24
28
|
"scripts": {
|
|
29
|
+
"build": "unbuild",
|
|
25
30
|
"lint": "eslint .",
|
|
26
31
|
"lint:fix": "eslint . --fix",
|
|
27
32
|
"release": "bumpp"
|
|
@@ -32,6 +37,8 @@
|
|
|
32
37
|
"devDependencies": {
|
|
33
38
|
"@antfu/eslint-config": "^2.21.1",
|
|
34
39
|
"bumpp": "^9.4.1",
|
|
35
|
-
"eslint": "^9.5.0"
|
|
40
|
+
"eslint": "^9.5.0",
|
|
41
|
+
"typescript": "^5.4.5",
|
|
42
|
+
"unbuild": "3.0.0-rc.2"
|
|
36
43
|
}
|
|
37
44
|
}
|
package/src/constants.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export const TRANSLATIONS = {
|
|
2
|
-
en: {
|
|
3
|
-
'modal.info': 'Thanks for purchasing {plugin}! Please enter your email address and order ID to activate your license.',
|
|
4
|
-
'modal.help.orderId': '<a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Get your order number</a> from Lemon Squeezy or <a href="mailto:hello@kirby.tools">contact us</a> if you cannot find it.',
|
|
5
|
-
'activate': 'Activate',
|
|
6
|
-
'activated': 'Plugin activated',
|
|
7
|
-
},
|
|
8
|
-
de: {
|
|
9
|
-
'modal.info': 'Dankeschön für den Kauf vom {plugin}! Bitte gib deine E-Mail-Adresse und Bestellnummer ein, um deine Lizenz zu aktivieren.',
|
|
10
|
-
'modal.help.orderId': 'Rufe die <a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Bestellnummer von Lemon Squeezy ab</a> oder <a href="mailto:hello@kirby.tools">kontaktiere uns</a>, wenn du sie nicht finden kannst.',
|
|
11
|
-
'activate': 'Aktivieren',
|
|
12
|
-
'activated': 'Plugin aktiviert',
|
|
13
|
-
},
|
|
14
|
-
fr: {
|
|
15
|
-
'modal.info': 'Merci d\'avoir acheté {plugin} ! Veuillez saisir votre adresse e-mail et votre numéro de commande pour activer votre licence.',
|
|
16
|
-
'modal.help.orderId': 'Obtenez votre numéro de commande sur <a href="https://app.lemonsqueezy.com/my-orders" target="_blank">Lemon Squeezy</a> ou <a href="mailto:hello@kirby.tools">contactez-nous</a> si vous ne le trouvez pas.',
|
|
17
|
-
'activate': 'Activer',
|
|
18
|
-
'activated': 'Plugin activé',
|
|
19
|
-
},
|
|
20
|
-
}
|
package/src/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { useLicense } from './license.mjs'
|
package/src/license.mjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { useApi, usePanel } from 'kirbyuse'
|
|
2
|
-
import { t } from './utils.mjs'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @param {object} options
|
|
6
|
-
* @param {string} options.label
|
|
7
|
-
* @param {string} options.apiNamespace
|
|
8
|
-
* @returns {object}
|
|
9
|
-
*/
|
|
10
|
-
export function useLicense({
|
|
11
|
-
label,
|
|
12
|
-
apiNamespace,
|
|
13
|
-
}) {
|
|
14
|
-
const panel = usePanel()
|
|
15
|
-
const api = useApi()
|
|
16
|
-
const isLocalhost = _isLocalhost()
|
|
17
|
-
|
|
18
|
-
const register = async (email, orderId) => {
|
|
19
|
-
if (!email || !orderId) {
|
|
20
|
-
throw new Error('Email and order ID are required')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const response = await api.post(`${apiNamespace}/register`, { email, orderId })
|
|
24
|
-
if (!response?.ok) {
|
|
25
|
-
throw new Error('Registration failed')
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return true
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const openLicenseModal = () => {
|
|
32
|
-
panel.dialog.open({
|
|
33
|
-
component: 'k-form-dialog',
|
|
34
|
-
props: {
|
|
35
|
-
submitButton: {
|
|
36
|
-
icon: 'check',
|
|
37
|
-
theme: 'love',
|
|
38
|
-
text: t('activate', { plugin: label }),
|
|
39
|
-
},
|
|
40
|
-
fields: {
|
|
41
|
-
info: {
|
|
42
|
-
type: 'info',
|
|
43
|
-
text: t('modal.info', { plugin: label }),
|
|
44
|
-
},
|
|
45
|
-
email: {
|
|
46
|
-
label: panel.t('email'),
|
|
47
|
-
type: 'email',
|
|
48
|
-
},
|
|
49
|
-
orderId: {
|
|
50
|
-
label: 'Order ID',
|
|
51
|
-
type: 'text',
|
|
52
|
-
help: t('modal.help.orderId', { plugin: label }),
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
on: {
|
|
57
|
-
submit: async (event) => {
|
|
58
|
-
const { email, orderId } = event
|
|
59
|
-
if (!email || !orderId) {
|
|
60
|
-
panel.notification.error('Email and order ID are required')
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
await register(email, Number(orderId))
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
panel.notification.error(error.message)
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
panel.dialog.close()
|
|
73
|
-
await panel.view.reload()
|
|
74
|
-
panel.notification.success(t('activated'), { plugin: label })
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
isLocalhost,
|
|
82
|
-
openLicenseModal,
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function _isLocalhost() {
|
|
87
|
-
const { hostname } = window.location
|
|
88
|
-
const isLocalhost = ['localhost', '127.0.0.1', '::1'].includes(hostname)
|
|
89
|
-
const isTestDomain = hostname.endsWith('.test')
|
|
90
|
-
|
|
91
|
-
return isLocalhost || isTestDomain
|
|
92
|
-
}
|
package/src/utils.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { usePanel } from 'kirbyuse'
|
|
2
|
-
import { TRANSLATIONS } from './constants.mjs'
|
|
3
|
-
|
|
4
|
-
export function t(key, data, fallback) {
|
|
5
|
-
const panel = usePanel()
|
|
6
|
-
const languageCode = panel.translation.code
|
|
7
|
-
const translation = TRANSLATIONS?.[languageCode]?.[key] ?? fallback
|
|
8
|
-
if (!translation)
|
|
9
|
-
return
|
|
10
|
-
|
|
11
|
-
return data ? template(translation, data) : translation
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function template(input, values, fallback) {
|
|
15
|
-
return input.replace(
|
|
16
|
-
/\{(\w+)\}/g,
|
|
17
|
-
(_, key) => values[key] || ((typeof fallback === 'function' ? fallback(key) : fallback) ?? key),
|
|
18
|
-
)
|
|
19
|
-
}
|