@tenthbilling/expo 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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/android/build.gradle +23 -0
  4. package/android/src/main/AndroidManifest.xml +2 -0
  5. package/android/src/main/java/com/tenthbilling/expo/TenthStoreModule.kt +669 -0
  6. package/app.plugin.js +3 -0
  7. package/dist/api/client.d.ts +107 -0
  8. package/dist/api/client.js +139 -0
  9. package/dist/api/types.d.ts +17 -0
  10. package/dist/api/types.js +1 -0
  11. package/dist/cache.d.ts +23 -0
  12. package/dist/cache.js +40 -0
  13. package/dist/errors.d.ts +18 -0
  14. package/dist/errors.js +42 -0
  15. package/dist/generated/openapi.d.ts +3208 -0
  16. package/dist/generated/openapi.js +1 -0
  17. package/dist/identity.d.ts +35 -0
  18. package/dist/identity.js +92 -0
  19. package/dist/index.d.ts +12 -0
  20. package/dist/index.js +10 -0
  21. package/dist/keys.d.ts +8 -0
  22. package/dist/keys.js +14 -0
  23. package/dist/native/module.d.ts +12 -0
  24. package/dist/native/module.js +14 -0
  25. package/dist/native/spec.d.ts +166 -0
  26. package/dist/native/spec.js +1 -0
  27. package/dist/products.d.ts +32 -0
  28. package/dist/products.js +213 -0
  29. package/dist/react/Paywall.d.ts +28 -0
  30. package/dist/react/Paywall.js +19 -0
  31. package/dist/react/TenthProvider.d.ts +11 -0
  32. package/dist/react/TenthProvider.js +23 -0
  33. package/dist/react/context.d.ts +4 -0
  34. package/dist/react/context.js +8 -0
  35. package/dist/react/hooks.d.ts +48 -0
  36. package/dist/react/hooks.js +68 -0
  37. package/dist/react/present.d.ts +15 -0
  38. package/dist/react/present.js +47 -0
  39. package/dist/react/usePaywall.d.ts +12 -0
  40. package/dist/react/usePaywall.js +109 -0
  41. package/dist/storage/storage.d.ts +14 -0
  42. package/dist/storage/storage.js +63 -0
  43. package/dist/storage/storage.web.d.ts +4 -0
  44. package/dist/storage/storage.web.js +35 -0
  45. package/dist/store/apple.d.ts +19 -0
  46. package/dist/store/apple.js +127 -0
  47. package/dist/store/context.d.ts +46 -0
  48. package/dist/store/context.js +21 -0
  49. package/dist/store/google.d.ts +22 -0
  50. package/dist/store/google.js +145 -0
  51. package/dist/store/preview.d.ts +16 -0
  52. package/dist/store/preview.js +68 -0
  53. package/dist/stripe/appearance.d.ts +47 -0
  54. package/dist/stripe/appearance.js +44 -0
  55. package/dist/stripe/flows.d.ts +23 -0
  56. package/dist/stripe/flows.js +145 -0
  57. package/dist/stripe/payment-sheet.d.ts +1 -0
  58. package/dist/stripe/payment-sheet.js +24 -0
  59. package/dist/stripe/sheet-registry.d.ts +16 -0
  60. package/dist/stripe/sheet-registry.js +7 -0
  61. package/dist/stripe/web-step.d.ts +10 -0
  62. package/dist/stripe/web-step.js +4 -0
  63. package/dist/stripe/web-step.web.d.ts +4 -0
  64. package/dist/stripe/web-step.web.js +55 -0
  65. package/dist/tenth.d.ts +125 -0
  66. package/dist/tenth.js +329 -0
  67. package/dist/updates.d.ts +15 -0
  68. package/dist/updates.js +30 -0
  69. package/expo-module.config.json +9 -0
  70. package/ios/CountryCodes.swift +52 -0
  71. package/ios/TenthBilling.podspec +31 -0
  72. package/ios/TenthStoreExceptions.swift +88 -0
  73. package/ios/TenthStoreMapping.swift +108 -0
  74. package/ios/TenthStoreModule.swift +213 -0
  75. package/ios/TenthStoreRecords.swift +105 -0
  76. package/package.json +94 -3
  77. package/plugin/withTenth.js +249 -0
