samsar-js 0.48.19 → 0.48.20
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/README.md +21 -0
- package/dist/index.d.ts +67 -2
- package/dist/index.js +72 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -512,6 +512,7 @@ Video model support notes:
|
|
|
512
512
|
Upcoming `/v2` omni route adapters:
|
|
513
513
|
- `/v2` is additive; `/v1` is not deprecated.
|
|
514
514
|
- `createV2VideoFromText`, `createV2VideoFromImageList`, `updateV2VideoOutroImage`, `addV2VideoOutroImage`, `getV2Status`, `getV2Credits`, `listV2Requests`, and `createV2Session` call the new omni route surface.
|
|
515
|
+
- Programmatic user billing helpers include `createV2UserRechargeCredits`, `refreshV2UserToken`, `getV2UserCredits`, `getV2UserUsageLogs`, and `getV2UserPaymentStatus`.
|
|
515
516
|
- Omit `externalUser` for internal account billing, pass `externalUser` to scope an external user with the account API key, or authenticate the client directly with an external-user auth token/API key.
|
|
516
517
|
|
|
517
518
|
```ts
|
|
@@ -535,6 +536,26 @@ const v2Status = await platform.getV2Status(v2Video.data.request_id!);
|
|
|
535
536
|
console.log(v2Status.data.status);
|
|
536
537
|
```
|
|
537
538
|
|
|
539
|
+
Programmatic user recharge and OAuth-style refresh token rotation:
|
|
540
|
+
|
|
541
|
+
```ts
|
|
542
|
+
const publicClient = new SamsarClient({});
|
|
543
|
+
|
|
544
|
+
const checkout = await publicClient.createV2UserRechargeCredits({
|
|
545
|
+
amount: 25,
|
|
546
|
+
email: 'customer@example.com',
|
|
547
|
+
redirect_url: 'https://example.com/samsar/callback',
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
// After the Stripe webhook calls your redirect_url with authToken, refreshToken, and expiryDate:
|
|
551
|
+
const userClient = new SamsarClient({ apiKey: authToken });
|
|
552
|
+
const refreshed = await userClient.refreshV2UserToken(refreshToken);
|
|
553
|
+
|
|
554
|
+
localStorage.setItem('authToken', refreshed.data.authToken);
|
|
555
|
+
localStorage.setItem('refreshToken', refreshed.data.refreshToken);
|
|
556
|
+
localStorage.setItem('expiryDate', refreshed.data.expiryDate);
|
|
557
|
+
```
|
|
558
|
+
|
|
538
559
|
Each method returns `{ data, status, headers, creditsCharged, creditsRemaining, raw }`. Non-2xx responses throw `SamsarRequestError` containing status, body, and credit headers (if present).
|
|
539
560
|
|
|
540
561
|
## Billing notes
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Respo
|
|
|
2
2
|
type QueryValue = string | number | boolean | null | undefined;
|
|
3
3
|
type QueryParams = Record<string, QueryValue>;
|
|
4
4
|
export interface SamsarClientOptions {
|
|
5
|
-
apiKey
|
|
5
|
+
apiKey?: string;
|
|
6
6
|
baseUrl?: string;
|
|
7
7
|
timeoutMs?: number;
|
|
8
8
|
fetch?: FetchLike;
|
|
@@ -1334,6 +1334,61 @@ export interface V2RequestsListResponse {
|
|
|
1334
1334
|
externalUser?: ExternalUserSummary | null;
|
|
1335
1335
|
[key: string]: unknown;
|
|
1336
1336
|
}
|
|
1337
|
+
export interface V2UserRechargeCreditsRequest {
|
|
1338
|
+
amount: number;
|
|
1339
|
+
email: string;
|
|
1340
|
+
redirect_url?: string;
|
|
1341
|
+
redirectUrl?: string;
|
|
1342
|
+
[key: string]: unknown;
|
|
1343
|
+
}
|
|
1344
|
+
export interface V2UserRechargeCreditsResponse {
|
|
1345
|
+
url: string;
|
|
1346
|
+
checkoutSessionId?: string | null;
|
|
1347
|
+
paymentStatusEndpoint?: string;
|
|
1348
|
+
amount: number;
|
|
1349
|
+
amountCents: number;
|
|
1350
|
+
credits: number;
|
|
1351
|
+
currency: string;
|
|
1352
|
+
redirectUrl?: string;
|
|
1353
|
+
[key: string]: unknown;
|
|
1354
|
+
}
|
|
1355
|
+
export interface V2UserTokenRefreshRequest {
|
|
1356
|
+
refreshToken?: string;
|
|
1357
|
+
refresh_token?: string;
|
|
1358
|
+
}
|
|
1359
|
+
export interface V2UserTokenResponse {
|
|
1360
|
+
tokenType?: 'Bearer' | string;
|
|
1361
|
+
authToken: string;
|
|
1362
|
+
refreshToken: string;
|
|
1363
|
+
expiryDate: string;
|
|
1364
|
+
expiresInSeconds?: number;
|
|
1365
|
+
refreshTokenExpiresAt?: string;
|
|
1366
|
+
[key: string]: unknown;
|
|
1367
|
+
}
|
|
1368
|
+
export interface UsageLogItem {
|
|
1369
|
+
id?: string;
|
|
1370
|
+
source?: string;
|
|
1371
|
+
credits?: number;
|
|
1372
|
+
balanceAfter?: number | null;
|
|
1373
|
+
metadata?: Record<string, unknown>;
|
|
1374
|
+
direction?: string;
|
|
1375
|
+
createdAt?: string;
|
|
1376
|
+
updatedAt?: string;
|
|
1377
|
+
[key: string]: unknown;
|
|
1378
|
+
}
|
|
1379
|
+
export interface UsageLogsResponse {
|
|
1380
|
+
items: UsageLogItem[];
|
|
1381
|
+
pagination?: {
|
|
1382
|
+
page?: number;
|
|
1383
|
+
pageSize?: number;
|
|
1384
|
+
totalItems?: number;
|
|
1385
|
+
totalPages?: number;
|
|
1386
|
+
hasNextPage?: boolean;
|
|
1387
|
+
hasPreviousPage?: boolean;
|
|
1388
|
+
[key: string]: unknown;
|
|
1389
|
+
};
|
|
1390
|
+
[key: string]: unknown;
|
|
1391
|
+
}
|
|
1337
1392
|
export interface ExternalArchiveResponse {
|
|
1338
1393
|
request?: ExternalRequestSummary | null;
|
|
1339
1394
|
external_user?: ExternalUserSummary | null;
|
|
@@ -1516,7 +1571,7 @@ export declare class SamsarRequestError extends Error {
|
|
|
1516
1571
|
constructor(message: string, init: SamsarErrorInit);
|
|
1517
1572
|
}
|
|
1518
1573
|
export declare class SamsarClient {
|
|
1519
|
-
private readonly apiKey
|
|
1574
|
+
private readonly apiKey?;
|
|
1520
1575
|
private readonly baseUrl;
|
|
1521
1576
|
private readonly timeoutMs;
|
|
1522
1577
|
private readonly fetchFn;
|
|
@@ -1535,7 +1590,17 @@ export declare class SamsarClient {
|
|
|
1535
1590
|
getV2<T = Record<string, unknown>>(path: string, options?: V2RequestOptions): Promise<SamsarResult<T>>;
|
|
1536
1591
|
createV2Session(options?: V2RequestOptions): Promise<SamsarResult<V2SessionResponse>>;
|
|
1537
1592
|
getV2Credits(options?: V2RequestOptions): Promise<SamsarResult<CreditsBalanceResponse | ExternalCreditsBalanceResponse>>;
|
|
1593
|
+
getV2UserCredits(options?: V2RequestOptions): Promise<SamsarResult<CreditsBalanceResponse>>;
|
|
1594
|
+
getV2UserUsageLogs(options?: V2RequestOptions & {
|
|
1595
|
+
page?: number;
|
|
1596
|
+
pageSize?: number;
|
|
1597
|
+
limit?: number;
|
|
1598
|
+
}): Promise<SamsarResult<UsageLogsResponse>>;
|
|
1538
1599
|
listV2Requests(options?: V2RequestOptions): Promise<SamsarResult<V2RequestsListResponse>>;
|
|
1600
|
+
createV2UserRechargeCredits(payload: V2UserRechargeCreditsRequest, options?: V2RequestOptions): Promise<SamsarResult<V2UserRechargeCreditsResponse>>;
|
|
1601
|
+
refreshV2UserToken(payload: string | V2UserTokenRefreshRequest, options?: V2RequestOptions): Promise<SamsarResult<V2UserTokenResponse>>;
|
|
1602
|
+
refreshV2UserAuthToken(payload: string | V2UserTokenRefreshRequest, options?: V2RequestOptions): Promise<SamsarResult<V2UserTokenResponse>>;
|
|
1603
|
+
getV2UserPaymentStatus(payload: PaymentStatusRequest, options?: V2RequestOptions): Promise<SamsarResult<PaymentStatusResponse>>;
|
|
1539
1604
|
createV2LoginToken(options?: V2RequestOptions & {
|
|
1540
1605
|
redirect?: string;
|
|
1541
1606
|
}): Promise<SamsarResult<CreateLoginTokenResponse | ExternalCreateLoginTokenResponse>>;
|
package/dist/index.js
CHANGED
|
@@ -270,10 +270,7 @@ function normalizeSessionPublicationInput(input, context) {
|
|
|
270
270
|
}
|
|
271
271
|
export class SamsarClient {
|
|
272
272
|
constructor(options) {
|
|
273
|
-
|
|
274
|
-
throw new Error('apiKey is required to initialize SamsarClient');
|
|
275
|
-
}
|
|
276
|
-
this.apiKey = options.apiKey;
|
|
273
|
+
this.apiKey = options?.apiKey?.trim() || undefined;
|
|
277
274
|
this.baseUrl = trimTrailingSlash(options.baseUrl ?? DEFAULT_BASE_URL);
|
|
278
275
|
this.timeoutMs = options.timeoutMs ?? 30000;
|
|
279
276
|
this.fetchFn = options.fetch ?? globalThis.fetch;
|
|
@@ -307,9 +304,79 @@ export class SamsarClient {
|
|
|
307
304
|
async getV2Credits(options) {
|
|
308
305
|
return this.getV2('credits', options);
|
|
309
306
|
}
|
|
307
|
+
async getV2UserCredits(options) {
|
|
308
|
+
return this.getV2('user/credits', options);
|
|
309
|
+
}
|
|
310
|
+
async getV2UserUsageLogs(options) {
|
|
311
|
+
const query = {
|
|
312
|
+
...(options?.query ?? {}),
|
|
313
|
+
};
|
|
314
|
+
if (options?.page !== undefined) {
|
|
315
|
+
query.page = options.page;
|
|
316
|
+
}
|
|
317
|
+
if (options?.pageSize !== undefined) {
|
|
318
|
+
query.pageSize = options.pageSize;
|
|
319
|
+
}
|
|
320
|
+
if (options?.limit !== undefined) {
|
|
321
|
+
query.limit = options.limit;
|
|
322
|
+
}
|
|
323
|
+
return this.getV2('user/usage/logs', {
|
|
324
|
+
...(options ?? {}),
|
|
325
|
+
query,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
310
328
|
async listV2Requests(options) {
|
|
311
329
|
return this.getV2('requests', options);
|
|
312
330
|
}
|
|
331
|
+
async createV2UserRechargeCredits(payload, options) {
|
|
332
|
+
const amount = Number(payload?.amount);
|
|
333
|
+
if (!Number.isFinite(amount) || amount <= 0) {
|
|
334
|
+
throw new Error('amount must be a positive dollar amount');
|
|
335
|
+
}
|
|
336
|
+
if (!payload?.email || typeof payload.email !== 'string') {
|
|
337
|
+
throw new Error('email is required');
|
|
338
|
+
}
|
|
339
|
+
const redirectUrl = payload.redirect_url ?? payload.redirectUrl;
|
|
340
|
+
if (!redirectUrl || typeof redirectUrl !== 'string') {
|
|
341
|
+
throw new Error('redirect_url is required');
|
|
342
|
+
}
|
|
343
|
+
return this.postV2('user/recharge_credits', {
|
|
344
|
+
...payload,
|
|
345
|
+
amount,
|
|
346
|
+
email: payload.email.trim(),
|
|
347
|
+
redirect_url: redirectUrl.trim(),
|
|
348
|
+
}, options);
|
|
349
|
+
}
|
|
350
|
+
async refreshV2UserToken(payload, options) {
|
|
351
|
+
const refreshToken = typeof payload === 'string'
|
|
352
|
+
? payload
|
|
353
|
+
: (payload?.refreshToken ?? payload?.refresh_token);
|
|
354
|
+
if (!refreshToken || typeof refreshToken !== 'string') {
|
|
355
|
+
throw new Error('refreshToken is required');
|
|
356
|
+
}
|
|
357
|
+
return this.postV2('user/refresh_token', { refreshToken: refreshToken.trim() }, options);
|
|
358
|
+
}
|
|
359
|
+
async refreshV2UserAuthToken(payload, options) {
|
|
360
|
+
return this.refreshV2UserToken(payload, options);
|
|
361
|
+
}
|
|
362
|
+
async getV2UserPaymentStatus(payload, options) {
|
|
363
|
+
const query = {
|
|
364
|
+
...(options?.query ?? {}),
|
|
365
|
+
};
|
|
366
|
+
if (payload?.checkoutSessionId) {
|
|
367
|
+
query.checkoutSessionId = payload.checkoutSessionId;
|
|
368
|
+
}
|
|
369
|
+
if (payload?.paymentIntentId) {
|
|
370
|
+
query.paymentIntentId = payload.paymentIntentId;
|
|
371
|
+
}
|
|
372
|
+
if (payload?.setupIntentId) {
|
|
373
|
+
query.setupIntentId = payload.setupIntentId;
|
|
374
|
+
}
|
|
375
|
+
return this.getV2('user/payment_status', {
|
|
376
|
+
...(options ?? {}),
|
|
377
|
+
query,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
313
380
|
async createV2LoginToken(options) {
|
|
314
381
|
const body = options?.redirect ? { redirect: options.redirect } : {};
|
|
315
382
|
return this.postV2('create_login_token', body, options);
|
|
@@ -1620,7 +1687,7 @@ export class SamsarClient {
|
|
|
1620
1687
|
}
|
|
1621
1688
|
buildHeaders(options) {
|
|
1622
1689
|
const headers = {
|
|
1623
|
-
Authorization: `Bearer ${this.apiKey}
|
|
1690
|
+
Authorization: this.apiKey ? `Bearer ${this.apiKey}` : undefined,
|
|
1624
1691
|
'Content-Type': options.body ? 'application/json' : undefined,
|
|
1625
1692
|
'x-external-user-api-key': options.externalUserApiKey ?? this.externalUserApiKey,
|
|
1626
1693
|
...this.defaultHeaders,
|