@friendlycaptcha/server-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/.github/workflows/ci.yml +47 -0
  2. package/.github/workflows/publish.yml +18 -0
  3. package/.prettierrc +3 -0
  4. package/CHANGELOG.md +5 -0
  5. package/LICENSE +21 -0
  6. package/README.md +47 -0
  7. package/dist/api/errors.d.ts +54 -0
  8. package/dist/api/errors.js +64 -0
  9. package/dist/api/errors.js.map +1 -0
  10. package/dist/api/index.d.ts +1 -0
  11. package/dist/api/index.js +2 -0
  12. package/dist/api/index.js.map +1 -0
  13. package/dist/api/types.d.ts +59 -0
  14. package/dist/api/types.js +2 -0
  15. package/dist/api/types.js.map +1 -0
  16. package/dist/client/client.d.ts +53 -0
  17. package/dist/client/client.js +100 -0
  18. package/dist/client/client.js.map +1 -0
  19. package/dist/client/errors.d.ts +37 -0
  20. package/dist/client/errors.js +34 -0
  21. package/dist/client/errors.js.map +1 -0
  22. package/dist/client/index.d.ts +3 -0
  23. package/dist/client/index.js +4 -0
  24. package/dist/client/index.js.map +1 -0
  25. package/dist/client/result.d.ts +67 -0
  26. package/dist/client/result.js +125 -0
  27. package/dist/client/result.js.map +1 -0
  28. package/dist/client/version.gen.d.ts +1 -0
  29. package/dist/client/version.gen.js +4 -0
  30. package/dist/client/version.gen.js.map +1 -0
  31. package/dist/index.d.ts +292 -0
  32. package/dist/index.js +3 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/tsdoc-metadata.json +11 -0
  35. package/docs/index.md +12 -0
  36. package/docs/server-sdk.failed_due_to_client_error_code.md +15 -0
  37. package/docs/server-sdk.failed_to_decode_response_error_code.md +13 -0
  38. package/docs/server-sdk.failed_to_encode_error_code.md +13 -0
  39. package/docs/server-sdk.friendlycaptchaclient._constructor_.md +20 -0
  40. package/docs/server-sdk.friendlycaptchaclient.md +26 -0
  41. package/docs/server-sdk.friendlycaptchaclient.verifycaptcharesponse.md +30 -0
  42. package/docs/server-sdk.friendlycaptchaoptions.apikey.md +13 -0
  43. package/docs/server-sdk.friendlycaptchaoptions.fetch.md +13 -0
  44. package/docs/server-sdk.friendlycaptchaoptions.md +24 -0
  45. package/docs/server-sdk.friendlycaptchaoptions.sitekey.md +11 -0
  46. package/docs/server-sdk.friendlycaptchaoptions.siteverifyendpoint.md +15 -0
  47. package/docs/server-sdk.friendlycaptchaoptions.strict.md +15 -0
  48. package/docs/server-sdk.md +41 -0
  49. package/docs/server-sdk.request_failed_error_code.md +13 -0
  50. package/docs/server-sdk.request_failed_timeout_error_code.md +13 -0
  51. package/docs/server-sdk.siteverifyerrorresponse.error.md +11 -0
  52. package/docs/server-sdk.siteverifyerrorresponse.md +20 -0
  53. package/docs/server-sdk.siteverifyerrorresponse.success.md +11 -0
  54. package/docs/server-sdk.siteverifyerrorresponseerrordata.detail.md +11 -0
  55. package/docs/server-sdk.siteverifyerrorresponseerrordata.error_code.md +11 -0
  56. package/docs/server-sdk.siteverifyerrorresponseerrordata.md +20 -0
  57. package/docs/server-sdk.siteverifyresponse.md +14 -0
  58. package/docs/server-sdk.siteverifyresponsechallengedata.md +20 -0
  59. package/docs/server-sdk.siteverifyresponsechallengedata.origin.md +13 -0
  60. package/docs/server-sdk.siteverifyresponsechallengedata.timestamp.md +13 -0
  61. package/docs/server-sdk.siteverifyresponsedata.challenge.md +11 -0
  62. package/docs/server-sdk.siteverifyresponsedata.md +19 -0
  63. package/docs/server-sdk.siteverifysuccessresponse.data.md +11 -0
  64. package/docs/server-sdk.siteverifysuccessresponse.md +20 -0
  65. package/docs/server-sdk.siteverifysuccessresponse.success.md +11 -0
  66. package/docs/server-sdk.verifyclienterrorcode.md +14 -0
  67. package/docs/server-sdk.verifyresult._constructor_.md +20 -0
  68. package/docs/server-sdk.verifyresult.clienterrortype.md +11 -0
  69. package/docs/server-sdk.verifyresult.geterrorcode.md +15 -0
  70. package/docs/server-sdk.verifyresult.getresponse.md +17 -0
  71. package/docs/server-sdk.verifyresult.getresponseerror.md +17 -0
  72. package/docs/server-sdk.verifyresult.isclienterror.md +19 -0
  73. package/docs/server-sdk.verifyresult.isdecodeerror.md +17 -0
  74. package/docs/server-sdk.verifyresult.isencodeerror.md +17 -0
  75. package/docs/server-sdk.verifyresult.isrequestortimeouterror.md +17 -0
  76. package/docs/server-sdk.verifyresult.isstrict.md +17 -0
  77. package/docs/server-sdk.verifyresult.md +44 -0
  78. package/docs/server-sdk.verifyresult.response.md +13 -0
  79. package/docs/server-sdk.verifyresult.shouldaccept.md +17 -0
  80. package/docs/server-sdk.verifyresult.shouldreject.md +17 -0
  81. package/docs/server-sdk.verifyresult.status.md +13 -0
  82. package/docs/server-sdk.verifyresult.wasabletoverify.md +17 -0
  83. package/package.json +44 -0
  84. package/src/api/errors.ts +77 -0
  85. package/src/api/index.ts +1 -0
  86. package/src/api/types.ts +66 -0
  87. package/src/client/client.ts +154 -0
  88. package/src/client/errors.ts +42 -0
  89. package/src/client/index.ts +3 -0
  90. package/src/client/result.ts +151 -0
  91. package/src/client/version.gen.ts +3 -0
  92. package/src/index.ts +2 -0
  93. package/test/client/client.test.ts +48 -0
  94. package/test/client/mock.test.ts +44 -0
  95. package/tsconfig.json +20 -0
