mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
/*
|
|||
|
|
REST API library for /verify route.
|
||
|
|
*/
|
||
|
|
|
||
|
|
// Public npm libraries.
|
||
|
|
import Router from 'koa-router'
|
||
|
|
|
||
|
|
// Local libraries.
|
||
|
|
import VerifyRESTControllerLib from './controller.js'
|
||
|
|
|
||
|
|
import config from '../../../../config/index.js'
|
||
|
|
|
||
|
|
class VerifyRouter {
|
||
|
|
constructor (localConfig = {}) {
|
||
|
|
// Dependency Injection.
|
||
|
|
this.adapters = localConfig.adapters
|
||
|
|
if (!this.adapters) {
|
||
|
|
throw new Error(
|
||
|
|
'Instance of Adapters library required when instantiating Verify Router.'
|
||
|
|
)
|
||
|
|
}
|
||
|
|
this.useCases = localConfig.useCases
|
||
|
|
if (!this.useCases) {
|
||
|
|
throw new Error(
|
||
|
|
'Instance of Use Cases library required when instantiating Verify Router.'
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
const dependencies = {
|
||
|
|
adapters: this.adapters,
|
||
|
|
useCases: this.useCases
|
||
|
|
}
|
||
|
|
|
||
|
|
// Encapsulate dependencies.
|
||
|
|
this.config = config
|
||
|
|
this.verifyRESTController = new VerifyRESTControllerLib(dependencies)
|
||
|
|
|
||
|
|
// Instantiate the router and set the base route.
|
||
|
|
const baseUrl = '/verify'
|
||
|
|
this.router = new Router({ prefix: baseUrl })
|
||
|
|
}
|
||
|
|
|
||
|
|
attach (app) {
|
||
|
|
if (!app) {
|
||
|
|
throw new Error(
|
||
|
|
'Must pass app object when attaching REST API controllers.'
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Define the routes and attach the controller.
|
||
|
|
this.router.post('/', this.verifyRESTController.verify)
|
||
|
|
|
||
|
|
// Attach the Controller routes to the Koa app.
|
||
|
|
app.use(this.router.routes())
|
||
|
|
app.use(this.router.allowedMethods())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
export default VerifyRouter
|