@kirby-tools/licensing 0.1.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/LICENSE +21 -0
- package/README.md +7 -0
- package/package.json +37 -0
- package/src/constants.mjs +20 -0
- package/src/index.mjs +1 -0
- package/src/license.mjs +92 -0
- package/src/utils.mjs +19 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-PRESENT Johann Schopplich
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kirby-tools/licensing",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"packageManager": "pnpm@9.3.0",
|
|
6
|
+
"description": "Shared tooling for licensing in the Panel",
|
|
7
|
+
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://github.com/kirby-tools/licensing#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/kirby-tools/licensing.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/kirby-tools/licensing/issues"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./src/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"lint:fix": "eslint . --fix",
|
|
27
|
+
"release": "bumpp"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"kirbyuse": "^0.4.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@antfu/eslint-config": "^2.21.1",
|
|
34
|
+
"bumpp": "^9.4.1",
|
|
35
|
+
"eslint": "^9.5.0"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useLicense } from './license.mjs'
|
package/src/license.mjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|