1
+ import type { AuthorizationInput } from "./authorize/index.js" ;
1
2
import type { ServiceName } from "./services.js" ;
2
3
3
4
export type UserOpData = {
@@ -14,7 +15,6 @@ export type PolicyResult = {
14
15
} ;
15
16
16
17
export type CoreServiceConfig = {
17
- enforceAuth ?: boolean ;
18
18
apiUrl : string ;
19
19
serviceScope : ServiceName ;
20
20
serviceApiKey : string ;
@@ -23,105 +23,131 @@ export type CoreServiceConfig = {
23
23
includeUsage ?: boolean ;
24
24
} ;
25
25
26
- type Usage = {
27
- storage ?: {
28
- sumFileSizeBytes : number ;
29
- } ;
30
- embeddedWallets ?: {
31
- countWalletAddresses : number ;
26
+ export type TeamAndProjectResponse = {
27
+ team : TeamResponse ;
28
+ project ?: ProjectResponse | null ;
29
+ } ;
30
+
31
+ export type ApiResponse = {
32
+ data : TeamAndProjectResponse | null ;
33
+ error : {
34
+ code : string ;
35
+ statusCode : number ;
36
+ message : string ;
32
37
} ;
33
38
} ;
34
39
35
- export type ApiKeyMetadata = {
40
+ export type TeamResponse = {
36
41
id : string ;
37
- key : string ;
38
- accountId : string ;
39
- accountStatus : "noCustomer" | "noPayment" | "validPayment" | "invalidPayment" ;
40
- accountPlan : "free" | "growth" | "pro" | "enterprise" ;
41
- creatorWalletAddress : string ;
42
- secretHash : string ;
43
- walletAddresses : string [ ] ;
44
- domains : string [ ] ;
45
- bundleIds : string [ ] ;
46
- redirectUrls : string [ ] ;
47
- services : {
48
- name : string ;
49
- targetAddresses : string [ ] ;
50
- actions : string [ ] ;
51
- } [ ] ;
52
- usage ?: Usage ;
53
- limits : Partial < Record < ServiceName , number > > ;
54
- rateLimits : Partial < Record < ServiceName , number > > ;
55
- policyResult ?: PolicyResult ;
42
+ name : string ;
43
+ slug : string ;
44
+ image : string | null ;
45
+ billingPlan : string ;
46
+ createdAt : Date ;
47
+ updatedAt : Date | null ;
48
+ billingEmail : string | null ;
49
+ billingStatus : string | null ;
50
+ growthTrialEligible : boolean | null ;
51
+ enabledScopes : ServiceName [ ] ;
56
52
} ;
57
53
58
- export type AccountMetadata = {
54
+ export type ProjectResponse = {
59
55
id : string ;
56
+ teamId : string ;
57
+ createdAt : Date ;
58
+ updatedAt : Date | null ;
59
+ publishableKey : string ;
60
60
name : string ;
61
- creatorWalletAddress : string ;
62
- usage ?: Usage ;
63
- limits : Partial < Record < ServiceName , number > > ;
64
- rateLimits : Partial < Record < ServiceName , number > > ;
61
+ slug : string ;
62
+ image : string | null ;
63
+ domains : string [ ] ;
64
+ bundleIds : string [ ] ;
65
+ services : (
66
+ | {
67
+ name : "pay" ;
68
+ actions : never [ ] ;
69
+ payoutAddress : string | null ;
70
+ }
71
+ | {
72
+ name : "storage" ;
73
+ actions : ( "read" | "write" ) [ ] ;
74
+ }
75
+ | {
76
+ name : "rpc" ;
77
+ actions : never [ ] ;
78
+ }
79
+ | {
80
+ name : "insight" ;
81
+ actions : never [ ] ;
82
+ }
83
+ | {
84
+ name : "nebula" ;
85
+ actions : never [ ] ;
86
+ }
87
+ | {
88
+ name : "bundler" ;
89
+ actions : never [ ] ;
90
+ allowedChainIds ?: number [ ] | null ;
91
+ allowedContractAddresses ?: string [ ] | null ;
92
+ allowedWallets ?: string [ ] | null ;
93
+ blockedWallets ?: string [ ] | null ;
94
+ bypassWallets ?: string [ ] | null ;
95
+ limits ?: {
96
+ global ?: {
97
+ maxSpend : string ;
98
+ maxSpendUnit : "usd" | "native" ;
99
+ } | null ;
100
+ } | null ;
101
+ serverVerifier ?: {
102
+ url : string ;
103
+ headers ?: {
104
+ key : string ;
105
+ value : string ;
106
+ } [ ] ;
107
+ } | null ;
108
+ }
109
+ | {
110
+ name : "embeddedWallets" ;
111
+ actions : never [ ] ;
112
+ redirectUrls ?: string [ ] | null ;
113
+ applicationName ?: string | null ;
114
+ applicationImageUrl ?: string | null ;
115
+ recoveryShareManagement ?: string | null ;
116
+ customAuthentication ?: CustomAuthenticationServiceSchema | null ;
117
+ customAuthEndpoint ?: CustomAuthEndpointServiceSchema | null ;
118
+ }
119
+ ) [ ] ;
120
+ walletAddresses : string [ ] ;
65
121
} ;
66
122
67
- export type ApiResponse = {
68
- data : ApiKeyMetadata | null ;
69
- error : {
70
- code : string ;
71
- statusCode : number ;
72
- message : string ;
73
- } ;
123
+ type CustomAuthenticationServiceSchema = {
124
+ jwksUri : string ;
125
+ aud : string ;
74
126
} ;
75
127
76
- export type ApiAccountResponse = {
77
- data : AccountMetadata | null ;
78
- error : {
79
- code : string ;
80
- statusCode : number ;
81
- message : string ;
82
- } ;
128
+ type CustomAuthEndpointServiceSchema = {
129
+ authEndpoint : string ;
130
+ customHeaders : {
131
+ key : string ;
132
+ value : string ;
133
+ } [ ] ;
83
134
} ;
84
135
85
- export async function fetchKeyMetadataFromApi (
86
- clientId : string ,
136
+ export async function fetchTeamAndProject (
137
+ authData : AuthorizationInput ,
87
138
config : CoreServiceConfig ,
88
139
) : Promise < ApiResponse > {
89
- const { apiUrl, serviceScope, serviceApiKey, includeUsage = true } = config ;
90
- const url = `${ apiUrl } /v1/keys/use?clientId=${ clientId } &scope=${ serviceScope } &includeUsage=${ includeUsage } ` ;
91
- const response = await fetch ( url , {
92
- method : "GET" ,
93
- headers : {
94
- "x-service-api-key" : serviceApiKey ,
95
- "content-type" : "application/json" ,
96
- } ,
97
- } ) ;
98
-
99
- let text = "" ;
100
- try {
101
- text = await response . text ( ) ;
102
- return JSON . parse ( text ) ;
103
- } catch {
104
- throw new Error (
105
- `Error fetching key metadata from API: ${ response . status } - ${ text } ` ,
106
- ) ;
107
- }
108
- }
140
+ const { apiUrl, serviceApiKey } = config ;
109
141
110
- export async function fetchAccountFromApi (
111
- jwt : string ,
112
- config : CoreServiceConfig ,
113
- useWalletAuth : boolean ,
114
- ) : Promise < ApiAccountResponse > {
115
- const { apiUrl, serviceApiKey, includeUsage = true } = config ;
116
- const url = useWalletAuth
117
- ? `${ apiUrl } /v1/wallet/me?includeUsage=${ includeUsage } `
118
- : `${ apiUrl } /v1/account/me?includeUsage=${ includeUsage } ` ;
142
+ const clientId = authData . clientId ;
143
+ const url = `${ apiUrl } /v2/keys/use${ clientId ? `?clientId=${ clientId } ` : "" } ` ;
119
144
const response = await fetch ( url , {
120
145
method : "GET" ,
121
146
headers : {
147
+ ...( authData . secretKey ? { "x-secret-key" : authData . secretKey } : { } ) ,
148
+ ...( authData . jwt ? { Authorization : `Bearer ${ authData . jwt } ` } : { } ) ,
122
149
"x-service-api-key" : serviceApiKey ,
123
150
"content-type" : "application/json" ,
124
- authorization : `Bearer ${ jwt } ` ,
125
151
} ,
126
152
} ) ;
127
153
@@ -131,13 +157,13 @@ export async function fetchAccountFromApi(
131
157
return JSON . parse ( text ) ;
132
158
} catch {
133
159
throw new Error (
134
- `Error fetching account from API: ${ response . status } - ${ text } ` ,
160
+ `Error fetching key metadata from API: ${ response . status } - ${ text } ` ,
135
161
) ;
136
162
}
137
163
}
138
164
139
165
export async function updateRateLimitedAt (
140
- apiKeyId : string ,
166
+ projectId : string ,
141
167
config : CoreServiceConfig ,
142
168
) : Promise < void > {
143
169
const { apiUrl, serviceScope : scope , serviceApiKey } = config ;
@@ -151,7 +177,7 @@ export async function updateRateLimitedAt(
151
177
"content-type" : "application/json" ,
152
178
} ,
153
179
body : JSON . stringify ( {
154
- apiKeyId,
180
+ apiKeyId : projectId , // projectId is the apiKeyId
155
181
scope,
156
182
} ) ,
157
183
} ) ;
0 commit comments