mppx 0.6.19 → 0.6.21
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/CHANGELOG.md +14 -0
- package/dist/Challenge.d.ts +2 -2
- package/dist/Challenge.d.ts.map +1 -1
- package/dist/Challenge.js +1 -1
- package/dist/Challenge.js.map +1 -1
- package/dist/Method.d.ts +34 -0
- package/dist/Method.d.ts.map +1 -1
- package/dist/Method.js +3 -1
- package/dist/Method.js.map +1 -1
- package/dist/Receipt.d.ts +1 -0
- package/dist/Receipt.d.ts.map +1 -1
- package/dist/Receipt.js +2 -0
- package/dist/Receipt.js.map +1 -1
- package/dist/client/Methods.d.ts +1 -0
- package/dist/client/Methods.d.ts.map +1 -1
- package/dist/client/Methods.js +1 -0
- package/dist/client/Methods.js.map +1 -1
- package/dist/client/Mppx.d.ts +12 -1
- package/dist/client/Mppx.d.ts.map +1 -1
- package/dist/client/Mppx.js +127 -10
- package/dist/client/Mppx.js.map +1 -1
- package/dist/client/internal/Fetch.d.ts +69 -1
- package/dist/client/internal/Fetch.d.ts.map +1 -1
- package/dist/client/internal/Fetch.js +250 -20
- package/dist/client/internal/Fetch.js.map +1 -1
- package/dist/middlewares/elysia.d.ts.map +1 -1
- package/dist/middlewares/elysia.js +14 -0
- package/dist/middlewares/elysia.js.map +1 -1
- package/dist/middlewares/express.d.ts.map +1 -1
- package/dist/middlewares/express.js +1 -2
- package/dist/middlewares/express.js.map +1 -1
- package/dist/middlewares/hono.d.ts.map +1 -1
- package/dist/middlewares/hono.js +14 -0
- package/dist/middlewares/hono.js.map +1 -1
- package/dist/middlewares/internal/mppx.d.ts +1 -1
- package/dist/middlewares/internal/mppx.d.ts.map +1 -1
- package/dist/middlewares/internal/mppx.js +2 -1
- package/dist/middlewares/internal/mppx.js.map +1 -1
- package/dist/middlewares/nextjs.d.ts.map +1 -1
- package/dist/middlewares/nextjs.js +14 -0
- package/dist/middlewares/nextjs.js.map +1 -1
- package/dist/proxy/Proxy.d.ts.map +1 -1
- package/dist/proxy/Proxy.js +2 -2
- package/dist/proxy/Proxy.js.map +1 -1
- package/dist/proxy/Service.d.ts.map +1 -1
- package/dist/proxy/Service.js +1 -1
- package/dist/proxy/Service.js.map +1 -1
- package/dist/server/Mppx.d.ts +96 -5
- package/dist/server/Mppx.d.ts.map +1 -1
- package/dist/server/Mppx.js +739 -115
- package/dist/server/Mppx.js.map +1 -1
- package/dist/stripe/server/internal/html.gen.d.ts +1 -1
- package/dist/stripe/server/internal/html.gen.d.ts.map +1 -1
- package/dist/stripe/server/internal/html.gen.js +1 -1
- package/dist/stripe/server/internal/html.gen.js.map +1 -1
- package/dist/tempo/Methods.d.ts +96 -0
- package/dist/tempo/Methods.d.ts.map +1 -1
- package/dist/tempo/Methods.js +97 -0
- package/dist/tempo/Methods.js.map +1 -1
- package/dist/tempo/client/Methods.d.ts +3 -0
- package/dist/tempo/client/Methods.d.ts.map +1 -1
- package/dist/tempo/client/Methods.js +3 -0
- package/dist/tempo/client/Methods.js.map +1 -1
- package/dist/tempo/client/Subscription.d.ts +114 -0
- package/dist/tempo/client/Subscription.d.ts.map +1 -0
- package/dist/tempo/client/Subscription.js +100 -0
- package/dist/tempo/client/Subscription.js.map +1 -0
- package/dist/tempo/client/index.d.ts +1 -0
- package/dist/tempo/client/index.d.ts.map +1 -1
- package/dist/tempo/client/index.js +1 -0
- package/dist/tempo/client/index.js.map +1 -1
- package/dist/tempo/index.d.ts +1 -0
- package/dist/tempo/index.d.ts.map +1 -1
- package/dist/tempo/index.js +1 -0
- package/dist/tempo/index.js.map +1 -1
- package/dist/tempo/server/Methods.d.ts +5 -0
- package/dist/tempo/server/Methods.d.ts.map +1 -1
- package/dist/tempo/server/Methods.js +5 -0
- package/dist/tempo/server/Methods.js.map +1 -1
- package/dist/tempo/server/Subscription.d.ts +221 -0
- package/dist/tempo/server/Subscription.d.ts.map +1 -0
- package/dist/tempo/server/Subscription.js +637 -0
- package/dist/tempo/server/Subscription.js.map +1 -0
- package/dist/tempo/server/index.d.ts +1 -0
- package/dist/tempo/server/index.d.ts.map +1 -1
- package/dist/tempo/server/index.js +1 -0
- package/dist/tempo/server/index.js.map +1 -1
- package/dist/tempo/server/internal/html.gen.d.ts +1 -1
- package/dist/tempo/server/internal/html.gen.d.ts.map +1 -1
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/server/internal/html.gen.js.map +1 -1
- package/dist/tempo/subscription/KeyAuthorization.d.ts +282 -0
- package/dist/tempo/subscription/KeyAuthorization.d.ts.map +1 -0
- package/dist/tempo/subscription/KeyAuthorization.js +297 -0
- package/dist/tempo/subscription/KeyAuthorization.js.map +1 -0
- package/dist/tempo/subscription/Receipt.d.ts +10 -0
- package/dist/tempo/subscription/Receipt.d.ts.map +1 -0
- package/dist/tempo/subscription/Receipt.js +16 -0
- package/dist/tempo/subscription/Receipt.js.map +1 -0
- package/dist/tempo/subscription/Store.d.ts +99 -0
- package/dist/tempo/subscription/Store.d.ts.map +1 -0
- package/dist/tempo/subscription/Store.js +292 -0
- package/dist/tempo/subscription/Store.js.map +1 -0
- package/dist/tempo/subscription/Types.d.ts +65 -0
- package/dist/tempo/subscription/Types.d.ts.map +1 -0
- package/dist/tempo/subscription/Types.js +2 -0
- package/dist/tempo/subscription/Types.js.map +1 -0
- package/dist/tempo/subscription/index.d.ts +6 -0
- package/dist/tempo/subscription/index.d.ts.map +1 -0
- package/dist/tempo/subscription/index.js +4 -0
- package/dist/tempo/subscription/index.js.map +1 -0
- package/dist/zod.d.ts +7 -0
- package/dist/zod.d.ts.map +1 -1
- package/dist/zod.js +18 -0
- package/dist/zod.js.map +1 -1
- package/package.json +3 -3
- package/src/Challenge.test.ts +13 -0
- package/src/Challenge.ts +3 -3
- package/src/Method.ts +46 -1
- package/src/Receipt.ts +2 -0
- package/src/client/Methods.ts +1 -0
- package/src/client/Mppx.test-d.ts +55 -0
- package/src/client/Mppx.test.ts +181 -0
- package/src/client/Mppx.ts +248 -16
- package/src/client/internal/Fetch.test-d.ts +31 -0
- package/src/client/internal/Fetch.test.ts +261 -0
- package/src/client/internal/Fetch.ts +467 -24
- package/src/middlewares/elysia.test.ts +31 -1
- package/src/middlewares/elysia.ts +13 -0
- package/src/middlewares/express.ts +1 -5
- package/src/middlewares/hono.test.ts +30 -1
- package/src/middlewares/hono.ts +13 -0
- package/src/middlewares/internal/mppx.ts +5 -6
- package/src/middlewares/nextjs.test.ts +28 -1
- package/src/middlewares/nextjs.ts +13 -0
- package/src/proxy/Proxy.test.ts +69 -0
- package/src/proxy/Proxy.ts +2 -5
- package/src/proxy/Service.test.ts +34 -0
- package/src/proxy/Service.ts +7 -0
- package/src/server/Mppx.authorize.test.ts +210 -0
- package/src/server/Mppx.test-d.ts +73 -1
- package/src/server/Mppx.test.ts +965 -3
- package/src/server/Mppx.ts +1138 -140
- package/src/stripe/server/internal/html/package.json +1 -1
- package/src/stripe/server/internal/html.gen.ts +1 -1
- package/src/tempo/Methods.test.ts +131 -0
- package/src/tempo/Methods.ts +136 -0
- package/src/tempo/Subscription.integration.test.ts +591 -0
- package/src/tempo/client/Methods.ts +3 -0
- package/src/tempo/client/Subscription.test.ts +131 -0
- package/src/tempo/client/Subscription.ts +155 -0
- package/src/tempo/client/index.ts +1 -0
- package/src/tempo/index.ts +1 -0
- package/src/tempo/server/Methods.ts +5 -0
- package/src/tempo/server/Subscription.test.ts +1410 -0
- package/src/tempo/server/Subscription.ts +1014 -0
- package/src/tempo/server/index.ts +1 -0
- package/src/tempo/server/internal/html/package.json +1 -1
- package/src/tempo/server/internal/html.gen.ts +1 -1
- package/src/tempo/subscription/KeyAuthorization.test.ts +204 -0
- package/src/tempo/subscription/KeyAuthorization.ts +394 -0
- package/src/tempo/subscription/Receipt.ts +28 -0
- package/src/tempo/subscription/Store.test.ts +554 -0
- package/src/tempo/subscription/Store.ts +431 -0
- package/src/tempo/subscription/Types.ts +68 -0
- package/src/tempo/subscription/index.ts +23 -0
- package/src/zod.test.ts +23 -1
- package/src/zod.ts +24 -0
package/src/middlewares/hono.ts
CHANGED
|
@@ -63,11 +63,24 @@ export function payment<const intent extends Mppx_internal.AnyMethodFn>(
|
|
|
63
63
|
: c.req.raw
|
|
64
64
|
const result = await intent(options)(request)
|
|
65
65
|
if (result.status === 402) return result.challenge
|
|
66
|
+
const managementResponse = getManagementResponse(result)
|
|
67
|
+
if (managementResponse) return managementResponse
|
|
66
68
|
await next()
|
|
67
69
|
c.res = result.withReceipt(c.res)
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
72
|
|
|
73
|
+
function getManagementResponse(result: { withReceipt: (response?: Response) => Response }) {
|
|
74
|
+
try {
|
|
75
|
+
return result.withReceipt()
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (Mppx_core.isMissingReceiptResponseError(error)) {
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
throw error
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
71
84
|
export type DiscoveryConfig = Omit<GenerateConfig, 'routes'> & {
|
|
72
85
|
auto?: boolean
|
|
73
86
|
path?: string
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DiscoveryHandler } from '../../discovery/OpenApi.js'
|
|
2
2
|
import type * as Method from '../../Method.js'
|
|
3
|
+
import { reservedMppxKeys } from '../../server/Mppx.js'
|
|
3
4
|
import type * as Mppx from '../../server/Mppx.js'
|
|
4
5
|
|
|
5
6
|
export type AnyMethodFn = Mppx.AnyMethodFn
|
|
@@ -17,11 +18,8 @@ type WrapNested<obj, handler> = {
|
|
|
17
18
|
export type Wrap<mppx, handler> = {
|
|
18
19
|
// `compose` is omitted — it returns a raw HTTP handler, not a
|
|
19
20
|
// middleware-shaped result. Use `Mppx.compose()` static instead.
|
|
20
|
-
//
|
|
21
|
-
[key in keyof mppx as key extends 'compose' ? never : key]: key extends
|
|
22
|
-
| 'methods'
|
|
23
|
-
| 'realm'
|
|
24
|
-
| 'transport'
|
|
21
|
+
// Reserved Mppx keys are copied through unchanged, not wrapped as handlers.
|
|
22
|
+
[key in keyof mppx as key extends 'compose' ? never : key]: key extends Mppx.ReservedKey
|
|
25
23
|
? mppx[key]
|
|
26
24
|
: mppx[key] extends (options: infer options) => any
|
|
27
25
|
? (o: options) => handler & DiscoveryMeta
|
|
@@ -54,7 +52,8 @@ export function wrap<mppx extends Mppx.Mppx<any, any>, handler>(
|
|
|
54
52
|
}
|
|
55
53
|
result[key] = wrapWithMeta
|
|
56
54
|
// Also set shorthand intent key if Mppx registered it (no collision)
|
|
57
|
-
if ((mppx as any)[mi.intent])
|
|
55
|
+
if (!reservedMppxKeys.has(mi.intent) && (mppx as any)[mi.intent])
|
|
56
|
+
result[mi.intent] = wrapWithMeta
|
|
58
57
|
// Build nested handlers: wrapped.tempo.charge(...)
|
|
59
58
|
if (!result[mi.name] || typeof result[mi.name] !== 'object')
|
|
60
59
|
result[mi.name] = {} as Record<string, unknown>
|
|
@@ -2,7 +2,7 @@ import * as http from 'node:http'
|
|
|
2
2
|
|
|
3
3
|
import { Challenge, Credential, Receipt } from 'mppx'
|
|
4
4
|
import { Mppx as Mppx_client, session as sessionIntent, tempo as tempo_client } from 'mppx/client'
|
|
5
|
-
import { Mppx, discovery } from 'mppx/nextjs'
|
|
5
|
+
import { Mppx, discovery, payment } from 'mppx/nextjs'
|
|
6
6
|
import { tempo as tempo_server } from 'mppx/server'
|
|
7
7
|
import type { Address } from 'viem'
|
|
8
8
|
import { Addresses } from 'viem/tempo'
|
|
@@ -34,6 +34,33 @@ function createServer(handler: (request: Request) => Promise<Response> | Respons
|
|
|
34
34
|
|
|
35
35
|
const secretKey = 'test-secret-key'
|
|
36
36
|
|
|
37
|
+
describe('payment', () => {
|
|
38
|
+
test('short-circuits management responses', async () => {
|
|
39
|
+
let handlerRan = false
|
|
40
|
+
const intent = () => async () => ({
|
|
41
|
+
status: 200 as const,
|
|
42
|
+
withReceipt: () =>
|
|
43
|
+
new Response(null, {
|
|
44
|
+
headers: { 'Payment-Receipt': 'management-receipt' },
|
|
45
|
+
status: 204,
|
|
46
|
+
}),
|
|
47
|
+
})
|
|
48
|
+
const handler = payment(intent as any, {} as any, () => {
|
|
49
|
+
handlerRan = true
|
|
50
|
+
return Response.json({ data: 'content' })
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const server = await createServer(handler)
|
|
54
|
+
const response = await globalThis.fetch(server.url)
|
|
55
|
+
expect(response.status).toBe(204)
|
|
56
|
+
expect(response.headers.get('Payment-Receipt')).toBe('management-receipt')
|
|
57
|
+
expect(await response.text()).toBe('')
|
|
58
|
+
expect(handlerRan).toBe(false)
|
|
59
|
+
|
|
60
|
+
server.close()
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
37
64
|
function createChargeHarness(feePayer: boolean) {
|
|
38
65
|
const mppx = Mppx.create({
|
|
39
66
|
methods: [
|
|
@@ -61,11 +61,24 @@ export function payment<const intent extends Mppx_internal.AnyMethodFn>(
|
|
|
61
61
|
return async (request) => {
|
|
62
62
|
const result = await intent(options)(request)
|
|
63
63
|
if (result.status === 402) return result.challenge
|
|
64
|
+
const managementResponse = getManagementResponse(result)
|
|
65
|
+
if (managementResponse) return managementResponse
|
|
64
66
|
const response = await handler(request)
|
|
65
67
|
return result.withReceipt(response)
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
function getManagementResponse(result: { withReceipt: (response?: Response) => Response }) {
|
|
72
|
+
try {
|
|
73
|
+
return result.withReceipt()
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (Mppx_core.isMissingReceiptResponseError(error)) {
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
throw error
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
69
82
|
export type DiscoveryConfig = Omit<GenerateConfig, 'routes'> & {
|
|
70
83
|
routes?: RouteConfig[]
|
|
71
84
|
}
|
package/src/proxy/Proxy.test.ts
CHANGED
|
@@ -111,6 +111,75 @@ function createUpstream(handler: (req: Request) => Response | Promise<Response>)
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
describe('create', () => {
|
|
114
|
+
test('behavior: paid routes emit mppx server events', async () => {
|
|
115
|
+
const events: string[] = []
|
|
116
|
+
upstream = await createUpstream(() => Response.json({ data: 'ok' }))
|
|
117
|
+
|
|
118
|
+
const method = Method.from({
|
|
119
|
+
name: 'mock',
|
|
120
|
+
intent: 'charge',
|
|
121
|
+
schema: {
|
|
122
|
+
credential: { payload: z.object({ token: z.string() }) },
|
|
123
|
+
request: z.object({ amount: z.string() }),
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
const handler = Mppx_server.create({
|
|
127
|
+
methods: [
|
|
128
|
+
Method.toServer(method, {
|
|
129
|
+
async verify() {
|
|
130
|
+
return Receipt.from({
|
|
131
|
+
method: 'mock',
|
|
132
|
+
status: 'success',
|
|
133
|
+
timestamp: new Date().toISOString(),
|
|
134
|
+
reference: 'proxy-reference',
|
|
135
|
+
})
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
],
|
|
139
|
+
realm: 'api.example.com',
|
|
140
|
+
secretKey,
|
|
141
|
+
})
|
|
142
|
+
handler.onChallengeCreated((context) => {
|
|
143
|
+
events.push(`challenge:${context.error?.name}`)
|
|
144
|
+
})
|
|
145
|
+
handler.onPaymentSuccess((context) => {
|
|
146
|
+
events.push(`payment:${context.receipt.reference}`)
|
|
147
|
+
})
|
|
148
|
+
handler.onPaymentFailed((context) => {
|
|
149
|
+
events.push(`failed:${context.error.name}`)
|
|
150
|
+
})
|
|
151
|
+
const proxy = ApiProxy.create({
|
|
152
|
+
services: [
|
|
153
|
+
Service.from('api', {
|
|
154
|
+
baseUrl: upstream.url,
|
|
155
|
+
routes: {
|
|
156
|
+
'GET /v1/data': handler['mock/charge']({ amount: '1' }),
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
],
|
|
160
|
+
})
|
|
161
|
+
proxyServer = await Http.createServer(proxy.listener)
|
|
162
|
+
|
|
163
|
+
const challengeResponse = await fetch(`${proxyServer.url}/api/v1/data`)
|
|
164
|
+
expect(challengeResponse.status).toBe(402)
|
|
165
|
+
|
|
166
|
+
const challenge = Challenge.fromResponse(challengeResponse)
|
|
167
|
+
const authorization = Credential.serialize(
|
|
168
|
+
Credential.from({
|
|
169
|
+
challenge,
|
|
170
|
+
payload: { token: 'ok' },
|
|
171
|
+
}),
|
|
172
|
+
)
|
|
173
|
+
const paid = await fetch(`${proxyServer.url}/api/v1/data`, {
|
|
174
|
+
headers: { Authorization: authorization },
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
expect(paid.status).toBe(200)
|
|
178
|
+
expect(paid.headers.get('Payment-Receipt')).toBeTruthy()
|
|
179
|
+
expect(await paid.json()).toEqual({ data: 'ok' })
|
|
180
|
+
expect(events).toEqual(['challenge:PaymentRequiredError', 'payment:proxy-reference'])
|
|
181
|
+
})
|
|
182
|
+
|
|
114
183
|
test('behavior: GET /openapi.json returns discovery JSON', async () => {
|
|
115
184
|
const proxy = ApiProxy.create({
|
|
116
185
|
categories: ['gateway'],
|
package/src/proxy/Proxy.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type * as http from 'node:http'
|
|
|
3
3
|
import * as Credential from '../Credential.js'
|
|
4
4
|
import { generateProxy } from '../discovery/OpenApi.js'
|
|
5
5
|
import * as Scope from '../server/internal/scope.js'
|
|
6
|
+
import * as Mppx from '../server/Mppx.js'
|
|
6
7
|
import * as Request from '../server/Request.js'
|
|
7
8
|
import * as Headers from './internal/Headers.js'
|
|
8
9
|
import * as Route from './internal/Route.js'
|
|
@@ -146,11 +147,7 @@ export function create(config: create.Config): Proxy {
|
|
|
146
147
|
try {
|
|
147
148
|
return (result.withReceipt as () => Response)()
|
|
148
149
|
} catch (error) {
|
|
149
|
-
if (
|
|
150
|
-
error instanceof Error &&
|
|
151
|
-
error.message === 'withReceipt() requires a response argument'
|
|
152
|
-
)
|
|
153
|
-
return null
|
|
150
|
+
if (Mppx.isMissingReceiptResponseError(error)) return null
|
|
154
151
|
throw error
|
|
155
152
|
}
|
|
156
153
|
})()
|
|
@@ -122,6 +122,40 @@ describe('paymentOf', () => {
|
|
|
122
122
|
})
|
|
123
123
|
expect(Service.paymentOf({ pay: handler, options: {} })).toBeNull()
|
|
124
124
|
})
|
|
125
|
+
|
|
126
|
+
test('behavior: strips function internals from payment metadata', () => {
|
|
127
|
+
const handler = Object.assign(
|
|
128
|
+
async () => ({
|
|
129
|
+
status: 200 as const,
|
|
130
|
+
withReceipt: <T>(r: T) => r,
|
|
131
|
+
}),
|
|
132
|
+
{
|
|
133
|
+
_internal: {
|
|
134
|
+
_canonicalRequest: () => ({}),
|
|
135
|
+
_stableBinding: () => ({}),
|
|
136
|
+
amount: '1',
|
|
137
|
+
authorize: () => undefined,
|
|
138
|
+
decimals: 6,
|
|
139
|
+
defaults: {},
|
|
140
|
+
intent: 'charge',
|
|
141
|
+
name: 'mock',
|
|
142
|
+
request: () => ({}),
|
|
143
|
+
respond: () => undefined,
|
|
144
|
+
schema: {},
|
|
145
|
+
stableBinding: () => ({}),
|
|
146
|
+
transport: {},
|
|
147
|
+
verify: () => undefined,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
expect(Service.paymentOf(handler as never)).toEqual({
|
|
153
|
+
amount: '1000000',
|
|
154
|
+
decimals: 6,
|
|
155
|
+
intent: 'charge',
|
|
156
|
+
method: 'mock',
|
|
157
|
+
})
|
|
158
|
+
})
|
|
125
159
|
})
|
|
126
160
|
|
|
127
161
|
describe('getOptions', () => {
|
package/src/proxy/Service.ts
CHANGED
|
@@ -216,6 +216,13 @@ export function paymentOf(endpoint: Endpoint): Record<string, unknown> | null {
|
|
|
216
216
|
defaults: _,
|
|
217
217
|
schema: _s,
|
|
218
218
|
_canonicalRequest,
|
|
219
|
+
_stableBinding: _sb,
|
|
220
|
+
authorize: _a,
|
|
221
|
+
request: _r,
|
|
222
|
+
respond: _re,
|
|
223
|
+
stableBinding: _st,
|
|
224
|
+
transport: _t,
|
|
225
|
+
verify: _v,
|
|
219
226
|
...rest
|
|
220
227
|
} = handler._internal as Record<string, unknown>
|
|
221
228
|
const amount = (() => {
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Challenge, Credential, Method, z } from 'mppx'
|
|
2
|
+
import { Mppx } from 'mppx/server'
|
|
3
|
+
import { describe, expect, test } from 'vp/test'
|
|
4
|
+
import * as Http from '~test/Http.js'
|
|
5
|
+
|
|
6
|
+
const realm = 'api.example.com'
|
|
7
|
+
const secretKey = 'test-secret-key'
|
|
8
|
+
|
|
9
|
+
function successReceipt(method = 'mock') {
|
|
10
|
+
return {
|
|
11
|
+
method,
|
|
12
|
+
reference: 'ref-1',
|
|
13
|
+
status: 'success',
|
|
14
|
+
timestamp: '2025-01-01T00:00:00.000Z',
|
|
15
|
+
} as const
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('authorize hook', () => {
|
|
19
|
+
test('grants access without a Payment credential', async () => {
|
|
20
|
+
const method = Method.toServer(
|
|
21
|
+
Method.from({
|
|
22
|
+
name: 'mock',
|
|
23
|
+
intent: 'subscription',
|
|
24
|
+
schema: {
|
|
25
|
+
credential: { payload: z.object({ token: z.string() }) },
|
|
26
|
+
request: z.object({ amount: z.string() }),
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
{
|
|
30
|
+
async authorize() {
|
|
31
|
+
return { receipt: successReceipt() }
|
|
32
|
+
},
|
|
33
|
+
async verify() {
|
|
34
|
+
return successReceipt()
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const handler = Mppx.create({ methods: [method], realm, secretKey })
|
|
40
|
+
const result = await handler['mock/subscription']({ amount: '1' })(
|
|
41
|
+
new Request('https://example.com/resource'),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
expect(result.status).toBe(200)
|
|
45
|
+
if (result.status !== 200) throw new Error('expected authorize success')
|
|
46
|
+
|
|
47
|
+
const response = result.withReceipt(new Response('OK'))
|
|
48
|
+
expect(response.headers.get('Payment-Receipt')).toBeTruthy()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('toNodeListener forwards authorize management responses', async () => {
|
|
52
|
+
const method = Method.toServer(
|
|
53
|
+
Method.from({
|
|
54
|
+
name: 'mock',
|
|
55
|
+
intent: 'subscription',
|
|
56
|
+
schema: {
|
|
57
|
+
credential: { payload: z.object({ token: z.string() }) },
|
|
58
|
+
request: z.object({ amount: z.string() }),
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
{
|
|
62
|
+
async authorize() {
|
|
63
|
+
return {
|
|
64
|
+
receipt: successReceipt(),
|
|
65
|
+
response: new Response('retry later', {
|
|
66
|
+
headers: { 'Retry-After': '1' },
|
|
67
|
+
status: 409,
|
|
68
|
+
}),
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
async verify() {
|
|
72
|
+
return successReceipt()
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
const handler = Mppx.create({ methods: [method], realm, secretKey })
|
|
78
|
+
const server = await Http.createServer(async (req, res) => {
|
|
79
|
+
const result = await Mppx.toNodeListener(handler['mock/subscription']({ amount: '1' }))(
|
|
80
|
+
req,
|
|
81
|
+
res,
|
|
82
|
+
)
|
|
83
|
+
if (result.status === 402) return
|
|
84
|
+
res.end('OK')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const response = await fetch(server.url)
|
|
88
|
+
expect(response.status).toBe(409)
|
|
89
|
+
expect(response.headers.get('Retry-After')).toBe('1')
|
|
90
|
+
expect(response.headers.get('Payment-Receipt')).toBeTruthy()
|
|
91
|
+
expect(await response.text()).toBe('retry later')
|
|
92
|
+
|
|
93
|
+
server.close()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('compose evaluates authorize hooks sequentially on no-credential requests', async () => {
|
|
97
|
+
const calls: string[] = []
|
|
98
|
+
const createMethod = (
|
|
99
|
+
name: 'alpha' | 'beta',
|
|
100
|
+
authorizeResult?: ReturnType<typeof successReceipt>,
|
|
101
|
+
) =>
|
|
102
|
+
Method.toServer(
|
|
103
|
+
Method.from({
|
|
104
|
+
name,
|
|
105
|
+
intent: 'charge',
|
|
106
|
+
schema: {
|
|
107
|
+
credential: { payload: z.object({ token: z.string() }) },
|
|
108
|
+
request: z.object({ amount: z.string() }),
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
{
|
|
112
|
+
async authorize() {
|
|
113
|
+
calls.push(`${name}:start`)
|
|
114
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
115
|
+
calls.push(`${name}:end`)
|
|
116
|
+
return authorizeResult ? { receipt: authorizeResult } : undefined
|
|
117
|
+
},
|
|
118
|
+
async verify() {
|
|
119
|
+
return successReceipt(name)
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const alpha = createMethod('alpha')
|
|
125
|
+
const beta = createMethod('beta', successReceipt('beta'))
|
|
126
|
+
const handler = Mppx.create({ methods: [alpha, beta], realm, secretKey })
|
|
127
|
+
|
|
128
|
+
const result = await handler.compose(
|
|
129
|
+
[alpha, { amount: '1' }],
|
|
130
|
+
[beta, { amount: '1' }],
|
|
131
|
+
)(new Request('https://example.com/resource'))
|
|
132
|
+
|
|
133
|
+
expect(result.status).toBe(200)
|
|
134
|
+
expect(calls).toEqual(['alpha:start', 'alpha:end', 'beta:start', 'beta:end'])
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('stableBinding can reject mismatched subscription routes', async () => {
|
|
138
|
+
const method = Method.toServer(
|
|
139
|
+
Method.from({
|
|
140
|
+
name: 'mock',
|
|
141
|
+
intent: 'subscription',
|
|
142
|
+
schema: {
|
|
143
|
+
credential: { payload: z.object({ token: z.string() }) },
|
|
144
|
+
request: z.object({
|
|
145
|
+
amount: z.string(),
|
|
146
|
+
chainId: z.optional(z.number()),
|
|
147
|
+
currency: z.string(),
|
|
148
|
+
periodCount: z.string(),
|
|
149
|
+
periodUnit: z.enum(['day', 'week']),
|
|
150
|
+
recipient: z.string(),
|
|
151
|
+
subscriptionExpires: z.string(),
|
|
152
|
+
}),
|
|
153
|
+
},
|
|
154
|
+
}),
|
|
155
|
+
{
|
|
156
|
+
stableBinding(request) {
|
|
157
|
+
return {
|
|
158
|
+
amount: request.amount,
|
|
159
|
+
chainId: request.chainId,
|
|
160
|
+
currency: request.currency,
|
|
161
|
+
periodCount: request.periodCount,
|
|
162
|
+
periodUnit: request.periodUnit,
|
|
163
|
+
recipient: request.recipient,
|
|
164
|
+
subscriptionExpires: request.subscriptionExpires,
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
async verify() {
|
|
168
|
+
return successReceipt()
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
const handler = Mppx.create({ methods: [method], realm, secretKey })
|
|
174
|
+
const first = await handler['mock/subscription']({
|
|
175
|
+
amount: '1',
|
|
176
|
+
currency: 'usd',
|
|
177
|
+
periodCount: '30',
|
|
178
|
+
periodUnit: 'day',
|
|
179
|
+
recipient: 'alice',
|
|
180
|
+
subscriptionExpires: '2026-01-01T00:00:00Z',
|
|
181
|
+
})(new Request('https://example.com/cheap'))
|
|
182
|
+
|
|
183
|
+
expect(first.status).toBe(402)
|
|
184
|
+
if (first.status !== 402) throw new Error('expected challenge')
|
|
185
|
+
|
|
186
|
+
const credential = Credential.from({
|
|
187
|
+
challenge: Challenge.fromResponse(first.challenge),
|
|
188
|
+
payload: { token: 'ok' },
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
const second = await handler['mock/subscription']({
|
|
192
|
+
amount: '1',
|
|
193
|
+
currency: 'usd',
|
|
194
|
+
periodCount: '60',
|
|
195
|
+
periodUnit: 'day',
|
|
196
|
+
recipient: 'alice',
|
|
197
|
+
subscriptionExpires: '2026-01-01T00:00:00Z',
|
|
198
|
+
})(
|
|
199
|
+
new Request('https://example.com/expensive', {
|
|
200
|
+
headers: { Authorization: Credential.serialize(credential) },
|
|
201
|
+
}),
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
expect(second.status).toBe(402)
|
|
205
|
+
if (second.status !== 402) throw new Error('expected mismatch challenge')
|
|
206
|
+
|
|
207
|
+
const body = (await second.challenge.json()) as { detail: string }
|
|
208
|
+
expect(body.detail).toContain('periodCount')
|
|
209
|
+
})
|
|
210
|
+
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Method, z } from 'mppx'
|
|
2
|
-
import { Mppx } from 'mppx/server'
|
|
2
|
+
import { Mppx, tempo } from 'mppx/server'
|
|
3
3
|
import { assertType, describe, expectTypeOf, test } from 'vp/test'
|
|
4
4
|
|
|
5
5
|
const mockChargeA = Method.from({
|
|
@@ -151,6 +151,7 @@ describe('Mppx type tests', () => {
|
|
|
151
151
|
amount: '100',
|
|
152
152
|
currency: '0x01',
|
|
153
153
|
decimals: 6,
|
|
154
|
+
expires: new Date('2026-01-01T00:00:00Z'),
|
|
154
155
|
recipient: '0x02',
|
|
155
156
|
})
|
|
156
157
|
|
|
@@ -170,6 +171,56 @@ describe('Mppx type tests', () => {
|
|
|
170
171
|
expectTypeOf(mppx.verifyCredential).toBeFunction()
|
|
171
172
|
})
|
|
172
173
|
|
|
174
|
+
test('server events receive typed method context', () => {
|
|
175
|
+
const mppx = Mppx.create({
|
|
176
|
+
methods: [alphaMethod],
|
|
177
|
+
realm,
|
|
178
|
+
secretKey,
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
mppx.on('payment.success', (context) => {
|
|
182
|
+
expectTypeOf(context.challenge.method).toEqualTypeOf<'alpha'>()
|
|
183
|
+
expectTypeOf(context.credential?.payload.token).toEqualTypeOf<string | undefined>()
|
|
184
|
+
})
|
|
185
|
+
mppx.on('challenge.created', (context) => {
|
|
186
|
+
expectTypeOf(context.method.name).toEqualTypeOf<'alpha'>()
|
|
187
|
+
expectTypeOf(context.error).toMatchTypeOf<Error | undefined>()
|
|
188
|
+
})
|
|
189
|
+
mppx.on('payment.failed', (context) => {
|
|
190
|
+
expectTypeOf(context.error).toMatchTypeOf<Error>()
|
|
191
|
+
expectTypeOf(context.credential).toMatchTypeOf<unknown>()
|
|
192
|
+
})
|
|
193
|
+
mppx.on('*', (event) => {
|
|
194
|
+
expectTypeOf(event.name).toMatchTypeOf<
|
|
195
|
+
'challenge.created' | 'payment.failed' | 'payment.success'
|
|
196
|
+
>()
|
|
197
|
+
if (event.name === 'payment.failed') expectTypeOf(event.payload.error).toMatchTypeOf<Error>()
|
|
198
|
+
if (event.name === 'challenge.created')
|
|
199
|
+
expectTypeOf(event.payload.error).toMatchTypeOf<Error | undefined>()
|
|
200
|
+
if (event.name === 'payment.success')
|
|
201
|
+
expectTypeOf(event.payload.receipt.status).toEqualTypeOf<'success'>()
|
|
202
|
+
})
|
|
203
|
+
mppx.onChallengeCreated((context) => {
|
|
204
|
+
expectTypeOf(context.input).toEqualTypeOf<Request | undefined>()
|
|
205
|
+
expectTypeOf(context.method.name).toEqualTypeOf<'alpha'>()
|
|
206
|
+
expectTypeOf(context.request.amount).toEqualTypeOf<string>()
|
|
207
|
+
expectTypeOf(context.error).toMatchTypeOf<Error | undefined>()
|
|
208
|
+
})
|
|
209
|
+
mppx.onPaymentSuccess((context) => {
|
|
210
|
+
expectTypeOf(context.challenge.method).toEqualTypeOf<'alpha'>()
|
|
211
|
+
expectTypeOf(context.credential?.payload.token).toEqualTypeOf<string | undefined>()
|
|
212
|
+
expectTypeOf(context.envelope?.challenge.intent).toEqualTypeOf<'charge' | undefined>()
|
|
213
|
+
expectTypeOf(context.receipt.status).toEqualTypeOf<'success'>()
|
|
214
|
+
expectTypeOf(context.request.recipient).toEqualTypeOf<string>()
|
|
215
|
+
})
|
|
216
|
+
mppx.onPaymentFailed((context) => {
|
|
217
|
+
expectTypeOf(context.credential).toMatchTypeOf<unknown>()
|
|
218
|
+
expectTypeOf(context.error).toMatchTypeOf<Error>()
|
|
219
|
+
expectTypeOf(context.method.intent).toEqualTypeOf<'charge'>()
|
|
220
|
+
expectTypeOf(context.request.currency).toEqualTypeOf<string>()
|
|
221
|
+
})
|
|
222
|
+
})
|
|
223
|
+
|
|
173
224
|
test('handler options and verifyCredential accept scope', () => {
|
|
174
225
|
const mppx = Mppx.create({ methods: [alphaMethod], realm, secretKey })
|
|
175
226
|
|
|
@@ -187,4 +238,25 @@ describe('Mppx type tests', () => {
|
|
|
187
238
|
Promise<unknown>
|
|
188
239
|
>()
|
|
189
240
|
})
|
|
241
|
+
|
|
242
|
+
test('tempo subscription accepts ergonomic date and period inputs', () => {
|
|
243
|
+
const method = tempo.subscription({
|
|
244
|
+
amount: '10',
|
|
245
|
+
currency: '0x20c0000000000000000000000000000000000001',
|
|
246
|
+
periodCount: 1,
|
|
247
|
+
periodUnit: 'day',
|
|
248
|
+
recipient: '0x1234567890abcdef1234567890abcdef12345678',
|
|
249
|
+
resolve: async () => ({ key: 'user-1:plan:pro' }),
|
|
250
|
+
subscriptionExpires: new Date('2026-01-01T00:00:00Z'),
|
|
251
|
+
})
|
|
252
|
+
const mppx = Mppx.create({ methods: [method], realm, secretKey })
|
|
253
|
+
|
|
254
|
+
expectTypeOf(
|
|
255
|
+
mppx.tempo.subscription({
|
|
256
|
+
expires: new Date('2026-01-01T00:00:00Z'),
|
|
257
|
+
periodCount: 1n,
|
|
258
|
+
subscriptionExpires: new Date('2026-01-01T00:00:00Z'),
|
|
259
|
+
}),
|
|
260
|
+
).toBeFunction()
|
|
261
|
+
})
|
|
190
262
|
})
|