@internetarchive/recaptcha-manager 0.0.1-alpha.2 → 0.0.1-alpha.3

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.
@@ -14,8 +14,8 @@ export declare class RecaptchaManager implements RecaptchaManagerInterface {
14
14
  private lazyLoader;
15
15
  private defaultSiteKey?;
16
16
  private recaptchaCache;
17
- constructor(options: {
18
- defaultSiteKey: string;
17
+ constructor(options?: {
18
+ defaultSiteKey?: string;
19
19
  lazyLoader?: LazyLoaderServiceInterface;
20
20
  grecaptchaLibrary?: ReCaptchaV2.ReCaptcha;
21
21
  });
@@ -4,9 +4,9 @@ export class RecaptchaManager {
4
4
  constructor(options) {
5
5
  var _a;
6
6
  this.recaptchaCache = {};
7
- this.defaultSiteKey = options.defaultSiteKey;
8
- this.lazyLoader = (_a = options.lazyLoader) !== null && _a !== void 0 ? _a : new LazyLoaderService();
9
- this.grecaptchaLibraryCache = options.grecaptchaLibrary;
7
+ this.defaultSiteKey = options === null || options === void 0 ? void 0 : options.defaultSiteKey;
8
+ this.lazyLoader = (_a = options === null || options === void 0 ? void 0 : options.lazyLoader) !== null && _a !== void 0 ? _a : new LazyLoaderService();
9
+ this.grecaptchaLibraryCache = options === null || options === void 0 ? void 0 : options.grecaptchaLibrary;
10
10
  }
11
11
  /** @inheritdoc */
12
12
  async getRecaptchaWidget(options) {
@@ -1 +1 @@
1
- {"version":3,"file":"recaptcha-manager.js","sourceRoot":"","sources":["../../src/recaptcha-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAElB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAYrD,MAAM,OAAO,gBAAgB;IAO3B,YAAY,OAIX;;QANO,mBAAc,GAAoC,EAAE,CAAC;QAO3D,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,CAAC,UAAU,mCAAI,IAAI,iBAAiB,EAAE,CAAC;QAChE,IAAI,CAAC,sBAAsB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAC1D,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,kBAAkB,CAAC,OAGxB;;QACC,MAAM,GAAG,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,IAAI,CAAC,cAAc,CAAC;QACpD,IAAI,CAAC,GAAG,EAAE;YACR,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACzD;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,MAAM;YAAE,OAAO,MAAM,CAAC;QAE1B,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC3D,MAAM,SAAS,GAAG,IAAI,eAAe,CACnC;YACE,OAAO,EAAE,GAAG;YACZ,iBAAiB;SAClB,EACD,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,eAAe,CACzB,CAAC;QAEF,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QACrC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,mBAAmB;QAC/B,IAAI,IAAI,CAAC,sBAAsB,EAAE;YAC/B,OAAO,IAAI,CAAC,sBAAsB,CAAC;SACpC;QACD,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;YAC1B,MAAc,CAAC,wBAAwB,GAAG,GAAS,EAAE;gBACpD,UAAU,CAAC,GAAG,EAAE;oBACd,8CAA8C;oBAC9C,OAAQ,MAAc,CAAC,wBAAwB,CAAC;gBAClD,CAAC,EAAE,EAAE,CAAC,CAAC;gBACP,IAAI,CAAC,sBAAsB,GAAG,MAAM,CAAC,UAAU,CAAC;gBAChD,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC7B,CAAC,CAAC;YAEF,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;gBACzB,GAAG,EAAE,yFAAyF;aAC/F,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CAIF","sourcesContent":["import {\n LazyLoaderService,\n LazyLoaderServiceInterface,\n} from '@internetarchive/lazy-loader-service';\nimport { RecaptchaWidget } from './recaptcha-widget';\n\nexport interface RecaptchaManagerInterface {\n /**\n * Load a recaptcha widget for a given site key or the default site key.\n */\n getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidget>;\n}\n\nexport class RecaptchaManager implements RecaptchaManagerInterface {\n private lazyLoader: LazyLoaderServiceInterface;\n\n private defaultSiteKey?: string;\n\n private recaptchaCache: Record<string, RecaptchaWidget> = {};\n\n constructor(options: {\n defaultSiteKey: string;\n lazyLoader?: LazyLoaderServiceInterface;\n grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded\n }) {\n this.defaultSiteKey = options.defaultSiteKey;\n this.lazyLoader = options.lazyLoader ?? new LazyLoaderService();\n this.grecaptchaLibraryCache = options.grecaptchaLibrary;\n }\n\n /** @inheritdoc */\n async getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidget> {\n const key = options?.siteKey ?? this.defaultSiteKey;\n if (!key) {\n throw new Error('RecaptchaManager requires a site key');\n }\n\n const cached = this.recaptchaCache[key];\n if (cached) return cached;\n\n const grecaptchaLibrary = await this.getRecaptchaLibrary();\n const recaptcha = new RecaptchaWidget(\n {\n siteKey: key,\n grecaptchaLibrary,\n },\n options?.recaptchaParams\n );\n\n this.recaptchaCache[key] = recaptcha;\n return recaptcha;\n }\n\n /**\n * Load the Recaptch library from Google.\n *\n * @returns Promise<ReCaptchaV2.ReCaptcha>\n */\n private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {\n if (this.grecaptchaLibraryCache) {\n return this.grecaptchaLibraryCache;\n }\n return new Promise(resolve => {\n (window as any).grecaptchaLoadedCallback = (): void => {\n setTimeout(() => {\n // remove the callback when we're done with it\n delete (window as any).grecaptchaLoadedCallback;\n }, 10);\n this.grecaptchaLibraryCache = window.grecaptcha;\n resolve(window.grecaptcha);\n };\n\n this.lazyLoader.loadScript({\n src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',\n });\n });\n }\n\n /** don't use directly, use `getRecaptchaLibrary()` */\n private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;\n}\n"]}
1
+ {"version":3,"file":"recaptcha-manager.js","sourceRoot":"","sources":["../../src/recaptcha-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAElB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAYrD,MAAM,OAAO,gBAAgB;IAO3B,YAAY,OAIX;;QANO,mBAAc,GAAoC,EAAE,CAAC;QAO3D,IAAI,CAAC,cAAc,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,CAAC;QAC9C,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,IAAI,iBAAiB,EAAE,CAAC;QACjE,IAAI,CAAC,sBAAsB,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,iBAAiB,CAAC;IAC3D,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,kBAAkB,CAAC,OAGxB;;QACC,MAAM,GAAG,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,IAAI,CAAC,cAAc,CAAC;QACpD,IAAI,CAAC,GAAG,EAAE;YACR,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;SACzD;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,MAAM;YAAE,OAAO,MAAM,CAAC;QAE1B,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC3D,MAAM,SAAS,GAAG,IAAI,eAAe,CACnC;YACE,OAAO,EAAE,GAAG;YACZ,iBAAiB;SAClB,EACD,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,eAAe,CACzB,CAAC;QAEF,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QACrC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,mBAAmB;QAC/B,IAAI,IAAI,CAAC,sBAAsB,EAAE;YAC/B,OAAO,IAAI,CAAC,sBAAsB,CAAC;SACpC;QACD,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;YAC1B,MAAc,CAAC,wBAAwB,GAAG,GAAS,EAAE;gBACpD,UAAU,CAAC,GAAG,EAAE;oBACd,8CAA8C;oBAC9C,OAAQ,MAAc,CAAC,wBAAwB,CAAC;gBAClD,CAAC,EAAE,EAAE,CAAC,CAAC;gBACP,IAAI,CAAC,sBAAsB,GAAG,MAAM,CAAC,UAAU,CAAC;gBAChD,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC7B,CAAC,CAAC;YAEF,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;gBACzB,GAAG,EAAE,yFAAyF;aAC/F,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CAIF","sourcesContent":["import {\n LazyLoaderService,\n LazyLoaderServiceInterface,\n} from '@internetarchive/lazy-loader-service';\nimport { RecaptchaWidget } from './recaptcha-widget';\n\nexport interface RecaptchaManagerInterface {\n /**\n * Load a recaptcha widget for a given site key or the default site key.\n */\n getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidget>;\n}\n\nexport class RecaptchaManager implements RecaptchaManagerInterface {\n private lazyLoader: LazyLoaderServiceInterface;\n\n private defaultSiteKey?: string;\n\n private recaptchaCache: Record<string, RecaptchaWidget> = {};\n\n constructor(options?: {\n defaultSiteKey?: string;\n lazyLoader?: LazyLoaderServiceInterface;\n grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded\n }) {\n this.defaultSiteKey = options?.defaultSiteKey;\n this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();\n this.grecaptchaLibraryCache = options?.grecaptchaLibrary;\n }\n\n /** @inheritdoc */\n async getRecaptchaWidget(options?: {\n siteKey?: string;\n recaptchaParams?: ReCaptchaV2.Parameters;\n }): Promise<RecaptchaWidget> {\n const key = options?.siteKey ?? this.defaultSiteKey;\n if (!key) {\n throw new Error('RecaptchaManager requires a site key');\n }\n\n const cached = this.recaptchaCache[key];\n if (cached) return cached;\n\n const grecaptchaLibrary = await this.getRecaptchaLibrary();\n const recaptcha = new RecaptchaWidget(\n {\n siteKey: key,\n grecaptchaLibrary,\n },\n options?.recaptchaParams\n );\n\n this.recaptchaCache[key] = recaptcha;\n return recaptcha;\n }\n\n /**\n * Load the Recaptch library from Google.\n *\n * @returns Promise<ReCaptchaV2.ReCaptcha>\n */\n private async getRecaptchaLibrary(): Promise<ReCaptchaV2.ReCaptcha> {\n if (this.grecaptchaLibraryCache) {\n return this.grecaptchaLibraryCache;\n }\n return new Promise(resolve => {\n (window as any).grecaptchaLoadedCallback = (): void => {\n setTimeout(() => {\n // remove the callback when we're done with it\n delete (window as any).grecaptchaLoadedCallback;\n }, 10);\n this.grecaptchaLibraryCache = window.grecaptcha;\n resolve(window.grecaptcha);\n };\n\n this.lazyLoader.loadScript({\n src: 'https://www.google.com/recaptcha/api.js?onload=grecaptchaLoadedCallback&render=explicit',\n });\n });\n }\n\n /** don't use directly, use `getRecaptchaLibrary()` */\n private grecaptchaLibraryCache?: ReCaptchaV2.ReCaptcha;\n}\n"]}
File without changes
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ // /* eslint-disable camelcase */
3
+ // /* eslint-disable @typescript-eslint/no-explicit-any */
4
+ // /* eslint-disable @typescript-eslint/no-unused-vars */
5
+ // export enum MockGrecaptchaMode {
6
+ // Success,
7
+ // Expired,
8
+ // Error,
9
+ // }
10
+ // export class MockGrecaptcha implements ReCaptchaV2.ReCaptcha {
11
+ // renderCalled = false;
12
+ // executeCalled = false;
13
+ // resetCalled = false;
14
+ // getResponseCalled = false;
15
+ // mode: MockGrecaptchaMode;
16
+ // addDelay: boolean;
17
+ // callback?: (response: string) => void;
18
+ // 'expired-callback'?: () => void;
19
+ // 'error-callback'?: () => void;
20
+ // render(
21
+ // container: string | HTMLElement,
22
+ // parameters?: ReCaptchaV2.Parameters | undefined,
23
+ // inherit?: boolean | undefined,
24
+ // ): number {
25
+ // this.callback = parameters?.callback?.bind(this);
26
+ // this['error-callback'] = parameters?.['error-callback']?.bind(this);
27
+ // this['expired-callback'] = parameters?.['expired-callback']?.bind(this);
28
+ // this.renderCalled = true;
29
+ // return 1;
30
+ // }
31
+ // reset(opt_widget_id?: number | undefined): void {
32
+ // this.resetCalled = true;
33
+ // }
34
+ // getResponse(opt_widget_id?: number | undefined): string {
35
+ // this.getResponseCalled = true;
36
+ // return 'foo';
37
+ // }
38
+ // // execute(opt_widget_id?: number): void {
39
+ // // this.executeCalled = true;
40
+ // // if (this.addDelay) {
41
+ // // setTimeout(this.callCallback, 100);
42
+ // // } else {
43
+ // // this.callCallback();
44
+ // // }
45
+ // // }
46
+ // // /**
47
+ // // * Programatically invoke the reCAPTCHA check. Used if the invisible reCAPTCHA is on a div instead of a button.
48
+ // // * @param siteKey the key of your site
49
+ // // * @param action the action
50
+ // // *
51
+ // // * @return a promise-like object containing the token
52
+ // // */
53
+ // // async execute(siteKey: string, action: ReCaptchaV2.Action): Promise<string> {
54
+ // // return '';
55
+ // // }
56
+ // // execute(opt_widget_id?: number): void {
57
+ // // this.executeCalled = true;
58
+ // // if (this.addDelay) {
59
+ // // setTimeout(this.callCallback, 100);
60
+ // // } else {
61
+ // // this.callCallback();
62
+ // // }
63
+ // // }
64
+ // // execute(opt_widget_id?: number): void {
65
+ // // }
66
+ // // async execute(siteKey: string, action: ReCaptchaV2.Action): PromiseLike<string> {
67
+ // // return '';
68
+ // // }
69
+ // // async execute(siteKey?: any, action?: any): void | PromiseLike<string> {
70
+ // // }
71
+ // ready(callback: () => void): void {
72
+ // }
73
+ // private callCallback(): void {
74
+ // switch (this.mode) {
75
+ // case MockGrecaptchaMode.Success:
76
+ // if (this.callback) {
77
+ // this.callback('foo');
78
+ // }
79
+ // break;
80
+ // case MockGrecaptchaMode.Error:
81
+ // if (this['error-callback']) {
82
+ // this['error-callback']();
83
+ // }
84
+ // break;
85
+ // case MockGrecaptchaMode.Expired:
86
+ // if (this['expired-callback']) {
87
+ // this['expired-callback']();
88
+ // }
89
+ // break;
90
+ // default:
91
+ // break;
92
+ // }
93
+ // }
94
+ // constructor(mode: MockGrecaptchaMode, addDelay = false) {
95
+ // this.mode = mode;
96
+ // this.addDelay = addDelay;
97
+ // }
98
+ // }
99
+ //# sourceMappingURL=mock-grecaptcha.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock-grecaptcha.js","sourceRoot":"","sources":["../../test/mock-grecaptcha.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC,0DAA0D;AAC1D,yDAAyD;AAEzD,mCAAmC;AACnC,aAAa;AACb,aAAa;AACb,WAAW;AACX,IAAI;AAEJ,iEAAiE;AACjE,0BAA0B;AAE1B,2BAA2B;AAE3B,yBAAyB;AAEzB,+BAA+B;AAE/B,8BAA8B;AAE9B,uBAAuB;AAEvB,2CAA2C;AAE3C,qCAAqC;AAErC,mCAAmC;AAEnC,YAAY;AACZ,uCAAuC;AACvC,uDAAuD;AACvD,qCAAqC;AACrC,gBAAgB;AAChB,wDAAwD;AACxD,2EAA2E;AAC3E,+EAA+E;AAC/E,gCAAgC;AAChC,gBAAgB;AAChB,MAAM;AAEN,sDAAsD;AACtD,+BAA+B;AAC/B,MAAM;AAEN,8DAA8D;AAC9D,qCAAqC;AACrC,oBAAoB;AACpB,MAAM;AAEN,+CAA+C;AAC/C,oCAAoC;AAEpC,8BAA8B;AAC9B,+CAA+C;AAC/C,kBAAkB;AAClB,gCAAgC;AAChC,WAAW;AACX,SAAS;AACT,WAAW;AACX,uHAAuH;AACvH,8CAA8C;AAC9C,mCAAmC;AACnC,UAAU;AACV,6DAA6D;AAC7D,WAAW;AAEX,qFAAqF;AACrF,oBAAoB;AACpB,SAAS;AAET,+CAA+C;AAC/C,oCAAoC;AAEpC,8BAA8B;AAC9B,+CAA+C;AAC/C,kBAAkB;AAClB,gCAAgC;AAChC,WAAW;AACX,SAAS;AAET,+CAA+C;AAE/C,SAAS;AAET,yFAAyF;AACzF,oBAAoB;AACpB,SAAS;AAET,gFAAgF;AAEhF,SAAS;AAET,wCAAwC;AAExC,MAAM;AAEN,mCAAmC;AACnC,2BAA2B;AAC3B,yCAAyC;AACzC,+BAA+B;AAC/B,kCAAkC;AAClC,YAAY;AACZ,iBAAiB;AACjB,uCAAuC;AACvC,wCAAwC;AACxC,sCAAsC;AACtC,YAAY;AACZ,iBAAiB;AACjB,yCAAyC;AACzC,0CAA0C;AAC1C,wCAAwC;AACxC,YAAY;AACZ,iBAAiB;AACjB,iBAAiB;AACjB,iBAAiB;AACjB,QAAQ;AACR,MAAM;AAEN,8DAA8D;AAC9D,wBAAwB;AACxB,gCAAgC;AAChC,MAAM;AACN,IAAI","sourcesContent":["// /* eslint-disable camelcase */\n// /* eslint-disable @typescript-eslint/no-explicit-any */\n// /* eslint-disable @typescript-eslint/no-unused-vars */\n\n// export enum MockGrecaptchaMode {\n// Success,\n// Expired,\n// Error,\n// }\n\n// export class MockGrecaptcha implements ReCaptchaV2.ReCaptcha {\n// renderCalled = false;\n\n// executeCalled = false;\n\n// resetCalled = false;\n\n// getResponseCalled = false;\n\n// mode: MockGrecaptchaMode;\n\n// addDelay: boolean;\n\n// callback?: (response: string) => void;\n\n// 'expired-callback'?: () => void;\n\n// 'error-callback'?: () => void;\n\n// render(\n// container: string | HTMLElement,\n// parameters?: ReCaptchaV2.Parameters | undefined,\n// inherit?: boolean | undefined,\n// ): number {\n// this.callback = parameters?.callback?.bind(this);\n// this['error-callback'] = parameters?.['error-callback']?.bind(this);\n// this['expired-callback'] = parameters?.['expired-callback']?.bind(this);\n// this.renderCalled = true;\n// return 1;\n// }\n\n// reset(opt_widget_id?: number | undefined): void {\n// this.resetCalled = true;\n// }\n\n// getResponse(opt_widget_id?: number | undefined): string {\n// this.getResponseCalled = true;\n// return 'foo';\n// }\n\n// // execute(opt_widget_id?: number): void {\n// // this.executeCalled = true;\n\n// // if (this.addDelay) {\n// // setTimeout(this.callCallback, 100);\n// // } else {\n// // this.callCallback();\n// // }\n// // }\n// // /**\n// // * Programatically invoke the reCAPTCHA check. Used if the invisible reCAPTCHA is on a div instead of a button.\n// // * @param siteKey the key of your site\n// // * @param action the action\n// // *\n// // * @return a promise-like object containing the token\n// // */\n\n// // async execute(siteKey: string, action: ReCaptchaV2.Action): Promise<string> {\n// // return '';\n// // }\n\n// // execute(opt_widget_id?: number): void {\n// // this.executeCalled = true;\n\n// // if (this.addDelay) {\n// // setTimeout(this.callCallback, 100);\n// // } else {\n// // this.callCallback();\n// // }\n// // }\n\n// // execute(opt_widget_id?: number): void {\n\n// // }\n\n// // async execute(siteKey: string, action: ReCaptchaV2.Action): PromiseLike<string> {\n// // return '';\n// // }\n\n// // async execute(siteKey?: any, action?: any): void | PromiseLike<string> {\n\n// // }\n\n// ready(callback: () => void): void {\n\n// }\n\n// private callCallback(): void {\n// switch (this.mode) {\n// case MockGrecaptchaMode.Success:\n// if (this.callback) {\n// this.callback('foo');\n// }\n// break;\n// case MockGrecaptchaMode.Error:\n// if (this['error-callback']) {\n// this['error-callback']();\n// }\n// break;\n// case MockGrecaptchaMode.Expired:\n// if (this['expired-callback']) {\n// this['expired-callback']();\n// }\n// break;\n// default:\n// break;\n// }\n// }\n\n// constructor(mode: MockGrecaptchaMode, addDelay = false) {\n// this.mode = mode;\n// this.addDelay = addDelay;\n// }\n// }\n"]}
@@ -1,3 +1,69 @@
1
1
  "use strict";
2
- describe('YourWebComponent', () => { });
2
+ // import { html, fixture, expect } from '@open-wc/testing';
3
+ // import { MockGrecaptcha, MockGrecaptchaMode } from './mock-grecaptcha';
4
+ // import { RecaptchaManager } from '../src/recaptcha-manager';
5
+ describe('ReCaptcha Manager', () => {
6
+ // it('can execute the recaptcha like a Promise', async () => {
7
+ // const recaptchaElement = (await fixture(html`
8
+ // <div></div>
9
+ // `)) as HTMLElement;
10
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success);
11
+ // const recaptchaManager = new RecaptchaManager({
12
+ // grecaptchaLibrary: mockGrecaptcha,
13
+ // siteKey: '123',
14
+ // });
15
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
16
+ // const result = await recaptchaManager.execute();
17
+ // expect(result).to.equal('foo');
18
+ // });
19
+ // it('can error properly like a Promise', async () => {
20
+ // const recaptchaElement = (await fixture(html`
21
+ // <div></div>
22
+ // `)) as HTMLElement;
23
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Error);
24
+ // const recaptchaManager = new RecaptchaManager({
25
+ // grecaptchaLibrary: mockGrecaptcha,
26
+ // siteKey: '123',
27
+ // });
28
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
29
+ // try {
30
+ // await recaptchaManager.execute();
31
+ // expect.fail('should not get here');
32
+ // } catch (error) {
33
+ // expect(error).to.equal('error');
34
+ // }
35
+ // });
36
+ // it('can expire properly like a Promise', async () => {
37
+ // const recaptchaElement = (await fixture(html`
38
+ // <div></div>
39
+ // `)) as HTMLElement;
40
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Expired);
41
+ // const recaptchaManager = new RecaptchaManager({
42
+ // grecaptchaLibrary: mockGrecaptcha,
43
+ // siteKey: '123',
44
+ // });
45
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
46
+ // try {
47
+ // await recaptchaManager.execute();
48
+ // expect.fail('should not get here');
49
+ // } catch (error) {
50
+ // expect(error).to.equal('expired');
51
+ // }
52
+ // });
53
+ // it('resets the captcha if execute() gets called more than once', async () => {
54
+ // const recaptchaElement = (await fixture(html`
55
+ // <div></div>
56
+ // `)) as HTMLElement;
57
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success, true);
58
+ // const recaptchaManager = new RecaptchaManager({
59
+ // grecaptchaLibrary: mockGrecaptcha,
60
+ // siteKey: '123',
61
+ // });
62
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
63
+ // recaptchaManager.execute();
64
+ // expect(mockGrecaptcha.resetCalled).to.be.false;
65
+ // recaptchaManager.execute();
66
+ // expect(mockGrecaptcha.resetCalled).to.be.true;
67
+ // });
68
+ });
3
69
  //# sourceMappingURL=recaptcha-manager.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"recaptcha-manager.test.js","sourceRoot":"","sources":["../../test/recaptcha-manager.test.ts"],"names":[],"mappings":";AAAA,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC","sourcesContent":["describe('YourWebComponent', () => {});\n"]}
1
+ {"version":3,"file":"recaptcha-manager.test.js","sourceRoot":"","sources":["../../test/recaptcha-manager.test.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,0EAA0E;AAC1E,+DAA+D;AAE/D,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,+DAA+D;IAC/D,kDAAkD;IAClD,kBAAkB;IAClB,wBAAwB;IACxB,2EAA2E;IAC3E,oDAAoD;IACpD,yCAAyC;IACzC,sBAAsB;IACtB,QAAQ;IACR,kEAAkE;IAClE,qDAAqD;IACrD,oCAAoC;IACpC,MAAM;IAEN,wDAAwD;IACxD,kDAAkD;IAClD,kBAAkB;IAClB,wBAAwB;IACxB,yEAAyE;IACzE,oDAAoD;IACpD,yCAAyC;IACzC,sBAAsB;IACtB,QAAQ;IACR,kEAAkE;IAClE,UAAU;IACV,wCAAwC;IACxC,0CAA0C;IAC1C,sBAAsB;IACtB,uCAAuC;IACvC,MAAM;IACN,MAAM;IAEN,yDAAyD;IACzD,kDAAkD;IAClD,kBAAkB;IAClB,wBAAwB;IACxB,2EAA2E;IAC3E,oDAAoD;IACpD,yCAAyC;IACzC,sBAAsB;IACtB,QAAQ;IACR,kEAAkE;IAClE,UAAU;IACV,wCAAwC;IACxC,0CAA0C;IAC1C,sBAAsB;IACtB,yCAAyC;IACzC,MAAM;IACN,MAAM;IAEN,iFAAiF;IACjF,kDAAkD;IAClD,kBAAkB;IAClB,wBAAwB;IACxB,iFAAiF;IACjF,oDAAoD;IACpD,yCAAyC;IACzC,sBAAsB;IACtB,QAAQ;IACR,kEAAkE;IAClE,gCAAgC;IAChC,oDAAoD;IACpD,gCAAgC;IAChC,mDAAmD;IACnD,MAAM;AACR,CAAC,CAAC,CAAC","sourcesContent":["// import { html, fixture, expect } from '@open-wc/testing';\n// import { MockGrecaptcha, MockGrecaptchaMode } from './mock-grecaptcha';\n// import { RecaptchaManager } from '../src/recaptcha-manager';\n\ndescribe('ReCaptcha Manager', () => {\n // it('can execute the recaptcha like a Promise', async () => {\n // const recaptchaElement = (await fixture(html`\n // <div></div>\n // `)) as HTMLElement;\n // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success);\n // const recaptchaManager = new RecaptchaManager({\n // grecaptchaLibrary: mockGrecaptcha,\n // siteKey: '123',\n // });\n // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');\n // const result = await recaptchaManager.execute();\n // expect(result).to.equal('foo');\n // });\n\n // it('can error properly like a Promise', async () => {\n // const recaptchaElement = (await fixture(html`\n // <div></div>\n // `)) as HTMLElement;\n // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Error);\n // const recaptchaManager = new RecaptchaManager({\n // grecaptchaLibrary: mockGrecaptcha,\n // siteKey: '123',\n // });\n // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');\n // try {\n // await recaptchaManager.execute();\n // expect.fail('should not get here');\n // } catch (error) {\n // expect(error).to.equal('error');\n // }\n // });\n\n // it('can expire properly like a Promise', async () => {\n // const recaptchaElement = (await fixture(html`\n // <div></div>\n // `)) as HTMLElement;\n // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Expired);\n // const recaptchaManager = new RecaptchaManager({\n // grecaptchaLibrary: mockGrecaptcha,\n // siteKey: '123',\n // });\n // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');\n // try {\n // await recaptchaManager.execute();\n // expect.fail('should not get here');\n // } catch (error) {\n // expect(error).to.equal('expired');\n // }\n // });\n\n // it('resets the captcha if execute() gets called more than once', async () => {\n // const recaptchaElement = (await fixture(html`\n // <div></div>\n // `)) as HTMLElement;\n // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success, true);\n // const recaptchaManager = new RecaptchaManager({\n // grecaptchaLibrary: mockGrecaptcha,\n // siteKey: '123',\n // });\n // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');\n // recaptchaManager.execute();\n // expect(mockGrecaptcha.resetCalled).to.be.false;\n // recaptchaManager.execute();\n // expect(mockGrecaptcha.resetCalled).to.be.true;\n // });\n});\n"]}
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "license": "AGPL-3.0-only",
9
9
  "author": "Internet Archive",
10
- "version": "0.0.1-alpha.2",
10
+ "version": "0.0.1-alpha.3",
11
11
  "main": "dist/index.js",
12
12
  "module": "dist/index.js",
13
13
  "scripts": {
@@ -21,14 +21,14 @@ export class RecaptchaManager implements RecaptchaManagerInterface {
21
21
 
22
22
  private recaptchaCache: Record<string, RecaptchaWidget> = {};
23
23
 
24
- constructor(options: {
25
- defaultSiteKey: string;
24
+ constructor(options?: {
25
+ defaultSiteKey?: string;
26
26
  lazyLoader?: LazyLoaderServiceInterface;
27
27
  grecaptchaLibrary?: ReCaptchaV2.ReCaptcha; // allows dependency injection or will be lazy loaded
28
28
  }) {
29
- this.defaultSiteKey = options.defaultSiteKey;
30
- this.lazyLoader = options.lazyLoader ?? new LazyLoaderService();
31
- this.grecaptchaLibraryCache = options.grecaptchaLibrary;
29
+ this.defaultSiteKey = options?.defaultSiteKey;
30
+ this.lazyLoader = options?.lazyLoader ?? new LazyLoaderService();
31
+ this.grecaptchaLibraryCache = options?.grecaptchaLibrary;
32
32
  }
33
33
 
34
34
  /** @inheritdoc */
@@ -0,0 +1,124 @@
1
+ // /* eslint-disable camelcase */
2
+ // /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ // /* eslint-disable @typescript-eslint/no-unused-vars */
4
+
5
+ // export enum MockGrecaptchaMode {
6
+ // Success,
7
+ // Expired,
8
+ // Error,
9
+ // }
10
+
11
+ // export class MockGrecaptcha implements ReCaptchaV2.ReCaptcha {
12
+ // renderCalled = false;
13
+
14
+ // executeCalled = false;
15
+
16
+ // resetCalled = false;
17
+
18
+ // getResponseCalled = false;
19
+
20
+ // mode: MockGrecaptchaMode;
21
+
22
+ // addDelay: boolean;
23
+
24
+ // callback?: (response: string) => void;
25
+
26
+ // 'expired-callback'?: () => void;
27
+
28
+ // 'error-callback'?: () => void;
29
+
30
+ // render(
31
+ // container: string | HTMLElement,
32
+ // parameters?: ReCaptchaV2.Parameters | undefined,
33
+ // inherit?: boolean | undefined,
34
+ // ): number {
35
+ // this.callback = parameters?.callback?.bind(this);
36
+ // this['error-callback'] = parameters?.['error-callback']?.bind(this);
37
+ // this['expired-callback'] = parameters?.['expired-callback']?.bind(this);
38
+ // this.renderCalled = true;
39
+ // return 1;
40
+ // }
41
+
42
+ // reset(opt_widget_id?: number | undefined): void {
43
+ // this.resetCalled = true;
44
+ // }
45
+
46
+ // getResponse(opt_widget_id?: number | undefined): string {
47
+ // this.getResponseCalled = true;
48
+ // return 'foo';
49
+ // }
50
+
51
+ // // execute(opt_widget_id?: number): void {
52
+ // // this.executeCalled = true;
53
+
54
+ // // if (this.addDelay) {
55
+ // // setTimeout(this.callCallback, 100);
56
+ // // } else {
57
+ // // this.callCallback();
58
+ // // }
59
+ // // }
60
+ // // /**
61
+ // // * Programatically invoke the reCAPTCHA check. Used if the invisible reCAPTCHA is on a div instead of a button.
62
+ // // * @param siteKey the key of your site
63
+ // // * @param action the action
64
+ // // *
65
+ // // * @return a promise-like object containing the token
66
+ // // */
67
+
68
+ // // async execute(siteKey: string, action: ReCaptchaV2.Action): Promise<string> {
69
+ // // return '';
70
+ // // }
71
+
72
+ // // execute(opt_widget_id?: number): void {
73
+ // // this.executeCalled = true;
74
+
75
+ // // if (this.addDelay) {
76
+ // // setTimeout(this.callCallback, 100);
77
+ // // } else {
78
+ // // this.callCallback();
79
+ // // }
80
+ // // }
81
+
82
+ // // execute(opt_widget_id?: number): void {
83
+
84
+ // // }
85
+
86
+ // // async execute(siteKey: string, action: ReCaptchaV2.Action): PromiseLike<string> {
87
+ // // return '';
88
+ // // }
89
+
90
+ // // async execute(siteKey?: any, action?: any): void | PromiseLike<string> {
91
+
92
+ // // }
93
+
94
+ // ready(callback: () => void): void {
95
+
96
+ // }
97
+
98
+ // private callCallback(): void {
99
+ // switch (this.mode) {
100
+ // case MockGrecaptchaMode.Success:
101
+ // if (this.callback) {
102
+ // this.callback('foo');
103
+ // }
104
+ // break;
105
+ // case MockGrecaptchaMode.Error:
106
+ // if (this['error-callback']) {
107
+ // this['error-callback']();
108
+ // }
109
+ // break;
110
+ // case MockGrecaptchaMode.Expired:
111
+ // if (this['expired-callback']) {
112
+ // this['expired-callback']();
113
+ // }
114
+ // break;
115
+ // default:
116
+ // break;
117
+ // }
118
+ // }
119
+
120
+ // constructor(mode: MockGrecaptchaMode, addDelay = false) {
121
+ // this.mode = mode;
122
+ // this.addDelay = addDelay;
123
+ // }
124
+ // }
@@ -1 +1,71 @@
1
- describe('YourWebComponent', () => {});
1
+ // import { html, fixture, expect } from '@open-wc/testing';
2
+ // import { MockGrecaptcha, MockGrecaptchaMode } from './mock-grecaptcha';
3
+ // import { RecaptchaManager } from '../src/recaptcha-manager';
4
+
5
+ describe('ReCaptcha Manager', () => {
6
+ // it('can execute the recaptcha like a Promise', async () => {
7
+ // const recaptchaElement = (await fixture(html`
8
+ // <div></div>
9
+ // `)) as HTMLElement;
10
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success);
11
+ // const recaptchaManager = new RecaptchaManager({
12
+ // grecaptchaLibrary: mockGrecaptcha,
13
+ // siteKey: '123',
14
+ // });
15
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
16
+ // const result = await recaptchaManager.execute();
17
+ // expect(result).to.equal('foo');
18
+ // });
19
+
20
+ // it('can error properly like a Promise', async () => {
21
+ // const recaptchaElement = (await fixture(html`
22
+ // <div></div>
23
+ // `)) as HTMLElement;
24
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Error);
25
+ // const recaptchaManager = new RecaptchaManager({
26
+ // grecaptchaLibrary: mockGrecaptcha,
27
+ // siteKey: '123',
28
+ // });
29
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
30
+ // try {
31
+ // await recaptchaManager.execute();
32
+ // expect.fail('should not get here');
33
+ // } catch (error) {
34
+ // expect(error).to.equal('error');
35
+ // }
36
+ // });
37
+
38
+ // it('can expire properly like a Promise', async () => {
39
+ // const recaptchaElement = (await fixture(html`
40
+ // <div></div>
41
+ // `)) as HTMLElement;
42
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Expired);
43
+ // const recaptchaManager = new RecaptchaManager({
44
+ // grecaptchaLibrary: mockGrecaptcha,
45
+ // siteKey: '123',
46
+ // });
47
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
48
+ // try {
49
+ // await recaptchaManager.execute();
50
+ // expect.fail('should not get here');
51
+ // } catch (error) {
52
+ // expect(error).to.equal('expired');
53
+ // }
54
+ // });
55
+
56
+ // it('resets the captcha if execute() gets called more than once', async () => {
57
+ // const recaptchaElement = (await fixture(html`
58
+ // <div></div>
59
+ // `)) as HTMLElement;
60
+ // const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success, true);
61
+ // const recaptchaManager = new RecaptchaManager({
62
+ // grecaptchaLibrary: mockGrecaptcha,
63
+ // siteKey: '123',
64
+ // });
65
+ // recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
66
+ // recaptchaManager.execute();
67
+ // expect(mockGrecaptcha.resetCalled).to.be.false;
68
+ // recaptchaManager.execute();
69
+ // expect(mockGrecaptcha.resetCalled).to.be.true;
70
+ // });
71
+ });