-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c72145d
commit 119ef68
Showing
12 changed files
with
605 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/bsync/src/db/migrations/20241205T030533572Z-subs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Kysely, sql } from 'kysely' | ||
|
||
export async function up(db: Kysely<unknown>): Promise<void> { | ||
await db.schema | ||
.createTable('subs_op') | ||
.addColumn('id', 'bigserial', (col) => col.primaryKey()) | ||
.addColumn('actorDid', 'varchar', (col) => col.notNull()) | ||
.addColumn('entitlements', 'jsonb', (col) => col.notNull()) | ||
.addColumn('createdAt', 'timestamptz', (col) => | ||
col.notNull().defaultTo(sql`CURRENT_TIMESTAMP`), | ||
) | ||
.execute() | ||
await db.schema | ||
.createTable('subs_item') | ||
.addColumn('actorDid', 'varchar', (col) => col.primaryKey()) | ||
.addColumn('entitlements', 'jsonb', (col) => col.notNull()) | ||
.addColumn('fromId', 'bigint', (col) => col.notNull()) | ||
.execute() | ||
} | ||
|
||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
await db.schema.dropTable('subs_item').execute() | ||
await db.schema.dropTable('subs_op').execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ColumnType, Selectable } from 'kysely' | ||
|
||
export interface SubsItem { | ||
actorDid: string | ||
// https://github.com/kysely-org/kysely/issues/137 | ||
entitlements: ColumnType<string[], string, string> | ||
fromId: number | ||
} | ||
|
||
export type SubsItemEntry = Selectable<SubsItem> | ||
|
||
export const tableName = 'subs_item' | ||
|
||
export type PartialDB = { [tableName]: SubsItem } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ColumnType, GeneratedAlways, Selectable } from 'kysely' | ||
|
||
export interface SubsOp { | ||
id: GeneratedAlways<number> | ||
actorDid: string | ||
// https://github.com/kysely-org/kysely/issues/137 | ||
entitlements: ColumnType<string[], string, string> | ||
createdAt: GeneratedAlways<Date> | ||
} | ||
|
||
export type SubsOpEntry = Selectable<SubsOp> | ||
|
||
export const tableName = 'subs_op' | ||
|
||
export type PartialDB = { [tableName]: SubsOp } | ||
|
||
export const createSubsOpChannel = 'subs_op_create' // used with listen/notify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './revenueCatClient' | ||
export * from './revenueCatWebhookHandler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
type Config = { | ||
v1ApiKey: string | ||
v1ApiUrl: string | ||
webhookAuthorization: string | ||
} | ||
|
||
// Reference: https://www.revenuecat.com/docs/api-v1#tag/customers | ||
export type GetSubscriberResponse = { | ||
subscriber: Subscriber | ||
} | ||
|
||
export type Subscriber = { | ||
entitlements: { | ||
[entitlementIdentifier: string]: Entitlement | ||
} | ||
} | ||
|
||
export type Entitlement = { | ||
expires_date: string | ||
} | ||
|
||
export class RevenueCatClient { | ||
private v1ApiKey: string | ||
private v1ApiUrl: string | ||
private webhookAuthorization: string | ||
|
||
constructor({ v1ApiKey, v1ApiUrl, webhookAuthorization }: Config) { | ||
this.v1ApiKey = v1ApiKey | ||
this.v1ApiUrl = v1ApiUrl | ||
this.webhookAuthorization = webhookAuthorization | ||
} | ||
|
||
private async fetch<T extends object>( | ||
path: string, | ||
method: string = 'GET', | ||
): Promise<T> { | ||
const url = new URL(path, this.v1ApiUrl) | ||
const res = await fetch(url, { | ||
method, | ||
headers: { | ||
Authorization: `Bearer ${this.v1ApiKey}`, | ||
}, | ||
}) | ||
|
||
if (!res.ok) { | ||
throw new Error(`Failed to fetch ${path}: ${res.statusText}`) | ||
} | ||
|
||
return res.json() as T | ||
} | ||
|
||
private getSubscriber(did: string): Promise<GetSubscriberResponse> { | ||
return this.fetch<GetSubscriberResponse>( | ||
`/subscribers/${encodeURIComponent(did)}`, | ||
) | ||
} | ||
|
||
async getEntitlementIdentifiers(did: string): Promise<string[]> { | ||
const { subscriber } = await this.getSubscriber(did) | ||
|
||
const now = Date.now() | ||
return Object.entries(subscriber.entitlements) | ||
.filter( | ||
([_, entitlement]) => | ||
now < new Date(entitlement.expires_date).valueOf(), | ||
) | ||
.map(([entitlementIdentifier]) => entitlementIdentifier) | ||
} | ||
|
||
isWebhookAuthorizationValid(authorization: string | undefined): boolean { | ||
return authorization === this.webhookAuthorization | ||
} | ||
} |
Oops, something went wrong.