sendclap 0.3.0 → 0.5.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.
- package/README.md +4 -3
- package/dist/cjs/index.d.ts +20 -7
- package/dist/cjs/index.js +15 -3
- package/dist/esm/index.d.ts +20 -7
- package/dist/esm/index.js +13 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ try {
|
|
|
19
19
|
settings.instanceId ??= (await licenses.activate(key, await instanceName())).instance_id // first run
|
|
20
20
|
const r = await licenses.validate(key, settings.instanceId)
|
|
21
21
|
if (r.valid) settings.lastValidated = Date.now()
|
|
22
|
-
else lockApp(r.status) // "not_found" (unknown key), "disabled" (refunded or disputed) or "not_activated"
|
|
22
|
+
else lockApp(r.status) // "not_found" (unknown key), "disabled" (refunded or disputed), "expired" (subscription over) or "not_activated"
|
|
23
23
|
} catch (e) {
|
|
24
24
|
// offline: keep working for 7 days after the last successful check
|
|
25
25
|
if (!(e instanceof NetworkError) || Date.now() - settings.lastValidated > 7 * 86400_000) lockApp('unverified')
|
|
@@ -27,7 +27,7 @@ try {
|
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
- `activate(licenseKey, instanceName)` registers this device and returns `{ instance_id, license, product }`. Activating the same instance again counts once, and the instance id is simply the name you passed.
|
|
30
|
-
- `validate(licenseKey, instanceId?)` returns `{ valid, status, license?, product? }`. Its `status` is `active`, `not_activated` (the key exists but this device isn't activated), `disabled` (the payment was refunded or disputed) or `not_found` (unknown key). It throws only when no definite answer came back, which is how you tell "offline" from "not found".
|
|
30
|
+
- `validate(licenseKey, instanceId?)` returns `{ valid, status, license?, product? }`. Its `status` is `active`, `not_activated` (the key exists but this device isn't activated), `expired` (a subscription key past `license.expires_at`), `disabled` (the payment was refunded or disputed) or `not_found` (unknown key). It throws only when no definite answer came back, which is how you tell "offline" from "not found".
|
|
31
31
|
- `deactivate(licenseKey, instanceId)` frees one activation so the customer can move to another machine. It throws `InstanceNotFoundError` if that instance wasn't activated.
|
|
32
32
|
|
|
33
33
|
To tell a customer why a license doesn't work, branch on the status, or catch the typed error from `activate`:
|
|
@@ -44,7 +44,7 @@ try { await licenses.activate(key, name) } catch (e) {
|
|
|
44
44
|
|
|
45
45
|
`instanceName()` returns the hostname in Node, Bun, Deno and Electron's main process, and a random id kept in `localStorage` in browsers. The name is sent to Sendclap and shown to the creator in the activity log, and machines that share a hostname share an activation. If that matters, generate your own id (for example `crypto.randomUUID()` saved on first run) and pass it instead.
|
|
46
46
|
|
|
47
|
-
Cache the result of the last successful `validate` and validate again at startup or once a day. On a `NetworkError`, accept the cached result for a grace period. On `not_found`, `disabled` or `not_activated`, lock.
|
|
47
|
+
Cache the result of the last successful `validate` and validate again at startup or once a day. On a `NetworkError`, accept the cached result for a grace period. On `not_found`, `disabled`, `expired` or `not_activated`, lock.
|
|
48
48
|
|
|
49
49
|
## Creator API
|
|
50
50
|
|
|
@@ -79,6 +79,7 @@ Every error is a `SendclapError` with `.status`, `.code` (the server's machine-r
|
|
|
79
79
|
| --- | --- | --- |
|
|
80
80
|
| `InvalidLicenseError` | `license_not_found` | The key doesn't exist (404) |
|
|
81
81
|
| `DisabledLicenseError` | `license_disabled` | The key was disabled after a refund or a dispute (403) |
|
|
82
|
+
| `ExpiredLicenseError` | `license_expired` | The subscription key has expired, and `.license.expires_at` says when (403) |
|
|
82
83
|
| `InstanceNotFoundError` | `instance_not_found` | `deactivate` for an instance that isn't activated (404) |
|
|
83
84
|
| `ActivationLimitError` | `activation_limit` | Every activation is in use, and `.license` shows the counts (409) |
|
|
84
85
|
| `NetworkError` | `network_error` | No response arrived (offline, DNS, TLS or timeout), and `.status` is 0 |
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.5.0";
|
|
2
2
|
export interface ClientOptions {
|
|
3
3
|
/** Defaults to https://sendclap.com. */
|
|
4
4
|
baseUrl?: string;
|
|
@@ -23,6 +23,12 @@ export declare class InvalidLicenseError extends SendclapError {
|
|
|
23
23
|
export declare class DisabledLicenseError extends SendclapError {
|
|
24
24
|
constructor(message: string, body?: unknown);
|
|
25
25
|
}
|
|
26
|
+
/** The subscription license key has expired (`license_expired`); `license.expires_at` says when. */
|
|
27
|
+
export declare class ExpiredLicenseError extends SendclapError {
|
|
28
|
+
readonly license: LicenseInfo;
|
|
29
|
+
readonly product: ProductRef;
|
|
30
|
+
constructor(message: string, license: LicenseInfo, product: ProductRef, body?: unknown);
|
|
31
|
+
}
|
|
26
32
|
/** Every activation of the key is in use; deactivate another instance first (`activation_limit`). */
|
|
27
33
|
export declare class ActivationLimitError extends SendclapError {
|
|
28
34
|
readonly license: LicenseInfo;
|
|
@@ -42,6 +48,8 @@ export interface LicenseInfo {
|
|
|
42
48
|
key: string;
|
|
43
49
|
activations: number;
|
|
44
50
|
max_activations: number;
|
|
51
|
+
/** ISO 8601 time a subscription key expires, or null for a key bought outright. */
|
|
52
|
+
expires_at: string | null;
|
|
45
53
|
}
|
|
46
54
|
export interface ProductRef {
|
|
47
55
|
id?: number;
|
|
@@ -58,19 +66,20 @@ export interface DeactivateResult {
|
|
|
58
66
|
}
|
|
59
67
|
export interface ValidateResult {
|
|
60
68
|
valid: boolean;
|
|
61
|
-
/** `active`: valid. `not_activated`: the key exists but this instance isn't activated. `not_found`: unknown key. `disabled`: refunded or disputed. */
|
|
62
|
-
status: 'active' | 'not_activated' | 'disabled' | 'not_found';
|
|
63
|
-
/** Present for `active` and `
|
|
69
|
+
/** `active`: valid. `not_activated`: the key exists but this instance isn't activated. `not_found`: unknown key. `expired`: a subscription key past `license.expires_at`. `disabled`: refunded or disputed. */
|
|
70
|
+
status: 'active' | 'not_activated' | 'expired' | 'disabled' | 'not_found';
|
|
71
|
+
/** Present for `active`, `not_activated` and `expired`. */
|
|
64
72
|
license?: LicenseInfo;
|
|
65
73
|
product?: ProductRef;
|
|
66
74
|
}
|
|
67
75
|
export interface CheckoutParams {
|
|
76
|
+
/** A `product` checkout for a subscription product comes back with `type: 'membership'`. */
|
|
68
77
|
type: 'tip' | 'product' | 'membership';
|
|
69
78
|
/** Tip amount in US dollars, 1 to 10000. */
|
|
70
79
|
amount?: number;
|
|
71
80
|
product_id?: number;
|
|
72
81
|
tier_id?: number;
|
|
73
|
-
/** Required for memberships. */
|
|
82
|
+
/** Required for memberships and subscription products (the server answers 400 `bad_request` without it). */
|
|
74
83
|
email?: string;
|
|
75
84
|
method?: 'card' | 'crypto';
|
|
76
85
|
message?: string;
|
|
@@ -175,6 +184,7 @@ export interface IssuedLicense {
|
|
|
175
184
|
max_activations: number;
|
|
176
185
|
disabled: boolean;
|
|
177
186
|
issued_at: string;
|
|
187
|
+
expires_at: string | null;
|
|
178
188
|
}
|
|
179
189
|
export interface Dispute {
|
|
180
190
|
id: string;
|
|
@@ -196,15 +206,18 @@ export interface Product {
|
|
|
196
206
|
issues_license_key: boolean;
|
|
197
207
|
page: string;
|
|
198
208
|
x402: string | null;
|
|
209
|
+
/** `null` for a one-time product, `week`, `month` or `year` for a subscription product. */
|
|
210
|
+
interval: string | null;
|
|
199
211
|
}
|
|
200
212
|
export interface Membership {
|
|
201
213
|
id: number;
|
|
202
214
|
name: string;
|
|
203
215
|
description: string;
|
|
204
216
|
price: number;
|
|
205
|
-
currency: string;
|
|
217
|
+
currency: string; /** `week`, `month` or `year`. */
|
|
206
218
|
interval: string;
|
|
207
219
|
x402: string | null;
|
|
220
|
+
license_product_id: number | null;
|
|
208
221
|
}
|
|
209
222
|
export interface Creator {
|
|
210
223
|
handle: string;
|
|
@@ -235,7 +248,7 @@ export declare class LicenseClient {
|
|
|
235
248
|
private options;
|
|
236
249
|
constructor(options?: ClientOptions);
|
|
237
250
|
private call;
|
|
238
|
-
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
251
|
+
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError, ExpiredLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
239
252
|
activate(licenseKey: string, instanceName: string): Promise<ActivateResult>;
|
|
240
253
|
/** Checks the key (and, with an instance id, that this device is activated). Throws only when no definite answer came back. */
|
|
241
254
|
validate(licenseKey: string, instanceId?: string): Promise<ValidateResult>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -35,9 +35,9 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
};
|
|
36
36
|
})();
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.ApiClient = exports.LicenseClient = exports.NetworkError = exports.InstanceNotFoundError = exports.ActivationLimitError = exports.DisabledLicenseError = exports.InvalidLicenseError = exports.SendclapError = exports.VERSION = void 0;
|
|
38
|
+
exports.ApiClient = exports.LicenseClient = exports.NetworkError = exports.InstanceNotFoundError = exports.ActivationLimitError = exports.ExpiredLicenseError = exports.DisabledLicenseError = exports.InvalidLicenseError = exports.SendclapError = exports.VERSION = void 0;
|
|
39
39
|
exports.instanceName = instanceName;
|
|
40
|
-
exports.VERSION = '0.
|
|
40
|
+
exports.VERSION = '0.5.0';
|
|
41
41
|
const DEFAULT_BASE_URL = 'https://sendclap.com';
|
|
42
42
|
const DEFAULT_TIMEOUT_MS = 15000;
|
|
43
43
|
// ---------- errors ----------
|
|
@@ -71,6 +71,16 @@ class DisabledLicenseError extends SendclapError {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
exports.DisabledLicenseError = DisabledLicenseError;
|
|
74
|
+
/** The subscription license key has expired (`license_expired`); `license.expires_at` says when. */
|
|
75
|
+
class ExpiredLicenseError extends SendclapError {
|
|
76
|
+
constructor(message, license, product, body) {
|
|
77
|
+
super(message, 403, 'license_expired', body);
|
|
78
|
+
this.license = license;
|
|
79
|
+
this.product = product;
|
|
80
|
+
this.name = 'ExpiredLicenseError';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.ExpiredLicenseError = ExpiredLicenseError;
|
|
74
84
|
/** Every activation of the key is in use; deactivate another instance first (`activation_limit`). */
|
|
75
85
|
class ActivationLimitError extends SendclapError {
|
|
76
86
|
constructor(message, license, product, body) {
|
|
@@ -139,6 +149,8 @@ async function request(o, method, path, init = {}) {
|
|
|
139
149
|
throw new InvalidLicenseError(message, data);
|
|
140
150
|
if (code === 'license_disabled')
|
|
141
151
|
throw new DisabledLicenseError(message, data);
|
|
152
|
+
if (code === 'license_expired')
|
|
153
|
+
throw new ExpiredLicenseError(message, data.license, data.product, data);
|
|
142
154
|
if (code === 'instance_not_found')
|
|
143
155
|
throw new InstanceNotFoundError(message, data);
|
|
144
156
|
if (code === 'activation_limit')
|
|
@@ -154,7 +166,7 @@ class LicenseClient {
|
|
|
154
166
|
call(action, licenseKey, instanceId) {
|
|
155
167
|
return request(this.options, 'POST', `/licenses/${action}`, { body: { license_key: licenseKey, ...(instanceId ? { instance_id: instanceId } : {}) } });
|
|
156
168
|
}
|
|
157
|
-
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
169
|
+
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError, ExpiredLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
158
170
|
async activate(licenseKey, instanceName) {
|
|
159
171
|
const { instance_id, license, product } = await this.call('activate', licenseKey, instanceName);
|
|
160
172
|
return { instance_id, license, product };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.5.0";
|
|
2
2
|
export interface ClientOptions {
|
|
3
3
|
/** Defaults to https://sendclap.com. */
|
|
4
4
|
baseUrl?: string;
|
|
@@ -23,6 +23,12 @@ export declare class InvalidLicenseError extends SendclapError {
|
|
|
23
23
|
export declare class DisabledLicenseError extends SendclapError {
|
|
24
24
|
constructor(message: string, body?: unknown);
|
|
25
25
|
}
|
|
26
|
+
/** The subscription license key has expired (`license_expired`); `license.expires_at` says when. */
|
|
27
|
+
export declare class ExpiredLicenseError extends SendclapError {
|
|
28
|
+
readonly license: LicenseInfo;
|
|
29
|
+
readonly product: ProductRef;
|
|
30
|
+
constructor(message: string, license: LicenseInfo, product: ProductRef, body?: unknown);
|
|
31
|
+
}
|
|
26
32
|
/** Every activation of the key is in use; deactivate another instance first (`activation_limit`). */
|
|
27
33
|
export declare class ActivationLimitError extends SendclapError {
|
|
28
34
|
readonly license: LicenseInfo;
|
|
@@ -42,6 +48,8 @@ export interface LicenseInfo {
|
|
|
42
48
|
key: string;
|
|
43
49
|
activations: number;
|
|
44
50
|
max_activations: number;
|
|
51
|
+
/** ISO 8601 time a subscription key expires, or null for a key bought outright. */
|
|
52
|
+
expires_at: string | null;
|
|
45
53
|
}
|
|
46
54
|
export interface ProductRef {
|
|
47
55
|
id?: number;
|
|
@@ -58,19 +66,20 @@ export interface DeactivateResult {
|
|
|
58
66
|
}
|
|
59
67
|
export interface ValidateResult {
|
|
60
68
|
valid: boolean;
|
|
61
|
-
/** `active`: valid. `not_activated`: the key exists but this instance isn't activated. `not_found`: unknown key. `disabled`: refunded or disputed. */
|
|
62
|
-
status: 'active' | 'not_activated' | 'disabled' | 'not_found';
|
|
63
|
-
/** Present for `active` and `
|
|
69
|
+
/** `active`: valid. `not_activated`: the key exists but this instance isn't activated. `not_found`: unknown key. `expired`: a subscription key past `license.expires_at`. `disabled`: refunded or disputed. */
|
|
70
|
+
status: 'active' | 'not_activated' | 'expired' | 'disabled' | 'not_found';
|
|
71
|
+
/** Present for `active`, `not_activated` and `expired`. */
|
|
64
72
|
license?: LicenseInfo;
|
|
65
73
|
product?: ProductRef;
|
|
66
74
|
}
|
|
67
75
|
export interface CheckoutParams {
|
|
76
|
+
/** A `product` checkout for a subscription product comes back with `type: 'membership'`. */
|
|
68
77
|
type: 'tip' | 'product' | 'membership';
|
|
69
78
|
/** Tip amount in US dollars, 1 to 10000. */
|
|
70
79
|
amount?: number;
|
|
71
80
|
product_id?: number;
|
|
72
81
|
tier_id?: number;
|
|
73
|
-
/** Required for memberships. */
|
|
82
|
+
/** Required for memberships and subscription products (the server answers 400 `bad_request` without it). */
|
|
74
83
|
email?: string;
|
|
75
84
|
method?: 'card' | 'crypto';
|
|
76
85
|
message?: string;
|
|
@@ -175,6 +184,7 @@ export interface IssuedLicense {
|
|
|
175
184
|
max_activations: number;
|
|
176
185
|
disabled: boolean;
|
|
177
186
|
issued_at: string;
|
|
187
|
+
expires_at: string | null;
|
|
178
188
|
}
|
|
179
189
|
export interface Dispute {
|
|
180
190
|
id: string;
|
|
@@ -196,15 +206,18 @@ export interface Product {
|
|
|
196
206
|
issues_license_key: boolean;
|
|
197
207
|
page: string;
|
|
198
208
|
x402: string | null;
|
|
209
|
+
/** `null` for a one-time product, `week`, `month` or `year` for a subscription product. */
|
|
210
|
+
interval: string | null;
|
|
199
211
|
}
|
|
200
212
|
export interface Membership {
|
|
201
213
|
id: number;
|
|
202
214
|
name: string;
|
|
203
215
|
description: string;
|
|
204
216
|
price: number;
|
|
205
|
-
currency: string;
|
|
217
|
+
currency: string; /** `week`, `month` or `year`. */
|
|
206
218
|
interval: string;
|
|
207
219
|
x402: string | null;
|
|
220
|
+
license_product_id: number | null;
|
|
208
221
|
}
|
|
209
222
|
export interface Creator {
|
|
210
223
|
handle: string;
|
|
@@ -235,7 +248,7 @@ export declare class LicenseClient {
|
|
|
235
248
|
private options;
|
|
236
249
|
constructor(options?: ClientOptions);
|
|
237
250
|
private call;
|
|
238
|
-
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
251
|
+
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError, ExpiredLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
239
252
|
activate(licenseKey: string, instanceName: string): Promise<ActivateResult>;
|
|
240
253
|
/** Checks the key (and, with an instance id, that this device is activated). Throws only when no definite answer came back. */
|
|
241
254
|
validate(licenseKey: string, instanceId?: string): Promise<ValidateResult>;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Sendclap client for JavaScript and TypeScript: license checks for the apps you ship, and the creator REST API for your server.
|
|
2
2
|
// Zero dependencies; uses the global fetch (Node 18+, Bun, Deno, browsers, Electron).
|
|
3
|
-
export const VERSION = '0.
|
|
3
|
+
export const VERSION = '0.5.0';
|
|
4
4
|
const DEFAULT_BASE_URL = 'https://sendclap.com';
|
|
5
5
|
const DEFAULT_TIMEOUT_MS = 15000;
|
|
6
6
|
// ---------- errors ----------
|
|
@@ -31,6 +31,15 @@ export class DisabledLicenseError extends SendclapError {
|
|
|
31
31
|
this.name = 'DisabledLicenseError';
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
/** The subscription license key has expired (`license_expired`); `license.expires_at` says when. */
|
|
35
|
+
export class ExpiredLicenseError extends SendclapError {
|
|
36
|
+
constructor(message, license, product, body) {
|
|
37
|
+
super(message, 403, 'license_expired', body);
|
|
38
|
+
this.license = license;
|
|
39
|
+
this.product = product;
|
|
40
|
+
this.name = 'ExpiredLicenseError';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
34
43
|
/** Every activation of the key is in use; deactivate another instance first (`activation_limit`). */
|
|
35
44
|
export class ActivationLimitError extends SendclapError {
|
|
36
45
|
constructor(message, license, product, body) {
|
|
@@ -96,6 +105,8 @@ async function request(o, method, path, init = {}) {
|
|
|
96
105
|
throw new InvalidLicenseError(message, data);
|
|
97
106
|
if (code === 'license_disabled')
|
|
98
107
|
throw new DisabledLicenseError(message, data);
|
|
108
|
+
if (code === 'license_expired')
|
|
109
|
+
throw new ExpiredLicenseError(message, data.license, data.product, data);
|
|
99
110
|
if (code === 'instance_not_found')
|
|
100
111
|
throw new InstanceNotFoundError(message, data);
|
|
101
112
|
if (code === 'activation_limit')
|
|
@@ -111,7 +122,7 @@ export class LicenseClient {
|
|
|
111
122
|
call(action, licenseKey, instanceId) {
|
|
112
123
|
return request(this.options, 'POST', `/licenses/${action}`, { body: { license_key: licenseKey, ...(instanceId ? { instance_id: instanceId } : {}) } });
|
|
113
124
|
}
|
|
114
|
-
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
125
|
+
/** Registers this device against the key. Throws InvalidLicenseError, DisabledLicenseError, ExpiredLicenseError or ActivationLimitError. Activating the same instance twice counts once. The returned instance_id is the name you passed. */
|
|
115
126
|
async activate(licenseKey, instanceName) {
|
|
116
127
|
const { instance_id, license, product } = await this.call('activate', licenseKey, instanceName);
|
|
117
128
|
return { instance_id, license, product };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sendclap",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Official Sendclap client: license key checks for the apps you ship and the creator REST API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": { "type": "git", "url": "git+https://github.com/backmeupplz/sendclap-sdk.git", "directory": "js" },
|