@nehorai/credits 1.0.2 → 1.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.
Files changed (134) hide show
  1. package/dist/adapters/index.cjs +463 -0
  2. package/dist/adapters/index.cjs.map +1 -0
  3. package/dist/adapters/index.d.cts +66 -0
  4. package/dist/adapters/index.d.ts +66 -3
  5. package/dist/adapters/index.js +435 -1
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/auth/index.cjs +96 -0
  8. package/dist/auth/index.cjs.map +1 -0
  9. package/dist/auth/{api-key.d.ts → index.d.cts} +9 -6
  10. package/dist/auth/index.d.ts +37 -5
  11. package/dist/auth/index.js +65 -7
  12. package/dist/auth/index.js.map +1 -1
  13. package/dist/core/index.cjs +522 -0
  14. package/dist/core/index.cjs.map +1 -0
  15. package/dist/core/index.d.cts +119 -0
  16. package/dist/core/index.d.ts +115 -25
  17. package/dist/core/index.js +475 -27
  18. package/dist/core/index.js.map +1 -1
  19. package/dist/{core/deferred.d.ts → deferred-C7rrRfkm.d.cts} +7 -6
  20. package/dist/{core/deferred.js → deferred-C7rrRfkm.d.ts} +23 -29
  21. package/dist/index.cjs +2506 -0
  22. package/dist/index.cjs.map +1 -0
  23. package/dist/index.d.cts +437 -0
  24. package/dist/index.d.ts +429 -49
  25. package/dist/index.js +2399 -62
  26. package/dist/index.js.map +1 -1
  27. package/dist/repository/index.cjs +816 -0
  28. package/dist/repository/index.cjs.map +1 -0
  29. package/dist/repository/{memory/index.d.ts → index.d.cts} +35 -5
  30. package/dist/repository/index.d.ts +97 -7
  31. package/dist/repository/index.js +783 -10
  32. package/dist/repository/index.js.map +1 -1
  33. package/dist/sdk/index.cjs +608 -0
  34. package/dist/sdk/index.cjs.map +1 -0
  35. package/dist/sdk/index.d.cts +495 -0
  36. package/dist/sdk/index.d.ts +491 -9
  37. package/dist/sdk/index.js +568 -12
  38. package/dist/sdk/index.js.map +1 -1
  39. package/dist/service/index.cjs +670 -0
  40. package/dist/service/index.cjs.map +1 -0
  41. package/dist/service/{credits-service.d.ts → index.d.cts} +9 -7
  42. package/dist/service/index.d.ts +141 -3
  43. package/dist/service/index.js +641 -1
  44. package/dist/service/index.js.map +1 -1
  45. package/dist/{core/types.d.ts → types-BBxIWHK2.d.cts} +25 -35
  46. package/dist/types-BBxIWHK2.d.ts +277 -0
  47. package/dist/types-D1vdeBcA.d.cts +305 -0
  48. package/dist/{auth/types.d.ts → types-OZU3I5OM.d.cts} +5 -4
  49. package/dist/types-OZU3I5OM.d.ts +32 -0
  50. package/dist/{repository/types.d.ts → types-aJsyv8df.d.ts} +35 -13
  51. package/package.json +14 -13
  52. package/dist/adapters/generic.d.ts +0 -18
  53. package/dist/adapters/generic.d.ts.map +0 -1
  54. package/dist/adapters/generic.js +0 -147
  55. package/dist/adapters/generic.js.map +0 -1
  56. package/dist/adapters/index.d.ts.map +0 -1
  57. package/dist/adapters/types.d.ts +0 -45
  58. package/dist/adapters/types.d.ts.map +0 -1
  59. package/dist/adapters/types.js +0 -8
  60. package/dist/adapters/types.js.map +0 -1
  61. package/dist/auth/api-key.d.ts.map +0 -1
  62. package/dist/auth/api-key.js +0 -94
  63. package/dist/auth/api-key.js.map +0 -1
  64. package/dist/auth/index.d.ts.map +0 -1
  65. package/dist/auth/types.d.ts.map +0 -1
  66. package/dist/auth/types.js +0 -2
  67. package/dist/auth/types.js.map +0 -1
  68. package/dist/config/costs.d.ts +0 -61
  69. package/dist/config/costs.d.ts.map +0 -1
  70. package/dist/config/costs.js +0 -86
  71. package/dist/config/costs.js.map +0 -1
  72. package/dist/config/index.d.ts +0 -165
  73. package/dist/config/index.d.ts.map +0 -1
  74. package/dist/config/index.js +0 -286
  75. package/dist/config/index.js.map +0 -1
  76. package/dist/core/deferred.d.ts.map +0 -1
  77. package/dist/core/deferred.js.map +0 -1
  78. package/dist/core/errors.d.ts +0 -78
  79. package/dist/core/errors.d.ts.map +0 -1
  80. package/dist/core/errors.js +0 -110
  81. package/dist/core/errors.js.map +0 -1
  82. package/dist/core/index.d.ts.map +0 -1
  83. package/dist/core/operations.d.ts +0 -36
  84. package/dist/core/operations.d.ts.map +0 -1
  85. package/dist/core/operations.js +0 -87
  86. package/dist/core/operations.js.map +0 -1
  87. package/dist/core/types.d.ts.map +0 -1
  88. package/dist/core/types.js +0 -93
  89. package/dist/core/types.js.map +0 -1
  90. package/dist/index.d.ts.map +0 -1
  91. package/dist/notifications/index.d.ts +0 -76
  92. package/dist/notifications/index.d.ts.map +0 -1
  93. package/dist/notifications/index.js +0 -182
  94. package/dist/notifications/index.js.map +0 -1
  95. package/dist/repository/index.d.ts.map +0 -1
  96. package/dist/repository/memory/index.d.ts.map +0 -1
  97. package/dist/repository/memory/index.js +0 -518
  98. package/dist/repository/memory/index.js.map +0 -1
  99. package/dist/repository/types.d.ts.map +0 -1
  100. package/dist/repository/types.js +0 -39
  101. package/dist/repository/types.js.map +0 -1
  102. package/dist/repository/utils.d.ts +0 -22
  103. package/dist/repository/utils.d.ts.map +0 -1
  104. package/dist/repository/utils.js +0 -40
  105. package/dist/repository/utils.js.map +0 -1
  106. package/dist/sdk/admin-client.d.ts +0 -146
  107. package/dist/sdk/admin-client.d.ts.map +0 -1
  108. package/dist/sdk/admin-client.js +0 -196
  109. package/dist/sdk/admin-client.js.map +0 -1
  110. package/dist/sdk/client.d.ts +0 -144
  111. package/dist/sdk/client.d.ts.map +0 -1
  112. package/dist/sdk/client.js +0 -247
  113. package/dist/sdk/client.js.map +0 -1
  114. package/dist/sdk/errors.d.ts +0 -91
  115. package/dist/sdk/errors.d.ts.map +0 -1
  116. package/dist/sdk/errors.js +0 -185
  117. package/dist/sdk/errors.js.map +0 -1
  118. package/dist/sdk/index.d.ts.map +0 -1
  119. package/dist/sdk/types.d.ts +0 -110
  120. package/dist/sdk/types.d.ts.map +0 -1
  121. package/dist/sdk/types.js +0 -20
  122. package/dist/sdk/types.js.map +0 -1
  123. package/dist/service/credits-service.d.ts.map +0 -1
  124. package/dist/service/credits-service.js +0 -372
  125. package/dist/service/credits-service.js.map +0 -1
  126. package/dist/service/index.d.ts.map +0 -1
  127. package/dist/utils/error-utils.d.ts +0 -48
  128. package/dist/utils/error-utils.d.ts.map +0 -1
  129. package/dist/utils/error-utils.js +0 -57
  130. package/dist/utils/error-utils.js.map +0 -1
  131. package/dist/utils/index.d.ts +0 -54
  132. package/dist/utils/index.d.ts.map +0 -1
  133. package/dist/utils/index.js +0 -72
  134. package/dist/utils/index.js.map +0 -1
