@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,125 @@
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
+ /**
3
+ * The result of a captcha siteverify request.
4
+ *
5
+ * @public
6
+ */
7
+ export class VerifyResult {
8
+ constructor(strict) {
9
+ /**
10
+ * The HTTP status code of the response.
11
+ * `-1` if there was no response.
12
+ */
13
+ this.status = -1;
14
+ /**
15
+ * The response from the Friendly Captcha API, or null if the request was not made at all.
16
+ */
17
+ this.response = null;
18
+ this.clientErrorType = null;
19
+ this.strict = strict;
20
+ }
21
+ /**
22
+ * @returns Whether strict mode was enabled for this verification.
23
+ */
24
+ isStrict() {
25
+ return this.strict;
26
+ }
27
+ /**
28
+ * @returns Whether the captcha should be accepted.
29
+ * Note that this does not necessarily mean it was verified.
30
+ */
31
+ shouldAccept() {
32
+ if (this.wasAbleToVerify()) {
33
+ // We want to reject in case we were not able to encode the captcha response sent by the client.
34
+ // This is because an attacker could send malformed data that can not be encoded, and if we would accept that
35
+ // they could circumvent the captcha.
36
+ if (this.isEncodeError()) {
37
+ return false;
38
+ }
39
+ return this.response.success === true;
40
+ }
41
+ if (this.clientErrorType !== null) {
42
+ if (this.strict) {
43
+ // In strict mode we reject on any error.
44
+ return false;
45
+ }
46
+ else if (this.clientErrorType === REQUEST_FAILED_ERROR_CODE ||
47
+ this.clientErrorType === REQUEST_FAILED_TIMEOUT_ERROR_CODE ||
48
+ this.clientErrorType === FAILED_DUE_TO_CLIENT_ERROR_CODE ||
49
+ this.clientErrorType === FAILED_TO_DECODE_RESPONSE_ERROR_CODE) {
50
+ // In case of failures that are not the captcha response being invalid or rejected, we accept.
51
+ // This is because we don't want to lock out all users in case of a temporary network outage or a misconfiguration.
52
+ return true;
53
+ }
54
+ else {
55
+ return false;
56
+ }
57
+ }
58
+ throw new Error("Implementation error in @friendlycaptcha/server-sdk shouldAccept: errorCode should never be undefined if success is false. " +
59
+ JSON.stringify(this));
60
+ }
61
+ /**
62
+ * @returns The reverse of `shouldAccept()`.
63
+ */
64
+ shouldReject() {
65
+ return !this.shouldAccept();
66
+ }
67
+ /**
68
+ * Was unable to encode the captcha response. This means the captcha response was invalid and should never be accepted.
69
+ */
70
+ isEncodeError() {
71
+ return this.clientErrorType === FAILED_TO_ENCODE_ERROR_CODE;
72
+ }
73
+ /**
74
+ * Something went wrong making the request to the Friendly Captcha API, perhaps there is a network connection issue?
75
+ */
76
+ isRequestOrTimeoutError() {
77
+ return this.clientErrorType === REQUEST_FAILED_ERROR_CODE || this.clientErrorType === REQUEST_FAILED_TIMEOUT_ERROR_CODE;
78
+ }
79
+ /**
80
+ * Something went wrong decoding the response from the Friendly Captcha API.
81
+ */
82
+ isDecodeError() {
83
+ return this.clientErrorType === FAILED_TO_DECODE_RESPONSE_ERROR_CODE;
84
+ }
85
+ /**
86
+ * Something went wrong on the client side, this generally means your configuration is wrong.
87
+ * Check your secrets (API key) and sitekey.
88
+ *
89
+ * See `getResponseError()` for more information.
90
+ */
91
+ isClientError() {
92
+ return this.clientErrorType === FAILED_DUE_TO_CLIENT_ERROR_CODE;
93
+ }
94
+ /**
95
+ * @returns The response from the Friendly Captcha API, or null if the request was not made at all.
96
+ */
97
+ getResponse() {
98
+ return this.response;
99
+ }
100
+ /**
101
+ * @returns The `error` field form the response, or null if it is not present.
102
+ */
103
+ getResponseError() {
104
+ if (!this.response || this.response.success)
105
+ return null;
106
+ return this.response.error;
107
+ }
108
+ getErrorCode() {
109
+ return this.clientErrorType;
110
+ }
111
+ /**
112
+ * Whether the request to verify the captcha was completed. In other words: the API responded with status 200.'
113
+ * If this is false, you should notify yourself and use `getErrorCode()` and `getResponseError()` to see what is wrong.
114
+ */
115
+ wasAbleToVerify() {
116
+ // If we failed to encode, we actually consider `wasAbleToVerify` to be true. This is because we don't want to
117
+ // alert on failed encoding: an attacker could send such malformed data that it fails to encode.
118
+ if (this.isEncodeError()) {
119
+ return true;
120
+ }
121
+ // We got a status 200, and we were able to actually make the request and decode its response.
122
+ return this.status === 200 && !this.isRequestOrTimeoutError() && !this.isDecodeError();
123
+ }
124
+ }
125
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../src/client/result.ts"],"names":[],"mappings":"AACA,OAAO,EACL,+BAA+B,EAC/B,oCAAoC,EACpC,2BAA2B,EAC3B,yBAAyB,EACzB,iCAAiC,GAElC,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,MAAM,OAAO,YAAY;IAevB,YAAY,MAAe;QAZ3B;;;WAGG;QACI,WAAM,GAAW,CAAC,CAAC,CAAC;QAE3B;;WAEG;QACI,aAAQ,GAA8B,IAAI,CAAC;QAC3C,oBAAe,GAAiC,IAAI,CAAC;QAG1D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,QAAQ;QACb,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;OAGG;IACI,YAAY;QACjB,IAAI,IAAI,CAAC,eAAe,EAAE,EAAE;YAC1B,gGAAgG;YAChG,6GAA6G;YAC7G,qCAAqC;YACrC,IAAI,IAAI,CAAC,aAAa,EAAE,EAAE;gBACxB,OAAO,KAAK,CAAC;aACd;YAED,OAAO,IAAI,CAAC,QAAS,CAAC,OAAO,KAAK,IAAI,CAAC;SACxC;QACD,IAAI,IAAI,CAAC,eAAe,KAAK,IAAI,EAAE;YACjC,IAAI,IAAI,CAAC,MAAM,EAAE;gBACf,yCAAyC;gBACzC,OAAO,KAAK,CAAC;aACd;iBAAM,IACL,IAAI,CAAC,eAAe,KAAK,yBAAyB;gBAClD,IAAI,CAAC,eAAe,KAAK,iCAAiC;gBAC1D,IAAI,CAAC,eAAe,KAAK,+BAA+B;gBACxD,IAAI,CAAC,eAAe,KAAK,oCAAoC,EAC7D;gBACA,8FAA8F;gBAC9F,mHAAmH;gBACnH,OAAO,IAAI,CAAC;aACb;iBAAM;gBACL,OAAO,KAAK,CAAC;aACd;SACF;QAED,MAAM,IAAI,KAAK,CACb,6HAA6H;YAC3H,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CACvB,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,YAAY;QACjB,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,aAAa;QAClB,OAAO,IAAI,CAAC,eAAe,KAAK,2BAA2B,CAAC;IAC9D,CAAC;IAED;;OAEG;IACI,uBAAuB;QAC5B,OAAO,IAAI,CAAC,eAAe,KAAK,yBAAyB,IAAI,IAAI,CAAC,eAAe,KAAK,iCAAiC,CAAC;IAC1H,CAAC;IAED;;OAEG;IACI,aAAa;QAClB,OAAO,IAAI,CAAC,eAAe,KAAK,oCAAoC,CAAC;IACvE,CAAC;IAED;;;;;OAKG;IACI,aAAa;QAClB,OAAO,IAAI,CAAC,eAAe,KAAK,+BAA+B,CAAC;IAClE,CAAC;IACD;;OAEG;IACI,WAAW;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,gBAAgB;QACrB,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QACzD,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;IAC7B,CAAC;IAEM,YAAY;QACjB,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACI,eAAe;QACpB,+GAA+G;QAC/G,gGAAgG;QAChG,IAAI,IAAI,CAAC,aAAa,EAAE,EAAE;YACxB,OAAO,IAAI,CAAC;SACb;QAED,8FAA8F;QAC9F,OAAO,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,uBAAuB,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IACzF,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ export declare const SDK_VERSION = "0.1.0";
@@ -0,0 +1,4 @@
1
+ // This file is auto-generated by scripts/updateVersion.mjs
2
+ // Do not edit this file directly
3
+ export const SDK_VERSION = "0.1.0";
4
+ //# sourceMappingURL=version.gen.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.gen.js","sourceRoot":"","sources":["../../../src/client/version.gen.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,iCAAiC;AACjC,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC"}
@@ -0,0 +1,292 @@
1
+ /**
2
+ * The API key you provided was invalid.
3
+ *
4
+ * HTTP status 401.
5
+ */
6
+ declare const AUTH_INVALID = "auth_invalid";
7
+
8
+ /**
9
+ * You forgot to set the `X-API-Key` header.
10
+ *
11
+ * HTTP status 401.
12
+ */
13
+ declare const AUTH_REQUIRED = "auth_required";
14
+
15
+ /**
16
+ * Something else is wrong with your request, e.g. your request body is empty.
17
+ */
18
+ declare const BAD_REQUEST = "bad_request";
19
+
20
+ /**
21
+ * An error occured on the client side that could've been prevented. This generally means your configuration is wrong.
22
+ *
23
+ * Check your API key and sitekey.
24
+ * @public
25
+ */
26
+ export declare const FAILED_DUE_TO_CLIENT_ERROR_CODE = "request_failed_due_to_client_error";
27
+
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
+ /**
36
+ * Failed to encode the captcha response. This means the captcha response was invalid and should never be accepted.
37
+ *
38
+ * @public
39
+ */
40
+ export declare const FAILED_TO_ENCODE_ERROR_CODE = "failed_to_encode_request";
41
+
42
+ /**
43
+ * A client for the Friendly Captcha API.
44
+ * @public
45
+ */
46
+ export declare class FriendlyCaptchaClient {
47
+ private sitekey?;
48
+ private apiKey;
49
+ private siteverifyEndpoint;
50
+ private strict;
51
+ private fetch;
52
+ constructor(opts: FriendlyCaptchaOptions);
53
+ /**
54
+ * Verify a captcha response.
55
+ *
56
+ * @param response - The response token from the captcha.
57
+ * @param opts - Optional options object:
58
+ * * `timeout`: The timeout in milliseconds. Defaults to 20 seconds.
59
+ * * `sitekey`: The sitekey to use for this request. Defaults to the sitekey passed to the constructor (if any).
60
+ * @returns A promise that always resolves to a `VerifyResult` object, which contains the fields `shouldAccept()` and `wasAbleToVerify()`. This promise never rejects.
61
+ */
62
+ verifyCaptchaResponse(response: string, opts?: {
63
+ timeout?: number;
64
+ sitekey?: string;
65
+ }): Promise<VerifyResult>;
66
+ }
67
+
68
+ /**
69
+ * Configuration options when creating a new `FriendlyCaptchaClient`.
70
+ * @public
71
+ */
72
+ export declare interface FriendlyCaptchaOptions {
73
+ sitekey?: string;
74
+ /**
75
+ * Friendly Captcha API Key.
76
+ */
77
+ apiKey: string;
78
+ /**
79
+ * The endpoint to use for the API. Can be "eu", "global", or a custom URL.
80
+ *
81
+ * Defaults to `"global"`.
82
+ */
83
+ siteverifyEndpoint?: string;
84
+ /**
85
+ * Whether to use strict mode, which rejects all captcha responses which were not strictly verified.
86
+ *
87
+ * This is not recommended. Defaults to `false`.
88
+ */
89
+ strict?: boolean;
90
+ /**
91
+ * The fetch implementation to use. Defaults to `globalThis.fetch`.
92
+ */
93
+ fetch?: typeof globalThis.fetch;
94
+ }
95
+
96
+ /**
97
+ * Something went wrong within the server. (Should never happen).
98
+ *
99
+ * HTTP status 500.
100
+ */
101
+ declare const INTERNAL_SERVER_ERROR = "internal_server_error";
102
+
103
+ /**
104
+ *
105
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
106
+ *
107
+ * @public
108
+ */
109
+ export declare const REQUEST_FAILED_ERROR_CODE = "request_failed";
110
+
111
+ /**
112
+ *
113
+ * The request couldn't be made, perhaps there is a network outage, DNS issue, or the API is unreachable.
114
+ *
115
+ * @public
116
+ */
117
+ export declare const REQUEST_FAILED_TIMEOUT_ERROR_CODE = "request_failed_due_to_timeout";
118
+
119
+ /**
120
+ * The response has already been used.
121
+ *
122
+ * HTTP status 200.
123
+ */
124
+ declare const RESPONSE_DUPLICATE = "response_duplicate";
125
+
126
+ /**
127
+ * The response you provided was invalid (perhaps the user tried to work around the captcha).
128
+ *
129
+ * HTTP status 200.
130
+ */
131
+ declare const RESPONSE_INVALID = "response_invalid";
132
+
133
+ /**
134
+ * You forgot to add the response parameter.
135
+ *
136
+ * HTTP status 400.
137
+ */
138
+ declare const RESPONSE_MISSING = "response_missing";
139
+
140
+ /**
141
+ * The response has expired.
142
+ *
143
+ * HTTP status 200.
144
+ */
145
+ declare const RESPONSE_TIMEOUT = "response_timeout";
146
+
147
+ /**
148
+ * The sitekey in your request is invalid.
149
+ *
150
+ * HTTP status 400.
151
+ */
152
+ declare const SITEKEY_INVALID = "sitekey_invalid";
153
+
154
+ declare 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;
155
+
156
+ /**
157
+ * @public
158
+ */
159
+ export declare interface SiteverifyErrorResponse {
160
+ success: false;
161
+ error: SiteverifyErrorResponseErrorData;
162
+ }
163
+
164
+ /**
165
+ * @public
166
+ */
167
+ export declare interface SiteverifyErrorResponseErrorData {
168
+ error_code: SiteverifyErrorCode;
169
+ detail: string;
170
+ }
171
+
172
+ /**
173
+ * The request we make to the Frienldy Captcha API.
174
+ * @internal
175
+ */
176
+ export declare interface SiteverifyRequest {
177
+ /**
178
+ * The response value that the user submitted in the frc-captcha-response field
179
+ */
180
+ response: string;
181
+ /**
182
+ * Optional: the sitekey that you want to make sure the puzzle was generated from.
183
+ */
184
+ sitekey?: string;
185
+ }
186
+
187
+ /**
188
+ * @public
189
+ */
190
+ export declare type SiteverifyResponse = SiteverifySuccessResponse | SiteverifyErrorResponse;
191
+
192
+ /**
193
+ * @public
194
+ */
195
+ export declare interface SiteverifyResponseChallengeData {
196
+ /**
197
+ * Timestamp when the captcha challenge was completed (RFC3339).
198
+ */
199
+ timestamp: string;
200
+ /**
201
+ * The origin of the site where the captcha was solved (if known, can be empty string if not known).
202
+ */
203
+ origin: string;
204
+ }
205
+
206
+ /**
207
+ * @public
208
+ */
209
+ export declare interface SiteverifyResponseData {
210
+ challenge: SiteverifyResponseChallengeData;
211
+ }
212
+
213
+ /**
214
+ * @public
215
+ */
216
+ export declare interface SiteverifySuccessResponse {
217
+ success: true;
218
+ data: SiteverifyResponseData;
219
+ }
220
+
221
+ /**
222
+ * @public
223
+ */
224
+ export declare 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;
225
+
226
+ /**
227
+ * The result of a captcha siteverify request.
228
+ *
229
+ * @public
230
+ */
231
+ export declare class VerifyResult {
232
+ private strict;
233
+ /**
234
+ * The HTTP status code of the response.
235
+ * `-1` if there was no response.
236
+ */
237
+ status: number;
238
+ /**
239
+ * The response from the Friendly Captcha API, or null if the request was not made at all.
240
+ */
241
+ response: SiteverifyResponse | null;
242
+ clientErrorType: VerifyClientErrorCode | null;
243
+ constructor(strict: boolean);
244
+ /**
245
+ * @returns Whether strict mode was enabled for this verification.
246
+ */
247
+ isStrict(): boolean;
248
+ /**
249
+ * @returns Whether the captcha should be accepted.
250
+ * Note that this does not necessarily mean it was verified.
251
+ */
252
+ shouldAccept(): boolean;
253
+ /**
254
+ * @returns The reverse of `shouldAccept()`.
255
+ */
256
+ shouldReject(): boolean;
257
+ /**
258
+ * Was unable to encode the captcha response. This means the captcha response was invalid and should never be accepted.
259
+ */
260
+ isEncodeError(): boolean;
261
+ /**
262
+ * Something went wrong making the request to the Friendly Captcha API, perhaps there is a network connection issue?
263
+ */
264
+ isRequestOrTimeoutError(): boolean;
265
+ /**
266
+ * Something went wrong decoding the response from the Friendly Captcha API.
267
+ */
268
+ isDecodeError(): boolean;
269
+ /**
270
+ * Something went wrong on the client side, this generally means your configuration is wrong.
271
+ * Check your secrets (API key) and sitekey.
272
+ *
273
+ * See `getResponseError()` for more information.
274
+ */
275
+ isClientError(): boolean;
276
+ /**
277
+ * @returns The response from the Friendly Captcha API, or null if the request was not made at all.
278
+ */
279
+ getResponse(): SiteverifyResponse | null;
280
+ /**
281
+ * @returns The `error` field form the response, or null if it is not present.
282
+ */
283
+ getResponseError(): SiteverifyErrorResponseErrorData | null;
284
+ getErrorCode(): VerifyClientErrorCode | null;
285
+ /**
286
+ * Whether the request to verify the captcha was completed. In other words: the API responded with status 200.'
287
+ * If this is false, you should notify yourself and use `getErrorCode()` and `getResponseError()` to see what is wrong.
288
+ */
289
+ wasAbleToVerify(): boolean;
290
+ }
291
+
292
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./api/index.js";
2
+ export * from "./client/index.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.38.0"
9
+ }
10
+ ]
11
+ }
package/docs/index.md ADDED
@@ -0,0 +1,12 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md)
4
+
5
+ ## API Reference
6
+
7
+ ## Packages
8
+
9
+ | Package | Description |
10
+ | --- | --- |
11
+ | [@friendlycaptcha/server-sdk](./server-sdk.md) | |
12
+
@@ -0,0 +1,15 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FAILED\_DUE\_TO\_CLIENT\_ERROR\_CODE](./server-sdk.failed_due_to_client_error_code.md)
4
+
5
+ ## FAILED\_DUE\_TO\_CLIENT\_ERROR\_CODE variable
6
+
7
+ An error occured on the client side that could've been prevented. This generally means your configuration is wrong.
8
+
9
+ Check your API key and sitekey.
10
+
11
+ **Signature:**
12
+
13
+ ```typescript
14
+ FAILED_DUE_TO_CLIENT_ERROR_CODE = "request_failed_due_to_client_error"
15
+ ```
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FAILED\_TO\_DECODE\_RESPONSE\_ERROR\_CODE](./server-sdk.failed_to_decode_response_error_code.md)
4
+
5
+ ## FAILED\_TO\_DECODE\_RESPONSE\_ERROR\_CODE variable
6
+
7
+ The response from the Friendly Captcha API could not be decoded.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ FAILED_TO_DECODE_RESPONSE_ERROR_CODE = "verification_response_could_not_be_decoded"
13
+ ```
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FAILED\_TO\_ENCODE\_ERROR\_CODE](./server-sdk.failed_to_encode_error_code.md)
4
+
5
+ ## FAILED\_TO\_ENCODE\_ERROR\_CODE variable
6
+
7
+ Failed to encode the captcha response. This means the captcha response was invalid and should never be accepted.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ FAILED_TO_ENCODE_ERROR_CODE = "failed_to_encode_request"
13
+ ```
@@ -0,0 +1,20 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaClient](./server-sdk.friendlycaptchaclient.md) &gt; [(constructor)](./server-sdk.friendlycaptchaclient._constructor_.md)
4
+
5
+ ## FriendlyCaptchaClient.(constructor)
6
+
7
+ Constructs a new instance of the `FriendlyCaptchaClient` class
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ constructor(opts: FriendlyCaptchaOptions);
13
+ ```
14
+
15
+ ## Parameters
16
+
17
+ | Parameter | Type | Description |
18
+ | --- | --- | --- |
19
+ | opts | [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md) | |
20
+
@@ -0,0 +1,26 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaClient](./server-sdk.friendlycaptchaclient.md)
4
+
5
+ ## FriendlyCaptchaClient class
6
+
7
+ A client for the Friendly Captcha API.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ export declare class FriendlyCaptchaClient
13
+ ```
14
+
15
+ ## Constructors
16
+
17
+ | Constructor | Modifiers | Description |
18
+ | --- | --- | --- |
19
+ | [(constructor)(opts)](./server-sdk.friendlycaptchaclient._constructor_.md) | | Constructs a new instance of the <code>FriendlyCaptchaClient</code> class |
20
+
21
+ ## Methods
22
+
23
+ | Method | Modifiers | Description |
24
+ | --- | --- | --- |
25
+ | [verifyCaptchaResponse(response, opts)](./server-sdk.friendlycaptchaclient.verifycaptcharesponse.md) | | Verify a captcha response. |
26
+
@@ -0,0 +1,30 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaClient](./server-sdk.friendlycaptchaclient.md) &gt; [verifyCaptchaResponse](./server-sdk.friendlycaptchaclient.verifycaptcharesponse.md)
4
+
5
+ ## FriendlyCaptchaClient.verifyCaptchaResponse() method
6
+
7
+ Verify a captcha response.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ verifyCaptchaResponse(response: string, opts?: {
13
+ timeout?: number;
14
+ sitekey?: string;
15
+ }): Promise<VerifyResult>;
16
+ ```
17
+
18
+ ## Parameters
19
+
20
+ | Parameter | Type | Description |
21
+ | --- | --- | --- |
22
+ | response | string | The response token from the captcha. |
23
+ | opts | { timeout?: number; sitekey?: string; } | _(Optional)_ Optional options object: \* <code>timeout</code>: The timeout in milliseconds. Defaults to 20 seconds. \* <code>sitekey</code>: The sitekey to use for this request. Defaults to the sitekey passed to the constructor (if any). |
24
+
25
+ **Returns:**
26
+
27
+ Promise&lt;[VerifyResult](./server-sdk.verifyresult.md)<!-- -->&gt;
28
+
29
+ A promise that always resolves to a `VerifyResult` object, which contains the fields `shouldAccept()` and `wasAbleToVerify()`<!-- -->. This promise never rejects.
30
+
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md) &gt; [apiKey](./server-sdk.friendlycaptchaoptions.apikey.md)
4
+
5
+ ## FriendlyCaptchaOptions.apiKey property
6
+
7
+ Friendly Captcha API Key.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ apiKey: string;
13
+ ```
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md) &gt; [fetch](./server-sdk.friendlycaptchaoptions.fetch.md)
4
+
5
+ ## FriendlyCaptchaOptions.fetch property
6
+
7
+ The fetch implementation to use. Defaults to `globalThis.fetch`<!-- -->.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ fetch?: typeof globalThis.fetch;
13
+ ```
@@ -0,0 +1,24 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md)
4
+
5
+ ## FriendlyCaptchaOptions interface
6
+
7
+ Configuration options when creating a new `FriendlyCaptchaClient`<!-- -->.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ export interface FriendlyCaptchaOptions
13
+ ```
14
+
15
+ ## Properties
16
+
17
+ | Property | Modifiers | Type | Description |
18
+ | --- | --- | --- | --- |
19
+ | [apiKey](./server-sdk.friendlycaptchaoptions.apikey.md) | | string | Friendly Captcha API Key. |
20
+ | [fetch?](./server-sdk.friendlycaptchaoptions.fetch.md) | | typeof globalThis.fetch | _(Optional)_ The fetch implementation to use. Defaults to <code>globalThis.fetch</code>. |
21
+ | [sitekey?](./server-sdk.friendlycaptchaoptions.sitekey.md) | | string | _(Optional)_ |
22
+ | [siteverifyEndpoint?](./server-sdk.friendlycaptchaoptions.siteverifyendpoint.md) | | string | <p>_(Optional)_ The endpoint to use for the API. Can be "eu", "global", or a custom URL.</p><p>Defaults to <code>&quot;global&quot;</code>.</p> |
23
+ | [strict?](./server-sdk.friendlycaptchaoptions.strict.md) | | boolean | <p>_(Optional)_ Whether to use strict mode, which rejects all captcha responses which were not strictly verified.</p><p>This is not recommended. Defaults to <code>false</code>.</p> |
24
+
@@ -0,0 +1,11 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md) &gt; [sitekey](./server-sdk.friendlycaptchaoptions.sitekey.md)
4
+
5
+ ## FriendlyCaptchaOptions.sitekey property
6
+
7
+ **Signature:**
8
+
9
+ ```typescript
10
+ sitekey?: string;
11
+ ```
@@ -0,0 +1,15 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [@friendlycaptcha/server-sdk](./server-sdk.md) &gt; [FriendlyCaptchaOptions](./server-sdk.friendlycaptchaoptions.md) &gt; [siteverifyEndpoint](./server-sdk.friendlycaptchaoptions.siteverifyendpoint.md)
4
+
5
+ ## FriendlyCaptchaOptions.siteverifyEndpoint property
6
+
7
+ The endpoint to use for the API. Can be "eu", "global", or a custom URL.
8
+
9
+ Defaults to `"global"`<!-- -->.
10
+
11
+ **Signature:**
12
+
13
+ ```typescript
14
+ siteverifyEndpoint?: string;
15
+ ```