@tenthbilling/paywall 0.0.0-stage → 0.1.0

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.
Files changed (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -2
  3. package/dist/components/Badge.d.ts +8 -0
  4. package/dist/components/Badge.js +14 -0
  5. package/dist/components/Button.d.ts +17 -0
  6. package/dist/components/Button.js +60 -0
  7. package/dist/components/Card.d.ts +13 -0
  8. package/dist/components/Card.js +29 -0
  9. package/dist/components/CloseButton.d.ts +10 -0
  10. package/dist/components/CloseButton.js +21 -0
  11. package/dist/components/ComparisonTable.d.ts +20 -0
  12. package/dist/components/ComparisonTable.js +43 -0
  13. package/dist/components/CreditPackOption.d.ts +16 -0
  14. package/dist/components/CreditPackOption.js +38 -0
  15. package/dist/components/FeatureList.d.ts +14 -0
  16. package/dist/components/FeatureList.js +15 -0
  17. package/dist/components/PaywallFooter.d.ts +17 -0
  18. package/dist/components/PaywallFooter.js +46 -0
  19. package/dist/components/PaywallOption.d.ts +26 -0
  20. package/dist/components/PaywallOption.js +41 -0
  21. package/dist/components/Radio.d.ts +8 -0
  22. package/dist/components/Radio.js +21 -0
  23. package/dist/components/RadioGroup.d.ts +23 -0
  24. package/dist/components/RadioGroup.js +42 -0
  25. package/dist/components/Text.d.ts +10 -0
  26. package/dist/components/Text.js +8 -0
  27. package/dist/components/TextField.d.ts +19 -0
  28. package/dist/components/TextField.js +38 -0
  29. package/dist/components/dimensions.d.ts +29 -0
  30. package/dist/components/dimensions.js +21 -0
  31. package/dist/components/icons.d.ts +7 -0
  32. package/dist/components/icons.js +15 -0
  33. package/dist/controller.d.ts +97 -0
  34. package/dist/controller.js +1 -0
  35. package/dist/format.d.ts +5 -0
  36. package/dist/format.js +3 -0
  37. package/dist/index.d.ts +28 -0
  38. package/dist/index.js +26 -0
  39. package/dist/paywall/PaywallScaffold.d.ts +52 -0
  40. package/dist/paywall/PaywallScaffold.js +42 -0
  41. package/dist/paywall/PaywallView.d.ts +38 -0
  42. package/dist/paywall/PaywallView.js +219 -0
  43. package/dist/paywall/copy.d.ts +36 -0
  44. package/dist/paywall/copy.js +90 -0
  45. package/dist/paywall/price.d.ts +38 -0
  46. package/dist/paywall/price.js +124 -0
  47. package/dist/paywall/selection.d.ts +17 -0
  48. package/dist/paywall/selection.js +13 -0
  49. package/dist/paywall/slots.d.ts +46 -0
  50. package/dist/paywall/slots.js +1 -0
  51. package/dist/paywall/strings.d.ts +13 -0
  52. package/dist/paywall/strings.js +10 -0
  53. package/dist/paywall/templates.d.ts +31 -0
  54. package/dist/paywall/templates.js +60 -0
  55. package/dist/paywall/theme.d.ts +41 -0
  56. package/dist/paywall/theme.js +104 -0
  57. package/dist/paywall/url.d.ts +22 -0
  58. package/dist/paywall/url.js +48 -0
  59. package/dist/schema/config.d.ts +264 -0
  60. package/dist/schema/config.js +149 -0
  61. package/dist/schema/defaults.d.ts +2 -0
  62. package/dist/schema/defaults.js +20 -0
  63. package/dist/schema/index.d.ts +2 -0
  64. package/dist/schema/index.js +2 -0
  65. package/dist/theme.d.ts +14 -0
  66. package/dist/theme.js +14 -0
  67. package/package.json +66 -3
@@ -0,0 +1,149 @@
1
+ import { z } from 'zod';
2
+ export const PAYWALL_SCHEMA_VERSION = 1;
3
+ export const MAX_PAYWALL_CONFIG_BYTES = 32 * 1024;
4
+ const CONTROL = /[\u0000-\u001f\u007f-\u009f]/;
5
+ const MARKUP = /[<>]/;
6
+ export const paywallText = (max) => z
7
+ .string()
8
+ .min(1)
9
+ .max(max)
10
+ .refine((v) => !MARKUP.test(v) && !CONTROL.test(v) && v.trim() === v, "Can't contain <, > or control characters, or start or end with spaces");
11
+ export const httpsUrl = z
12
+ .string()
13
+ .max(2048)
14
+ .refine((v) => {
15
+ try {
16
+ const u = new URL(v);
17
+ return u.protocol === 'https:' && !u.username && !u.password && u.hostname.length > 0;
18
+ }
19
+ catch {
20
+ return false;
21
+ }
22
+ }, 'An https URL without credentials');
23
+ export const hexColor = z
24
+ .string()
25
+ .regex(/^#[0-9a-fA-F]{6}$/, 'A 6-digit hex color such as #0f5257');
26
+ export const assetId = z.uuid();
27
+ export const localeTag = z
28
+ .string()
29
+ .regex(/^[a-z]{2,3}(-[A-Za-z0-9]{2,8}){0,2}$/, 'A locale such as en or pt-BR');
30
+ const productKey = z.string().regex(/^[a-z0-9][a-z0-9_.-]{0,63}$/);
31
+ export const TEMPLATES = ['hero', 'tiers', 'comparison', 'credits'];
32
+ export const FEATURE_ICONS = [
33
+ 'check',
34
+ 'sparkles',
35
+ 'zap',
36
+ 'shield-check',
37
+ 'cloud',
38
+ 'download',
39
+ 'infinity',
40
+ 'lock-open',
41
+ 'star',
42
+ 'users',
43
+ 'clock',
44
+ 'gift',
45
+ ];
46
+ const schemeTheme = z.strictObject({
47
+ brand: hexColor.optional(),
48
+ brand_hover: hexColor.optional(),
49
+ surface_raised: hexColor.optional(),
50
+ });
51
+ export const paywallTheme = z.strictObject({
52
+ light: schemeTheme.optional(),
53
+ dark: schemeTheme.optional(),
54
+ display_font: z
55
+ .string()
56
+ .regex(/^[A-Za-z0-9 _-]{1,64}$/)
57
+ .optional(),
58
+ color_scheme: z.enum(['system', 'light', 'dark']).optional(),
59
+ });
60
+ const feature = z.strictObject({
61
+ text: paywallText(80),
62
+ icon: z.enum(FEATURE_ICONS).optional(),
63
+ });
64
+ const localizedCopy = z.strictObject({
65
+ title: paywallText(80),
66
+ subtitle: paywallText(200).optional(),
67
+ features: z.array(feature).max(8).optional(),
68
+ cta: paywallText(60).optional(),
69
+ footnote: paywallText(160).optional(),
70
+ comparison: z
71
+ .array(z.strictObject({
72
+ label: paywallText(60),
73
+ values: z
74
+ .record(productKey, z.union([z.boolean(), paywallText(24)]))
75
+ .refine((r) => Object.keys(r).length <= 6, 'At most 6 products'),
76
+ }))
77
+ .max(12)
78
+ .optional(),
79
+ });
80
+ export const paywallCopy = z
81
+ .strictObject({
82
+ default_locale: localeTag,
83
+ locales: z
84
+ .record(localeTag, localizedCopy)
85
+ .refine((r) => Object.keys(r).length >= 1 && Object.keys(r).length <= 40, '1 to 40 locales'),
86
+ })
87
+ .refine((c) => c.default_locale in c.locales, 'default_locale must be one of the locales');
88
+ export const paywallLayout = z.strictObject({
89
+ hero_image: assetId.optional(),
90
+ close_button: z.boolean().optional(),
91
+ restore_button: z.boolean().optional(),
92
+ terms_url: httpsUrl.optional(),
93
+ privacy_url: httpsUrl.optional(),
94
+ });
95
+ export const paywallConfig = z.strictObject({
96
+ schema_version: z.literal(PAYWALL_SCHEMA_VERSION),
97
+ template: z.enum(TEMPLATES),
98
+ theme: paywallTheme,
99
+ copy: paywallCopy,
100
+ layout: paywallLayout,
101
+ });
102
+ export const servedPaywall = z.strictObject({
103
+ id: z.uuid(),
104
+ version: z.number().int().positive(),
105
+ config: paywallConfig,
106
+ assets: z
107
+ .record(assetId, z.string().max(2048))
108
+ .refine((r) => Object.keys(r).length <= 20, 'At most 20 assets'),
109
+ });
110
+ export function parseServedPaywall(input, apiOrigin) {
111
+ const parsed = servedPaywall.safeParse(input);
112
+ if (!parsed.success)
113
+ return null;
114
+ for (const url of Object.values(parsed.data.assets)) {
115
+ let u;
116
+ try {
117
+ u = new URL(url);
118
+ }
119
+ catch {
120
+ return null;
121
+ }
122
+ if (u.origin !== apiOrigin || u.username || u.password)
123
+ return null;
124
+ }
125
+ return parsed.data;
126
+ }
127
+ export function validatePaywallConfig(input) {
128
+ const size = new TextEncoder().encode(JSON.stringify(input ?? null)).length;
129
+ if (size > MAX_PAYWALL_CONFIG_BYTES) {
130
+ return {
131
+ ok: false,
132
+ issues: [
133
+ {
134
+ code: 'custom',
135
+ path: [],
136
+ message: `A paywall config can be at most ${MAX_PAYWALL_CONFIG_BYTES} bytes`,
137
+ input,
138
+ },
139
+ ],
140
+ };
141
+ }
142
+ const parsed = paywallConfig.safeParse(input);
143
+ return parsed.success
144
+ ? { ok: true, config: parsed.data }
145
+ : { ok: false, issues: parsed.error.issues };
146
+ }
147
+ export function referencedAssets(config) {
148
+ return config.layout.hero_image ? [config.layout.hero_image] : [];
149
+ }
@@ -0,0 +1,2 @@
1
+ import type { PaywallConfig, PaywallTemplate } from './config';
2
+ export declare function defaultPaywallConfig(template?: PaywallTemplate): PaywallConfig;
@@ -0,0 +1,20 @@
1
+ import { PAYWALL_SCHEMA_VERSION } from './config';
2
+ export function defaultPaywallConfig(template = 'tiers') {
3
+ return {
4
+ schema_version: PAYWALL_SCHEMA_VERSION,
5
+ template,
6
+ theme: {},
7
+ copy: {
8
+ default_locale: 'en',
9
+ locales: {
10
+ en: {
11
+ title: template === 'credits' ? 'Get more credits' : 'Upgrade',
12
+ subtitle: template === 'credits'
13
+ ? 'Credits are added to your account as soon as the purchase completes.'
14
+ : 'Unlock every feature. Cancel anytime.',
15
+ },
16
+ },
17
+ },
18
+ layout: {},
19
+ };
20
+ }
@@ -0,0 +1,2 @@
1
+ export * from './config';
2
+ export { defaultPaywallConfig } from './defaults';
@@ -0,0 +1,2 @@
1
+ export * from './config';
2
+ export { defaultPaywallConfig } from './defaults';
@@ -0,0 +1,14 @@
1
+ import { type Theme, type ThemeName } from '@tenthbilling/design';
2
+ import { type ReactNode } from 'react';
3
+ export type ThemePreference = ThemeName | 'system';
4
+ /**
5
+ * Provides the design-system theme (design system §4 "Theme switching"): follows the OS setting
6
+ * by default; pass `preference` to force light or dark. `theme` provides a fully resolved theme
7
+ * instead (the paywall passes its tenant-themed one, see `resolvePaywallTheme`).
8
+ */
9
+ export declare function ThemeProvider({ preference, theme, children, }: {
10
+ preference?: ThemePreference;
11
+ theme?: Theme;
12
+ children: ReactNode;
13
+ }): import("react").JSX.Element;
14
+ export declare function useTheme(): Theme;
package/dist/theme.js ADDED
@@ -0,0 +1,14 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { themes } from '@tenthbilling/design';
3
+ import { createContext, useContext, useMemo } from 'react';
4
+ import { useColorScheme } from 'react-native';
5
+ const ThemeContext = createContext(themes.light);
6
+ export function ThemeProvider({ preference = 'system', theme, children, }) {
7
+ const system = useColorScheme();
8
+ const name = preference === 'system' ? (system === 'dark' ? 'dark' : 'light') : preference;
9
+ const value = useMemo(() => theme ?? themes[name], [theme, name]);
10
+ return _jsx(ThemeContext.Provider, { value: value, children: children });
11
+ }
12
+ export function useTheme() {
13
+ return useContext(ThemeContext);
14
+ }
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "@tenthbilling/paywall",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "Tenth paywall renderer, templates and config schema, used by @tenthbilling/expo",
6
+ "license": "MIT",
7
+ "author": "Tenth",
8
+ "homepage": "https://tenthbilling.com/docs/expo",
9
+ "type": "module",
10
+ "main": "./dist/index.js",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./schema": {
17
+ "types": "./dist/schema/index.d.ts",
18
+ "default": "./dist/schema/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "README.md"
28
+ ],
29
+ "dependencies": {
30
+ "zod": "^4.6.7",
31
+ "@tenthbilling/design": "0.1.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/react": "~19.3.0",
35
+ "lucide-react-native": "1.55.0",
36
+ "react": "19.3.0",
37
+ "react-dom": "19.3.0",
38
+ "react-native": "0.88.0",
39
+ "react-native-svg": "15.16.0",
40
+ "react-native-web": "^0.21.4"
41
+ },
42
+ "peerDependencies": {
43
+ "react": "^19.3.0",
44
+ "react-native": ">=0.88.0",
45
+ "lucide-react-native": "^1.55.0",
46
+ "react-native-svg": "^15.16.0"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "react": {
50
+ "optional": true
51
+ },
52
+ "react-native": {
53
+ "optional": true
54
+ },
55
+ "lucide-react-native": {
56
+ "optional": true
57
+ },
58
+ "react-native-svg": {
59
+ "optional": true
60
+ }
61
+ },
62
+ "scripts": {
63
+ "build": "node ../../scripts/packages/clean-dist.mjs && tsc -p tsconfig.build.json && tsc -p tsconfig.build.json --declaration --emitDeclarationOnly --removeComments false",
64
+ "lint": "eslint src",
65
+ "typecheck": "tsc -p tsconfig.json",
66
+ "test": "vitest run"
67
+ },
68
+ "types": "./dist/index.d.ts"
6
69
  }