11import "./polyfill" ;
2- import { Polar as PolarSdk } from "@polar-sh/sdk" ;
2+ import { PolarCore } from "@polar-sh/sdk/core.js" ;
3+ import { customersCreate } from "@polar-sh/sdk/funcs/customersCreate.js" ;
4+ import { checkoutsCreate } from "@polar-sh/sdk/funcs/checkoutsCreate.js" ;
5+ import { customerSessionsCreate } from "@polar-sh/sdk/funcs/customerSessionsCreate.js" ;
6+ import { subscriptionsUpdate } from "@polar-sh/sdk/funcs/subscriptionsUpdate.js" ;
7+
38import type { Checkout } from "@polar-sh/sdk/models/components/checkout.js" ;
49import type { WebhookProductCreatedPayload } from "@polar-sh/sdk/models/components/webhookproductcreatedpayload.js" ;
510import type { WebhookProductUpdatedPayload } from "@polar-sh/sdk/models/components/webhookproductupdatedpayload.js" ;
@@ -48,7 +53,7 @@ export class Polar<
4853 DataModel extends GenericDataModel = GenericDataModel ,
4954 Products extends Record < string , string > = Record < string , string > ,
5055> {
51- public sdk : PolarSdk ;
56+ public polar : PolarCore ;
5257 public products : Products ;
5358 private organizationToken : string ;
5459 private webhookSecret : string ;
@@ -77,7 +82,7 @@ export class Polar<
7782 ( process . env [ "POLAR_SERVER" ] as "sandbox" | "production" ) ??
7883 "sandbox" ;
7984
80- this . sdk = new PolarSdk ( {
85+ this . polar = new PolarCore ( {
8186 accessToken : this . organizationToken ,
8287 server : this . server ,
8388 } ) ;
@@ -116,20 +121,23 @@ export class Polar<
116121 const customerId =
117122 dbCustomer ?. id ||
118123 (
119- await this . sdk . customers . create ( {
124+ await customersCreate ( this . polar , {
120125 email,
121126 metadata : {
122127 userId,
123128 } ,
124129 } )
125- ) . id ;
130+ ) . value ?. id ;
131+ if ( ! customerId ) {
132+ throw new Error ( "Customer not created" ) ;
133+ }
126134 if ( ! dbCustomer ) {
127135 await ctx . runMutation ( this . component . lib . insertCustomer , {
128136 id : customerId ,
129137 userId,
130138 } ) ;
131139 }
132- return this . sdk . checkouts . create ( {
140+ const checkout = await checkoutsCreate ( this . polar , {
133141 allowDiscountCodes : true ,
134142 customerId,
135143 embedOrigin : origin ,
@@ -138,6 +146,10 @@ export class Polar<
138146 ? { products : productIds }
139147 : { products : productIds } ) ,
140148 } ) ;
149+ if ( ! checkout . value ) {
150+ throw new Error ( "Checkout not created" ) ;
151+ }
152+ return checkout . value ;
141153 }
142154 async createCustomerPortalSession (
143155 ctx : GenericActionCtx < DataModel > ,
@@ -152,11 +164,14 @@ export class Polar<
152164 throw new Error ( "Customer not found" ) ;
153165 }
154166
155- const session = await this . sdk . customerSessions . create ( {
167+ const session = await customerSessionsCreate ( this . polar , {
156168 customerId : customer . id ,
157169 } ) ;
170+ if ( ! session . value ) {
171+ throw new Error ( "Customer session not created" ) ;
172+ }
158173
159- return { url : session . customerPortalUrl } ;
174+ return { url : session . value . customerPortalUrl } ;
160175 }
161176 listProducts (
162177 ctx : RunQueryCtx ,
@@ -209,12 +224,16 @@ export class Polar<
209224 if ( subscription . productId === productId ) {
210225 throw new Error ( "Subscription already on this product" ) ;
211226 }
212- await this . sdk . subscriptions . update ( {
227+ const updatedSubscription = await subscriptionsUpdate ( this . polar , {
213228 id : subscription . id ,
214229 subscriptionUpdate : {
215230 productId,
216231 } ,
217232 } ) ;
233+ if ( ! updatedSubscription . value ) {
234+ throw new Error ( "Subscription not updated" ) ;
235+ }
236+ return updatedSubscription . value ;
218237 }
219238 async cancelSubscription (
220239 ctx : RunActionCtx ,
@@ -228,13 +247,17 @@ export class Polar<
228247 if ( subscription . status !== "active" ) {
229248 throw new Error ( "Subscription is not active" ) ;
230249 }
231- await this . sdk . subscriptions . update ( {
250+ const updatedSubscription = await subscriptionsUpdate ( this . polar , {
232251 id : subscription . id ,
233252 subscriptionUpdate : {
234253 cancelAtPeriodEnd : revokeImmediately ? undefined : true ,
235254 revoke : revokeImmediately ? true : undefined ,
236255 } ,
237256 } ) ;
257+ if ( ! updatedSubscription . value ) {
258+ throw new Error ( "Subscription not updated" ) ;
259+ }
260+ return updatedSubscription . value ;
238261 }
239262 api ( ) {
240263 return {
0 commit comments