@alien_org/contract 0.1.4 → 0.2.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/index.cjs CHANGED
@@ -34,8 +34,24 @@ function getReleaseVersion(method, payload) {
34
34
  //#endregion
35
35
  //#region src/methods/versions/index.ts
36
36
  /**
37
+ * Compare two semver versions.
38
+ * @returns negative if a < b, 0 if a === b, positive if a > b
39
+ */
40
+ function compareVersions(a, b) {
41
+ const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
42
+ const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
43
+ if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
44
+ if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
45
+ return (aPatch ?? 0) - (bPatch ?? 0);
46
+ }
47
+ /**
37
48
  * Check if a method is supported in a given version.
38
49
  *
50
+ * Uses the minimum version that introduced the method and returns true if
51
+ * the given version is >= that minimum (semver comparison). This supports
52
+ * versions not explicitly listed in releases (e.g. 0.1.4 when method was
53
+ * added in 0.1.1).
54
+ *
39
55
  * @param method - The method name to check.
40
56
  * @param version - The contract version (must be a valid version string, not undefined).
41
57
  * @returns `true` if the method is supported in the given version, `false` otherwise.
@@ -45,22 +61,16 @@ function getReleaseVersion(method, payload) {
45
61
  * handled at a higher level before calling this function.
46
62
  */
47
63
  function isMethodSupported(method, version) {
48
- const methods = releases[version];
49
- if (!methods) return false;
50
- return methods.some((m) => typeof m === "string" ? m === method : m.method === method);
64
+ const minVersion = getMethodMinVersion(method);
65
+ if (!minVersion) return false;
66
+ return compareVersions(version, minVersion) >= 0;
51
67
  }
52
68
  /**
53
69
  * Get the minimum version that supports a method.
54
70
  * Returns undefined if method not found in any version.
55
71
  */
56
72
  function getMethodMinVersion(method) {
57
- const sorted = Object.keys(releases).sort((a, b) => {
58
- const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
59
- const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
60
- if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
61
- if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
62
- return (aPatch ?? 0) - (bPatch ?? 0);
63
- });
73
+ const sorted = Object.keys(releases).sort(compareVersions);
64
74
  for (const version of sorted) {
65
75
  const methods = releases[version];
66
76
  if (!methods) continue;
package/dist/index.d.cts CHANGED
@@ -269,8 +269,9 @@ interface Methods {
269
269
  * Optional display fields (`title`, `caption`, `iconUrl`, `quantity`)
270
270
  * are shown on the payment approval screen.
271
271
  *
272
- * Set `test: true` for test mode - no real payment is made, but webhooks
273
- * are fired with `test: true` flag. Use for development and testing.
272
+ * Set `test` to a scenario string (e.g. `'paid'`, `'error:insufficient_balance'`)
273
+ * for test mode - no real payment is made, but the specified scenario is
274
+ * simulated. Use for development and testing.
274
275
  *
275
276
  * @since 0.1.1
276
277
  * @schema
@@ -336,7 +337,7 @@ interface Methods {
336
337
  *
337
338
  * | Scenario | Client | Webhook |
338
339
  * |----------|--------|---------|
339
- * | `true` / `'paid'` | `paid` | `finalized` |
340
+ * | `'paid'` | `paid` | `finalized` |
340
341
  * | `'paid:failed'` | `paid` | `failed` |
341
342
  * | `'cancelled'` | `cancelled` | none |
342
343
  * | `'error:*'` | `failed` | none |
@@ -362,7 +363,7 @@ interface Methods {
362
363
  * @since 0.1.1
363
364
  * @schema
364
365
  */
365
- test?: boolean | PaymentTestScenario;
366
+ test?: PaymentTestScenario;
366
367
  }>>;
367
368
  /**
368
369
  * Write text to the system clipboard.
@@ -455,6 +456,11 @@ declare const releases: Record<Version, (MethodName | {
455
456
  /**
456
457
  * Check if a method is supported in a given version.
457
458
  *
459
+ * Uses the minimum version that introduced the method and returns true if
460
+ * the given version is >= that minimum (semver comparison). This supports
461
+ * versions not explicitly listed in releases (e.g. 0.1.4 when method was
462
+ * added in 0.1.1).
463
+ *
458
464
  * @param method - The method name to check.
459
465
  * @param version - The contract version (must be a valid version string, not undefined).
460
466
  * @returns `true` if the method is supported in the given version, `false` otherwise.
package/dist/index.d.mts CHANGED
@@ -269,8 +269,9 @@ interface Methods {
269
269
  * Optional display fields (`title`, `caption`, `iconUrl`, `quantity`)
270
270
  * are shown on the payment approval screen.
271
271
  *
272
- * Set `test: true` for test mode - no real payment is made, but webhooks
273
- * are fired with `test: true` flag. Use for development and testing.
272
+ * Set `test` to a scenario string (e.g. `'paid'`, `'error:insufficient_balance'`)
273
+ * for test mode - no real payment is made, but the specified scenario is
274
+ * simulated. Use for development and testing.
274
275
  *
275
276
  * @since 0.1.1
276
277
  * @schema
@@ -336,7 +337,7 @@ interface Methods {
336
337
  *
337
338
  * | Scenario | Client | Webhook |
338
339
  * |----------|--------|---------|
339
- * | `true` / `'paid'` | `paid` | `finalized` |
340
+ * | `'paid'` | `paid` | `finalized` |
340
341
  * | `'paid:failed'` | `paid` | `failed` |
341
342
  * | `'cancelled'` | `cancelled` | none |
342
343
  * | `'error:*'` | `failed` | none |
@@ -362,7 +363,7 @@ interface Methods {
362
363
  * @since 0.1.1
363
364
  * @schema
364
365
  */
365
- test?: boolean | PaymentTestScenario;
366
+ test?: PaymentTestScenario;
366
367
  }>>;
367
368
  /**
368
369
  * Write text to the system clipboard.
@@ -455,6 +456,11 @@ declare const releases: Record<Version, (MethodName | {
455
456
  /**
456
457
  * Check if a method is supported in a given version.
457
458
  *
459
+ * Uses the minimum version that introduced the method and returns true if
460
+ * the given version is >= that minimum (semver comparison). This supports
461
+ * versions not explicitly listed in releases (e.g. 0.1.4 when method was
462
+ * added in 0.1.1).
463
+ *
458
464
  * @param method - The method name to check.
459
465
  * @param version - The contract version (must be a valid version string, not undefined).
460
466
  * @returns `true` if the method is supported in the given version, `false` otherwise.
package/dist/index.mjs CHANGED
@@ -33,8 +33,24 @@ function getReleaseVersion(method, payload) {
33
33
  //#endregion
34
34
  //#region src/methods/versions/index.ts
35
35
  /**
36
+ * Compare two semver versions.
37
+ * @returns negative if a < b, 0 if a === b, positive if a > b
38
+ */
39
+ function compareVersions(a, b) {
40
+ const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
41
+ const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
42
+ if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
43
+ if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
44
+ return (aPatch ?? 0) - (bPatch ?? 0);
45
+ }
46
+ /**
36
47
  * Check if a method is supported in a given version.
37
48
  *
49
+ * Uses the minimum version that introduced the method and returns true if
50
+ * the given version is >= that minimum (semver comparison). This supports
51
+ * versions not explicitly listed in releases (e.g. 0.1.4 when method was
52
+ * added in 0.1.1).
53
+ *
38
54
  * @param method - The method name to check.
39
55
  * @param version - The contract version (must be a valid version string, not undefined).
40
56
  * @returns `true` if the method is supported in the given version, `false` otherwise.
@@ -44,22 +60,16 @@ function getReleaseVersion(method, payload) {
44
60
  * handled at a higher level before calling this function.
45
61
  */
46
62
  function isMethodSupported(method, version) {
47
- const methods = releases[version];
48
- if (!methods) return false;
49
- return methods.some((m) => typeof m === "string" ? m === method : m.method === method);
63
+ const minVersion = getMethodMinVersion(method);
64
+ if (!minVersion) return false;
65
+ return compareVersions(version, minVersion) >= 0;
50
66
  }
51
67
  /**
52
68
  * Get the minimum version that supports a method.
53
69
  * Returns undefined if method not found in any version.
54
70
  */
55
71
  function getMethodMinVersion(method) {
56
- const sorted = Object.keys(releases).sort((a, b) => {
57
- const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
58
- const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
59
- if (aMajor !== bMajor) return (aMajor ?? 0) - (bMajor ?? 0);
60
- if (aMinor !== bMinor) return (aMinor ?? 0) - (bMinor ?? 0);
61
- return (aPatch ?? 0) - (bPatch ?? 0);
62
- });
72
+ const sorted = Object.keys(releases).sort(compareVersions);
63
73
  for (const version of sorted) {
64
74
  const methods = releases[version];
65
75
  if (!methods) continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alien_org/contract",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",