@usequota/nextjs 0.2.1 → 0.3.0

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/server.mjs CHANGED
@@ -1,47 +1,39 @@
1
+ // src/server.ts
1
2
  import {
2
- QuotaError,
3
- QuotaInsufficientCreditsError,
4
- QuotaNotConnectedError,
5
- QuotaRateLimitError,
6
- QuotaTokenExpiredError,
3
+ QuotaError as QuotaError2,
4
+ QuotaInsufficientCreditsError as QuotaInsufficientCreditsError2,
5
+ QuotaNotConnectedError as QuotaNotConnectedError2,
6
+ QuotaTokenExpiredError as QuotaTokenExpiredError2,
7
+ QuotaRateLimitError as QuotaRateLimitError2,
7
8
  errorFromResponse
8
- } from "./chunk-BMI3VFWV.mjs";
9
+ } from "@usequota/core";
10
+ import { QuotaClient } from "@usequota/core";
11
+ import { parseSSEStream } from "@usequota/core";
12
+ import { exchangeCodeForToken, refreshAccessToken } from "@usequota/core";
13
+ import {
14
+ verifyWebhookSignature,
15
+ parseWebhook,
16
+ createWebhookHandler
17
+ } from "@usequota/core";
18
+ import { InMemoryTokenStorage } from "@usequota/core";
9
19
 
10
20
  // src/route-handlers.ts
11
21
  import { cookies } from "next/headers";
12
22
 
13
23
  // src/token-refresh.ts
