@experiwall/react 0.2.1 → 0.3.1
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.
- package/dist/server.d.mts +46 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.js +46 -0
- package/dist/server.mjs +21 -0
- package/package.json +15 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for Experiwall.
|
|
3
|
+
*
|
|
4
|
+
* Use this module in Next.js server components, Route Handlers,
|
|
5
|
+
* or any server runtime to resolve experiments during SSR —
|
|
6
|
+
* the variant is in the initial HTML with zero loading state.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { fetchExperiments } from "@experiwall/react/server";
|
|
10
|
+
*
|
|
11
|
+
* // In a server component:
|
|
12
|
+
* const { assignments } = await fetchExperiments({
|
|
13
|
+
* apiKey: "ew_pub_...",
|
|
14
|
+
* aliasId: cookies().get("ew_anon_id")?.value,
|
|
15
|
+
* });
|
|
16
|
+
* const variant = assignments["my-experiment"] ?? "control";
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface ServerConfig {
|
|
20
|
+
apiKey: string;
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
userId?: string;
|
|
23
|
+
aliasId?: string;
|
|
24
|
+
environment?: string;
|
|
25
|
+
}
|
|
26
|
+
interface ExperimentsResponse {
|
|
27
|
+
user_seed: number;
|
|
28
|
+
assignments: Record<string, string>;
|
|
29
|
+
experiments?: Record<string, {
|
|
30
|
+
variants: {
|
|
31
|
+
key: string;
|
|
32
|
+
weight: number;
|
|
33
|
+
}[];
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Fetch experiment assignments from the Experiwall API.
|
|
38
|
+
*
|
|
39
|
+
* Call this from server components to resolve variants at SSR time.
|
|
40
|
+
* Pass a `userId` or `aliasId` to identify the visitor (e.g. from a cookie).
|
|
41
|
+
* If neither is provided, a random ID is generated automatically so the
|
|
42
|
+
* call always succeeds (useful on first visit before a cookie is set).
|
|
43
|
+
*/
|
|
44
|
+
declare function fetchExperiments(config: ServerConfig): Promise<ExperimentsResponse>;
|
|
45
|
+
|
|
46
|
+
export { type ExperimentsResponse, type ServerConfig, fetchExperiments };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for Experiwall.
|
|
3
|
+
*
|
|
4
|
+
* Use this module in Next.js server components, Route Handlers,
|
|
5
|
+
* or any server runtime to resolve experiments during SSR —
|
|
6
|
+
* the variant is in the initial HTML with zero loading state.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { fetchExperiments } from "@experiwall/react/server";
|
|
10
|
+
*
|
|
11
|
+
* // In a server component:
|
|
12
|
+
* const { assignments } = await fetchExperiments({
|
|
13
|
+
* apiKey: "ew_pub_...",
|
|
14
|
+
* aliasId: cookies().get("ew_anon_id")?.value,
|
|
15
|
+
* });
|
|
16
|
+
* const variant = assignments["my-experiment"] ?? "control";
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface ServerConfig {
|
|
20
|
+
apiKey: string;
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
userId?: string;
|
|
23
|
+
aliasId?: string;
|
|
24
|
+
environment?: string;
|
|
25
|
+
}
|
|
26
|
+
interface ExperimentsResponse {
|
|
27
|
+
user_seed: number;
|
|
28
|
+
assignments: Record<string, string>;
|
|
29
|
+
experiments?: Record<string, {
|
|
30
|
+
variants: {
|
|
31
|
+
key: string;
|
|
32
|
+
weight: number;
|
|
33
|
+
}[];
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Fetch experiment assignments from the Experiwall API.
|
|
38
|
+
*
|
|
39
|
+
* Call this from server components to resolve variants at SSR time.
|
|
40
|
+
* Pass a `userId` or `aliasId` to identify the visitor (e.g. from a cookie).
|
|
41
|
+
* If neither is provided, a random ID is generated automatically so the
|
|
42
|
+
* call always succeeds (useful on first visit before a cookie is set).
|
|
43
|
+
*/
|
|
44
|
+
declare function fetchExperiments(config: ServerConfig): Promise<ExperimentsResponse>;
|
|
45
|
+
|
|
46
|
+
export { type ExperimentsResponse, type ServerConfig, fetchExperiments };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
fetchExperiments: () => fetchExperiments
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(server_exports);
|
|
26
|
+
var DEFAULT_BASE_URL = "https://www.experiwall.com";
|
|
27
|
+
async function fetchExperiments(config) {
|
|
28
|
+
const base = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
29
|
+
const url = new URL("/api/sdk/init", base);
|
|
30
|
+
const aliasId = config.aliasId ?? (!config.userId ? crypto.randomUUID() : void 0);
|
|
31
|
+
if (config.userId) url.searchParams.set("user_id", config.userId);
|
|
32
|
+
if (aliasId) url.searchParams.set("alias_id", aliasId);
|
|
33
|
+
if (config.environment) url.searchParams.set("environment", config.environment);
|
|
34
|
+
const res = await fetch(url.toString(), {
|
|
35
|
+
headers: { "x-api-key": config.apiKey },
|
|
36
|
+
cache: "no-store"
|
|
37
|
+
});
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
throw new Error(`Experiwall API error: ${res.status}`);
|
|
40
|
+
}
|
|
41
|
+
return res.json();
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
fetchExperiments
|
|
46
|
+
});
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://www.experiwall.com";
|
|
3
|
+
async function fetchExperiments(config) {
|
|
4
|
+
const base = config.baseUrl ?? DEFAULT_BASE_URL;
|
|
5
|
+
const url = new URL("/api/sdk/init", base);
|
|
6
|
+
const aliasId = config.aliasId ?? (!config.userId ? crypto.randomUUID() : void 0);
|
|
7
|
+
if (config.userId) url.searchParams.set("user_id", config.userId);
|
|
8
|
+
if (aliasId) url.searchParams.set("alias_id", aliasId);
|
|
9
|
+
if (config.environment) url.searchParams.set("environment", config.environment);
|
|
10
|
+
const res = await fetch(url.toString(), {
|
|
11
|
+
headers: { "x-api-key": config.apiKey },
|
|
12
|
+
cache: "no-store"
|
|
13
|
+
});
|
|
14
|
+
if (!res.ok) {
|
|
15
|
+
throw new Error(`Experiwall API error: ${res.status}`);
|
|
16
|
+
}
|
|
17
|
+
return res.json();
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
fetchExperiments
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@experiwall/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Experiwall React SDK — code-first experimentation and A/B testing",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server.d.ts",
|
|
16
|
+
"import": "./dist/server.mjs",
|
|
17
|
+
"require": "./dist/server.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
8
20
|
"files": [
|
|
9
21
|
"dist"
|
|
10
22
|
],
|
|
11
23
|
"scripts": {
|
|
12
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
13
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
24
|
+
"build": "tsup src/index.ts src/server.ts --format cjs,esm --dts --clean",
|
|
25
|
+
"dev": "tsup src/index.ts src/server.ts --format cjs,esm --dts --watch"
|
|
14
26
|
},
|
|
15
27
|
"peerDependencies": {
|
|
16
28
|
"react": ">=18",
|