@blinkdotnew/sdk 2.3.5 → 2.3.7

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/dist/index.d.mts CHANGED
@@ -946,6 +946,10 @@ declare class HttpClient {
946
946
  dbPost<T = any>(table: string, body: any, options?: {
947
947
  returning?: boolean;
948
948
  }): Promise<BlinkResponse<T | T[]>>;
949
+ dbUpsert<T = any>(table: string, body: any, options?: {
950
+ onConflict?: string;
951
+ returning?: boolean;
952
+ }): Promise<BlinkResponse<T | T[]>>;
949
953
  dbPatch<T = any>(table: string, body: any, searchParams?: Record<string, string>, options?: {
950
954
  returning?: boolean;
951
955
  }): Promise<BlinkResponse<T[]>>;
package/dist/index.d.ts CHANGED
@@ -946,6 +946,10 @@ declare class HttpClient {
946
946
  dbPost<T = any>(table: string, body: any, options?: {
947
947
  returning?: boolean;
948
948
  }): Promise<BlinkResponse<T | T[]>>;
949
+ dbUpsert<T = any>(table: string, body: any, options?: {
950
+ onConflict?: string;
951
+ returning?: boolean;
952
+ }): Promise<BlinkResponse<T | T[]>>;
949
953
  dbPatch<T = any>(table: string, body: any, searchParams?: Record<string, string>, options?: {
950
954
  returning?: boolean;
951
955
  }): Promise<BlinkResponse<T[]>>;
package/dist/index.js CHANGED
@@ -531,6 +531,28 @@ var HttpClient = class {
531
531
  data: convertedData
532
532
  };
533
533
  }
534
+ async dbUpsert(table, body, options = {}) {
535
+ const headers = {};
536
+ if (options.returning) {
537
+ headers.Prefer = "return=representation";
538
+ }
539
+ const convertedBody = convertKeysToSnakeCase(body);
540
+ const onConflict = options.onConflict || "id";
541
+ const response = await this.request(
542
+ `/api/db/${this.projectId}/rest/v1/${table}`,
543
+ {
544
+ method: "POST",
545
+ body: convertedBody,
546
+ headers,
547
+ searchParams: { on_conflict: onConflict }
548
+ }
549
+ );
550
+ const convertedData = convertKeysToCamelCase(response.data);
551
+ return {
552
+ ...response,
553
+ data: convertedData
554
+ };
555
+ }
534
556
  async dbPatch(table, body, searchParams, options = {}) {
535
557
  const headers = {};
536
558
  if (options.returning) {
@@ -605,7 +627,7 @@ var HttpClient = class {
605
627
  const blobWithType = options.contentType ? new Blob([file], { type: options.contentType }) : file;
606
628
  formData.append("file", blobWithType);
607
629
  } else if (typeof Buffer !== "undefined" && file instanceof Buffer) {
608
- const blob = new Blob([file], { type: options.contentType || "application/octet-stream" });
630
+ const blob = new Blob([new Uint8Array(file)], { type: options.contentType || "application/octet-stream" });
609
631
  formData.append("file", blob);
610
632
  } else {
611
633
  throw new BlinkValidationError("Unsupported file type");
@@ -1731,7 +1753,7 @@ var BlinkAuth = class {
1731
1753
  token_type: result.token_type,
1732
1754
  expires_in: result.expires_in,
1733
1755
  refresh_expires_in: result.refresh_expires_in
1734
- }, true);
1756
+ }, true, result.user);
1735
1757
  return result.user;
1736
1758
  } catch (error) {
1737
1759
  if (error instanceof BlinkAuthError) {
@@ -1771,7 +1793,7 @@ var BlinkAuth = class {
1771
1793
  token_type: result.token_type,
1772
1794
  expires_in: result.expires_in,
1773
1795
  refresh_expires_in: result.refresh_expires_in
1774
- }, true);
1796
+ }, true, result.user);
1775
1797
  return result.user;
