@happ-cli/sdk 0.1.0 → 0.1.2
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.d.ts +40 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,22 @@ export interface EvalResult {
|
|
|
27
27
|
remaining?: number;
|
|
28
28
|
effectiveSubscriberId?: string;
|
|
29
29
|
}
|
|
30
|
+
export interface SubscribeOptions {
|
|
31
|
+
/** The subscriber's email address — stored in Happ for identification in the dashboard. */
|
|
32
|
+
email?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface IdentifyOptions {
|
|
35
|
+
/** The subscriber's email address — updates or sets the email on their Happ record. */
|
|
36
|
+
email?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface SubscriptionInfo {
|
|
39
|
+
/** The plan key as defined in happ.config.yaml (e.g. "free", "pro"). */
|
|
40
|
+
planKey: string;
|
|
41
|
+
/** The human-readable plan name (e.g. "Free", "Pro"). */
|
|
42
|
+
planName: string;
|
|
43
|
+
/** Subscription status — typically "active". */
|
|
44
|
+
status: string;
|
|
45
|
+
}
|
|
30
46
|
/** Human-readable denial messages keyed by reason code from the Happ API. */
|
|
31
47
|
export declare const DENIAL_MESSAGES: Record<string, string>;
|
|
32
48
|
export declare class HappClient {
|
|
@@ -45,6 +61,30 @@ export declare class HappClient {
|
|
|
45
61
|
* Fire-and-forget — never throws or blocks the response.
|
|
46
62
|
*/
|
|
47
63
|
increment(featureKey: string, subscriberId: string): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Provision a subscriber onto a plan.
|
|
66
|
+
* Call this after a user signs up or upgrades in your app.
|
|
67
|
+
* Fire-and-forget — never throws or blocks the response.
|
|
68
|
+
*
|
|
69
|
+
* @param subscriberId Your app's user ID (stable, opaque)
|
|
70
|
+
* @param planKey The Happ plan key (e.g. "free", "pro")
|
|
71
|
+
* @param options Optional metadata — `email` surfaces in the Happ dashboard
|
|
72
|
+
*/
|
|
73
|
+
subscribe(subscriberId: string, planKey: string, options?: SubscribeOptions): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Associate metadata (e.g. email) with an existing subscriber.
|
|
76
|
+
* Call this on sign-in so the Happ dashboard shows recognisable identities.
|
|
77
|
+
* Fire-and-forget — never throws or blocks the response.
|
|
78
|
+
*
|
|
79
|
+
* @param subscriberId Your app's user ID
|
|
80
|
+
* @param options Metadata to attach — `email` surfaces in the Happ dashboard
|
|
81
|
+
*/
|
|
82
|
+
identify(subscriberId: string, options?: IdentifyOptions): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Fetch the active subscription for a subscriber.
|
|
85
|
+
* Returns `null` if the subscriber has no active subscription or the API is unreachable.
|
|
86
|
+
*/
|
|
87
|
+
getSubscription(subscriberId: string): Promise<SubscriptionInfo | null>;
|
|
48
88
|
}
|
|
49
89
|
/** Create a Happ client. Options default to HAPP_API_URL and HAPP_API_KEY env vars. */
|
|
50
90
|
export declare function createHappClient(options?: HappClientOptions): HappClient;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAMH,MAAM,WAAW,iBAAiB;IAChC,iFAAiF;IACjF,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,8DAA8D;IAC9D,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,qBAAqB,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,6EAA6E;AAC7E,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,M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A;CACf;AAED,6EAA6E;AAC7E,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAMlD,CAAA;AAID,qBAAa,UAAU;IACrB,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACjC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;gBAErB,OAAO,GAAE,iBAAsB;IAK3C,oDAAoD;IACpD,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,WAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAW1E;;;OAGG;IACG,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAYzE;;;OAGG;IACG,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAYxE;;;;;;;;OAQG;IACG,SAAS,CACb,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC;IAgBhB;;;;;;;OAOG;IACG,QAAQ,CACZ,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,IAAI,CAAC;IAahB;;;OAGG;IACG,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC;CAgB9E;AAED,uFAAuF;AACvF,wBAAgB,gBAAgB,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,UAAU,CAExE"}
|
package/dist/index.js
CHANGED
|
@@ -72,6 +72,73 @@ export class HappClient {
|
|
|
72
72
|
// intentionally swallowed — usage tracking must never block the user
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Provision a subscriber onto a plan.
|
|
77
|
+
* Call this after a user signs up or upgrades in your app.
|
|
78
|
+
* Fire-and-forget — never throws or blocks the response.
|
|
79
|
+
*
|
|
80
|
+
* @param subscriberId Your app's user ID (stable, opaque)
|
|
81
|
+
* @param planKey The Happ plan key (e.g. "free", "pro")
|
|
82
|
+
* @param options Optional metadata — `email` surfaces in the Happ dashboard
|
|
83
|
+
*/
|
|
84
|
+
async subscribe(subscriberId, planKey, options = {}) {
|
|
85
|
+
try {
|
|
86
|
+
await this.request("/api/subscriptions", {
|
|
87
|
+
method: "PUT",
|
|
88
|
+
headers: { "Content-Type": "application/json" },
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
subscriberId,
|
|
91
|
+
planKey,
|
|
92
|
+
...(options.email ? { subscriberEmail: options.email } : {}),
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// intentionally swallowed — provisioning failure must never block signup
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Associate metadata (e.g. email) with an existing subscriber.
|
|
102
|
+
* Call this on sign-in so the Happ dashboard shows recognisable identities.
|
|
103
|
+
* Fire-and-forget — never throws or blocks the response.
|
|
104
|
+
*
|
|
105
|
+
* @param subscriberId Your app's user ID
|
|
106
|
+
* @param options Metadata to attach — `email` surfaces in the Happ dashboard
|
|
107
|
+
*/
|
|
108
|
+
async identify(subscriberId, options = {}) {
|
|
109
|
+
if (!options.email)
|
|
110
|
+
return; // nothing to send
|
|
111
|
+
try {
|
|
112
|
+
await this.request(`/api/subscriptions/${encodeURIComponent(subscriberId)}`, {
|
|
113
|
+
method: "PATCH",
|
|
114
|
+
headers: { "Content-Type": "application/json" },
|
|
115
|
+
body: JSON.stringify({ subscriberEmail: options.email }),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
// intentionally swallowed — identify must never block sign-in
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Fetch the active subscription for a subscriber.
|
|
124
|
+
* Returns `null` if the subscriber has no active subscription or the API is unreachable.
|
|
125
|
+
*/
|
|
126
|
+
async getSubscription(subscriberId) {
|
|
127
|
+
try {
|
|
128
|
+
const res = await this.request(`/api/subscriptions/${encodeURIComponent(subscriberId)}`);
|
|
129
|
+
if (!res.ok)
|
|
130
|
+
return null;
|
|
131
|
+
const data = await res.json();
|
|
132
|
+
return {
|
|
133
|
+
planKey: data.plan.key,
|
|
134
|
+
planName: data.plan.name,
|
|
135
|
+
status: data.status,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
75
142
|
}
|
|
76
143
|
/** Create a Happ client. Options default to HAPP_API_URL and HAPP_API_KEY env vars. */
|
|
77
144
|
export function createHappClient(options) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,eAAe,GAAG,sCAAsC,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,eAAe,GAAG,sCAAsC,CAAA;AAqC9D,6EAA6E;AAC7E,MAAM,CAAC,MAAM,eAAe,GAA2B;IACrD,cAAc,EAAQ,8DAA8D;IACpF,eAAe,EAAO,+BAA+B;IACrD,WAAW,EAAW,oDAAoD;IAC1E,gBAAgB,EAAM,oCAAoC;IAC1D,oBAAoB,EAAE,gCAAgC;CACvD,CAAA;AAED,iFAAiF;AAEjF,MAAM,OAAO,UAAU;IAIrB,YAAY,UAA6B,EAAE;QACzC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,eAAe,CAAA;QAC9E,IAAI,CAAC,MAAM,GAAG,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