@@ -0,0 +1,47 @@
1
+ name: Javascript SDK tests
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ build_and_test:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+
16
+ - name: Use Node.js 18.x
17
+ uses: actions/setup-node@v3
18
+ with:
19
+ node-version: 18.x
20
+ cache: "npm"
21
+ cache-dependency-path: "package-lock.json"
22
+
23
+ - name: Install dependencies
24
+ run: |
25
+ npm ci
26
+
27
+ - name: Run the SDK testserver
28
+ run: |
29
+ docker run -d -p 1090:1090 friendlycaptcha/sdk-testserver:latest
30
+ echo "SDK Test server started. Checking its health..."
31
+ for i in {1..20}; do
32
+ if curl -s http://localhost:1090; then
33
+ echo "SDK test server is responding!"
34
+ break
35
+ else
36
+ echo "Attempt $i: SDK test server not yet responding. Waiting for 1 second..."
37
+ sleep 1
38
+ fi
39
+ done
40
+
41
+ - name: Test
42
+ run: |
43
+ npm t
44
+
45
+ - name: Build
46
+ run: |
47
+ npm run build:dist
@@ -0,0 +1,18 @@
1
+ name: Publish to NPM
2
+ on:
3
+ release:
4
+ types: [published]
5
+ jobs:
6
+ publish:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - uses: actions/setup-node@v3
11
+ with:
12
+ node-version: '20.x'
13
+ registry-url: 'https://registry.npmjs.org'
14
+ - run: npm ci
15
+ - run: npm run build:dist
16
+ - run: npm publish --access public
17
+ env:
18
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/.prettierrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "printWidth": 119
3
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ Initial release.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Friendly Captcha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # friendly-captcha-javascript
2
+
3
+ A Javascript client for the [Friendly Captcha](https://friendlycaptcha.com) service. This client allows for easy integration and verification of captcha responses with the Friendly Captcha API.
4
+
5
+ > This library is for [Friendly Captcha v2](https://developer.friendlycaptcha.com) only. If you are looking for V1, look [here](https://docs.friendlycaptcha.com)
6
+
7
+ **This is the library you use in your backend code**. For the code that you use in your frontend, see [@friendlycaptcha/sdk](https://github.com/FriendlyCaptcha/friendly-captcha-sdk).
8
+
9
+ ## Installation
10
+
11
+ **Install using [NPM](https://npmjs.com/)**
12
+
13
+ ```shell
14
+ npm install @friendlycaptcha/server-sdk
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ First configure and create a SDK client
20
+
21
+ ```javascript
22
+ import {FriendlyCaptchaClient} from "@friendlycaptcha/server-sdk";
23
+
24
+ // TODO
25
+ ```
26
+
27
+ ## Development
28
+
29
+ ### Install dependencies
30
+ ```shell
31
+ npm install
32
+ ```
33
+
34
+ ### Run the tests
35
+ ```shell
36
+ npm test
37
+ ```
38
+
39
+ ### Build for production
40
+
41
+ ```shell
42
+ npm run build:dist
43
+ ```
44
+
45
+ ## License
46
+
47
+ Open source under [MIT](./LICENSE).
@@ -0,0 +1,54 @@
1
+ export type SiteverifyErrorCode = typeof SITEKEY_INVALID | typeof AUTH_INVALID | typeof AUTH_REQUIRED | typeof BAD_REQUEST | typeof RESPONSE_TIMEOUT | typeof RESPONSE_DUPLICATE | typeof RESPONSE_INVALID | typeof RESPONSE_MISSING | typeof INTERNAL_SERVER_ERROR;
2
+ /**
3
+ * The API key you provided was invalid.
4
+ *
5
+ * HTTP status 401.
6
+ */
7
+ export declare const AUTH_INVALID = "auth_invalid";
8
+ /**
9
+ * You forgot to set the `X-API-Key` header.
10
+ *
11
+ * HTTP status 401.
12
+ */
13
+ export declare const AUTH_REQUIRED = "auth_required";
14
+ /**
15
+ * The sitekey in your request is invalid.
16
+ *
17
+ * HTTP status 400.
18
+ */
19
+ export declare const SITEKEY_INVALID = "sitekey_invalid";
20
+ /**
21
+ * Something else is wrong with your request, e.g. your request body is empty.
22
+ */
23
+ export declare const BAD_REQUEST = "bad_request";
24
+ /**
25
+ * The response has expired.
26
+ *
27
+ * HTTP status 200.
28
+ */
29
+ export declare const RESPONSE_TIMEOUT = "response_timeout";
30
+ /**
31
+ * The response has already been used.
32
+ *
33
+ * HTTP status 200.
34
+ */
35
+ export declare const RESPONSE_DUPLICATE = "response_duplicate";
36
+ /**
37
+ * The response you provided was invalid (perhaps the user tried to work around the captcha).
38
+ *
39
+ * HTTP status 200.
40
+ */
41
+ export declare const RESPONSE_INVALID = "response_invalid";
42
+ /**
43
+ * You forgot to add the response parameter.
44
+ *
45
+ * HTTP status 400.
46
+ */
47
+ export declare const RESPONSE_MISSING = "response_missing";
48
+ /**
49
+ * Something went wrong within the server. (Should never happen).
50
+ *
51
+ * HTTP status 500.
52
+ */
53
+ export declare const INTERNAL_SERVER_ERROR = "internal_server_error";
54
+ export declare const ERROR_CODE_TO_STATUS: Record<SiteverifyErrorCode, number>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The API key you provided was invalid.
3
+ *
4
+ * HTTP status 401.
5
+ */
6
+ export const AUTH_INVALID = "auth_invalid";
7
+ /**
8
+ * You forgot to set the `X-API-Key` header.
9
+ *
10
+ * HTTP status 401.
11
+ */
12
+ export const AUTH_REQUIRED = "auth_required";
13
+ /**
14
+ * The sitekey in your request is invalid.
15
+ *
16
+ * HTTP status 400.
17
+ */
18
+ export const SITEKEY_INVALID = "sitekey_invalid";
19
+ /**
20
+ * Something else is wrong with your request, e.g. your request body is empty.
21
+ */
22
+ export const BAD_REQUEST = "bad_request";
23
+ /**
24
+ * The response has expired.
25
+ *
26
+ * HTTP status 200.
27
+ */
28
+ export const RESPONSE_TIMEOUT = "response_timeout";
29
+ /**
30
+ * The response has already been used.
31
+ *
32
+ * HTTP status 200.
33
+ */
34
+ export const RESPONSE_DUPLICATE = "response_duplicate";
35
+ /**
36
+ * The response you provided was invalid (perhaps the user tried to work around the captcha).
37
+ *
38
+ * HTTP status 200.
39
+ */
40
+ export const RESPONSE_INVALID = "response_invalid";
41
+ /**
42
+ * You forgot to add the response parameter.
43
+ *
44
+ * HTTP status 400.
45
+ */
46
+ export const RESPONSE_MISSING = "response_missing";
47
+ /**
48
+ * Something went wrong within the server. (Should never happen).
49
+ *
50
+ * HTTP status 500.
51
+ */
52
+ export const INTERNAL_SERVER_ERROR = "internal_server_error";
53
+ export const ERROR_CODE_TO_STATUS = {
54
+ [SITEKEY_INVALID]: 400,
55
+ [AUTH_INVALID]: 401,
56
+ [AUTH_REQUIRED]: 401,
57
+ [BAD_REQUEST]: 400,
58
+ [RESPONSE_TIMEOUT]: 200,
59
+ [RESPONSE_DUPLICATE]: 200,
60
+ [RESPONSE_INVALID]: 200,
61
+ [RESPONSE_MISSING]: 400,
62
+ [INTERNAL_SERVER_ERROR]: 500,
63
+ };
64
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/api/errors.ts"],"names":[],"mappings":"AAWA;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC;AAC3C;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAAC;AAE7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAEjD;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,aAAa,CAAC;AACzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AACnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AACvD;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AACnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AACnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAE7D,MAAM,CAAC,MAAM,oBAAoB,GAAwC;IACvE,CAAC,eAAe,CAAC,EAAE,GAAG;IACtB,CAAC,YAAY,CAAC,EAAE,GAAG;IACnB,CAAC,aAAa,CAAC,EAAE,GAAG;IACpB,CAAC,WAAW,CAAC,EAAE,GAAG;IAClB,CAAC,gBAAgB,CAAC,EAAE,GAAG;IACvB,CAAC,kBAAkB,CAAC,EAAE,GAAG;IACzB,CAAC,gBAAgB,CAAC,EAAE,GAAG;IACvB,CAAC,gBAAgB,CAAC,EAAE,GAAG;IACvB,CAAC,qBAAqB,CAAC,EAAE,GAAG;CAC7B,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/api/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
@@ -0,0 +1,59 @@
1
+ import { SiteverifyErrorCode } from "./errors";
2
+ /**
3
+ * The request we make to the Frienldy Captcha API.
4
+ * @internal
5
+ */
6
+ export interface SiteverifyRequest {
7
+ /**
8
+ * The response value that the user submitted in the frc-captcha-response field
9
+ */
10
+ response: string;
11
+ /**
12
+ * Optional: the sitekey that you want to make sure the puzzle was generated from.
13
+ */
14
+ sitekey?: string;
15
+ }
16
+ /**
17
+ * @public
18
+ */
19
+ export interface SiteverifyResponseData {
20
+ challenge: SiteverifyResponseChallengeData;
21
+ }
22
+ /**
23
+ * @public
24
+ */
25
+ export interface SiteverifyResponseChallengeData {
26
+ /**
27
+ * Timestamp when the captcha challenge was completed (RFC3339).
28
+ */
29
+ timestamp: string;
30
+ /**
31
+ * The origin of the site where the captcha was solved (if known, can be empty string if not known).
32
+ */
33
+ origin: string;
34
+ }
35
+ /**
36
+ * @public
37
+ */
38
+ export interface SiteverifySuccessResponse {
39
+ success: true;
40
+ data: SiteverifyResponseData;
41
+ }
42
+ /**
43
+ * @public
44
+ */
45
+ export interface SiteverifyErrorResponseErrorData {
46
+ error_code: SiteverifyErrorCode;
47
+ detail: string;
48
+ }
49
+ /**
50
+ * @public
51
+ */
52
+ export interface SiteverifyErrorResponse {
53
+ success: false;
54
+ error: SiteverifyErrorResponseErrorData;
55
+ }
56
+ /**
57
+ * @public
58
+ */
59
+ export type SiteverifyResponse = SiteverifySuccessResponse | SiteverifyErrorResponse;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/api/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,53 @@
1
+ import { VerifyResult } from "./result.js";
2
+ /**
3
+ * Configuration options when creating a new `FriendlyCaptchaClient`.
4
+ * @public
5
+ */
6
+ export interface FriendlyCaptchaOptions {
7
+ sitekey?: string;
8
+ /**
9
+ * Friendly Captcha API Key.
10
+ */
11
+ apiKey: string;
12
+ /**
13
+ * The endpoint to use for the API. Can be "eu", "global", or a custom URL.
14
+ *
15
+ * Defaults to `"global"`.
16
+ */
17
+ siteverifyEndpoint?: string;
18
+ /**
19
+ * Whether to use strict mode, which rejects all captcha responses which were not strictly verified.
20
+ *
21
+ * This is not recommended. Defaults to `false`.
22
+ */
23
+ strict?: boolean;
24
+ /**
25
+ * The fetch implementation to use. Defaults to `globalThis.fetch`.
26
+ */
27
+ fetch?: typeof globalThis.fetch;
28
+ }
29
+ /**
30
+ * A client for the Friendly Captcha API.
31
+ * @public
32
+ */
33
+ export declare class FriendlyCaptchaClient {
34
+ private sitekey?;
35
+ private apiKey;
36
+ private siteverifyEndpoint;
37
+ private strict;
38
+ private fetch;
39
+ constructor(opts: FriendlyCaptchaOptions);
40
+ /**
41
+ * Verify a captcha response.
42
+ *
43
+ * @param response - The response token from the captcha.
44
+ * @param opts - Optional options object:
45
+ * * `timeout`: The timeout in milliseconds. Defaults to 20 seconds.
46
+ * * `sitekey`: The sitekey to use for this request. Defaults to the sitekey passed to the constructor (if any).
47
+ * @returns A promise that always resolves to a `VerifyResult` object, which contains the fields `shouldAccept()` and `wasAbleToVerify()`. This promise never rejects.
48
+ */
49
+ verifyCaptchaResponse(response: string, opts?: {
50
+ timeout?: number;
51
+ sitekey?: string;
52
+ }): Promise<VerifyResult>;
53
+ }
@@ -0,0 +1,100 @@
1
+ import { FAILED_DUE_TO_CLIENT_ERROR_CODE, FAILED_TO_DECODE_RESPONSE_ERROR_CODE, FAILED_TO_ENCODE_ERROR_CODE, REQUEST_FAILED_ERROR_CODE, REQUEST_FAILED_TIMEOUT_ERROR_CODE, } from "./errors.js";
2
+ import { VerifyResult } from "./result.js";
3
+ import { SDK_VERSION } from "./version.gen.js";
4
+ const GLOBAL_SITEVERIFY_ENDPOINT = "https://global.frcapi.com/api/v2/captcha/siteverify";
5
+ const EU_SITEVERIFY_ENDPOINT = "https://eu.frcapi.com/api/v2/captcha/siteverify";
6
+ /**
7
+ * A client for the Friendly Captcha API.
8
+ * @public
9
+ */
10
+ export class FriendlyCaptchaClient {
11
+ constructor(opts) {
12
+ this.sitekey = opts.sitekey;
13
+ if (!opts.apiKey) {
14
+ throw new Error("api key is required");
15
+ }
16
+ this.apiKey = opts.apiKey;
17
+ let siteverifyEndpoint = opts.siteverifyEndpoint || "global";
18
+ if (siteverifyEndpoint === "global") {
19
+ siteverifyEndpoint = GLOBAL_SITEVERIFY_ENDPOINT;
20
+ }
21
+ else if (siteverifyEndpoint === "eu") {
22
+ siteverifyEndpoint = EU_SITEVERIFY_ENDPOINT;
23
+ }
24
+ this.siteverifyEndpoint = siteverifyEndpoint;
25
+ this.strict = !!opts.strict;
26
+ this.fetch = opts.fetch || globalThis.fetch;
27
+ }
28
+ /**
29
+ * Verify a captcha response.
30
+ *
31
+ * @param response - The response token from the captcha.
32
+ * @param opts - Optional options object:
33
+ * * `timeout`: The timeout in milliseconds. Defaults to 20 seconds.
34
+ * * `sitekey`: The sitekey to use for this request. Defaults to the sitekey passed to the constructor (if any).
35
+ * @returns A promise that always resolves to a `VerifyResult` object, which contains the fields `shouldAccept()` and `wasAbleToVerify()`. This promise never rejects.
36
+ */
37
+ verifyCaptchaResponse(response, opts = {}) {
38
+ const siteverifyRequest = {
39
+ response,
40
+ };
41
+ if (this.sitekey || opts.sitekey) {
42
+ siteverifyRequest.sitekey = this.sitekey || opts.sitekey;
43
+ }
44
+ const result = new VerifyResult(this.strict);
45
+ let body;
46
+ try {
47
+ body = JSON.stringify(siteverifyRequest);
48
+ }
49
+ catch (e) {
50
+ result.clientErrorType = FAILED_TO_ENCODE_ERROR_CODE;
51
+ return Promise.resolve(result);
52
+ }
53
+ const headers = {
54
+ "Content-Type": "application/json",
55
+ Accept: "application/json",
56
+ "X-Frc-Sdk": "friendly-captcha-javascript-sdk@" + SDK_VERSION,
57
+ "X-Api-Key": this.apiKey,
58
+ };
59
+ const timeout = (opts === null || opts === void 0 ? void 0 : opts.timeout) || 20000;
60
+ return new Promise((resolve) => {
61
+ const controller = new AbortController();
62
+ const signal = controller.signal;
63
+ setTimeout(() => {
64
+ controller.abort();
65
+ result.clientErrorType = REQUEST_FAILED_TIMEOUT_ERROR_CODE;
66
+ resolve(result);
67
+ }, timeout);
68
+ this.fetch(this.siteverifyEndpoint, {
69
+ method: "POST",
70
+ headers,
71
+ body,
72
+ signal,
73
+ })
74
+ .then((response) => {
75
+ result.status = response.status;
76
+ if (response.status >= 400 && response.status < 500) {
77
+ result.clientErrorType = FAILED_DUE_TO_CLIENT_ERROR_CODE;
78
+ }
79
+ return response.json().catch(() => {
80
+ result.clientErrorType = FAILED_TO_DECODE_RESPONSE_ERROR_CODE;
81
+ resolve(result);
82
+ });
83
+ })
84
+ .then((json) => {
85
+ if (typeof json !== "object" || json === null) {
86
+ result.clientErrorType = FAILED_TO_DECODE_RESPONSE_ERROR_CODE;
87
+ resolve(result);
88
+ return;
89
+ }
90
+ result.response = json;
91
+ resolve(result);
92
+ })
93
+ .catch((e) => {
94
+ result.clientErrorType = REQUEST_FAILED_ERROR_CODE;
95
+ resolve(result);
96
+ });
97
+ });
98
+ }
99
+ }
100
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/client/client.ts"],"names":[],"mappings":"AACA,OAAO,EACL,+BAA+B,EAC/B,oCAAoC,EACpC,2BAA2B,EAC3B,yBAAyB,EACzB,iCAAiC,GAClC,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAiC/C,MAAM,0BAA0B,GAAG,qDAAqD,CAAC;AACzF,MAAM,sBAAsB,GAAG,iDAAiD,CAAC;AAEjF;;;GAGG;AACH,MAAM,OAAO,qBAAqB;IAQhC,YAAY,IAA4B;QACtC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAE5B,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;SACxC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAE1B,IAAI,kBAAkB,GAAG,IAAI,CAAC,kBAAkB,IAAI,QAAQ,CAAC;QAC7D,IAAI,kBAAkB,KAAK,QAAQ,EAAE;YACnC,kBAAkB,GAAG,0BAA0B,CAAC;SACjD;aAAM,IAAI,kBAAkB,KAAK,IAAI,EAAE;YACtC,kBAAkB,GAAG,sBAAsB,CAAC;SAC7C;Q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/**
2
+ * Failed to encode the captcha response. This means the captcha response was invalid and should never be accepted.
3
+ *
4
+ * @public
5
+ */
6
+ export declare const FAILED_TO_ENCODE_ERROR_CODE = "failed_to_encode_request";
7
+ /**
8
+ *
9
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
10
+ *
11
+ * @public
12
+ */
13
+ export declare const REQUEST_FAILED_ERROR_CODE = "request_failed";
14
+ /**
15
+ *
16
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
17
+ *
18
+ * @public
19
+ */
20
+ export declare const REQUEST_FAILED_TIMEOUT_ERROR_CODE = "request_failed_due_to_timeout";
21
+ /**
22
+ * An error occured on the client side that could've been prevented. This generally means your configuration is wrong.
23
+ *
24
+ * Check your API key and sitekey.
25
+ * @public
26
+ */
27
+ export declare const FAILED_DUE_TO_CLIENT_ERROR_CODE = "request_failed_due_to_client_error";
28
+ /**
29
+ * The response from the Friendly Captcha API could not be decoded.
30
+ *
31
+ * @public
32
+ */
33
+ export declare const FAILED_TO_DECODE_RESPONSE_ERROR_CODE = "verification_response_could_not_be_decoded";
34
+ /**
35
+ * @public
36
+ */
37
+ export type VerifyClientErrorCode = typeof FAILED_TO_ENCODE_ERROR_CODE | typeof REQUEST_FAILED_ERROR_CODE | typeof REQUEST_FAILED_TIMEOUT_ERROR_CODE | typeof FAILED_DUE_TO_CLIENT_ERROR_CODE | typeof FAILED_TO_DECODE_RESPONSE_ERROR_CODE;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Failed to encode the captcha response. This means the captcha response was invalid and should never be accepted.
3
+ *
4
+ * @public
5
+ */
6
+ export const FAILED_TO_ENCODE_ERROR_CODE = "failed_to_encode_request";
7
+ /**
8
+ *
9
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
10
+ *
11
+ * @public
12
+ */
13
+ export const REQUEST_FAILED_ERROR_CODE = "request_failed";
14
+ /**
15
+ *
16
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
17
+ *
18
+ * @public
19
+ */
20
+ export const REQUEST_FAILED_TIMEOUT_ERROR_CODE = "request_failed_due_to_timeout";
21
+ /**
22
+ * An error occured on the client side that could've been prevented. This generally means your configuration is wrong.
23
+ *
24
+ * Check your API key and sitekey.
25
+ * @public
26
+ */
27
+ export const FAILED_DUE_TO_CLIENT_ERROR_CODE = "request_failed_due_to_client_error";
28
+ /**
29
+ * The response from the Friendly Captcha API could not be decoded.
30
+ *
31
+ * @public
32
+ */
33
+ export const FAILED_TO_DECODE_RESPONSE_ERROR_CODE = "verification_response_could_not_be_decoded";
34
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/client/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,0BAA0B,CAAC;AACtE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,gBAAgB,CAAC;AAC1D;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,+BAA+B,CAAC;AACjF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,+BAA+B,GAAG,oCAAoC,CAAC;AACpF;;;;GAIG;AACH,MAAM,CAAC,MAAM,oCAAoC,GAAG,4CAA4C,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./client.js";
2
+ export * from "./result.js";
3
+ export * from "./errors.js";
@@ -0,0 +1,4 @@
1
+ export * from "./client.js";
2
+ export * from "./result.js";
3
+ export * from "./errors.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,67 @@
1
+ import type { SiteverifyErrorResponseErrorData, SiteverifyResponse } from "../api";
2
+ import { VerifyClientErrorCode } from "./errors.js";
3
+ /**
4
+ * The result of a captcha siteverify request.
5
+ *
6
+ * @public
7
+ */
8
+ export declare class VerifyResult {
9
+ private strict;
10
+ /**
11
+ * The HTTP status code of the response.
12
+ * `-1` if there was no response.
13
+ */
14
+ status: number;
15
+ /**
16
+ * The response from the Friendly Captcha API, or null if the request was not made at all.
17
+ */
18
+ response: SiteverifyResponse | null;
19
+ clientErrorType: VerifyClientErrorCode | null;
20
+ constructor(strict: boolean);
21
+ /**
22
+ * @returns Whether strict mode was enabled for this verification.
23
+ */
24
+ isStrict(): boolean;
25
+ /**
26
+ * @returns Whether the captcha should be accepted.
27
+ * Note that this does not necessarily mean it was verified.
28
+ */
29
+ shouldAccept(): boolean;
30
+ /**
31
+ * @returns The reverse of `shouldAccept()`.
32
+ */
33
+ shouldReject(): boolean;
34
+ /**
35
+ * Was unable to encode the captcha response. This means the captcha response was invalid and should never be accepted.
36
+ */
37
+ isEncodeError(): boolean;
38
+ /**
39
+ * Something went wrong making the request to the Friendly Captcha API, perhaps there is a network connection issue?
40
+ */
41
+ isRequestOrTimeoutError(): boolean;
42
+ /**
43
+ * Something went wrong decoding the response from the Friendly Captcha API.
44
+ */
45
+ isDecodeError(): boolean;
46
+ /**
47
+ * Something went wrong on the client side, this generally means your configuration is wrong.
48
+ * Check your secrets (API key) and sitekey.
49
+ *
50
+ * See `getResponseError()` for more information.
51
+ */
52
+ isClientError(): boolean;
53
+ /**
54
+ * @returns The response from the Friendly Captcha API, or null if the request was not made at all.
55
+ */
56
+ getResponse(): SiteverifyResponse | null;
57
+ /**
58
+ * @returns The `error` field form the response, or null if it is not present.
59
+ */
60
+ getResponseError(): SiteverifyErrorResponseErrorData | null;
61
+ getErrorCode(): VerifyClientErrorCode | null;
62
+ /**
63
+ * Whether the request to verify the captcha was completed. In other words: the API responded with status 200.'
64
+ * If this is false, you should notify yourself and use `getErrorCode()` and `getResponseError()` to see what is wrong.
65
+ */
66
+ wasAbleToVerify(): boolean;
67
+ }