Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ee71dd3

Browse files
committedMar 24, 2025·
fix: update types
1 parent 97612b4 commit ee71dd3

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed
 

‎packages/open-payments/src/client/grant.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('grant', (): void => {
132132
client,
133133
...deps
134134
}).request({ url }, testGrantRequest as GrantRequest)
135-
).rejects.toThrowError('Invalid Grant Request')
135+
).rejects.toThrow('Invalid Grant Request')
136136
}
137137
}
138138
)

‎packages/open-payments/src/client/grant.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
GrantContinuation,
1313
GrantRequest,
1414
GrantContinuationRequest,
15-
OutgoingPaymentLimitWithDebitAmount,
16-
OutgoingPaymentLimitWithReceiveAmount
15+
AccessOutgoingWithDebitAmount,
16+
AccessOutgoingWithReceiveAmount
1717
} from '../types'
1818
import { post, deleteRequest } from './requests'
1919

@@ -64,13 +64,14 @@ export const createGrantRoutes = (deps: GrantRouteDeps): GrantRoutes => {
6464
(el) => el.type === 'outgoing-payment'
6565
)
6666
if (
67-
(outgoingPaymentAccess?.limits as OutgoingPaymentLimitWithDebitAmount)
67+
(outgoingPaymentAccess?.limits as AccessOutgoingWithDebitAmount)
6868
?.debitAmount &&
69-
(outgoingPaymentAccess?.limits as OutgoingPaymentLimitWithReceiveAmount)
69+
(outgoingPaymentAccess?.limits as AccessOutgoingWithReceiveAmount)
7070
?.receiveAmount
7171
) {
7272
throw new OpenPaymentsClientError('Invalid Grant Request', {
73-
description: 'Invalid Request'
73+
description:
74+
'Only one of "debitAmount" or "receiveAmount" may be specified.'
7475
})
7576
}
7677
return post(

‎packages/open-payments/src/openapi/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('OpenAPI', (): void => {
5757
'application/json'
5858
]['schema']['properties']['access_token']['properties']['access'][
5959
'items'
60-
]['oneOf'][1]['properties']['limits']['anyOf'][1]['properties'][
60+
]['oneOf'][1]['properties']['limits']['oneOf'][1]['properties'][
6161
'debitAmount'
6262
]['properties']
6363
).sort()

‎packages/open-payments/src/types.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
import {
77
components as ASComponents,
88
paths as ASPaths,
9-
operations as ASOperations,
10-
components
9+
operations as ASOperations
1110
} from './openapi/generated/auth-server-types'
1211
import {
1312
components as WAComponents,
@@ -94,7 +93,7 @@ export type GrantContinuation = {
9493
continue: ASComponents['schemas']['continue']
9594
}
9695
export type GrantRequest = {
97-
access_token: ASOperations['post-request']['requestBody']['content']['application/json']['access_token']
96+
access_token: { access: GrantRequestAccessItem[] }
9897
client: ASOperations['post-request']['requestBody']['content']['application/json']['client']
9998
interact?: ASOperations['post-request']['requestBody']['content']['application/json']['interact']
10099
}
@@ -162,11 +161,34 @@ export const AccessAction: Record<
162161
ListAll: 'list-all'
163162
})
164163

165-
export type OutgoingPaymentLimitWithDebitAmount = Extract<
166-
ASComponents['schemas']['access-outgoing']['limits'],
167-
{ debitAmount: components['schemas']['amount'] }
168-
>
169-
export type OutgoingPaymentLimitWithReceiveAmount = Extract<
170-
ASComponents['schemas']['access-outgoing']['limits'],
171-
{ receiveAmount: components['schemas']['amount'] }
164+
// From: https://github.com/microsoft/TypeScript/issues/14094#issuecomment-373782604
165+
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never }
166+
type XOR<T, U> = T | U extends object
167+
? (Without<T, U> & U) | (Without<U, T> & T)
168+
: T | U
169+
type XOR3<T, U, V> = XOR<T, XOR<U, V>>
170+
export type AccessOutgoingBase = {
171+
receiver?: ASComponents['schemas']['receiver']
172+
interval?: ASComponents['schemas']['interval']
173+
}
174+
export type AccessOutgoingWithDebitAmount = AccessOutgoingBase & {
175+
debitAmount: ASComponents['schemas']['amount']
176+
}
177+
export type AccessOutgoingWithReceiveAmount = AccessOutgoingBase & {
178+
receiveAmount: ASComponents['schemas']['amount']
179+
}
180+
export type AccessOutgoingLimits = XOR3<
181+
AccessOutgoingBase,
182+
AccessOutgoingWithDebitAmount,
183+
AccessOutgoingWithReceiveAmount
172184
>
185+
export type AccessOutgoing = {
186+
type: 'outgoing-payment'
187+
actions: ('create' | 'read' | 'read-all' | 'list' | 'list-all')[]
188+
identifier: string
189+
limits?: AccessOutgoingLimits
190+
}
191+
export type GrantRequestAccessItem =
192+
| ASComponents['schemas']['access-incoming']
193+
| AccessOutgoing
194+
| ASComponents['schemas']['access-quote']

0 commit comments

Comments
 (0)
Please sign in to comment.