Skip to content

Commit

Permalink
refactor: replace HcaptchaError enum with string union type
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Oct 26, 2021
1 parent 7576305 commit c5639ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
25 changes: 12 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,29 @@ export interface HcaptchaResponse {
* @see {@link HcaptchaResponse}
* @see {@link https://docs.hcaptcha.com/#siteverify-error-codes-table}
*/
export enum HcaptchaError {
export type HcaptchaError =
/** Secret key is missing */
MissingInputSecret = "missing-input-secret",
| "missing-input-secret"
/** Secret key is invalid */
InvalidInputSecret = "invalid-input-secret",
| "invalid-input-secret"
/** User response token is missing */
MissingInputResponse = "missing-input-response",
| "missing-input-response"
/** User response token is invalid */
InvalidInputResponse = "invalid-input-response",
| "invalid-input-response"
/** Site key is invalid */
InvalidSiteKey = "invalid-sitekey",
| "invalid-sitekey"
/** Remote user IP is invalid */
InvalidRemoteIp = "invalid-remoteip",
| "invalid-remoteip"
/** Request is invalid */
BadRequest = "bad-request",
| "bad-request"
/** User response token is invalid or has already been checked */
InvalidOrAlreadySeenResponse = "invalid-or-already-seen-response",
| "invalid-or-already-seen-response"
/** Must use the test site key when using a test verification token */
NotUsingDummyPassCode = "not-using-dummy-passcode",
| "not-using-dummy-passcode"
/** Must use the test secret key when using a test verification token */
NotUsingDummySecret = "not-using-dummy-secret",
| "not-using-dummy-secret"
/** The site key is not associated to the secret key */
SiteKeySecretMismatch = "sitekey-secret-mismatch",
}
| "sitekey-secret-mismatch";

/**
* `RawHcaptchaResponse` represents the raw response to the verification challenge
Expand Down
16 changes: 8 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import FSPersister from "@pollyjs/persister-fs";
import nock from "nock";
import * as path from "path";
import { setupPolly } from "setup-polly-jest";
import { HcaptchaError, verifyHcaptchaToken } from "../src";
import { verifyHcaptchaToken } from "../src";

// See https://github.com/gribnoysup/setup-polly-jest/issues/23#issuecomment-890494186
// Polly.register(NodeHttpAdapter);
Expand Down Expand Up @@ -43,8 +43,8 @@ describe("verifyHcaptchaToken", () => {

expect(success).toEqual(false);
expect(errorCodes).toEqual([
HcaptchaError.MissingInputResponse,
HcaptchaError.MissingInputSecret,
"missing-input-response",
"missing-input-secret",
]);

expect(res).toMatchSnapshot();
Expand All @@ -59,7 +59,7 @@ describe("verifyHcaptchaToken", () => {
const { success, errorCodes } = res;

expect(success).toEqual(false);
expect(errorCodes).toEqual([HcaptchaError.MissingInputResponse]);
expect(errorCodes).toEqual(["missing-input-response"]);

expect(res).toMatchSnapshot();
});
Expand All @@ -74,7 +74,7 @@ describe("verifyHcaptchaToken", () => {
const { success, errorCodes } = res;

expect(success).toEqual(false);
expect(errorCodes).toEqual([HcaptchaError.InvalidInputResponse]);
expect(errorCodes).toEqual(["invalid-input-response"]);

expect(res).toMatchSnapshot();
});
Expand All @@ -92,7 +92,7 @@ describe("verifyHcaptchaToken", () => {

// NOTE: this error is returned by the API
// but is not documented in the hCaptcha developer docs
expect(errorCodes).toEqual([HcaptchaError.NotUsingDummySecret]);
expect(errorCodes).toEqual(["not-using-dummy-secret"]);

expect(res).toMatchSnapshot({
challengeTimestamp: expect.any(String),
Expand All @@ -113,7 +113,7 @@ describe("verifyHcaptchaToken", () => {

// NOTE: this error is returned by the API
// but is not documented in the hCaptcha developer docs
expect(errorCodes).toEqual([HcaptchaError.InvalidSiteKey]);
expect(errorCodes).toEqual(["invalid-sitekey"]);

expect(res).toMatchSnapshot();
});
Expand All @@ -133,7 +133,7 @@ describe("verifyHcaptchaToken", () => {

// NOTE: this error is returned by the API
// but is not documented in the hCaptcha developer docs
expect(errorCodes).toEqual([HcaptchaError.InvalidRemoteIp]);
expect(errorCodes).toEqual(["invalid-remoteip"]);

expect(res).toMatchSnapshot();
});
Expand Down

0 comments on commit c5639ab

Please sign in to comment.