Skip to content

Commit

Permalink
feat: update siteKey dynamically (#15)
Browse files Browse the repository at this point in the history
* chore: install & init

* chore: fix typo

* feat: add feature add siteKey dynamical

* feat: dynamic siteKey init example

* docs: update dynamic sitekey docs
  • Loading branch information
Dongkyuuuu authored Nov 5, 2024
1 parent 1031afa commit 1a2e3a4
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"recommendations": ["arcanis.vscode-zipfs"]
"recommendations": [
"arcanis.vscode-zipfs"
]
}
16 changes: 14 additions & 2 deletions .yarn/sdks/typescript/bin/tsc
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/bin/tsc
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/bin/tsc your application uses
module.exports = absRequire(`typescript/bin/tsc`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsc`));
16 changes: 14 additions & 2 deletions .yarn/sdks/typescript/bin/tsserver
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/bin/tsserver
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/bin/tsserver your application uses
module.exports = absRequire(`typescript/bin/tsserver`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsserver`));
16 changes: 14 additions & 2 deletions .yarn/sdks/typescript/lib/tsc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsc.js
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/lib/tsc.js your application uses
module.exports = absRequire(`typescript/lib/tsc.js`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/lib/tsc.js`));
37 changes: 30 additions & 7 deletions .yarn/sdks/typescript/lib/tsserver.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const moduleWrapper = tsserver => {
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsserver.js
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

const moduleWrapper = exports => {
return wrapWithUserWrapper(moduleWrapperFn(exports));
};

const moduleWrapperFn = tsserver => {
if (!process.versions.pnp) {
return tsserver;
}
Expand Down Expand Up @@ -214,11 +237,11 @@ const moduleWrapper = tsserver => {
return tsserver;
};

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsserver.js
require(absPnpApiPath).setup();
}
const [major, minor] = absRequire(`typescript/package.json`).version.split(`.`, 2).map(value => parseInt(value, 10));
// In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well.
// Ref https://github.com/microsoft/TypeScript/pull/55326
if (major > 5 || (major === 5 && minor >= 5)) {
moduleWrapper(absRequire(`typescript`));
}

// Defer to the real typescript/lib/tsserver.js your application uses
Expand Down
37 changes: 30 additions & 7 deletions .yarn/sdks/typescript/lib/tsserverlibrary.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const moduleWrapper = tsserver => {
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsserverlibrary.js
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

const moduleWrapper = exports => {
return wrapWithUserWrapper(moduleWrapperFn(exports));
};

const moduleWrapperFn = tsserver => {
if (!process.versions.pnp) {
return tsserver;
}
Expand Down Expand Up @@ -214,11 +237,11 @@ const moduleWrapper = tsserver => {
return tsserver;
};

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/tsserverlibrary.js
require(absPnpApiPath).setup();
}
const [major, minor] = absRequire(`typescript/package.json`).version.split(`.`, 2).map(value => parseInt(value, 10));
// In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well.
// Ref https://github.com/microsoft/TypeScript/pull/55326
if (major > 5 || (major === 5 && minor >= 5)) {
moduleWrapper(absRequire(`typescript`));
}

// Defer to the real typescript/lib/tsserverlibrary.js your application uses
Expand Down
20 changes: 16 additions & 4 deletions .yarn/sdks/typescript/lib/typescript.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire} = require(`module`);
const {createRequire, register} = require(`module`);
const {resolve} = require(`path`);
const {pathToFileURL} = require(`url`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
const isPnpLoaderEnabled = existsSync(absPnpLoaderPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/typescript.js
// Setup the environment to be able to require typescript
require(absPnpApiPath).setup();
if (isPnpLoaderEnabled && register) {
register(pathToFileURL(absPnpLoaderPath));
}
}
}

// Defer to the real typescript/lib/typescript.js your application uses
module.exports = absRequire(`typescript/lib/typescript.js`);
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript your application uses
module.exports = wrapWithUserWrapper(absRequire(`typescript`));
6 changes: 5 additions & 1 deletion .yarn/sdks/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"name": "typescript",
"version": "5.2.2-sdk",
"main": "./lib/typescript.js",
"type": "commonjs"
"type": "commonjs",
"bin": {
"tsc": "./bin/tsc",
"tsserver": "./bin/tsserver"
}
}
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { install } from "vue3-recaptcha-v2";

createApp(App)
.use(install, {
sitekey: "YOUT_SITE_KEY",
sitekey: "YOUR_SITE_KEY",
cnDomains: false, // Optional, If you use in China, set this value true
})
.mount("#app");
Expand All @@ -40,12 +40,39 @@ import { install } from "vue3-recaptcha-v2";

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(install, {
sitekey: "YOUT_SITE_KEY",
sitekey: "YOUR_SITE_KEY",
cnDomains: false,
});
});
```

### Dynamic SiteKey

If you want to add siteKey dynamically, follow this step.

**IMPORTANT::: The reCAPTCHA widget is rendered once, so changes to the siteKey after rendering will not be reflected.**

```typescript
import { createApp } from "vue";
import App from "./App.vue";

import { install } from "vue3-recaptcha-v2";

createApp(App)
.use(install) // Do not add siteKey when initialize app
.mount("#app");
```

```vue
<script setup lang="ts">
import { RecaptchaV2 } from "vue3-recaptcha-v2";
</script>
<template>
<RecaptchaV2 :sitekey="YOUR_SITE_KEY" />
</template>
```

More detail about install options, you can check this [Install Options](#install-options)

## Usage
Expand All @@ -61,7 +88,7 @@ import { RecaptchaV2 } from "vue3-recaptcha-v2";
const handleWidgetId = (widgetId: number) => {
console.log("Widget ID: ", widgetId);
};
const handleErrorCalback = () => {
const handleErrorCallback = () => {
console.log("Error callback");
};
const handleExpiredCallback = () => {
Expand All @@ -75,7 +102,7 @@ const handleLoadCallback = (response: unknown) => {
<template>
<RecaptchaV2
@widget-id="handleWidgetId"
@error-callback="handleErrorCalback"
@error-callback="handleErrorCallback"
@expired-callback="handleExpiredCallback"
@load-callback="handleLoadCallback"
/>
Expand Down
15 changes: 12 additions & 3 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script setup lang="ts">
import { ref } from "vue";
import { RecaptchaV2 } from "../../dist";
const handleWidgetId = (widgetId: string) => {
const siteKey = ref("");
const handleWidgetId = (widgetId: number) => {
console.log("Widget ID: ", widgetId);
};
const handleErrorCalback = () => {
const handleErrorCallback = () => {
console.log("Error callback");
};
const handleExpiredCallback = () => {
Expand All @@ -13,13 +16,19 @@ const handleExpiredCallback = () => {
const handleLoadCallback = (response: unknown) => {
console.log("Load callback", response);
};
const handleChangeSiteKey = () => {
siteKey.value = "12312323123";
};
</script>

<template>
<div>
<button @click="handleChangeSiteKey">Change</button>
<RecaptchaV2
:sitekey="siteKey"
@widget-id="handleWidgetId"
@error-callback="handleErrorCalback"
@error-callback="handleErrorCallback"
@expired-callback="handleExpiredCallback"
@load-callback="handleLoadCallback"
/>
Expand Down
6 changes: 1 addition & 5 deletions playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ import { install } from "../../dist";

const app = createApp(App);

app
.use(install, {
sitekey: "",
})
.mount("#app");
app.use(install).mount("#app");
Loading

0 comments on commit 1a2e3a4

Please sign in to comment.