Skip to content

Commit 270420c

Browse files
authored
add secureApi and scriptSource (#22)
1 parent 537bc1d commit 270420c

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ const { response } = await hcaptcha.execute({ async: true });
3535
| `loadAsync` | Boolean | No | `true` | Set if the script should be loaded asynchronously. |
3636
| `cleanup` | Boolean | No | `true` | Remove script tag after setup. |
3737
| `crossOrigin` | String | No | `-` | Set script cross origin attribute such as "anonymous". |
38+
| `scriptSource` | String | No | `https://js.hcaptcha.com/1/api.js` | Set script source URI. Takes precedence over `secureApi`. |
3839
| `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. |
40+
| `secureApi` | Boolean | No | `false` | See enterprise docs. |
3941
| `apihost` | String | No | `-` | See enterprise docs. |
4042
| `assethost` | String | No | `-` | See enterprise docs. |
4143
| `endpoint` | String | No | `-` | See enterprise docs. |

lib/__test__/script.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ describe('fetchScript', () => {
143143
const [script] = nodes;
144144
expect(script.src).toMatch(`${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`);
145145
});
146+
147+
it('should change hCaptcha JS API if secureApi is specified', async () => {
148+
const secureApi = true;
149+
await fetchScript({ secureApi });
150+
151+
const [script] = nodes;
152+
expect(script.src).toMatch(`https://js.hcaptcha.com/1/secure-api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`);
153+
});
154+
155+
it('should change hCaptcha JS API if scriptSource is specified', async () => {
156+
const scriptSource = 'hcaptcha.com/1/api.js';
157+
await fetchScript({ scriptSource });
158+
159+
const [script] = nodes;
160+
expect(script.src).toMatch(`${scriptSource}?onload=${HCAPTCHA_LOAD_FN_NAME}`);
161+
});
146162
});
147163

148164
describe('cleanup', () => {

lib/src/script.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export function fetchScript({
99
loadAsync = true,
1010
crossOrigin,
1111
apihost = 'https://js.hcaptcha.com',
12-
cleanup = true
12+
cleanup = true,
13+
secureApi = false,
14+
scriptSource = ''
1315
}: IScriptParams = {}) {
1416
const element = getMountElement(scriptLocation);
1517
const frame: any = getFrame(element);
@@ -18,7 +20,15 @@ export function fetchScript({
1820
const script = frame.document.createElement('script');
1921

2022
script.id = SCRIPT_ID;
21-
script.src = `${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`;
23+
if (scriptSource) {
24+
script.src = `${scriptSource}?onload=${HCAPTCHA_LOAD_FN_NAME}`;
25+
} else {
26+
if (secureApi) {
27+
script.src = `${apihost}/1/secure-api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`;
28+
} else {
29+
script.src = `${apihost}/1/api.js?onload=${HCAPTCHA_LOAD_FN_NAME}`;
30+
}
31+
}
2232
script.crossOrigin = crossOrigin;
2333
script.async = loadAsync;
2434

lib/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export interface IScriptParams {
22
scriptLocation?: HTMLElement;
3+
secureApi?: boolean;
4+
scriptSource?: string;
35
apihost?: string;
46
loadAsync?: boolean;
57
cleanup?: boolean;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hcaptcha/loader",
33
"description": "This is a JavaScript library to easily configure the loading of the hCaptcha JS client SDK with built-in error handling.",
4-
"version": "1.1.3",
4+
"version": "1.2.0",
55
"author": "hCaptcha team and contributors",
66
"license": "MIT",
77
"keywords": [

0 commit comments

Comments
 (0)