Skip to content

Commit

Permalink
add secureApi and scriptSource
Browse files Browse the repository at this point in the history
  • Loading branch information
e271828- committed Feb 13, 2024
1 parent 537bc1d commit ff7f18e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
16 changes: 16 additions & 0 deletions lib/__test__/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
14 changes: 12 additions & 2 deletions lib/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions lib/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface IScriptParams {
scriptLocation?: HTMLElement;
secureApi?: boolean;
scriptSource?: string;
apihost?: string;
loadAsync?: boolean;
cleanup?: boolean;
Expand Down

0 comments on commit ff7f18e

Please sign in to comment.