import Ajv from 'ajv'
const ajv = Ajv({
  allErrors: true,
  schemas: [
    require('./schema/introspect-request.json'),
    require('./schema/introspect-response.json'),
    require('./schema/refresh-token-request.json'),
    require('./schema/refresh-token-response.json')
  ]
})
// Change the implementation slightly...

const parseResponse = (schema) => {
  return (res) => {
    const validator = ajv.getSchema('http://localhost:3000/schemas/introspect-response.json#')
    const isValid = validator(res)
    if (!isValid) {
      const error = new Error('Invalid Response')
      error.description = validator.errors[0].message
      throw error
    }
    return res
  }
}



const parseRequest = (schema) => {
  return (req) => {
    const validator = ajv.getSchema(schema)
    const isValid = validator(req)
    if (!isValid) {
      const error = new Error('Invalid Request')
      error.description = validator.errors[0].message
      throw error
    }
    return req
  }
}

export default {
  introspectRequest: parseRequest('http://localhost:3000/schemas/introspect-request.json#'),
  introspectResponse: parseResponse('http://localhost:3000/schemas/introspect-response.json#'),
  refreshTokenRequest: parseRequest('http://localhost:3000/schemas/refresh-token-request.json#'),
  refreshTokenResponse: parseResponse('http://localhost:3000/schemas/refresh-token-response.json#'),
}

@@ -15,16 +15,59 @@
 m 'ajv'%0A
+import Parser from '../common/toolbox.js'%0A%0A
 const aj
@@ -359,17 +359,16 @@
 htly...%0A
-%0A
 const pa
@@ -374,656 +374,23 @@
 arse
-Response = (schema) =%3E %7B%0A  return (res) =%3E %7B%0A    const validator = ajv.getSchema('http://localhost:3000/schemas/introspect-response.json#')%0A    const isValid = validator(res)%0A    if (!isValid) %7B%0A      const error = new Error('Invalid Response')%0A      error.description = validator.errors%5B0%5D.message%0A      throw error%0A    %7D%0A    return res%0A  %7D%0A%7D%0A%0A%0A%0Aconst parseRequest = (schema) =%3E %7B%0A  return (req) =%3E %7B%0A    const validator = ajv.getSchema(schema)%0A    const isValid = validator(req)%0A    if (!isValid) %7B%0A      const error = new Error('Invalid Request')%0A      error.description = validator.errors%5B0%5D.message%0A      throw error%0A    %7D%0A    return req%0A  %7D%0A%7D
+r = Parser(ajv)
 %0A%0Aex
@@ -422,33 +422,35 @@
 ctRequest: parse
-R
+r.r
 equest('http://l
@@ -518,33 +518,35 @@
 tResponse: parse
-R
+r.r
 esponse('http://
@@ -625,17 +625,19 @@
 t: parse
-R
+r.r
 equest('
@@ -726,17 +726,19 @@
 e: parse
-R
+r.r
 esponse(

Update schema.js