{"version":3,"file":"index.mjs","sources":["../src/DrupalkitVerification.ts","../src/verification.ts"],"sourcesContent":["import { Drupalkit } from \"@drupal-kit/core\";\n\nimport { VerificationProvider } from \"./verification.js\";\n\n/*\ndeclare module \"@drupal-kit/core\" {\n  interface DrupalkitOptions {\n  }\n}\n*/\n\n/**\n * DrupalkitVerification plugin for Drupalkit.\n *\n * Provides a generic way to set and inject verification data\n * into Drupalkit.\n * This plugin requires the `verification` drupal module.\n *\n * @param drupalkit - The Drupalkit instance.\n */\nexport const DrupalkitVerification = (\n  drupalkit: Drupalkit,\n  //drupalkitOptions: DrupalkitOptions,\n) => {\n  const enhancedDrupalkit = drupalkit as Drupalkit &\n    ReturnType<typeof DrupalkitVerification>;\n\n  /**\n   * Add a verification via a verification provider function.\n   *\n   * This provider will be used for the next request only!\n   *\n   * @param provider - The verification provider function.\n   */\n  const addVerification = (provider: VerificationProvider) => {\n    enhancedDrupalkit.verification.provider = provider;\n  };\n\n  /**\n   * Removes an active verification provider.\n   */\n  const removeVerification = () => {\n    if (enhancedDrupalkit.verification.provider) {\n      enhancedDrupalkit.verification = {\n        provider: null,\n      };\n    }\n  };\n\n  /**\n   * If a verification is set, add it to the request.\n   */\n  drupalkit.hook.before(\"request\", async (requestOptions) => {\n    if (enhancedDrupalkit.verification.provider) {\n      await enhancedDrupalkit.verification.provider.provide(requestOptions);\n    }\n  });\n\n  /**\n   * Remove verification after each successful request.\n   */\n  drupalkit.hook.after(\"request\", () => {\n    removeVerification();\n  });\n\n  /**\n   * Extend the Drupalkit instance.\n   */\n  return {\n    addVerification,\n    removeVerification,\n    verification: {\n      provider: null as VerificationProvider | null,\n    },\n  };\n};\n","import { RequestRequestOptions } from \"@drupal-kit/types\";\n\n/**\n * All verification providers must implement this interface.\n */\nexport interface VerificationProvider {\n  provide(requestOptions: RequestRequestOptions): Promise<void> | void;\n}\n\n/**\n * A generic verification provider that adds\n * the verification data to the request via a\n * HTTP header.\n *\n * @param name - The HTTP header name.\n * @param value - The HTTP header value.\n */\nexport const headerVerification = (\n  name: string,\n  value: string,\n): VerificationProvider => {\n  return {\n    provide(requestOptions) {\n      requestOptions.headers[name] = value;\n    },\n  };\n};\n\n/**\n * Hash verification provider.\n *\n * Use this provider when using the `verification_hash` verification\n * provider drupal module.\n *\n * @param hash - The verification hash.\n */\nexport const hashVerification = (hash: string) =>\n  headerVerification(\"x-verification-hash\", hash);\n\n/**\n * Magic Code verification provider.\n *\n * Use this provider when using the `magic_code` verification\n * provider drupal module.\n *\n * @param code - The magic code.\n */\nexport const magicCodeVerification = (code: string) =>\n  headerVerification(\"x-verification-magic-code\", code);\n"],"names":[],"mappings":"AAoBO,MAAM,qBAAA,GAAwB,CACnC,SAAA,KAEG;AACH,EAAA,MAAM,iBAAA,GAAoB,SAAA;AAU1B,EAAA,MAAM,eAAA,GAAkB,CAAC,QAAA,KAAmC;AAC1D,IAAA,iBAAA,CAAkB,aAAa,QAAA,GAAW,QAAA;AAAA,EAC5C,CAAA;AAKA,EAAA,MAAM,qBAAqB,MAAM;AAC/B,IAAA,IAAI,iBAAA,CAAkB,aAAa,QAAA,EAAU;AAC3C,MAAA,iBAAA,CAAkB,YAAA,GAAe;AAAA,QAC/B,QAAA,EAAU;AAAA,OACZ;AAAA,IACF;AAAA,EACF,CAAA;AAKA,EAAA,SAAA,CAAU,IAAA,CAAK,MAAA,CAAO,SAAA,EAAW,OAAO,cAAA,KAAmB;AACzD,IAAA,IAAI,iBAAA,CAAkB,aAAa,QAAA,EAAU;AAC3C,MAAA,MAAM,iBAAA,CAAkB,YAAA,CAAa,QAAA,CAAS,OAAA,CAAQ,cAAc,CAAA;AAAA,IACtE;AAAA,EACF,CAAC,CAAA;AAKD,EAAA,SAAA,CAAU,IAAA,CAAK,KAAA,CAAM,SAAA,EAAW,MAAM;AACpC,IAAA,kBAAA,EAAmB;AAAA,EACrB,CAAC,CAAA;AAKD,EAAA,OAAO;AAAA,IACL,eAAA;AAAA,IACA,kBAAA;AAAA,IACA,YAAA,E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