Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@updatedev/js",
"version": "0.2.2",
"version": "0.3.0",
"description": "Update JavaScript SDK",
"main": "./dist/index.js",
"files": [
Expand Down
3 changes: 1 addition & 2 deletions src/providers/supabase/UpdateBillingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
81 changes: 81 additions & 0 deletions src/providers/supabase/UpdateEntitlementClient.ts
Original file line number Diff line number Diff line change
@@ -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<ListEntitlementsResponse> {
const { data, error } = await this.requestClient.request<string[]>({
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<CheckEntitlementResponse> {
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,
};
}
}
6 changes: 6 additions & 0 deletions src/providers/supabase/UpdateSupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,6 +33,7 @@ export class UpdateSupabaseClient<
realtime: SupabaseClient['realtime'];
billing: UpdateBillingClient;
organization: UpdateOrganizationClient;
entitlements: UpdateEntitlementClient;

constructor(
protected readonly apiKey: string,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/types/entitlement.ts
Original file line number Diff line number Diff line change
@@ -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;
};
};
1 change: 1 addition & 0 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ENVIRONMENT_HEADER = 'X-Update-Environment';