14
- var FETCH_TIMEOUT_MS = 1e4;
15
- async function refreshAccessTokenWithCredentials(opts) {
24
+ import { refreshAccessTokenWithCredentials } from "@usequota/core";
25
+ import { FETCH_TIMEOUT_MS } from "@usequota/core";
26
+ import { refreshAccessTokenWithCredentials as refreshAccessTokenWithCredentials2 } from "@usequota/core";
27
+ async function refreshAndPersistTokens(opts) {
28
+ let tokenData;
16
29
  try {
17
- const response = await fetch(`${opts.baseUrl}/oauth/token`, {
18
- method: "POST",
19
- headers: { "Content-Type": "application/json" },
20
- body: JSON.stringify({
21
- grant_type: "refresh_token",
22
- refresh_token: opts.refreshToken,
23
- client_id: opts.clientId,
24
- client_secret: opts.clientSecret
25
- }),
26
- signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
30
+ tokenData = await refreshAccessTokenWithCredentials({
31
+ refreshToken: opts.refreshToken,
32
+ clientId: opts.clientId,
33
+ clientSecret: opts.clientSecret,
34
+ baseUrl: opts.baseUrl
27
35
  });
28
- if (!response.ok) {
29
- return null;
30
- }
31
- return await response.json();
32
- } catch (error) {
33
- console.error("Failed to refresh Quota access token:", error);
34
- return null;
35
- }
36
- }
37
- async function refreshAndPersistTokens(opts) {
38
- const tokenData = await refreshAccessTokenWithCredentials({
39
- refreshToken: opts.refreshToken,
40
- clientId: opts.clientId,
41
- clientSecret: opts.clientSecret,
42
- baseUrl: opts.baseUrl
43
- });
44
- if (!tokenData) {
36
+ } catch {
45
37
  return null;
46
38
  }
47
39
  if (opts.tokenStorage && opts.request) {
@@ -521,6 +513,13 @@ function createQuotaRouteHandlers(config) {
521
513
 
522
514
  // src/with-quota-auth.ts
523
515
  import { cookies as cookies2 } from "next/headers";
516
+ import {
517
+ QuotaNotConnectedError,
518
+ QuotaTokenExpiredError,
519
+ QuotaInsufficientCreditsError,
520
+ QuotaRateLimitError,
521
+ QuotaError
522
+ } from "@usequota/core";
524
523
  var DEFAULT_BASE_URL2 = "https://api.usequota.app";
525
524
  var DEFAULT_COOKIE_PREFIX2 = "quota";
526
525
  var DEFAULT_COOKIE_MAX_AGE2 = 60 * 60 * 24 * 7;
@@ -717,6 +716,7 @@ function withQuotaAuth(config, handler) {
717
716
 
718
717
  // src/server.ts
719
718
  import { cookies as cookies3 } from "next/headers";
719
+ import { QuotaNotConnectedError as QuotaNotConnectedError3 } from "@usequota/core";
720
720
  var DEFAULT_BASE_URL3 = "https://api.usequota.app";
721
721
  var DEFAULT_COOKIE_PREFIX3 = "quota";
722
722
  var DEFAULT_STORAGE_MODE = "client";
@@ -767,13 +767,13 @@ async function getQuotaUser(config) {
767
767
  `${cookiePrefix}_refresh_token`
768
768
  )?.value;
769
769
  if (refreshTokenValue) {
770
- const refreshed = await refreshAccessTokenWithCredentials({
771
- refreshToken: refreshTokenValue,
772
- clientId: config.clientId,
773
- clientSecret: config.clientSecret,
774
- baseUrl
775
- });
776
- if (refreshed) {
770
+ try {
771
+ const refreshed = await refreshAccessTokenWithCredentials2({
772
+ refreshToken: refreshTokenValue,
773
+ clientId: config.clientId,
774
+ clientSecret: config.clientSecret,
775
+ baseUrl
776
+ });
777
777
  cookieStore.set(
778
778
  `${cookiePrefix}_access_token`,
779
779
  refreshed.access_token,
@@ -805,6 +805,8 @@ async function getQuotaUser(config) {
805
805
  if (retryResponse.ok) {
806
806
  return await retryResponse.json();
807
807
  }
808
+ } catch {
809
+ return null;
808
810
  }
809
811
  }
810
812
  }
@@ -821,7 +823,7 @@ async function requireQuotaAuth(config) {
821
823
  if (!user) {
822
824
  const { redirect } = await import("next/navigation");
823
825
  redirect("/");
824
- throw new QuotaNotConnectedError("Redirecting to /");
826
+ throw new QuotaNotConnectedError3("Redirecting to /");
825
827
  }
826
828
  return user;
827
829
  }
@@ -864,7 +866,7 @@ async function createQuotaCheckout(config) {
864
866
  `${cookiePrefix}_access_token`
865
867
  )?.value;
866
868
  if (!accessToken) {
867
- throw new QuotaNotConnectedError("Not authenticated");
869
+ throw new QuotaNotConnectedError3("Not authenticated");
868
870
  }
869
871
  headers["Authorization"] = `Bearer ${accessToken}`;
870
872
  }
@@ -900,17 +902,25 @@ async function clearQuotaAuth(config) {
900
902
  }
901
903
  }
902
904
  export {
903
- QuotaError,
904
- QuotaInsufficientCreditsError,
905
- QuotaNotConnectedError,
906
- QuotaRateLimitError,
907
- QuotaTokenExpiredError,
905
+ InMemoryTokenStorage,
906
+ QuotaClient,
907
+ QuotaError2 as QuotaError,
908
+ QuotaInsufficientCreditsError2 as QuotaInsufficientCreditsError,
909
+ QuotaNotConnectedError2 as QuotaNotConnectedError,
910
+ QuotaRateLimitError2 as QuotaRateLimitError,
911
+ QuotaTokenExpiredError2 as QuotaTokenExpiredError,
908
912
  clearQuotaAuth,
909
913
  createQuotaCheckout,
910
914
  createQuotaRouteHandlers,
915
+ createWebhookHandler,
911
916
  errorFromResponse,
917
+ exchangeCodeForToken,
912
918
  getQuotaPackages,
913
919
  getQuotaUser,
920
+ parseSSEStream,
921
+ parseWebhook,
922
+ refreshAccessToken,
914
923
  requireQuotaAuth,
924
+ verifyWebhookSignature,
915
925
  withQuotaAuth
916
926
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usequota/nextjs",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Next.js SDK for Quota — AI credit billing middleware, hooks, and components",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -8,7 +8,7 @@
8
8
  "url": "https://github.com/craigdossantos/quota",
9
9
  "directory": "packages/quota-nextjs"
10
10
  },
11
- "homepage": "https://usequota.app/docs/nextjs-sdk",
11
+ "homepage": "https://usequota.ai/docs/nextjs-sdk",
12
12
  "keywords": [
13
13
  "quota",
14
14
  "ai",
@@ -50,6 +50,7 @@
50
50
  "react": ">=18.0.0"
51
51
  },
52
52
  "dependencies": {
53
+ "@usequota/core": "^0.1.0",
53
54
  "@usequota/types": "^0.1.0"
54
55
  },
55
56
  "devDependencies": {