@stewardhq/sdk 0.3.0 → 0.6.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/LICENSE +201 -0
- package/README.md +332 -273
- package/dist/_chunks/errors.js +1 -1
- package/dist/_chunks/events.d.ts +386 -78
- package/dist/_chunks/events.js +1035 -19
- package/dist/_chunks/index.d.ts +3752 -305
- package/dist/_chunks/locale.d.ts +270 -2
- package/dist/_chunks/src.js +925 -31
- package/dist/_chunks/validators.d.ts +46 -2
- package/dist/_chunks/validators.js +128 -27
- package/dist/_chunks/webhook-core.d.ts +2 -2
- package/dist/contract.d.ts +4 -4
- package/dist/contract.js +4 -4
- package/dist/index.d.ts +447 -33
- package/dist/index.js +1659 -26
- package/dist/server.d.ts +66 -16
- package/dist/server.js +35 -4
- package/package.json +14 -13
- package/dist/_chunks/steward.d.ts +0 -189
- package/dist/_chunks/steward.js +0 -564
- package/dist/testing/fixtures/events/LOCK.json +0 -27
- package/dist/testing/fixtures/events/account.deleted.json +0 -36
- package/dist/testing/fixtures/events/account.state_changed.json +0 -53
- package/dist/testing/fixtures/events/account.updated.json +0 -37
- package/dist/testing/fixtures/events/checkout.completed.json +0 -36
- package/dist/testing/fixtures/events/checkout.expired.json +0 -26
- package/dist/testing/fixtures/events/checkout.failed.json +0 -27
- package/dist/testing/fixtures/events/invoice.created.json +0 -37
- package/dist/testing/fixtures/events/invoice.issued.json +0 -38
- package/dist/testing/fixtures/events/invoice.voided.json +0 -39
- package/dist/testing/fixtures/events/subscription.activated.json +0 -40
- package/dist/testing/fixtures/events/subscription.cancel_scheduled.json +0 -38
- package/dist/testing/fixtures/events/subscription.canceled.json +0 -29
- package/dist/testing/fixtures/events/subscription.expired.json +0 -27
- package/dist/testing/fixtures/events/subscription.payment_failed.json +0 -38
- package/dist/testing/fixtures/events/subscription.reactivated.json +0 -36
- package/dist/testing/fixtures/events/subscription.renewed.json +0 -39
- package/dist/testing/fixtures/events/subscription.suspended.json +0 -36
- package/dist/testing/fixtures/events/subscription.terminated.json +0 -28
- package/dist/testing.d.ts +0 -656
- package/dist/testing.js +0 -3684
package/dist/server.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { r as MutationOptions } from "./_chunks/
|
|
4
|
-
import { r as StewardLogger, t as StewardLocale } from "./_chunks/locale.js";
|
|
1
|
+
import { Ba as CheckoutPrefill, Da as PortalSession, Fc as Interval, Ga as CheckoutSessionCreateInput, Ja as CheckoutSessionCreated, No as AccountBenefit, Oa as PortalSessionCreateInput, Za as CheckoutStatus, io as PaymentMethod, vi as CheckoutSessionResponse } from "./_chunks/index.js";
|
|
2
|
+
import { O as AccountInclude, U as AccountState, Y as BillingProfile, a as BillingEvent, f as EventType, n as AnyBillingEvent, o as BillingEventOf, u as EventDetail } from "./_chunks/events.js";
|
|
3
|
+
import { r as StewardLogger, s as MutationOptions, t as StewardLocale } from "./_chunks/locale.js";
|
|
5
4
|
import { a as HeaderSource, s as VerifyRawFailure } from "./_chunks/webhook-core.js";
|
|
6
5
|
import "./_chunks/webhook.js";
|
|
7
6
|
//#region src/server/checkout.d.ts
|
|
@@ -56,6 +55,12 @@ type CheckoutCreateInput = {
|
|
|
56
55
|
displayName?: string;
|
|
57
56
|
/** Sayfanın ön doldurması (ad, e-posta, tür); kimlik verisi değil (K21). */
|
|
58
57
|
prefill?: CheckoutPrefill;
|
|
58
|
+
/**
|
|
59
|
+
* Upgrade: the account's live subscription this purchase replaces. Do NOT cancel it before the
|
|
60
|
+
* checkout: it keeps running until the payment is recorded (card or bank transfer) and is
|
|
61
|
+
* canceled in that same transaction; an abandoned checkout leaves it untouched.
|
|
62
|
+
*/
|
|
63
|
+
replacesSubscriptionId?: string;
|
|
59
64
|
};
|
|
60
65
|
type CheckoutCreated = {
|
|
61
66
|
id: string;
|
|
@@ -65,6 +70,12 @@ type CheckoutCreated = {
|
|
|
65
70
|
type CheckoutResult = {
|
|
66
71
|
status: CheckoutStatus;
|
|
67
72
|
planCode?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Chosen on the page: `bank_transfer` with `status: "open"` means the order waits for the
|
|
75
|
+
* customer's transfer (show "payment pending" and `account.bankTransferCode` from the state).
|
|
76
|
+
* Absent before a choice and from older steward.
|
|
77
|
+
*/
|
|
78
|
+
paymentMethod?: PaymentMethod;
|
|
68
79
|
/** Yalnızca `completed`: işlem sonrası hesap durumu (önbelleğe yazıldı). */
|
|
69
80
|
account?: AccountState;
|
|
70
81
|
};
|
|
@@ -106,12 +117,12 @@ export declare function CustomerPortal(options: CustomerPortalOptions): Customer
|
|
|
106
117
|
//#endregion
|
|
107
118
|
//#region src/server/webhooks.d.ts
|
|
108
119
|
/** Neden hook'u olan tipler: `account.state_changed` dışındaki her tip (neden olarak da gelirler). */
|
|
109
|
-
type CauseEventType = Exclude<EventType,
|
|
120
|
+
type CauseEventType = Exclude<EventType, 'account.state_changed'>;
|
|
110
121
|
type Pascal<S extends string> = S extends `${infer Head}_${infer Tail}` ? `${Capitalize<Head>}${Pascal<Tail>}` : Capitalize<S>;
|
|
111
122
|
/** `subscription.payment_failed` → `onSubscriptionPaymentFailed`. */
|
|
112
123
|
type EventHookName<T extends string> = T extends `${infer Scope}.${infer Name}` ? `on${Pascal<Scope>}${Pascal<Name>}` : `on${Pascal<T>}`;
|
|
113
|
-
type WebhookHandled =
|
|
114
|
-
type WebhookErrorCode =
|
|
124
|
+
type WebhookHandled = 'received' | 'no_state' | 'ignored' | 'deleted' | 'ping';
|
|
125
|
+
type WebhookErrorCode = 'not_configured' | VerifyRawFailure | 'invalid_payload' | 'id_mismatch' | 'state_unavailable' | 'profile_unavailable' | 'hook_failed';
|
|
115
126
|
type WebhookResult = {
|
|
116
127
|
status: 200;
|
|
117
128
|
body: {
|
|
@@ -140,7 +151,7 @@ type CauseHookArgs<T extends CauseEventType, S extends HookState = HookState> =
|
|
|
140
151
|
ref: string;
|
|
141
152
|
cause: T;
|
|
142
153
|
/** Filtresiz (eski) endpoint'te o tipin event'i, yeni endpoint'te `account.state_changed`. */
|
|
143
|
-
event: BillingEventOf<T> | BillingEventOf<
|
|
154
|
+
event: BillingEventOf<T> | BillingEventOf<'account.state_changed'>;
|
|
144
155
|
/** Teslimatın tam durumu (refetch'te canlı); yalnızca `refetch: false` + snapshot taşıyan eski gövdede `null`. */
|
|
145
156
|
state: S;
|
|
146
157
|
};
|
|
@@ -148,6 +159,35 @@ type NotificationHookArgs<T extends CauseEventType, S extends HookState = HookSt
|
|
|
148
159
|
/** `includeProfile: true` ise steward'dan okunan fatura profili (e-posta; K15). */
|
|
149
160
|
profile?: BillingProfile | null;
|
|
150
161
|
};
|
|
162
|
+
/** Benefit grant types with a dedicated hook (`onBenefitGranted` / `onBenefitRevoked` / `onBenefitCycled`). */
|
|
163
|
+
type BenefitHookEventType = 'benefit_grant.created' | 'benefit_grant.revoked' | 'benefit_grant.cycled';
|
|
164
|
+
type BenefitHookArgs<T extends BenefitHookEventType, S extends HookState = HookState> = {
|
|
165
|
+
ref: string;
|
|
166
|
+
cause: T;
|
|
167
|
+
/** The benefit grant event itself (the hook does not run for an `account.state_changed`, which carries no benefit). */
|
|
168
|
+
event: BillingEventOf<T>;
|
|
169
|
+
/** `event.data.detail.benefit`: the benefit as granted or revoked at the time of the event. */
|
|
170
|
+
benefit: AccountBenefit;
|
|
171
|
+
/** Grant log row id (`event.data.detail.grantId`). */
|
|
172
|
+
grantId: string;
|
|
173
|
+
/** The delivery's full state (live with refetch): `state.benefits` is the current set. */
|
|
174
|
+
state: S;
|
|
175
|
+
};
|
|
176
|
+
/** `onBenefitCycled` (`benefit_grant.cycled`): the renewed `meter_credit` grant and its new meter period. */
|
|
177
|
+
type BenefitCycledHookArgs<S extends HookState = HookState> = BenefitHookArgs<'benefit_grant.cycled', S> & {
|
|
178
|
+
periodStart: string;
|
|
179
|
+
periodEnd: string;
|
|
180
|
+
};
|
|
181
|
+
/** `onMeterThreshold` (`meter.threshold_crossed`, cause hook). */
|
|
182
|
+
type MeterThresholdHookArgs<S extends HookState = HookState> = CauseHookArgs<'meter.threshold_crossed', S> & {
|
|
183
|
+
/** The event's detail when the event itself was delivered; `null` for an `account.state_changed` caused by it. */
|
|
184
|
+
threshold: EventDetail<'meter.threshold_crossed'> | null;
|
|
185
|
+
};
|
|
186
|
+
/** `onMeterPeriodClosed` (`meter.period_closed`, cause hook). */
|
|
187
|
+
type MeterPeriodClosedHookArgs<S extends HookState = HookState> = CauseHookArgs<'meter.period_closed', S> & {
|
|
188
|
+
/** The event's detail when the event itself was delivered; `null` for an `account.state_changed` caused by it. */
|
|
189
|
+
period: EventDetail<'meter.period_closed'> | null;
|
|
190
|
+
};
|
|
151
191
|
type UnknownEventArgs<S extends HookState = HookState> = {
|
|
152
192
|
ref: string;
|
|
153
193
|
cause: string;
|
|
@@ -161,7 +201,7 @@ type AfterArgs<S extends HookState = HookState> = {
|
|
|
161
201
|
event: BillingEvent | AnyBillingEvent;
|
|
162
202
|
state: S;
|
|
163
203
|
/** Refetch açıkken `received` ya da (hesap silme teslimatı, F4e) `deleted`; `no_state` yalnızca `state: null` ile. */
|
|
164
|
-
handled: null extends S ? Exclude<WebhookHandled,
|
|
204
|
+
handled: null extends S ? Exclude<WebhookHandled, 'ping' | 'ignored'> : 'received' | 'deleted';
|
|
165
205
|
};
|
|
166
206
|
/** Dönüş değeri beklenir (`await`) ama kullanılmaz: sorgu/kuyruk promise'ı doğrudan dönebilir. */
|
|
167
207
|
type Hook<A> = (args: A) => unknown;
|
|
@@ -187,7 +227,13 @@ type WebhooksCache = {
|
|
|
187
227
|
invalidate?(ref: string): unknown;
|
|
188
228
|
};
|
|
189
229
|
type CauseHooks<S extends HookState> = { [T in CauseEventType as EventHookName<T>]?: Hook<CauseHookArgs<T, S>>; };
|
|
190
|
-
type WebhooksOptions<S extends HookState = HookState> = CauseHooks<S> & {
|
|
230
|
+
type WebhooksOptions<S extends HookState = HookState> = Omit<CauseHooks<S>, 'onMeterPeriodClosed'> & {
|
|
231
|
+
/** `meter.period_closed` (cause hook); `period` is the event's detail when the event itself was delivered. */
|
|
232
|
+
onMeterPeriodClosed?: Hook<MeterPeriodClosedHookArgs<S>>;
|
|
233
|
+
/** A meter's standing changed (`meter.threshold_crossed`, cause hook); after `onMeterThresholdCrossed`. */
|
|
234
|
+
onMeterThreshold?: Hook<MeterThresholdHookArgs<S>>;
|
|
235
|
+
/** A `meter_credit` grant was renewed (`benefit_grant.cycled`; the endpoint must list the type). After `onBenefitGrantCycled`. */
|
|
236
|
+
onBenefitCycled?: Hook<BenefitCycledHookArgs<S>>;
|
|
191
237
|
/** İmza sırları; metinse virgülle "yeni,eski". Verilmezse her teslimatta `STEWARD_WEBHOOK_SECRET`. */
|
|
192
238
|
secrets?: string | readonly string[];
|
|
193
239
|
/** Zaman damgası penceresi, saniye (varsayılan 300). */
|
|
@@ -204,9 +250,13 @@ type WebhooksOptions<S extends HookState = HookState> = CauseHooks<S> & {
|
|
|
204
250
|
includeProfile?: boolean;
|
|
205
251
|
/** Tam durumlu her teslimatta (tekrar dahil). `"ignore"` dönerse hesap bilinmiyor: 200, diğer hook'lar ve `after` koşmaz. */
|
|
206
252
|
onStateChanged?: Hook<StateChangedArgs>;
|
|
207
|
-
onPaymentFailed?: Hook<NotificationHookArgs<
|
|
208
|
-
onSuspended?: Hook<NotificationHookArgs<
|
|
209
|
-
onTerminated?: Hook<NotificationHookArgs<
|
|
253
|
+
onPaymentFailed?: Hook<NotificationHookArgs<'subscription.payment_failed', S>>;
|
|
254
|
+
onSuspended?: Hook<NotificationHookArgs<'subscription.suspended', S>>;
|
|
255
|
+
onTerminated?: Hook<NotificationHookArgs<'subscription.terminated', S>>;
|
|
256
|
+
/** A benefit was granted (`benefit_grant.created`; the endpoint must list the type). After `onBenefitGrantCreated`. */
|
|
257
|
+
onBenefitGranted?: Hook<BenefitHookArgs<'benefit_grant.created', S>>;
|
|
258
|
+
/** A benefit was revoked (`benefit_grant.revoked`; the endpoint must list the type). After `onBenefitGrantRevoked`. */
|
|
259
|
+
onBenefitRevoked?: Hook<BenefitHookArgs<'benefit_grant.revoked', S>>;
|
|
210
260
|
onUnknownEvent?: Hook<UnknownEventArgs<S>>;
|
|
211
261
|
/** Diğer hook'lardan sonra, her teslimatta (tekrar dahil; `"ignore"` hariç); ör. ağlara yayılma. */
|
|
212
262
|
after?: Hook<AfterArgs<S>>;
|
|
@@ -216,7 +266,7 @@ type WebhooksOptions<S extends HookState = HookState> = CauseHooks<S> & {
|
|
|
216
266
|
* `refetch` ve `steward` alanlarının tiplerinden hook durumu: `refetch: true` ya da (`refetch`
|
|
217
267
|
* verilmemiş ve `steward` verilmiş) → `AccountState`; aksi (ya da tip belirsizse) `AccountState | null`.
|
|
218
268
|
*/
|
|
219
|
-
type WebhooksHookState<R, St> = [R] extends [true] ? AccountState : [R] extends [undefined] ?
|
|
269
|
+
type WebhooksHookState<R, St> = [R] extends [true] ? AccountState : [R] extends [undefined] ? [undefined] extends [St] ? HookState : AccountState : HookState;
|
|
220
270
|
/** `Request`'in kullanılan kısmı (Hono `c.req.raw`, Next `req`, undici `Request`). */
|
|
221
271
|
type WebhookRequest = {
|
|
222
272
|
readonly headers: Headers;
|
|
@@ -228,9 +278,9 @@ type WebhooksHandler = {
|
|
|
228
278
|
/** Düz fonksiyon: ham gövde + başlıklar → `{status, body}` (ürün kendi yanıtını kurar). */
|
|
229
279
|
receive(rawBody: string, headers: HeaderSource): Promise<WebhookResult>;
|
|
230
280
|
};
|
|
231
|
-
export declare function Webhooks<const R extends boolean | undefined = undefined, St extends WebhooksSteward | undefined = undefined>(options: Omit<WebhooksOptions<WebhooksHookState<R, St>>,
|
|
281
|
+
export declare function Webhooks<const R extends boolean | undefined = undefined, St extends WebhooksSteward | undefined = undefined>(options: Omit<WebhooksOptions<WebhooksHookState<R, St>>, 'refetch' | 'steward'> & {
|
|
232
282
|
refetch?: R;
|
|
233
283
|
steward?: St;
|
|
234
284
|
}): WebhooksHandler;
|
|
235
285
|
//#endregion
|
|
236
|
-
export type { AfterArgs, CauseEventType, CauseHookArgs, CheckoutCache, CheckoutCreateInput, CheckoutCreated, CheckoutHandler, CheckoutOptions, CheckoutResult, CheckoutSteward, ConsentDoc, CustomerPortalClient, CustomerPortalCreateArgs, CustomerPortalOptions, CustomerPortalSteward, EventHookName, HookState, NotificationHookArgs, StateChangedArgs, UnknownEventArgs, WebhookErrorCode, WebhookHandled, WebhookRequest, WebhookResult, WebhooksCache, WebhooksHandler, WebhooksHookState, WebhooksOptions, WebhooksSteward };
|
|
286
|
+
export type { AfterArgs, BenefitCycledHookArgs, BenefitHookArgs, BenefitHookEventType, CauseEventType, CauseHookArgs, CheckoutCache, CheckoutCreateInput, CheckoutCreated, CheckoutHandler, CheckoutOptions, CheckoutResult, CheckoutSteward, ConsentDoc, CustomerPortalClient, CustomerPortalCreateArgs, CustomerPortalOptions, CustomerPortalSteward, EventHookName, HookState, MeterPeriodClosedHookArgs, MeterThresholdHookArgs, NotificationHookArgs, StateChangedArgs, UnknownEventArgs, WebhookErrorCode, WebhookHandled, WebhookRequest, WebhookResult, WebhooksCache, WebhooksHandler, WebhooksHookState, WebhooksOptions, WebhooksSteward };
|
package/dist/server.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Kn as IdSchema, Nn as ActorRefSchema, Un as ExternalRefSchema, a as EVENT_TYPES, l as PING_EVENT_TYPE, r as BillingEventSchema, t as AnyBillingEventSchema, u as PingSchema, w as AccountStateSchema } from "./_chunks/events.js";
|
|
2
2
|
import "./_chunks/src.js";
|
|
3
3
|
import { n as StewardConfigError, r as StewardContractError, t as StewardApiError } from "./_chunks/errors.js";
|
|
4
4
|
import { i as verifyWebhookRaw } from "./_chunks/webhook.js";
|
|
@@ -59,7 +59,8 @@ function Checkout(options) {
|
|
|
59
59
|
url
|
|
60
60
|
})) } : {},
|
|
61
61
|
...input.displayName !== void 0 ? { displayName: input.displayName } : {},
|
|
62
|
-
...input.prefill !== void 0 ? { prefill: input.prefill } : {}
|
|
62
|
+
...input.prefill !== void 0 ? { prefill: input.prefill } : {},
|
|
63
|
+
...input.replacesSubscriptionId !== void 0 ? { replacesSubscriptionId: input.replacesSubscriptionId } : {}
|
|
63
64
|
};
|
|
64
65
|
const session = await steward.checkoutSessions.create(input.ref, body, { actorRef: input.actorRef });
|
|
65
66
|
if (typeof session.url !== "string" || session.url === "") throw new StewardContractError({ message: "Checkout: steward hosted checkout adresi (url) döndürmedi; steward sürümü F2b öncesi mi?" });
|
|
@@ -73,17 +74,20 @@ function Checkout(options) {
|
|
|
73
74
|
if (!IdSchema.safeParse(id).success) throw notFound();
|
|
74
75
|
const session = await steward.checkoutSessions.get(id);
|
|
75
76
|
if (session.accountRef !== ref) throw notFound();
|
|
77
|
+
const method = session.paymentMethod ? { paymentMethod: session.paymentMethod } : {};
|
|
76
78
|
if (session.status === "completed" && session.account) {
|
|
77
79
|
await cache?.prime(session.account);
|
|
78
80
|
return {
|
|
79
81
|
status: session.status,
|
|
80
82
|
planCode: session.planCode,
|
|
83
|
+
...method,
|
|
81
84
|
account: session.account
|
|
82
85
|
};
|
|
83
86
|
}
|
|
84
87
|
return {
|
|
85
88
|
status: session.status,
|
|
86
|
-
planCode: session.planCode
|
|
89
|
+
planCode: session.planCode,
|
|
90
|
+
...method
|
|
87
91
|
};
|
|
88
92
|
}
|
|
89
93
|
};
|
|
@@ -126,6 +130,12 @@ const NOTIFICATION_HOOKS = {
|
|
|
126
130
|
"subscription.suspended": "onSuspended",
|
|
127
131
|
"subscription.terminated": "onTerminated"
|
|
128
132
|
};
|
|
133
|
+
const BENEFIT_HOOKS = {
|
|
134
|
+
"benefit_grant.created": "onBenefitGranted",
|
|
135
|
+
"benefit_grant.revoked": "onBenefitRevoked",
|
|
136
|
+
"benefit_grant.cycled": "onBenefitCycled"
|
|
137
|
+
};
|
|
138
|
+
const isBenefitHookEvent = (event) => Object.hasOwn(BENEFIT_HOOKS, event.type);
|
|
129
139
|
const CAUSE_TYPES = new Set(EVENT_TYPES.filter((t) => t !== "account.state_changed"));
|
|
130
140
|
const KNOWN_TYPES = new Set(EVENT_TYPES);
|
|
131
141
|
const isCauseType = (value) => CAUSE_TYPES.has(value);
|
|
@@ -282,12 +292,33 @@ function Webhooks(input) {
|
|
|
282
292
|
event: d.event,
|
|
283
293
|
state: d.state
|
|
284
294
|
};
|
|
285
|
-
|
|
295
|
+
const causeArgs = d.causeType === "meter.period_closed" ? {
|
|
296
|
+
...args,
|
|
297
|
+
period: d.event.type === "meter.period_closed" ? d.event.data.detail : null
|
|
298
|
+
} : args;
|
|
299
|
+
await hooks[eventHookName(d.causeType)]?.(causeArgs);
|
|
286
300
|
const notification = NOTIFICATION_HOOKS[d.causeType];
|
|
287
301
|
if (notification) await hooks[notification]?.(profile === void 0 ? args : {
|
|
288
302
|
...args,
|
|
289
303
|
profile
|
|
290
304
|
});
|
|
305
|
+
if (isBenefitHookEvent(d.event)) {
|
|
306
|
+
const { benefit, grantId } = d.event.data.detail;
|
|
307
|
+
const period = d.event.type === "benefit_grant.cycled" ? {
|
|
308
|
+
periodStart: d.event.data.detail.periodStart,
|
|
309
|
+
periodEnd: d.event.data.detail.periodEnd
|
|
310
|
+
} : {};
|
|
311
|
+
await hooks[BENEFIT_HOOKS[d.event.type]]?.({
|
|
312
|
+
...args,
|
|
313
|
+
benefit,
|
|
314
|
+
grantId,
|
|
315
|
+
...period
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (d.causeType === "meter.threshold_crossed") await hooks.onMeterThreshold?.({
|
|
319
|
+
...args,
|
|
320
|
+
threshold: d.event.type === "meter.threshold_crossed" ? d.event.data.detail : null
|
|
321
|
+
});
|
|
291
322
|
}
|
|
292
323
|
const handled = d.state ? "received" : "no_state";
|
|
293
324
|
await options.after?.({
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stewardhq/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Steward SDK: contract schemas, typed API client and webhook signing.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://github.com/z9cloud/steward/tree/main/sdk#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/z9cloud/steward/issues"
|
|
9
|
+
},
|
|
5
10
|
"type": "module",
|
|
6
11
|
"repository": {
|
|
7
12
|
"type": "git",
|
|
@@ -33,15 +38,11 @@
|
|
|
33
38
|
"types": "./dist/webhook/web.d.ts",
|
|
34
39
|
"default": "./dist/webhook/web.js"
|
|
35
40
|
},
|
|
36
|
-
"./testing": {
|
|
37
|
-
"types": "./dist/testing.d.ts",
|
|
38
|
-
"default": "./dist/testing.js"
|
|
39
|
-
},
|
|
40
|
-
"./testing/fixtures/events/*": "./dist/testing/fixtures/events/*",
|
|
41
41
|
"./package.json": "./package.json"
|
|
42
42
|
},
|
|
43
43
|
"files": [
|
|
44
|
-
"dist"
|
|
44
|
+
"dist",
|
|
45
|
+
"LICENSE"
|
|
45
46
|
],
|
|
46
47
|
"publishConfig": {
|
|
47
48
|
"access": "public",
|
|
@@ -51,12 +52,12 @@
|
|
|
51
52
|
"zod": "^4.0.0"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
|
-
"@types/node": "^24.
|
|
55
|
+
"@types/node": "^24.19.0",
|
|
55
56
|
"tsdown": "^0.23.0",
|
|
56
|
-
"typescript": "^
|
|
57
|
-
"vitest": "^5.0.
|
|
58
|
-
"workerd": "^1.
|
|
59
|
-
"@
|
|
57
|
+
"typescript": "^7.0.2",
|
|
58
|
+
"vitest": "^5.0.2",
|
|
59
|
+
"workerd": "^1.20260928.1",
|
|
60
|
+
"@stewardhq/contract": "0.5.0"
|
|
60
61
|
},
|
|
61
62
|
"scripts": {
|
|
62
63
|
"build": "tsdown && node scripts/check-dist.mjs",
|
|
@@ -64,6 +65,6 @@
|
|
|
64
65
|
"check:workerd": "node scripts/check-workerd.mjs",
|
|
65
66
|
"test": "pnpm run build && vitest run",
|
|
66
67
|
"typecheck": "tsc --noEmit",
|
|
67
|
-
"lint": "
|
|
68
|
+
"lint": "biome check src scripts tsdown.config.ts"
|
|
68
69
|
}
|
|
69
70
|
}
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { An as InvoiceIssueResponse, Cn as GrantCreateResponse, Gr as CatalogSyncResult, Gt as PlanSetInput, Kn as ManualInvoiceInput, Ln as InvoiceIssueInput, Mn as InvoiceVoidResponse, Or as CheckoutSessionCreated, Pn as ManualInvoiceResponse, Qn as CancelSubscriptionInput, R as WebhookEndpoint, Tr as CheckoutSessionCreateInput, U as WebhookEndpointRotateInput, Un as InvoiceVoidInput, Ur as CatalogSyncInput, V as WebhookEndpointList, Vr as Catalog, Yt as PlanSetResponse, _n as AccountUpsertResponse, at as ProductSettingsPatch, dr as PortalSession, en as ResyncResult, fr as PortalSessionCreateInput, hn as AccountDetail, it as ProductSettings, or as Stats, tr as GrantCreateInput, v as DoctorReport, xn as CheckoutSessionResponse, yn as CancelSubscriptionResponse, z as WebhookEndpointCreateInput, zr as AccountsRefreshResult } from "./index.js";
|
|
2
|
-
import { B as AccountUpsertInput, C as AccountDeleteResponse, D as AccountInclude, I as AccountState } from "./events.js";
|
|
3
|
-
//#region ../contract/src/client.d.ts
|
|
4
|
-
type CallOptions = {
|
|
5
|
-
idempotencyKey?: string;
|
|
6
|
-
actorRef?: string;
|
|
7
|
-
};
|
|
8
|
-
/** `getAccountState` sonucu: 304'te gövde yok, elindeki durum güncel. */
|
|
9
|
-
type AccountStateRead = {
|
|
10
|
-
notModified: false;
|
|
11
|
-
etag: string | null;
|
|
12
|
-
state: AccountState;
|
|
13
|
-
} | {
|
|
14
|
-
notModified: true;
|
|
15
|
-
etag: string;
|
|
16
|
-
};
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region src/retry.d.ts
|
|
19
|
-
type RetryClass = "network" | "5xx" | "429";
|
|
20
|
-
type RetryOptions = {
|
|
21
|
-
/** İlk istekten sonraki en çok tekrar sayısı; 0 kapatır. Varsayılan 2. */
|
|
22
|
-
attempts?: number;
|
|
23
|
-
/** Tekrar denenen hata sınıfları. Varsayılan hepsi. */
|
|
24
|
-
on?: readonly RetryClass[];
|
|
25
|
-
/** Tek bir beklemenin üst sınırı (Retry-After dahil), ms. Varsayılan 5000. */
|
|
26
|
-
maxDelayMs?: number;
|
|
27
|
-
/** Test için: bekleme. Varsayılan `setTimeout`. */
|
|
28
|
-
sleep?: (ms: number) => Promise<void>;
|
|
29
|
-
/** Test için: jitter kaynağı, [0, 1). Varsayılan `Math.random`. */
|
|
30
|
-
random?: () => number;
|
|
31
|
-
};
|
|
32
|
-
//#endregion
|
|
33
|
-
//#region src/steward.d.ts
|
|
34
|
-
type StewardEnv = Readonly<Record<string, string | undefined>>;
|
|
35
|
-
type StewardOptions = {
|
|
36
|
-
/** Varsayılan `STEWARD_URL`. Ürün adresi ve anahtarı ikisi de yoksa ve admin ayarları tamsa istemci yalnızca `admin.*` içindir. */
|
|
37
|
-
baseUrl?: string;
|
|
38
|
-
/** Varsayılan `STEWARD_API_KEY`. */
|
|
39
|
-
apiKey?: string;
|
|
40
|
-
/** Ayarların okunduğu ortam; varsayılan `process.env` (varsa). */
|
|
41
|
-
env?: StewardEnv;
|
|
42
|
-
fetch?: typeof fetch;
|
|
43
|
-
/** Deneme başına zaman aşımı, ms. Varsayılan 10 sn. */
|
|
44
|
-
timeoutMs?: number;
|
|
45
|
-
/**
|
|
46
|
-
* Mutasyonlarda varsayılan `X-Actor-Ref` (ör. `system`); çağrıdaki `actorRef` ezer. İkisi de
|
|
47
|
-
* yoksa mutasyon istek göndermeden `StewardConfigError` (`actor_ref_required`) fırlatır.
|
|
48
|
-
*/
|
|
49
|
-
actorRef?: string;
|
|
50
|
-
/** Varsayılan `STEWARD_ADMIN_URL`; yalnızca `admin.*` için. */
|
|
51
|
-
adminBaseUrl?: string;
|
|
52
|
-
/** Varsayılan `STEWARD_ADMIN_API_KEY`; yalnızca `admin.*` için. */
|
|
53
|
-
adminApiKey?: string;
|
|
54
|
-
retries?: RetryOptions;
|
|
55
|
-
};
|
|
56
|
-
/** Mutasyon çağrısı seçenekleri: `Idempotency-Key` ve `X-Actor-Ref`. */
|
|
57
|
-
type MutationOptions = CallOptions;
|
|
58
|
-
/** Koşullu katalog okuması: 304'te gövde yok, elindeki katalog güncel. */
|
|
59
|
-
type CatalogRead = {
|
|
60
|
-
notModified: false;
|
|
61
|
-
etag: string | null;
|
|
62
|
-
catalog: Catalog;
|
|
63
|
-
} | {
|
|
64
|
-
notModified: true;
|
|
65
|
-
etag: string;
|
|
66
|
-
};
|
|
67
|
-
type InvoiceDocument = {
|
|
68
|
-
contentType: string;
|
|
69
|
-
bytes: Uint8Array;
|
|
70
|
-
};
|
|
71
|
-
/** `GET /v1/me`: anahtarın ürünü ve kapsamı (kurulum doğrulaması). */
|
|
72
|
-
type StewardMe = {
|
|
73
|
-
product: string;
|
|
74
|
-
keyName: string;
|
|
75
|
-
scope: "product" | "admin" | "checkout";
|
|
76
|
-
};
|
|
77
|
-
type Steward = {
|
|
78
|
-
accounts: {
|
|
79
|
-
/** `PUT /v1/accounts/{ref}` — hesabı oluşturur ya da günceller. */
|
|
80
|
-
upsert(ref: string, input: AccountUpsertInput, options?: MutationOptions): Promise<AccountUpsertResponse>;
|
|
81
|
-
get(ref: string, options?: {
|
|
82
|
-
include?: readonly AccountInclude[];
|
|
83
|
-
}): Promise<AccountDetail>;
|
|
84
|
-
/** `GET /v1/accounts/{ref}/state`. */
|
|
85
|
-
state(ref: string): Promise<AccountState>;
|
|
86
|
-
/** Koşullu durum okuması (önbellekler için): `etag` verilir ve durum değişmediyse `{notModified: true}`. */
|
|
87
|
-
readState(ref: string, options?: {
|
|
88
|
-
etag?: string;
|
|
89
|
-
}): Promise<AccountStateRead>;
|
|
90
|
-
/**
|
|
91
|
-
* `PUT /v1/accounts/{ref}/plan` — atomik plan (K6): aktif plan grant'leri tek plan
|
|
92
|
-
* grant'iyle değişir, `planCode: null` kaldırır. Canlı abonelik planı hedefi
|
|
93
|
-
* geçiyorsa `mode: "ensure"` (varsayılan) sonuç + `warnings`, `"strict"`
|
|
94
|
-
* `StewardApiError` `subscription_plan_higher` (409). Yanıttaki `account` önbelleğe `prime` edilebilir.
|
|
95
|
-
*/
|
|
96
|
-
setPlan(ref: string, input: PlanSetInput, options?: MutationOptions): Promise<PlanSetResponse>;
|
|
97
|
-
/** `POST /v1/accounts/{ref}/resync` — saklı durumu `account.state_changed` (`cause: "resync"`) olarak yeniden yayınlar; version değişmez. */
|
|
98
|
-
resync(ref: string, options?: MutationOptions): Promise<ResyncResult>;
|
|
99
|
-
/**
|
|
100
|
-
* `DELETE /v1/accounts/{ref}` — KVKK anonimleştirme (F4e): profil ve kişisel veri silinir,
|
|
101
|
-
* faturalar kalır → `{ref, deletedAt}`. Sonra hesabın her ucu `account_deleted` (410; aynı
|
|
102
|
-
* ref'le `upsert` dahil). Canlı abonelikte `subscription_active` (409; önce `immediate`
|
|
103
|
-
* iptal), formu açık checkout'ta `checkout_in_progress` (409; süresi dolunca tekrar);
|
|
104
|
-
* zaten silinmişse `account_deleted` — iş tekrarında başarı sayılabilir.
|
|
105
|
-
*/
|
|
106
|
-
delete(ref: string, options?: MutationOptions): Promise<AccountDeleteResponse>;
|
|
107
|
-
};
|
|
108
|
-
checkoutSessions: {
|
|
109
|
-
create(ref: string, input: CheckoutSessionCreateInput, options?: MutationOptions): Promise<CheckoutSessionCreated>;
|
|
110
|
-
get(id: string): Promise<CheckoutSessionResponse>;
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* Hosted müşteri portalı (F3a): `POST /v1/accounts/{ref}/portal-sessions` → `{id, url, expiresAt}`.
|
|
114
|
-
* Tarayıcı `url`e yönlendirilir (iptal, ödemeyi yeniden dene, fatura, profil steward'da).
|
|
115
|
-
* Yetki üründe; `actorRef` portalı açan kullanıcı. Durum değişmez, event yok.
|
|
116
|
-
*/
|
|
117
|
-
portalSessions: {
|
|
118
|
-
create(ref: string, input?: PortalSessionCreateInput, options?: MutationOptions): Promise<PortalSession>;
|
|
119
|
-
};
|
|
120
|
-
subscriptions: {
|
|
121
|
-
cancel(id: string, input: CancelSubscriptionInput, options?: MutationOptions): Promise<CancelSubscriptionResponse>;
|
|
122
|
-
};
|
|
123
|
-
grants: {
|
|
124
|
-
create(ref: string, input: GrantCreateInput, options?: MutationOptions): Promise<GrantCreateResponse>;
|
|
125
|
-
revoke(id: string, options?: MutationOptions): Promise<void>;
|
|
126
|
-
};
|
|
127
|
-
invoices: {
|
|
128
|
-
issue(id: string, input: InvoiceIssueInput, options?: MutationOptions): Promise<InvoiceIssueResponse>;
|
|
129
|
-
document(id: string): Promise<InvoiceDocument>;
|
|
130
|
-
/**
|
|
131
|
-
* `POST /v1/accounts/{ref}/invoices` (F4f) — manuel taslak fatura (Enterprise satışı); `issue` ile kesilir.
|
|
132
|
-
* Hesabın fatura profili gerekir (422 `billing_profile_required`). `invoice.created` yayınlanır, durum değişmez.
|
|
133
|
-
*/
|
|
134
|
-
createManual(ref: string, input: ManualInvoiceInput, options?: MutationOptions): Promise<ManualInvoiceResponse>;
|
|
135
|
-
/**
|
|
136
|
-
* `POST /v1/invoices/{id}/void` (F4f) — yalnızca manuel fatura (taslak ya da kesilmiş; numara ve belge kalır).
|
|
137
|
-
* Ödemeli fatura 409 `invoice_has_payment`, zaten void 409 `invoice_not_voidable`. `invoice.voided` yayınlanır.
|
|
138
|
-
*/
|
|
139
|
-
void(id: string, input: InvoiceVoidInput, options?: MutationOptions): Promise<InvoiceVoidResponse>;
|
|
140
|
-
};
|
|
141
|
-
catalog: {
|
|
142
|
-
get(): Promise<Catalog>;
|
|
143
|
-
/** Conditional read (`If-None-Match`); used by `steward catalog push` to report the current version. */
|
|
144
|
-
read(options?: {
|
|
145
|
-
etag?: string;
|
|
146
|
-
}): Promise<CatalogRead>;
|
|
147
|
-
};
|
|
148
|
-
stats(): Promise<Stats>;
|
|
149
|
-
me(): Promise<StewardMe>;
|
|
150
|
-
/**
|
|
151
|
-
* `GET /v1/doctor` — kurulum teşhisi (K11): katalog senkronu, sağlayıcı, ayarlar, hosted
|
|
152
|
-
* sayfa hazırlığı, endpoint teslimat teşhisi (`wrong_secret` = son teslimat 401),
|
|
153
|
-
* dunning ve `warnings[{code, severity, message}]`; `ok` = `error` önemde uyarı yok.
|
|
154
|
-
* Ürün anahtarıyla çalışır (yalnızca admin ayarlı istemcide admin anahtarıyla). Sağlayıcı
|
|
155
|
-
* sağlığı yanıtı veren pod'un gözlemidir.
|
|
156
|
-
*/
|
|
157
|
-
doctor(): Promise<DoctorReport>;
|
|
158
|
-
/** Admin kapsamlı uçlar: `adminBaseUrl`/`adminApiKey` (ya da `STEWARD_ADMIN_*`) gerekir. */
|
|
159
|
-
admin: {
|
|
160
|
-
catalog: {
|
|
161
|
-
sync(input: CatalogSyncInput, options?: MutationOptions): Promise<CatalogSyncResult>;
|
|
162
|
-
};
|
|
163
|
-
accounts: {
|
|
164
|
-
refresh(options?: MutationOptions): Promise<AccountsRefreshResult>;
|
|
165
|
-
};
|
|
166
|
-
settings: {
|
|
167
|
-
/** `GET /v1/admin/settings`. */
|
|
168
|
-
get(): Promise<ProductSettings>;
|
|
169
|
-
/** `PATCH /v1/admin/settings`: verilmeyen alan değişmez, `null` temizler, `branding` sığ birleşir. */
|
|
170
|
-
update(patch: ProductSettingsPatch, options?: MutationOptions): Promise<ProductSettings>;
|
|
171
|
-
};
|
|
172
|
-
/**
|
|
173
|
-
* Webhook endpoint'leri. Sırrı çağıran üretir (`generateWebhookSecret()`) ve
|
|
174
|
-
* saklar; steward hiçbir yanıtta geri vermez.
|
|
175
|
-
*/
|
|
176
|
-
endpoints: {
|
|
177
|
-
list(): Promise<WebhookEndpointList>;
|
|
178
|
-
/** `eventTypes` verilmezse yalnızca `account.state_changed`. */
|
|
179
|
-
create(input: WebhookEndpointCreateInput, options?: MutationOptions): Promise<WebhookEndpoint>;
|
|
180
|
-
/** Yeni sır; eskisi ürün geçişi bitene kadar ikinci imza olarak kalır. */
|
|
181
|
-
rotate(id: string, input: WebhookEndpointRotateInput, options?: MutationOptions): Promise<WebhookEndpoint>;
|
|
182
|
-
/** `status: inactive`; geri alınmaz (yeni endpoint açılır). */
|
|
183
|
-
deactivate(id: string, options?: MutationOptions): Promise<WebhookEndpoint>;
|
|
184
|
-
};
|
|
185
|
-
};
|
|
186
|
-
};
|
|
187
|
-
declare function createSteward(options?: StewardOptions): Steward;
|
|
188
|
-
//#endregion
|
|
189
|
-
export { StewardEnv as a, createSteward as c, AccountStateRead as d, Steward as i, RetryClass as l, InvoiceDocument as n, StewardMe as o, MutationOptions as r, StewardOptions as s, CatalogRead as t, RetryOptions as u };
|