1776
1798
  } catch (error) {
1777
1799
  if (error instanceof BlinkAuthError) {
@@ -2504,7 +2526,7 @@ var BlinkAuth = class {
2504
2526
  token_type: result.token_type,
2505
2527
  expires_in: result.expires_in,
2506
2528
  refresh_expires_in: result.refresh_expires_in
2507
- }, true);
2529
+ }, true, result.user);
2508
2530
  return result.user;
2509
2531
  } catch (error) {
2510
2532
  if (error instanceof BlinkAuthError) {
@@ -2901,7 +2923,7 @@ var BlinkAuth = class {
2901
2923
  return false;
2902
2924
  }
2903
2925
  }
2904
- async setTokens(tokens, persist) {
2926
+ async setTokens(tokens, persist, knownUser) {
2905
2927
  const tokensWithTimestamp = {
2906
2928
  ...tokens,
2907
2929
  issued_at: tokens.issued_at || Math.floor(Date.now() / 1e3)
@@ -2911,7 +2933,8 @@ var BlinkAuth = class {
2911
2933
  hasAccessToken: !!tokensWithTimestamp.access_token,
2912
2934
  hasRefreshToken: !!tokensWithTimestamp.refresh_token,
2913
2935
  expiresIn: tokensWithTimestamp.expires_in,
2914
- issuedAt: tokensWithTimestamp.issued_at
2936
+ issuedAt: tokensWithTimestamp.issued_at,
2937
+ hasKnownUser: !!knownUser
2915
2938
  });
2916
2939
  if (persist) {
2917
2940
  try {
@@ -2927,32 +2950,39 @@ var BlinkAuth = class {
2927
2950
  console.log("\u{1F4A5} Error persisting tokens:", error);
2928
2951
  }
2929
2952
  }
2930
- let user = null;
2931
- try {
2932
- console.log("\u{1F464} Fetching user data...");
2933
- const response = await fetch(`${this.authUrl}/api/auth/me`, {
2934
- headers: {
2935
- "Authorization": `Bearer ${tokensWithTimestamp.access_token}`
2936
- }
2937
- });
2938
- console.log("\u{1F4E1} User fetch response:", {
2939
- status: response.status,
2940
- statusText: response.statusText,
2941
- ok: response.ok
2942
- });
2943
- if (response.ok) {
2944
- const data = await response.json();
2945
- user = data.user;
2946
- console.log("\u2705 User data fetched successfully:", {
2947
- id: user?.id,
2948
- email: user?.email,
2949
- displayName: user?.displayName
2953
+ let user = knownUser || null;
2954
+ if (!user) {
2955
+ try {
2956
+ console.log("\u{1F464} Fetching user data...");
2957
+ const response = await fetch(`${this.authUrl}/api/auth/me`, {
2958
+ headers: {
2959
+ "Authorization": `Bearer ${tokensWithTimestamp.access_token}`
2960
+ }
2950
2961
  });
2951
- } else {
2952
- console.log("\u274C Failed to fetch user data:", await response.text());
2962
+ console.log("\u{1F4E1} User fetch response:", {
2963
+ status: response.status,
2964
+ statusText: response.statusText,
2965
+ ok: response.ok
2966
+ });
2967
+ if (response.ok) {
2968
+ const data = await response.json();
2969
+ user = data.user;
2970
+ console.log("\u2705 User data fetched successfully:", {
2971
+ id: user?.id,
2972
+ email: user?.email,
2973
+ displayName: user?.displayName
2974
+ });
2975
+ } else {
2976
+ console.log("\u274C Failed to fetch user data:", await response.text());
2977
+ }
2978
+ } catch (error) {
2979
+ console.log("\u{1F4A5} Error fetching user data:", error);
2953
2980
  }
2954
- } catch (error) {
2955
- console.log("\u{1F4A5} Error fetching user data:", error);
2981
+ } else {
2982
+ console.log("\u2705 Using known user data (skipping /api/auth/me):", {
2983
+ id: user?.id,
2984
+ email: user?.email
2985
+ });
2956
2986
  }
2957
2987
  this.updateAuthState({
2958
2988
  user,
@@ -3252,21 +3282,12 @@ var BlinkTable = class {
3252
3282
  * Upsert a single record (insert or update on conflict)
3253
3283
  */
3254
3284
  async upsert(data, options = {}) {
3255
- const headers = {};
3256
- if (options.returning !== false) {
3257
- headers.Prefer = "return=representation";
3258
- }
3259
- if (options.onConflict) {
3260
- headers["Prefer"] = `${headers["Prefer"] || ""} resolution=merge-duplicates`.trim();
3261
- }
3262
3285
  const record = ensureRecordId(data);
3263
- const response = await this.httpClient.request(
3264
- `/api/db/${this.httpClient.projectId}/rest/v1/${this.actualTableName}?on_conflict=${options.onConflict || "id"}`,
3265
- {
3266
- method: "POST",
3267
- body: record,
3268
- headers
3269
- }
3286
+ const onConflict = options.onConflict || "id";
3287
+ const response = await this.httpClient.dbUpsert(
3288
+ this.actualTableName,
3289
+ record,
3290
+ { onConflict, returning: options.returning !== false }
3270
3291
  );
3271
3292
  const result = Array.isArray(response.data) ? response.data[0] : response.data;
3272
3293
  if (!result) {
@@ -3279,20 +3300,11 @@ var BlinkTable = class {
3279
3300
  */
3280
3301
  async upsertMany(data, options = {}) {
3281
3302
  const records = data.map(ensureRecordId);
3282
- const headers = {};
3283
- if (options.returning !== false) {
3284
- headers.Prefer = "return=representation";
3285
- }
3286
- if (options.onConflict) {
3287
- headers["Prefer"] = `${headers["Prefer"] || ""} resolution=merge-duplicates`.trim();
3288
- }
3289
- const response = await this.httpClient.request(
3290
- `/api/db/${this.httpClient.projectId}/rest/v1/${this.actualTableName}?on_conflict=${options.onConflict || "id"}`,
3291
- {
3292
- method: "POST",
3293
- body: records,
3294
- headers
3295
- }
3303
+ const onConflict = options.onConflict || "id";
3304
+ const response = await this.httpClient.dbUpsert(
3305
+ this.actualTableName,
3306
+ records,
3307
+ { onConflict, returning: options.returning !== false }
3296
3308
  );
3297
3309
  const results = Array.isArray(response.data) ? response.data : [response.data];
3298
3310
  return results;
package/dist/index.mjs CHANGED
@@ -529,6 +529,28 @@ var HttpClient = class {
529
529
  data: convertedData
530
530
  };
531
531
  }
532
+ async dbUpsert(table, body, options = {}) {
533
+ const headers = {};
534
+ if (options.returning) {
535
+ headers.Prefer = "return=representation";
536
+ }
537
+ const convertedBody = convertKeysToSnakeCase(body);
538
+ const onConflict = options.onConflict || "id";
539
+ const response = await this.request(
540
+ `/api/db/${this.projectId}/rest/v1/${table}`,
541
+ {
542
+ method: "POST",
543
+ body: convertedBody,
544
+ headers,
545
+ searchParams: { on_conflict: onConflict }
546
+ }
547
+ );
548
+ const convertedData = convertKeysToCamelCase(response.data);
549
+ return {
550
+ ...response,
551
+ data: convertedData
552
+ };
553
+ }
532
554
  async dbPatch(table, body, searchParams, options = {}) {
533
555
  const headers = {};
534
556
  if (options.returning) {
@@ -603,7 +625,7 @@ var HttpClient = class {
603
625
  const blobWithType = options.contentType ? new Blob([file], { type: options.contentType }) : file;
604
626
  formData.append("file", blobWithType);
605
627
  } else if (typeof Buffer !== "undefined" && file instanceof Buffer) {
606
- const blob = new Blob([file], { type: options.contentType || "application/octet-stream" });
628
+ const blob = new Blob([new Uint8Array(file)], { type: options.contentType || "application/octet-stream" });
607
629
  formData.append("file", blob);
608
630
  } else {
609
631
  throw new BlinkValidationError("Unsupported file type");
@@ -1729,7 +1751,7 @@ var BlinkAuth = class {
1729
1751
  token_type: result.token_type,
1730
1752
  expires_in: result.expires_in,
1731
1753
  refresh_expires_in: result.refresh_expires_in
1732
- }, true);
1754
+ }, true, result.user);
1733
1755
  return result.user;
1734
1756
  } catch (error) {
1735
1757
  if (error instanceof BlinkAuthError) {
@@ -1769,7 +1791,7 @@ var BlinkAuth = class {
1769
1791
  token_type: result.token_type,
1770
1792
  expires_in: result.expires_in,
1771
1793
  refresh_expires_in: result.refresh_expires_in
1772
- }, true);
1794
+ }, true, result.user);
1773
1795
  return result.user;
1774
1796
  } catch (error) {
1775
1797
  if (error instanceof BlinkAuthError) {
@@ -2502,7 +2524,7 @@ var BlinkAuth = class {
2502
2524
  token_type: result.token_type,
2503
2525
  expires_in: result.expires_in,
2504
2526
  refresh_expires_in: result.refresh_expires_in
2505
- }, true);
2527
+ }, true, result.user);
2506
2528
  return result.user;
2507
2529
  } catch (error) {
2508
2530
  if (error instanceof BlinkAuthError) {
@@ -2899,7 +2921,7 @@ var BlinkAuth = class {
2899
2921
  return false;
2900
2922
  }
2901
2923
  }
2902
- async setTokens(tokens, persist) {
2924
+ async setTokens(tokens, persist, knownUser) {
2903
2925
  const tokensWithTimestamp = {
2904
2926
  ...tokens,
2905
2927
  issued_at: tokens.issued_at || Math.floor(Date.now() / 1e3)
@@ -2909,7 +2931,8 @@ var BlinkAuth = class {
2909
2931
  hasAccessToken: !!tokensWithTimestamp.access_token,
2910
2932
  hasRefreshToken: !!tokensWithTimestamp.refresh_token,
2911
2933
  expiresIn: tokensWithTimestamp.expires_in,
2912
- issuedAt: tokensWithTimestamp.issued_at
2934
+ issuedAt: tokensWithTimestamp.issued_at,
2935
+ hasKnownUser: !!knownUser
2913
2936
  });
2914
2937
  if (persist) {
2915
2938
  try {
@@ -2925,32 +2948,39 @@ var BlinkAuth = class {
2925
2948
  console.log("\u{1F4A5} Error persisting tokens:", error);
2926
2949
  }
2927
2950
  }
2928
- let user = null;
2929
- try {
2930
- console.log("\u{1F464} Fetching user data...");
2931
- const response = await fetch(`${this.authUrl}/api/auth/me`, {
2932
- headers: {
2933
- "Authorization": `Bearer ${tokensWithTimestamp.access_token}`
2934
- }
2935
- });
2936
- console.log("\u{1F4E1} User fetch response:", {
2937
- status: response.status,
2938
- statusText: response.statusText,
2939
- ok: response.ok
2940
- });
2941
- if (response.ok) {
2942
- const data = await response.json();
2943
- user = data.user;
2944
- console.log("\u2705 User data fetched successfully:", {
2945
- id: user?.id,
2946
- email: user?.email,
2947
- displayName: user?.displayName
2951
+ let user = knownUser || null;
2952
+ if (!user) {
2953
+ try {
2954
+ console.log("\u{1F464} Fetching user data...");
2955
+ const response = await fetch(`${this.authUrl}/api/auth/me`, {
2956
+ headers: {
2957
+ "Authorization": `Bearer ${tokensWithTimestamp.access_token}`
2958
+ }
2948
2959
  });
2949
- } else {
2950
- console.log("\u274C Failed to fetch user data:", await response.text());
2960
+ console.log("\u{1F4E1} User fetch response:", {
2961
+ status: response.status,
2962
+ statusText: response.statusText,
2963
+ ok: response.ok
2964
+ });
2965
+ if (response.ok) {
2966
+ const data = await response.json();
2967
+ user = data.user;
2968
+ console.log("\u2705 User data fetched successfully:", {
2969
+ id: user?.id,
2970
+ email: user?.email,
2971
+ displayName: user?.displayName
2972
+ });
2973
+ } else {
2974
+ console.log("\u274C Failed to fetch user data:", await response.text());
2975
+ }
2976
+ } catch (error) {
2977
+ console.log("\u{1F4A5} Error fetching user data:", error);
2951
2978
  }
2952
- } catch (error) {
2953
- console.log("\u{1F4A5} Error fetching user data:", error);
2979
+ } else {
2980
+ console.log("\u2705 Using known user data (skipping /api/auth/me):", {
2981
+ id: user?.id,
2982
+ email: user?.email
2983
+ });
2954
2984
  }
2955
2985
  this.updateAuthState({
2956
2986
  user,
@@ -3250,21 +3280,12 @@ var BlinkTable = class {
3250
3280
  * Upsert a single record (insert or update on conflict)
3251
3281
  */
3252
3282
  async upsert(data, options = {}) {
3253
- const headers = {};
3254
- if (options.returning !== false) {
3255
- headers.Prefer = "return=representation";
3256
- }
3257
- if (options.onConflict) {
3258
- headers["Prefer"] = `${headers["Prefer"] || ""} resolution=merge-duplicates`.trim();
3259
- }
3260
3283
  const record = ensureRecordId(data);
3261
- const response = await this.httpClient.request(
3262
- `/api/db/${this.httpClient.projectId}/rest/v1/${this.actualTableName}?on_conflict=${options.onConflict || "id"}`,
3263
- {
3264
- method: "POST",
3265
- body: record,
3266
- headers
3267
- }
3284
+ const onConflict = options.onConflict || "id";
3285
+ const response = await this.httpClient.dbUpsert(
3286
+ this.actualTableName,
3287
+ record,
3288
+ { onConflict, returning: options.returning !== false }
3268
3289
  );
3269
3290
  const result = Array.isArray(response.data) ? response.data[0] : response.data;
3270
3291
  if (!result) {
@@ -3277,20 +3298,11 @@ var BlinkTable = class {
3277
3298
  */
3278
3299
  async upsertMany(data, options = {}) {
3279
3300
  const records = data.map(ensureRecordId);
3280
- const headers = {};
3281
- if (options.returning !== false) {
3282
- headers.Prefer = "return=representation";
3283
- }
3284
- if (options.onConflict) {
3285
- headers["Prefer"] = `${headers["Prefer"] || ""} resolution=merge-duplicates`.trim();
3286
- }
3287
- const response = await this.httpClient.request(
3288
- `/api/db/${this.httpClient.projectId}/rest/v1/${this.actualTableName}?on_conflict=${options.onConflict || "id"}`,
3289
- {
3290
- method: "POST",
3291
- body: records,
3292
- headers
3293
- }
3301
+ const onConflict = options.onConflict || "id";
3302
+ const response = await this.httpClient.dbUpsert(
3303
+ this.actualTableName,
3304
+ records,
3305
+ { onConflict, returning: options.returning !== false }
3294
3306
  );
3295
3307
  const results = Array.isArray(response.data) ? response.data : [response.data];
3296
3308
  return results;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blinkdotnew/sdk",
3
- "version": "2.3.5",
3
+ "version": "2.3.7",
4
4
  "description": "Blink TypeScript SDK for client-side applications - Zero-boilerplate CRUD + auth + AI + analytics + notifications for modern SaaS/AI apps",
5
5
  "keywords": [
6
6
  "blink",