mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
/*
|
|
REST API Controller library for the /verify route
|
|
*/
|
|
|
|
class VerifyRESTControllerLib {
|
|
constructor (localConfig = {}) {
|
|
// Dependency Injection.
|
|
this.adapters = localConfig.adapters
|
|
if (!this.adapters) {
|
|
throw new Error(
|
|
'Instance of Adapters library required when instantiating /verify REST Controller.'
|
|
)
|
|
}
|
|
this.useCases = localConfig.useCases
|
|
if (!this.useCases) {
|
|
throw new Error(
|
|
'Instance of Use Cases library required when instantifying /verify REST Controller.'
|
|
)
|
|
}
|
|
|
|
this.verify = this.verify.bind(this)
|
|
this.handleError = this.handleError.bind(this)
|
|
}
|
|
|
|
async verify (ctx) {
|
|
try {
|
|
const inputObj = ctx.request.body
|
|
|
|
const result = await this.useCases.verify.verify(inputObj)
|
|
|
|
ctx.body = result
|
|
} catch (err) {
|
|
// console.log(`err.message: ${err.message}`)
|
|
// console.log('err: ', err)
|
|
// ctx.throw(422, err.message)
|
|
this.handleError(ctx, err)
|
|
}
|
|
}
|
|
|
|
// DRY error handler
|
|
handleError (ctx, err) {
|
|
// If an HTTP status is specified by the buisiness logic, use that.
|
|
if (err.status) {
|
|
if (err.message) {
|
|
ctx.throw(err.status, err.message)
|
|
} else {
|
|
ctx.throw(err.status)
|
|
}
|
|
} else {
|
|
// By default use a 422 error if the HTTP status is not specified.
|
|
ctx.throw(422, err.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default VerifyRESTControllerLib
|