@worldcoin/idkit 2.3.0 → 2.4.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.
@@ -1,17 +1,32 @@
1
1
  import * as react from 'react';
2
2
  import * as zustand_traditional from 'zustand/traditional';
3
3
  import * as zustand_vanilla from 'zustand/vanilla';
4
- import { I as IDKITStage, a as ConfigSource, b as CallbackFn, C as Config } from './config-BRFx4nLT.cjs';
4
+ import { I as IDKITStage, c as ConfigSource, d as CallbackFn, C as Config } from './config-SH5TbLYj.cjs';
5
+ export { S as SupportedLanguage, a as getCurrentLanguage, g as getLocalizationConfig, b as getSupportedLanguages, s as setLocalizationConfig } from './config-SH5TbLYj.cjs';
5
6
  import { IDKitConfig, ISuccessResult, IErrorState } from '@worldcoin/idkit-core';
6
7
 
7
- type CleanWord<T> = T extends `${string}${' ' | ',' | '!' | '?' | '.' | '`'}${string}` ? never : T extends '' ? never : T;
8
- type ExtractPlaceholders<S extends string> = S extends `${string}:${infer Placeholder}` ? Placeholder extends `${infer Word}${' ' | ',' | '!' | '?' | '.' | '`'}${infer Rest}` ? CleanWord<Word> | ExtractPlaceholders<Rest> : never : never;
9
- type NoPlaceholder<S extends string> = S extends `${string}:${string}` ? never : S;
10
- type PlaceholderValues<S extends string> = {
11
- [K in ExtractPlaceholders<S>]: string;
8
+ /**
9
+ * ## Adding Translations
10
+ * To add a new language:
11
+ * 1. Create a new translation file in `src/lang/translations/[lang].ts`
12
+ * 2. Implement all translation strings in the new file
13
+ * 3. Add the language to the `translations` index
14
+ * 4. Update the `SupportedLanguage` type in `src/lang/types.ts`
15
+ */
16
+ /**
17
+ * Translation function with type-safe parameter support
18
+ *
19
+ * Examples:
20
+ * - __('Hello world') // No parameters needed
21
+ * - __('Invalid IDKitStage :stage.', { stage: 'init' }) // Parameter required
22
+ */
23
+ type HasPlaceholder<T extends string> = T extends `${string}:${string}` ? true : false;
24
+ type GetPlaceholderName<T extends string> = T extends `:${infer Name}${infer Rest}` ? Name extends `${infer Word}${' ' | ',' | ':' | '!' | '?' | '.'}${string}` ? Word : Name : never;
25
+ type GetAllPlaceholders<T extends string> = T extends `${infer Before}:${infer After}` ? GetAllPlaceholders<After> | GetPlaceholderName<`:${After}`> : never;
26
+ type TranslationParams<T extends string> = GetAllPlaceholders<T> extends never ? never : {
27
+ [K in GetAllPlaceholders<T>]: string;
12
28
  };
13
- declare function __<S extends `${string}:${string}`>(str: S, params: PlaceholderValues<S>): string;
14
- declare function __<S extends string>(str: NoPlaceholder<S>): string;
29
+ declare function __<T extends string>(str: T, ...args: HasPlaceholder<T> extends true ? [params: TranslationParams<T>] : []): string;
15
30
 
16
31
  type Props = {
17
32
  data: string;
@@ -1,17 +1,32 @@
1
1
  import * as react from 'react';
2
2
  import * as zustand_traditional from 'zustand/traditional';
3
3
  import * as zustand_vanilla from 'zustand/vanilla';
4
- import { I as IDKITStage, a as ConfigSource, b as CallbackFn, C as Config } from './config-BRFx4nLT.js';
4
+ import { I as IDKITStage, c as ConfigSource, d as CallbackFn, C as Config } from './config-SH5TbLYj.js';
5
+ export { S as SupportedLanguage, a as getCurrentLanguage, g as getLocalizationConfig, b as getSupportedLanguages, s as setLocalizationConfig } from './config-SH5TbLYj.js';
5
6
  import { IDKitConfig, ISuccessResult, IErrorState } from '@worldcoin/idkit-core';
6
7
 
7
- type CleanWord<T> = T extends `${string}${' ' | ',' | '!' | '?' | '.' | '`'}${string}` ? never : T extends '' ? never : T;
8
- type ExtractPlaceholders<S extends string> = S extends `${string}:${infer Placeholder}` ? Placeholder extends `${infer Word}${' ' | ',' | '!' | '?' | '.' | '`'}${infer Rest}` ? CleanWord<Word> | ExtractPlaceholders<Rest> : never : never;
9
- type NoPlaceholder<S extends string> = S extends `${string}:${string}` ? never : S;
10
- type PlaceholderValues<S extends string> = {
11
- [K in ExtractPlaceholders<S>]: string;
8
+ /**
9
+ * ## Adding Translations
10
+ * To add a new language:
11
+ * 1. Create a new translation file in `src/lang/translations/[lang].ts`
12
+ * 2. Implement all translation strings in the new file
13
+ * 3. Add the language to the `translations` index
14
+ * 4. Update the `SupportedLanguage` type in `src/lang/types.ts`
15
+ */
16
+ /**
17
+ * Translation function with type-safe parameter support
18
+ *
19
+ * Examples:
20
+ * - __('Hello world') // No parameters needed
21
+ * - __('Invalid IDKitStage :stage.', { stage: 'init' }) // Parameter required
22
+ */
23
+ type HasPlaceholder<T extends string> = T extends `${string}:${string}` ? true : false;
24
+ type GetPlaceholderName<T extends string> = T extends `:${infer Name}${infer Rest}` ? Name extends `${infer Word}${' ' | ',' | ':' | '!' | '?' | '.'}${string}` ? Word : Name : never;
25
+ type GetAllPlaceholders<T extends string> = T extends `${infer Before}:${infer After}` ? GetAllPlaceholders<After> | GetPlaceholderName<`:${After}`> : never;
26
+ type TranslationParams<T extends string> = GetAllPlaceholders<T> extends never ? never : {
27
+ [K in GetAllPlaceholders<T>]: string;
12
28
  };
13
- declare function __<S extends `${string}:${string}`>(str: S, params: PlaceholderValues<S>): string;
14
- declare function __<S extends string>(str: NoPlaceholder<S>): string;
29
+ declare function __<T extends string>(str: T, ...args: HasPlaceholder<T> extends true ? [params: TranslationParams<T>] : []): string;
15
30
 
16
31
  type Props = {
17
32
  data: string;
package/build/internal.js CHANGED
@@ -2,11 +2,19 @@ import {
2
2
  ConfigSource,
3
3
  QRCode_default,
4
4
  __,
5
- idkit_default
6
- } from "./chunk-3HCETHXP.js";
5
+ getCurrentLanguage,
6
+ getLocalizationConfig,
7
+ getSupportedLanguages,
8
+ idkit_default,
9
+ setLocalizationConfig
10
+ } from "./chunk-PWFTYFSZ.js";
7
11
  export {
8
12
  ConfigSource,
9
13
  QRCode_default as QRCode,
10
14
  __,
15
+ getCurrentLanguage,
16
+ getLocalizationConfig,
17
+ getSupportedLanguages,
18
+ setLocalizationConfig,
11
19
  idkit_default as useIDKitStore
12
20
  };
package/package.json CHANGED
@@ -103,7 +103,7 @@
103
103
  ]
104
104
  }
105
105
  },