@@ -0,0 +1,249 @@
1
+ // @ts-check
2
+ /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS: Expo require()s config plugins at prebuild */
3
+ // The @tenthbilling/expo config plugin.
4
+ //
5
+ // "plugins": [["@tenthbilling/expo", { "googlePlayBilling": true, "storeKitConfig": "store/Tenth.storekit" }]]
6
+ //
7
+ // - googlePlayBilling (default true): adds the com.android.vending.BILLING permission. With false
8
+ // the permission is blocked (tools:node="remove"), so the merged manifest doesn't declare it
9
+ // even though the Play Billing library asks for it, and Play purchases can't be made.
10
+ // - storeKitConfig (optional): a .storekit file inside the project, for StoreKit testing in Xcode.
11
+ // It is copied to ios/<project>/ and referenced in the Xcode project, but not added to any build
12
+ // phase, so it never ships in the app. To use it, pick it in the scheme's Run > Options >
13
+ // StoreKit Configuration. Tenth's server refuses Xcode-environment transactions, so
14
+ // it's for UI work only.
15
+ //
16
+ // StoreKit itself needs no entitlement or Info.plist key, so iOS needs nothing else.
17
+ //
18
+ // No shell commands run here: only plain file reads and one copy, after the path is validated.
19
+
20
+ const fs = require('node:fs');
21
+ const path = require('node:path');
22
+ const {
23
+ AndroidConfig,
24
+ IOSConfig,
25
+ createRunOncePlugin,
26
+ withAndroidManifest,
27
+ withXcodeProject,
28
+ } = require('expo/config-plugins');
29
+ const pkg = require('../package.json');
30
+
31
+ const BILLING_PERMISSION = 'com.android.vending.BILLING';
32
+ const STOREKIT_EXTENSION = '.storekit';
33
+ const MAX_STOREKIT_PATH_LENGTH = 1024;
34
+ const MAX_STOREKIT_BYTES = 5 * 1024 * 1024;
35
+ const STOREKIT_BASENAME = /^[A-Za-z0-9][A-Za-z0-9 ._-]{0,200}\.storekit$/;
36
+ const KNOWN_OPTIONS = ['googlePlayBilling', 'storeKitConfig'];
37
+
38
+ /**
39
+ * @typedef {object} TenthPluginOptions
40
+ * @property {boolean} [googlePlayBilling] Add the Play Billing permission (default true).
41
+ * @property {string} [storeKitConfig] Relative path to a .storekit file inside the project.
42
+ */
43
+
44
+ /**
45
+ * @typedef {object} ResolvedOptions
46
+ * @property {boolean} googlePlayBilling
47
+ * @property {string | null} storeKitConfig
48
+ */
49
+
50
+ /** @typedef {import('expo/config-plugins').AndroidManifest} AndroidManifest */
51
+ /** @typedef {import('expo/config-plugins').XcodeProject} XcodeProject */
52
+
53
+ /**
54
+ * @param {string} message
55
+ * @returns {Error}
56
+ */
57
+ function optionError(message) {
58
+ return new Error(`@tenthbilling/expo config plugin: ${message}`);
59
+ }
60
+
61
+ /**
62
+ * Checks the plugin's options and fills in defaults. Unknown keys and wrong types throw.
63
+ *
64
+ * @param {unknown} options
65
+ * @returns {ResolvedOptions}
66
+ */
67
+ function validateOptions(options) {
68
+ if (options === undefined || options === null) {
69
+ return { googlePlayBilling: true, storeKitConfig: null };
70
+ }
71
+ if (typeof options !== 'object' || Array.isArray(options)) {
72
+ throw optionError('options must be an object, such as { "googlePlayBilling": true }.');
73
+ }
74
+ const record = /** @type {Record<string, unknown>} */ (options);
75
+ const unknown = Object.keys(record).filter((key) => !KNOWN_OPTIONS.includes(key));
76
+ if (unknown.length > 0) {
77
+ throw optionError(
78
+ `unknown option ${unknown.map((key) => JSON.stringify(key)).join(', ')}. ` +
79
+ `Supported options: ${KNOWN_OPTIONS.join(', ')}.`,
80
+ );
81
+ }
82
+
83
+ const { googlePlayBilling, storeKitConfig } = record;
84
+ if (googlePlayBilling !== undefined && typeof googlePlayBilling !== 'boolean') {
85
+ throw optionError('"googlePlayBilling" must be true or false.');
86
+ }
87
+ if (storeKitConfig !== undefined) {
88
+ validateStoreKitPath(storeKitConfig);
89
+ }
90
+ return {
91
+ googlePlayBilling: googlePlayBilling ?? true,
92
+ storeKitConfig: /** @type {string | undefined} */ (storeKitConfig) ?? null,
93
+ };
94
+ }
95
+
96
+ /**
97
+ * The path must be relative, stay inside the project, and name a .storekit file.
98
+ *
99
+ * @param {unknown} value
100
+ * @returns {asserts value is string}
101
+ */
102
+ function validateStoreKitPath(value) {
103
+ if (typeof value !== 'string' || value.length === 0) {
104
+ throw optionError('"storeKitConfig" must be a non-empty string.');
105
+ }
106
+ if (value.length > MAX_STOREKIT_PATH_LENGTH || value.includes('\0')) {
107
+ throw optionError('"storeKitConfig" is not a valid path.');
108
+ }
109
+ if (path.posix.isAbsolute(value) || path.win32.isAbsolute(value)) {
110
+ throw optionError('"storeKitConfig" must be a path relative to the project root.');
111
+ }
112
+ const normalized = path.posix.normalize(value.replace(/\\/g, '/'));
113
+ if (normalized === '..' || normalized.startsWith('../')) {
114
+ throw optionError('"storeKitConfig" must be inside the project.');
115
+ }
116
+ if (!STOREKIT_BASENAME.test(path.posix.basename(normalized))) {
117
+ throw optionError(
118
+ `"storeKitConfig" must name a ${STOREKIT_EXTENSION} file (letters, digits, spaces, ".", "_", "-").`,
119
+ );
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Resolves the .storekit file on disk and checks it: a regular file (not a symlink), inside the
125
+ * project even after resolving symlinked folders, and of a sane size.
126
+ *
127
+ * @param {string} projectRoot
128
+ * @param {string} relativePath
129
+ * @returns {string} The file's real absolute path.
130
+ */
131
+ function resolveStoreKitFile(projectRoot, relativePath) {
132
+ validateStoreKitPath(relativePath);
133
+ const realRoot = fs.realpathSync(projectRoot);
134
+ const candidate = path.resolve(realRoot, relativePath);
135
+
136
+ /** @type {fs.Stats} */
137
+ let stats;
138
+ try {
139
+ stats = fs.lstatSync(candidate);
140
+ } catch {
141
+ throw optionError(`"storeKitConfig" file not found: ${relativePath}`);
142
+ }
143
+ if (stats.isSymbolicLink() || !stats.isFile()) {
144
+ throw optionError('"storeKitConfig" must be a regular file, not a symlink or folder.');
145
+ }
146
+ if (stats.size > MAX_STOREKIT_BYTES) {
147
+ throw optionError('"storeKitConfig" file is too large.');
148
+ }
149
+ const real = fs.realpathSync(candidate);
150
+ const inside = path.relative(realRoot, real);
151
+ if (inside === '' || inside.startsWith('..') || path.isAbsolute(inside)) {
152
+ throw optionError('"storeKitConfig" must be inside the project.');
153
+ }
154
+ return real;
155
+ }
156
+
157
+ /**
158
+ * Adds (or, with `enabled` false, blocks) the Play Billing permission. Pure: edits and returns
159
+ * the manifest object it's given.
160
+ *
161
+ * @param {AndroidManifest} manifest
162
+ * @param {boolean} enabled
163
+ * @returns {AndroidManifest}
164
+ */
165
+ function setBillingPermission(manifest, enabled) {
166
+ if (enabled) {
167
+ // Drop a block left by an earlier `googlePlayBilling: false`, then request it.
168
+ const existing = manifest.manifest['uses-permission'];
169
+ if (Array.isArray(existing)) {
170
+ manifest.manifest['uses-permission'] = existing.filter(
171
+ (entry) =>
172
+ !(
173
+ entry.$['android:name'] === BILLING_PERMISSION &&
174
+ /** @type {Record<string, string | undefined>} */ (entry.$)['tools:node'] === 'remove'
175
+ ),
176
+ );
177
+ }
178
+ AndroidConfig.Permissions.ensurePermissions(manifest, [BILLING_PERMISSION]);
179
+ return manifest;
180
+ }
181
+ const withTools = AndroidConfig.Manifest.ensureToolsAvailable(manifest);
182
+ return AndroidConfig.Permissions.addBlockedPermissions(withTools, [BILLING_PERMISSION]);
183
+ }
184
+
185
+ /**
186
+ * Copies the .storekit file next to the app's sources and references it in the Xcode project
187
+ * without adding it to any build phase (so it is not bundled into the app).
188
+ *
189
+ * @param {XcodeProject} project
190
+ * @param {{ projectRoot: string, platformProjectRoot: string, projectName: string, storeKitConfig: string }} input
191
+ * @returns {XcodeProject}
192
+ */
193
+ function addStoreKitConfig(
194
+ project,
195
+ { projectRoot, platformProjectRoot, projectName, storeKitConfig },
196
+ ) {
197
+ const source = resolveStoreKitFile(projectRoot, storeKitConfig);
198
+ const basename = path.basename(source);
199
+ if (!STOREKIT_BASENAME.test(basename)) {
200
+ throw optionError(`"storeKitConfig" must name a ${STOREKIT_EXTENSION} file.`);
201
+ }
202
+ const destinationDir = path.join(platformProjectRoot, projectName);
203
+ fs.mkdirSync(destinationDir, { recursive: true });
204
+ fs.copyFileSync(source, path.join(destinationDir, basename));
205
+
206
+ return IOSConfig.XcodeUtils.addFileToGroupAndLink({
207
+ filepath: `${projectName}/${basename}`,
208
+ groupName: projectName,
209
+ project,
210
+ addFileToProject({ project: xcodeProject, file }) {
211
+ xcodeProject.addToPbxFileReferenceSection(file);
212
+ },
213
+ });
214
+ }
215
+
216
+ /** @type {import('expo/config-plugins').ConfigPlugin<TenthPluginOptions | void>} */
217
+ const withTenth = (config, rawOptions) => {
218
+ const options = validateOptions(rawOptions);
219
+
220
+ config = withAndroidManifest(config, (modConfig) => {
221
+ modConfig.modResults = setBillingPermission(modConfig.modResults, options.googlePlayBilling);
222
+ return modConfig;
223
+ });
224
+
225
+ const storeKitConfig = options.storeKitConfig;
226
+ if (storeKitConfig !== null) {
227
+ config = withXcodeProject(config, (modConfig) => {
228
+ const { projectRoot, platformProjectRoot, projectName } = modConfig.modRequest;
229
+ if (!projectName) {
230
+ throw optionError('could not find the iOS project name to add the .storekit file to.');
231
+ }
232
+ modConfig.modResults = addStoreKitConfig(modConfig.modResults, {
233
+ projectRoot,
234
+ platformProjectRoot,
235
+ projectName,
236
+ storeKitConfig,
237
+ });
238
+ return modConfig;
239
+ });
240
+ }
241
+
242
+ return config;
243
+ };
244
+
245
+ module.exports = createRunOncePlugin(withTenth, pkg.name, pkg.version);
246
+ module.exports.validateOptions = validateOptions;
247
+ module.exports.setBillingPermission = setBillingPermission;
248
+ module.exports.resolveStoreKitFile = resolveStoreKitFile;
249
+ module.exports.BILLING_PERMISSION = BILLING_PERMISSION;