@@ -1,147 +0,0 @@
1
- /**
2
- * Generic credits adapter - framework agnostic
3
- *
4
- * This adapter uses standard JavaScript APIs and works in any
5
- * JavaScript environment (Node.js, browser, etc.).
6
- */
7
- import { commitReservationWithJournal, releaseReservationWithJournal, reserveCreditsForOperation, } from "../core/operations.js";
8
- /**
9
- * Generate a unique request ID
10
- */
11
- function generateRequestId() {
12
- const timestamp = Date.now();
13
- const randomPart = Math.random().toString(36).substring(2, 14);
14
- return `req_${timestamp}_${randomPart}`;
15
- }
16
- /**
17
- * Create a generic credits adapter
18
- *
19
- * This adapter:
20
- * - Authenticates users via the provided auth provider
21
- * - Reserves credits before action execution
22
- * - Commits credits on success, releases on failure
23
- * - Logs usage asynchronously via the deferred executor
24
- */
25
- export function createGenericAdapter(config) {
26
- const { repository, authProvider, deferred, operationCosts } = config;
27
- function getOperationCost(operationType) {
28
- return operationCosts[operationType] ?? 0;
29
- }
30
- function logUsage(params) {
31
- deferred.defer(async () => {
32
- await repository.logUsage({
33
- userId: params.userId,
34
- operationType: params.operationType,
35
- provider: "gemini",
36
- creditsUsed: params.creditsUsed,
37
- success: params.success,
38
- errorMessage: params.errorMessage,
39
- resourceId: params.resourceId,
40
- resourceType: params.resourceType,
41
- requestId: params.requestId,
42
- });
43
- });
44
- }
45
- return {
46
- withCredits(options, handler) {
47
- return async (data) => {
48
- const requestId = generateRequestId();
49
- // Authenticate
50
- const user = await authProvider.getCurrentUser();
51
- if (!user?.id) {
52
- return { success: false, error: "Authentication required" };
53
- }
54
- // Handle preview mode - skip credit handling entirely
55
- if (isPreviewMode(data)) {
56
- const dummyReservation = createDummyReservation(user.id, options.operationType);
57
- return handler(user, data, dummyReservation);
58
- }
59
- // Calculate cost
60
- const cost = options.customCost ?? getOperationCost(options.operationType);
61
- // Reserve credits
62
- let reservation;
63
- try {
64
- reservation = await reserveCreditsForOperation(repository, user.id, cost, options.operationType);
65
- }
66
- catch (error) {
67
- const message = error instanceof Error ? error.message : "Failed to reserve credits";
68
- logUsage({
69
- userId: user.id,
70
- operationType: options.operationType,
71
- creditsUsed: 0,
72
- success: false,
73
- errorMessage: message,
74
- resourceId: options.resourceId,
75
- resourceType: options.resourceType,
76
- requestId,
77
- });
78
- return { success: false, error: message };
79
- }
80
- // Execute action
81
- try {
82
- const result = await handler(user, data, reservation);
83
- if (result.success) {
84
- await commitReservationWithJournal(repository, user.id, reservation.id);
85
- logUsage({
86
- userId: user.id,
87
- operationType: options.operationType,
88
- creditsUsed: cost,
89
- success: true,
90
- resourceId: options.resourceId,
91
- resourceType: options.resourceType,
92
- requestId,
93
- });
94
- }
95
- else {
96
- await releaseReservationWithJournal(repository, user.id, reservation.id);
97
- logUsage({
98
- userId: user.id,
99
- operationType: options.operationType,
100
- creditsUsed: 0,
101
- success: false,
102
- errorMessage: result.error,
103
- resourceId: options.resourceId,
104
- resourceType: options.resourceType,
105
- requestId,
106
- });
107
- }
108
- return result;
109
- }
110
- catch (error) {
111
- const message = error instanceof Error ? error.message : "An unexpected error occurred";
112
- await releaseReservationWithJournal(repository, user.id, reservation.id);
113
- logUsage({
114
- userId: user.id,
115
- operationType: options.operationType,
116
- creditsUsed: 0,
117
- success: false,
118
- errorMessage: message,
119
- resourceId: options.resourceId,
120
- resourceType: options.resourceType,
121
- requestId,
122
- });
123
- console.error("Action error:", error);
124
- return { success: false, error: message };
125
- }
126
- };
127
- },
128
- };
129
- }
130
- function isPreviewMode(data) {
131
- return (typeof data === "object" &&
132
- data !== null &&
133
- "preview" in data &&
134
- data.preview === true);
135
- }
136
- function createDummyReservation(userId, operationType) {
137
- return {
138
- id: "preview-mode",
139
- userId,
140
- amount: 0,
141
- operationType,
142
- status: "released",
143
- createdAt: new Date().toISOString(),
144
- expiresAt: new Date().toISOString(),
145
- };
146
- }
147
- //# sourceMappingURL=generic.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"generic.js","sourceRoot":"","sources":["../../src/adapters/generic.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,EAC7B,0BAA0B,GAC3B,MAAM,uBAAuB,CAAC;AAE/B;;GAEG;AACH,SAAS,iBAAiB;IACxB,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7B,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC/D,OAAO,OAAO,SAAS,IAAI,UAAU,EAAE,CAAC;AAC1C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAA4B;IAC/D,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;IAEtE,SAAS,gBAAgB,CAAC,aAAqB;QAC7C,OAAO,cAAc,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED,SAAS,QAAQ,CAAC,MASjB;QACC,QAAQ,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE;YACxB,MAAM,UAAU,CAAC,QAAQ,CAAC;gBACxB,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,aAAa,EAAE,MAAM,CAAC,aAAa;gBACnC,QAAQ,EAAE,QAAQ;gBAClB,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,YAAY,EAAE,MAAM,CAAC,YAA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@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC"}
@@ -1,45 +0,0 @@
1
- /**
2
- * Adapter types for the credits system
3
- *
4
- * Defines the interface for framework-specific adapters that wrap
5
- * credit operations with different execution strategies.
6
- */
7
- import type { PortableReservation, WithCreditsOptions } from "../core/types.js";
8
- import type { CreditsUser, ICreditsAuthProvider } from "../auth/types.js";
9
- import type { ICreditRepository } from "../repository/types.js";
10
- import type { DeferredExecutor } from "../core/index.js";
11
- /**
12
- * Generic action result type
13
- */
14
- export interface ActionResult<T> {
15
- success: boolean;
16
- data?: T;
17
- error?: string;
18
- }
19
- /**
20
- * Handler function signature for credit-wrapped actions
21
- */
22
- export type CreditActionHandler<TInput, TOutput> = (user: CreditsUser, data: TInput, reservation: PortableReservation) => Promise<ActionResult<TOutput>>;
23
- /**
24
- * Configuration for creating a credits adapter
25
- */
26
- export interface CreditsAdapterConfig {
27
- /** Repository for credit operations */
28
- repository: ICreditRepository;
29
- /** Auth provider for user authentication */
30
- authProvider: ICreditsAuthProvider;
31
- /** Deferred executor for background tasks */
32
- deferred: DeferredExecutor;
33
- /** Operation cost lookup table */
34
- operationCosts: Record<string, number>;
35
- }
36
- /**
37
- * Interface for credits adapters
38
- */
39
- export interface CreditsAdapter {
40
- /**
41
- * Wrap an action function with credit handling
42
- */
43
- withCredits<TInput, TOutput>(options: WithCreditsOptions, handler: CreditActionHandler<TInput, TOutput>): (data: TInput) => Promise<ActionResult<TOutput>>;
44
- }
45
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/adapters/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,MAAM,EAAE,OAAO,IAAI,CACjD,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,mBAAmB,KAC7B,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,uCAAuC;IACvC,UAAU,EAAE,iBAAiB,CAAC;IAC9B,4CAA4C;IAC5C,YAAY,EAAE,oBAAoB,CAAC;IACnC,6CAA6C;IAC7C,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,kCAAkC;IAClC,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,OAAO,EACzB,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5C,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;CACrD"}
@@ -1,8 +0,0 @@
1
- /**
2
- * Adapter types for the credits system
3
- *
4
- * Defines the interface for framework-specific adapters that wrap
5
- * credit operations with different execution strategies.
6
- */
7
- export {};
8
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/adapters/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"api-key.d.ts","sourceRoot":"","sources":["../../src/auth/api-key.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAwBpE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAUhF;AAED;;;;;GAKG;AACH,qBAAa,qBAAsB,YAAW,oBAAoB;IAChE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,WAAW,CAAS;gBAEhB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IASjD,cAAc,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAa7C,iBAAiB,IAAI,OAAO,CAAC,OAAO,CAAC;CAG5C;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAarF;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAG7F"}
@@ -1,94 +0,0 @@
1
- /**
2
- * Timing-safe string comparison to prevent timing attacks
3
- */
4
- function timingSafeEqual(a, b) {
5
- if (!a || !b) {
6
- return false;
7
- }
8
- // Pad strings to same length to prevent length-based timing attacks
9
- const maxLength = Math.max(a.length, b.length);
10
- const paddedA = a.padEnd(maxLength);
11
- const paddedB = b.padEnd(maxLength);
12
- let result = 0;
13
- for (let i = 0; i < maxLength; i++) {
14
- result |= paddedA.charCodeAt(i) ^ paddedB.charCodeAt(i);
15
- }
16
- // Also check that lengths were originally equal
17
- return result === 0 && a.length === b.length;
18
- }
19
- /**
20
- * Verify a Bearer token from an Authorization header
21
- *
22
- * @param header - The Authorization header value (e.g., "Bearer secret123")
23
- * @param secret - The expected secret value
24
- * @returns True if token is valid
25
- */
26
- export function verifyBearerToken(header, secret) {
27
- if (!header || !secret) {
28
- return false;
29
- }
30
- // Extract token from "Bearer {token}" format
31
- const token = header.replace(/^Bearer\s+/i, "");
32
- // Use timing-safe comparison
33
- return timingSafeEqual(token, secret);
34
- }
35
- /**
36
- * API Key auth provider for admin routes
37
- *
38
- * Uses Bearer token authentication for external API access
39
- * The API key is stored in CREDITS_ADMIN_API_KEY environment variable
40
- */
41
- export class ApiKeyCreditsProvider {
42
- apiKey;
43
- isValidated = false;
44
- constructor(authHeader, apiKey) {
45
- this.apiKey = apiKey ?? process.env.CREDITS_ADMIN_API_KEY;
46
- // Validate the provided auth header using timing-safe comparison
47
- if (authHeader && this.apiKey) {
48
- this.isValidated = verifyBearerToken(authHeader, this.apiKey);
49
- }
50
- }
51
- async getCurrentUser() {
52
- if (!this.isValidated) {
53
- return null;
54
- }
55
- // API key auth returns a special "admin" user
56
- return {
57
- id: "api-admin",
58
- email: "api@admin.internal",
59
- name: "API Admin",
60
- };
61
- }
62
- async verifyAdminAccess() {
63
- return this.isValidated;
64
- }
65
- }
66
- /**
67
- * Verify an API key from a request header
68
- * @param authHeader - Authorization header value
69
- * @param apiKey - Optional API key to use (defaults to CREDITS_ADMIN_API_KEY env var)
70
- * @returns True if API key is valid
71
- */
72
- export function verifyAdminApiKey(authHeader, apiKey) {
73
- if (!authHeader) {
74
- return false;
75
- }
76
- const expectedKey = apiKey ?? process.env.CREDITS_ADMIN_API_KEY;
77
- if (!expectedKey) {
78
- console.warn("CREDITS_ADMIN_API_KEY not configured");
79
- return false;
80
- }
81
- // Use timing-safe comparison to prevent timing attacks
82
- return verifyBearerToken(authHeader, expectedKey);
83
- }
84
- /**
85
- * Create an API key auth provider from a request
86
- * @param request - Request object with headers
87
- * @param apiKey - Optional API key to use
88
- * @returns API key auth provider
89
- */
90
- export function createApiKeyProvider(request, apiKey) {
91
- const authHeader = request.headers.get("Authorization");
92
- return new ApiKeyCreditsProvider(authHeader, apiKey);
93
- }
94
- //# sourceMappingURL=api-key.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"api-key.js","sourceRoot":"","sources":["../../src/auth/api-key.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,SAAS,eAAe,CAAC,CAAS,EAAE,CAAS;IAC3C,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QACb,OAAO,KAAK,CAAC;IACf,CAAC;IAED,oEAAoE;IACpE,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpC,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAEpC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,MAAM,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAC1D,CAAC;IAED,gDAAgD;IAChD,OAAO,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAqB,EAAE,MAAc;IACrE,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;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B,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AACpD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAgB,EAAE,MAAe;IACpE,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IACxD,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;AACvD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,oBAAoB,EACpB,0BAA0B,EAC1B,WAAW,GACZ,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,cAAc,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,cAAc,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IAE9C;;;;OAIG;IACH,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,MAAM,oBAAoB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":""}
@@ -1,61 +0,0 @@
1
- import type { SubscriptionTier, TierConfig } from "../core/types.js";
2
- import { getValidOperationTypes, isValidOperationType } from "./index.js";
3
- /**
4
- * Get operation costs as a record
5
- * Returns all configured operation costs from the current config
6
- */
7
- export declare function getOperationCosts(): Record<string, number>;
8
- /**
9
- * Get the credit cost for an operation type
10
- * Uses the configuration system for dynamic costs
11
- *
12
- * @param type - Operation type (must be configured in the system)
13
- * @returns Credit cost for the operation
14
- * @throws Error if the operation type is not configured
15
- */
16
- export declare function getOperationCost(type: string): number;
17
- export { getValidOperationTypes, isValidOperationType };
18
- /**
19
- * Tier configurations with monthly limits and pricing
20
- *
21
- * NOTE: This is kept for backward compatibility.
22
- * The actual configs are loaded from configuration.
23
- * Use getTierConfig() instead of accessing this directly.
24
- */
25
- export declare const TIER_CONFIGS: Record<SubscriptionTier, TierConfig>;
26
- /**
27
- * Get tier configuration
28
- * Uses the configuration system for dynamic configs
29
- *
30
- * @param tier - Subscription tier
31
- * @returns Tier configuration
32
- */
33
- export declare function getTierConfig(tier: SubscriptionTier): TierConfig;
34
- /**
35
- * Get monthly credit limit for a tier
36
- * Returns Infinity for unlimited tier
37
- * Uses the configuration system for dynamic limits
38
- *
39
- * @param tier - Subscription tier
40
- * @returns Monthly credit limit
41
- */
42
- export declare function getMonthlyLimit(tier: SubscriptionTier): number;
43
- /**
44
- * Default credits for new users (free tier)
45
- * Uses the configuration system
46
- */
47
- export declare const DEFAULT_FREE_CREDITS = 25;
48
- /**
49
- * Get default free credits from configuration
50
- */
51
- export declare function getDefaultFreeCredits(): number;
52
- /**
53
- * Reservation expiry time in milliseconds
54
- * Uses the configuration system
55
- */
56
- export declare const RESERVATION_EXPIRY_MS: number;
57
- /**
58
- * Get reservation expiry time from configuration
59
- */
60
- export declare function getReservationExpiryMs(): number;
61
- //# sourceMappingURL=costs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"costs.d.ts","sourceRoot":"","sources":["../../src/config/costs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAKL,sBAAsB,EACtB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE1D;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAGD,OAAO,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,CAAC;AAExD;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,gBAAgB,EAAE,UAAU,CAa7D,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,gBAAgB,GAAG,UAAU,CAEhE;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,CAE9D;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,KAAK,CAAC;AAEvC;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAED;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QAAgB,CAAC;AAEnD;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,MAAM,CAE/C"}
@@ -1,86 +0,0 @@
1
- import { getConfig, getConfigOperationCost, getConfigTierConfig, getConfigMonthlyLimit, getValidOperationTypes, isValidOperationType, } from "./index.js";
2
- /**
3
- * Get operation costs as a record
4
- * Returns all configured operation costs from the current config
5
- */
6
- export function getOperationCosts() {
7
- return { ...getConfig().operationCosts };
8
- }
9
- /**
10
- * Get the credit cost for an operation type
11
- * Uses the configuration system for dynamic costs
12
- *
13
- * @param type - Operation type (must be configured in the system)
14
- * @returns Credit cost for the operation
15
- * @throws Error if the operation type is not configured
16
- */
17
- export function getOperationCost(type) {
18
- return getConfigOperationCost(type);
19
- }
20
- // Re-export validation helpers for convenience
21
- export { getValidOperationTypes, isValidOperationType };
22
- /**
23
- * Tier configurations with monthly limits and pricing
24
- *
25
- * NOTE: This is kept for backward compatibility.
26
- * The actual configs are loaded from configuration.
27
- * Use getTierConfig() instead of accessing this directly.
28
- */
29
- export const TIER_CONFIGS = {
30
- get free() {
31
- return getConfig().tierConfigs.free;
32
- },
33
- get basic() {
34
- return getConfig().tierConfigs.basic;
35
- },
36
- get premium() {
37
- return getConfig().tierConfigs.premium;
38
- },
39
- get unlimited() {
40
- return getConfig().tierConfigs.unlimited;
41
- },
42
- };
43
- /**
44
- * Get tier configuration
45
- * Uses the configuration system for dynamic configs
46
- *
47
- * @param tier - Subscription tier
48
- * @returns Tier configuration
49
- */
50
- export function getTierConfig(tier) {
51
- return getConfigTierConfig(tier);
52
- }
53
- /**
54
- * Get monthly credit limit for a tier
55
- * Returns Infinity for unlimited tier
56
- * Uses the configuration system for dynamic limits
57
- *
58
- * @param tier - Subscription tier
59
- * @returns Monthly credit limit
60
- */
61
- export function getMonthlyLimit(tier) {
62
- return getConfigMonthlyLimit(tier);
63
- }
64
- /**
65
- * Default credits for new users (free tier)
66
- * Uses the configuration system
67
- */
68
- export const DEFAULT_FREE_CREDITS = 25; // Static fallback, actual value from getConfig().defaultFreeCredits
69
- /**
70
- * Get default free credits from configuration
71
- */
72
- export function getDefaultFreeCredits() {
73
- return getConfig().defaultFreeCredits;
74
- }
75
- /**
76
- * Reservation expiry time in milliseconds
77
- * Uses the configuration system
78
- */
79
- export const RESERVATION_EXPIRY_MS = 5 * 60 * 1000; // Static fallback
80
- /**
81
- * Get reservation expiry time from configuration
82
- */
83
- export function getReservationExpiryMs() {
84
- return getConfig().reservationExpiryMs;
85
- }
86
- //# sourceMappingURL=costs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"costs.js","sourceRoot":"","sources":["../../src/config/costs.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACtB,oB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;AACH,MAAM,UAAU,sBAAsB;IACpC,OAAO,SAAS,EAAE,CAAC,mBAAmB,CAAC;AACzC,CAAC"}
@@ -1,165 +0,0 @@
1
- import { z } from "zod";
2
- import type { SubscriptionTier, TierConfig } from "../core/types.js";
3
- /**
4
- * Schema for credit system configuration
5
- */
6
- declare const creditSystemConfigSchema: z.ZodObject<{
7
- /**
8
- * Operation costs in credits
9
- * Keys are operation type names (any non-empty string)
10
- * Values are positive numbers representing credit cost
11
- */
12
- operationCosts: z.ZodRecord<z.ZodString, z.ZodNumber>;
13
- /**
14
- * Tier configurations
15
- */
16
- tierConfigs: z.ZodRecord<z.ZodEnum<["free", "basic", "premium", "unlimited"]>, z.ZodObject<{
17
- tier: z.ZodEnum<["free", "basic", "premium", "unlimited"]>;
18
- monthlyCredits: z.ZodNumber;
19
- priceUsd: z.ZodNumber;
20
- features: z.ZodArray<z.ZodString, "many">;
21
- }, "strip", z.ZodTypeAny, {
22
- tier: "free" | "basic" | "premium" | "unlimited";
23
- monthlyCredits: number;
24
- priceUsd: number;
25
- features: string[];
26
- }, {
27
- tier: "free" | "basic" | "premium" | "unlimited";
28
- monthlyCredits: number;
29
- priceUsd: number;
30
- features: string[];
31
- }>>;
32
- /**
33
- * Reservation expiry time in milliseconds
34
- */
35
- reservationExpiryMs: z.ZodDefault<z.ZodNumber>;
36
- /**
37
- * Default credits for new users (free tier)
38
- */
39
- defaultFreeCredits: z.ZodDefault<z.ZodNumber>;
40
- /**
41
- * Grace period in days after subscription expires before downgrade
42
- */
43
- subscriptionGracePeriodDays: z.ZodDefault<z.ZodNumber>;
44
- /**
45
- * Low balance notification threshold
46
- */
47
- lowBalanceThreshold: z.ZodDefault<z.ZodNumber>;
48
- /**
49
- * Cooldown in hours between low balance notifications
50
- */
51
- lowBalanceNotificationCooldownHours: z.ZodDefault<z.ZodNumber>;
52
- /**
53
- * Feature flags for optional features
54
- */
55
- features: z.ZodDefault<z.ZodObject<{
56
- journalEntries: z.ZodDefault<z.ZodBoolean>;
57
- notifications: z.ZodDefault<z.ZodBoolean>;
58
- subscriptionExpiry: z.ZodDefault<z.ZodBoolean>;
59
- usageHistory: z.ZodDefault<z.ZodBoolean>;
60
- }, "strip", z.ZodTypeAny, {
61
- journalEntries: boolean;
62
- notifications: boolean;
63
- subscriptionExpiry: boolean;
64
- usageHistory: boolean;
65
- }, {
66
- journalEntries?: boolean | undefined;
67
- notifications?: boolean | undefined;
68
- subscriptionExpiry?: boolean | undefined;
69
- usageHistory?: boolean | undefined;
70
- }>>;
71
- }, "strip", z.ZodTypeAny, {
72
- features: {
73
- journalEntries: boolean;
74
- notifications: boolean;
75
- subscriptionExpiry: boolean;
76
- usageHistory: boolean;
77
- };
78
- operationCosts: Record<string, number>;
79
- tierConfigs: Partial<Record<"free" | "basic" | "premium" | "unlimited", {
80
- tier: "free" | "basic" | "premium" | "unlimited";
81
- monthlyCredits: number;
82
- priceUsd: number;
83
- features: string[];
84
- }>>;
85
- reservationExpiryMs: number;
86
- defaultFreeCredits: number;
87
- subscriptionGracePeriodDays: number;
88
- lowBalanceThreshold: number;
89
- lowBalanceNotificationCooldownHours: number;
90
- }, {
91
- operationCosts: Record<string, number>;
92
- tierConfigs: Partial<Record<"free" | "basic" | "premium" | "unlimited", {
93
- tier: "free" | "basic" | "premium" | "unlimited";
94
- monthlyCredits: number;
95
- priceUsd: number;
96
- features: string[];
97
- }>>;
98
- features?: {
99
- journalEntries?: boolean | undefined;
100
- notifications?: boolean | undefined;
101
- subscriptionExpiry?: boolean | undefined;
102
- usageHistory?: boolean | undefined;
103
- } | undefined;
104
- reservationExpiryMs?: number | undefined;
105
- defaultFreeCredits?: number | undefined;
106
- subscriptionGracePeriodDays?: number | undefined;
107
- lowBalanceThreshold?: number | undefined;
108
- lowBalanceNotificationCooldownHours?: number | undefined;
109
- }>;
110
- /**
111
- * Credit system configuration type
112
- */
113
- export type CreditSystemConfig = z.infer<typeof creditSystemConfigSchema>;
114
- /**
115
- * Load configuration from environment variables
116
- * Can be extended to load from other sources (file, remote config, etc.)
117
- *
118
- * Operation costs are loaded from CREDITS_OPERATION_COSTS as JSON:
119
- * CREDITS_OPERATION_COSTS={"story_generation":5,"image_generation":10}
120
- */
121
- export declare function loadConfigFromEnv(): Partial<CreditSystemConfig>;
122
- /**
123
- * Initialize configuration
124
- * Merges default config with environment overrides
125
- */
126
- export declare function initializeConfig(overrides?: Partial<CreditSystemConfig>): CreditSystemConfig;
127
- /**
128
- * Get the current configuration
129
- */
130
- export declare function getConfig(): CreditSystemConfig;
131
- /**
132
- * Get all valid operation types from configuration
133
- * Returns the keys of the operationCosts object
134
- */
135
- export declare function getValidOperationTypes(): string[];
136
- /**
137
- * Check if an operation type is valid (configured in the system)
138
- * @param type - The operation type to check
139
- * @returns true if the operation type is configured
140
- */
141
- export declare function isValidOperationType(type: string): boolean;
142
- /**
143
- * Get operation cost from configuration
144
- * @throws Error if the operation type is not configured
145
- */
146
- export declare function getConfigOperationCost(operationType: string): number;
147
- /**
148
- * Get tier configuration from configuration
149
- */
150
- export declare function getConfigTierConfig(tier: SubscriptionTier): TierConfig;
151
- /**
152
- * Get monthly limit for a tier from configuration
153
- * Returns Infinity for unlimited tier
154
- */
155
- export declare function getConfigMonthlyLimit(tier: SubscriptionTier): number;
156
- /**
157
- * Check if a feature is enabled
158
- */
159
- export declare function isFeatureEnabled(feature: keyof CreditSystemConfig["features"]): boolean;
160
- /**
161
- * Reset configuration to defaults (for testing)
162
- */
163
- export declare function resetConfig(): void;
164
- export {};
165
- //# sourceMappingURL=index.d.ts.map