106
- "version": "2.3.0",
106
+ "version": "2.4.0",
107
107
  "scripts": {
108
108
  "build": "npm run build:css && tsup",
109
109
  "build:css": "npx tailwindcss -i ./src/styles/styles.css -o ./build/index.css --minify",
package/src/index.ts CHANGED
@@ -6,9 +6,22 @@ import { verifyCloudProof } from '@worldcoin/idkit-core/backend'
6
6
  import type { IVerifyResponse } from '@worldcoin/idkit-core/backend'
7
7
  import type { ISuccessResult, IErrorState } from '@worldcoin/idkit-core'
8
8
  import { VerificationLevel, VerificationState } from '@worldcoin/idkit-core'
9
+ import { setLocalizationConfig, getLocalizationConfig, getCurrentLanguage, getSupportedLanguages } from '@/lang'
10
+ import type { SupportedLanguage } from '@/lang'
9
11
 
10
- export { IDKitWidget, useIDKit, solidityEncode, verifyCloudProof, VerificationLevel, VerificationState }
11
- export type { ISuccessResult, IErrorState, IVerifyResponse, Config, WidgetProps }
12
+ export {
13
+ IDKitWidget,
14
+ useIDKit,
15
+ solidityEncode,
16
+ verifyCloudProof,
17
+ VerificationLevel,
18
+ VerificationState,
19
+ setLocalizationConfig,
20
+ getLocalizationConfig,
21
+ getCurrentLanguage,
22
+ getSupportedLanguages,
23
+ }
24
+ export type { ISuccessResult, IErrorState, IVerifyResponse, Config, WidgetProps, SupportedLanguage }
12
25
 
13
26
  // Session API
14
27
  export { useSession } from '@/hooks/useSession'
@@ -1,233 +0,0 @@
1
- // src/lang/index.ts
2
- var translations = {};
3
- var getLang = () => {
4
- if (!navigator?.languages) return;
5
- const supportedLang = navigator.languages.find((l) => translations[l] != void 0) ?? "";
6
- return translations[supportedLang];
7
- };
8
- var replaceParams = (str, params) => {
9
- let replaced = str;
10
- for (const [key, value] of Object.entries(params ?? {})) replaced = str.replace(`:${key}`, value);
11
- return replaced;
12
- };
13
- function __(str, params) {
14
- if (typeof navigator === "undefined") return str;
15
- return replaceParams(getLang()?.[str] ?? str, params);
16
- }
17
-
18
- // src/store/idkit.ts
19
- import { shallow } from "zustand/shallow";
20
- import { createWithEqualityFn } from "zustand/traditional";
21
- import {
22
- AppErrorCodes,
23
- DEFAULT_VERIFICATION_LEVEL
24
- } from "@worldcoin/idkit-core";
25
- var SELF_HOSTED_APP_ID = "self_hosted";
26
- var useIDKitStore = createWithEqualityFn()(
27
- (set, get) => ({
28
- app_id: "",
29
- signal: "",
30
- action: "",
31
- action_description: "",
32
- bridge_url: "",
33
- verification_level: DEFAULT_VERIFICATION_LEVEL,
34
- partner: false,
35
- open: false,
36
- result: null,
37
- errorTitle: "",
38
- errorDetail: "",
39
- autoClose: true,
40
- errorState: null,
41
- processing: false,
42
- errorCallbacks: {},
43
- verifyCallbacks: {},
44
- successCallbacks: {},
45
- stage: "WORLD_ID" /* WORLD_ID */,
46
- setStage: (stage) => set({ stage }),
47
- setErrorState: (errorState) => set({ errorState }),
48
- setProcessing: (processing) => set({ processing }),
49
- retryFlow: () => {
50
- set({ stage: "WORLD_ID" /* WORLD_ID */, errorState: null });
51
- },
52
- addErrorCallback: (cb, source) => {
53
- set((state) => {
54
- state.errorCallbacks[source] = cb;
55
- return state;
56
- });
57
- },
58
- addSuccessCallback: (cb, source) => {
59
- set((state) => {
60
- state.successCallbacks[source] = cb;
61
- return state;
62
- });
63
- },
64
- addVerificationCallback: (cb, source) => {
65
- set((state) => {
66
- state.verifyCallbacks[source] = cb;
67
- return state;
68
- });
69
- },
70
- setOptions: ({
71
- handleVerify,
72
- onSuccess,
73
- signal,
74
- action,
75
- app_id,
76
- partner,
77
- onError,
78
- verification_level,
79
- action_description,
80
- bridge_url,
81
- autoClose,
82
- advanced
83
- }, source) => {
84
- set({
85
- signal,
86
- action,
87
- bridge_url,
88
- action_description,
89
- autoClose: autoClose ?? true,
90
- app_id: advanced?.self_hosted ? SELF_HOSTED_APP_ID : app_id,
91
- verification_level: verification_level ?? DEFAULT_VERIFICATION_LEVEL,
92
- partner
93
- });
94
- get().addSuccessCallback(onSuccess, source);
95
- if (onError) get().addErrorCallback(onError, source);
96
- if (handleVerify) get().addVerificationCallback(handleVerify, source);
97
- },
98
- handleVerify: (result) => {
99
- set({ stage: "HOST_APP_VERIFICATION" /* HOST_APP_VERIFICATION */, processing: false });
100
- Promise.all(Object.values(get().verifyCallbacks).map(async (cb) => cb?.(result))).then(
101
- () => {
102
- set({ stage: "SUCCESS" /* SUCCESS */, result });
103
- if (get().autoClose) setTimeout(() => get().onOpenChange(false), 2500);
104
- },
105
- (response) => {
106
- let errorMessage = void 0;
107
- if (response && typeof response === "object" && response.message) {
108
- errorMessage = response.message;
109
- }
110
- set({
111
- stage: "ERROR" /* ERROR */,
112
- errorState: {
113
- code: AppErrorCodes.FailedByHostApp,
114
- message: errorMessage ? __(errorMessage) : void 0
115
- }
116
- });
117
- }
118
- );
119
- },
120
- onOpenChange: (open) => {
121
- if (open) {
122
- return set({ open });
123
- }
124
- const errorState = get().errorState;
125
- if (get().stage === "ERROR" /* ERROR */ && errorState) {
126
- const callbacks = get().errorCallbacks;
127
- requestAnimationFrame(() => Object.values(callbacks).forEach((cb) => void cb?.(errorState)));
128
- }
129
- const result = get().result;
130
- if (get().stage == "SUCCESS" /* SUCCESS */ && result) {
131
- const callbacks = get().successCallbacks;
132
- requestAnimationFrame(() => Object.values(callbacks).forEach((cb) => void cb?.(result)));
133
- }
134
- set({
135
- open,
136
- result: null,
137
- errorState: null,
138
- processing: false,
139
- stage: "WORLD_ID" /* WORLD_ID */
140
- });
141
- }
142
- }),
143
- shallow
144
- );
145
- var idkit_default = useIDKitStore;
146
-
147
- // src/types/config.ts
148
- var ConfigSource = /* @__PURE__ */ ((ConfigSource2) => {
149
- ConfigSource2["HOOK"] = "hook";
150
- ConfigSource2["PROPS"] = "props";
151
- ConfigSource2["MANUAL"] = "manual";
152
- return ConfigSource2;
153
- })(ConfigSource || {});
154
-
155
- // src/components/QRCode.tsx
156
- import { memo, useMemo } from "react";
157
- import QRCodeUtil from "qrcode/lib/core/qrcode.js";
158
- import { jsx } from "react/jsx-runtime";
159
- var generateMatrix = (data) => {
160
- const arr = QRCodeUtil.create(data, { errorCorrectionLevel: "M" }).modules.data;
161
- const sqrt = Math.sqrt(arr.length);
162
- return arr.reduce(
163
- (rows, key, index) => {
164
- if (index % sqrt === 0) rows.push([key]);
165
- else rows[rows.length - 1].push(key);
166
- return rows;
167
- },
168
- []
169
- );
170
- };
171
- var Qrcode = ({ data, size = 300 }) => {
172
- const dots = useMemo(() => {
173
- const dots2 = [];
174
- const matrix = generateMatrix(data);
175
- const cellSize = size / matrix.length;
176
- const qrList = [
177
- { x: 0, y: 0 },
178
- { x: 1, y: 0 },
179
- { x: 0, y: 1 }
180
- ];
181
- qrList.forEach(({ x, y }) => {
182
- const x1 = (matrix.length - 7) * cellSize * x;
183
- const y1 = (matrix.length - 7) * cellSize * y;
184
- for (let i = 0; i < 3; i++) {
185
- dots2.push(
186
- /* @__PURE__ */ jsx(
187
- "rect",
188
- {
189
- fill: "currentColor",
190
- x: x1 + cellSize * i,
191
- y: y1 + cellSize * i,
192
- width: cellSize * (7 - i * 2),
193
- height: cellSize * (7 - i * 2),
194
- rx: (i - 2) * -5,
195
- ry: (i - 2) * -5,
196
- className: i % 3 === 0 ? "text-black" : i % 3 === 1 ? "text-white" : "text-black"
197
- },
198
- `${i}-${x}-${y}`
199
- )
200
- );
201
- }
202
- });
203
- matrix.forEach((row, i) => {
204
- row.forEach((_, j) => {
205
- if (!matrix[i][j]) return;
206
- if (i < 7 && j < 7 || i > matrix.length - 8 && j < 7 || i < 7 && j > matrix.length - 8) return;
207
- dots2.push(
208
- /* @__PURE__ */ jsx(
209
- "circle",
210
- {
211
- fill: "currentColor",
212
- r: cellSize / 2.2,
213
- cx: i * cellSize + cellSize / 2,
214
- cy: j * cellSize + cellSize / 2,
215
- className: "text-black dark:text-white"
216
- },
217
- `circle-${i}-${j}`
218
- )
219
- );
220
- });
221
- });
222
- return dots2;
223
- }, [size, data]);
224
- return /* @__PURE__ */ jsx("svg", { height: size, width: size, "data-test-id": "qr-code", children: dots });
225
- };
226
- var QRCode_default = memo(Qrcode);
227
-
228
- export {
229
- __,
230
- idkit_default,
231
- ConfigSource,
232
- QRCode_default
233
- };