Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add secureApi and scriptSource #22

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
Loading