diff --git a/README.md b/README.md index b923c22..98d2717 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,9 @@ const { response } = await hcaptcha.execute({ async: true }); | `loadAsync` | Boolean | No | `true` | Set if the script should be loaded asynchronously. | | `cleanup` | Boolean | No | `true` | Remove script tag after setup. | | `crossOrigin` | String | No | `-` | Set script cross origin attribute such as "anonymous". | +| `scriptSource` | String | No | `https://js.hcaptcha.com/1/api.js` | Set script source URI. Takes precedence over `secureApi`. | | `scriptLocation` | HTMLElement | No | `document.head` | Location of where to append the script tag. Make sure to add it to an area that will persist to prevent loading multiple times in the same document view. | +| `secureApi` | Boolean | No | `false` | See enterprise docs. | | `apihost` | String | No | `-` | See enterprise docs. | | `assethost` | String | No | `-` | See enterprise docs. | | `endpoint` | String | No | `-` | See enterprise docs. | diff --git a/lib/__test__/script.test.ts b/lib/__test__/script.test.ts index 7ac5701..c60ca31 100644 --- a/lib/__test__/script.test.ts +++ b/lib/__test__/script.test.ts @@ -143,6 +143,22 @@ describe('fetchScript', () => { const [script] = nodes; expect(script.src).toMatch(`${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`); }); + + it('should change hCaptcha JS API if secureApi is specified', async () => { + const secureApi = true; + await fetchScript({ secureApi }); + + const [script] = nodes; + expect(script.src).toMatch(`https://js.hcaptcha.com/1/secure-api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`); + }); + + it('should change hCaptcha JS API if scriptSource is specified', async () => { + const scriptSource = 'hcaptcha.com/1/api.js'; + await fetchScript({ scriptSource }); + + const [script] = nodes; + expect(script.src).toMatch(`${scriptSource}?onload=${HCAPTCHA_LOAD_FN_NAME}`); + }); }); describe('cleanup', () => { diff --git a/lib/src/script.ts b/lib/src/script.ts index 65a9922..4e75055 100644 --- a/lib/src/script.ts +++ b/lib/src/script.ts @@ -9,7 +9,9 @@ export function fetchScript({ loadAsync = true, crossOrigin, apihost = 'https://js.hcaptcha.com', - cleanup = true + cleanup = true, + secureApi = false, + scriptSource = '' }: IScriptParams = {}) { const element = getMountElement(scriptLocation); const frame: any = getFrame(element); @@ -18,7 +20,15 @@ export function fetchScript({ const script = frame.document.createElement('script'); script.id = SCRIPT_ID; - script.src = `${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`; + if (scriptSource) { + script.src = `${scriptSource}?onload=${HCAPTCHA_LOAD_FN_NAME}`; + } else { + if (secureApi) { + script.src = `${apihost}/1/secure-api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`; + } else { + script.src = `${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`; + } + } script.crossOrigin = crossOrigin; script.async = loadAsync; diff --git a/lib/src/types.ts b/lib/src/types.ts index ceab92d..d8d8d70 100644 --- a/lib/src/types.ts +++ b/lib/src/types.ts @@ -1,5 +1,7 @@ export interface IScriptParams { scriptLocation?: HTMLElement; + secureApi?: boolean; + scriptSource?: string; apihost?: string; loadAsync?: boolean; cleanup?: boolean; diff --git a/package.json b/package.json index 2d5d858..3c878f0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hcaptcha/loader", "description": "This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling.", - "version": "1.1.3", + "version": "1.2.0", "author": "hCaptcha team and contributors", "license": "MIT", "keywords": [