diff --git a/package.json b/package.json index 80285ff..cafee26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@updatedev/js", - "version": "0.2.2", + "version": "0.3.0", "description": "Update JavaScript SDK", "main": "./dist/index.js", "files": [ diff --git a/src/providers/supabase/UpdateBillingClient.ts b/src/providers/supabase/UpdateBillingClient.ts index ff0ff6c..a99b236 100644 --- a/src/providers/supabase/UpdateBillingClient.ts +++ b/src/providers/supabase/UpdateBillingClient.ts @@ -10,8 +10,7 @@ import { CreateCheckoutSessionOptions, } from '../../types/billing'; import { UpdateClientBillingOptions } from '../../types/options'; - -const ENVIRONMENT_HEADER = 'X-Update-Billing-Environment'; +import { ENVIRONMENT_HEADER } from '../../types/internal'; export class UpdateBillingClient { private environment: string; diff --git a/src/providers/supabase/UpdateEntitlementClient.ts b/src/providers/supabase/UpdateEntitlementClient.ts new file mode 100644 index 0000000..5f910c5 --- /dev/null +++ b/src/providers/supabase/UpdateEntitlementClient.ts @@ -0,0 +1,81 @@ +import { RequestClient } from '../../utils/request'; +import { UpdateClientBillingOptions } from '../../types/options'; +import { + CheckEntitlementResponse, + ListEntitlementsResponse, +} from '../../types/entitlement'; +import { ENVIRONMENT_HEADER } from '../../types/internal'; + +export class UpdateEntitlementClient { + private environment: string; + private requestClient: RequestClient; + + constructor({ + environment, + requestClient, + }: UpdateClientBillingOptions & { + requestClient: RequestClient; + }) { + this.requestClient = requestClient; + this.environment = environment ?? 'test'; + } + + async list(): Promise { + const { data, error } = await this.requestClient.request({ + endpoint: '/entitlements', + method: 'GET', + headers: { + [ENVIRONMENT_HEADER]: this.environment, + }, + }); + + if (error) { + return { + data: { + entitlements: null, + }, + error: { + message: error.message, + }, + }; + } + + return { + data: { + entitlements: data, + }, + error: null, + }; + } + + async check(entitlement: string): Promise { + const { data, error } = await this.requestClient.request<{ + has_access: boolean; + }>({ + endpoint: '/entitlements/check', + method: 'POST', + body: { + entitlement, + }, + headers: { + [ENVIRONMENT_HEADER]: this.environment, + }, + }); + + if (error) { + return { + data: null, + error: { + message: error.message, + }, + }; + } + + return { + data: { + hasAccess: data.has_access, + }, + error: null, + }; + } +} diff --git a/src/providers/supabase/UpdateSupabaseClient.ts b/src/providers/supabase/UpdateSupabaseClient.ts index 08ab2f7..6a9d914 100644 --- a/src/providers/supabase/UpdateSupabaseClient.ts +++ b/src/providers/supabase/UpdateSupabaseClient.ts @@ -12,6 +12,7 @@ import { RequestClient } from '../../utils/request'; import { getAll, setAll } from '../../utils'; import { StorageClient, StorageOptions } from '../../utils/storage'; import { UpdateSupabaseClientOptions } from './types/options'; +import { UpdateEntitlementClient } from './UpdateEntitlementClient'; export class UpdateSupabaseClient< Database = any, @@ -32,6 +33,7 @@ export class UpdateSupabaseClient< realtime: SupabaseClient['realtime']; billing: UpdateBillingClient; organization: UpdateOrganizationClient; + entitlements: UpdateEntitlementClient; constructor( protected readonly apiKey: string, @@ -64,6 +66,10 @@ export class UpdateSupabaseClient< ...options?.billing, requestClient, }); + this.entitlements = new UpdateEntitlementClient({ + environment: options?.billing?.environment, + requestClient, + }); this.organization = new UpdateOrganizationClient({ requestClient, storageClient: this.storageClient, diff --git a/src/types/entitlement.ts b/src/types/entitlement.ts new file mode 100644 index 0000000..f9046a7 --- /dev/null +++ b/src/types/entitlement.ts @@ -0,0 +1,29 @@ +export type ListEntitlementsResponse = + | { + data: { + entitlements: string[]; + }; + error: null; + } + | { + data: { + entitlements: null; + }; + error: { + message: string; + }; + }; + +export type CheckEntitlementResponse = + | { + data: { + hasAccess: boolean; + }; + error: null; + } + | { + data: null; + error: { + message: string; + }; + }; diff --git a/src/types/internal.ts b/src/types/internal.ts new file mode 100644 index 0000000..ac9e61a --- /dev/null +++ b/src/types/internal.ts @@ -0,0 +1 @@ +export const ENVIRONMENT_HEADER = 'X-Update-Environment';