Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions widgets/subscription/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Widget } from "../src/Widget";
37 changes: 37 additions & 0 deletions widgets/subscription/api/runtime-shadow-widget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { WIDGET_ID } from "../src/Config.ts";

import type { WidgetApi } from "@reactedge/public-api/widget";
import type {ReactEdgeRuntimeConfig} from "@reactedge/public-api/runtime.ts";

import {ShadowHostProvider} from "@reactedge/framework/host.ts";
import {createRoot} from "react-dom/client";
import {WidgetRoot} from "../src/bootstrap/widget-root.tsx"
import {styles} from "../src/styles/entry.ts";

const mount = (
el: HTMLElement,
contract: unknown,
bootstrap: unknown,
runtime: ReactEdgeRuntimeConfig
) => {
const hostProvider = new ShadowHostProvider(styles)

createRoot(hostProvider.getMountedHost(el)).render(
<WidgetRoot
hostElement={el}
contract={contract}
bootstrap={bootstrap}
runtime={runtime}
/>
)
};

const api: WidgetApi = {
mount,
};

if (typeof window !== "undefined") {
window[`ReactEdge_${WIDGET_ID}`] = api;
}

export { mount };
15 changes: 15 additions & 0 deletions widgets/subscription/capability.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "subscription",
"widget": "subscription",
"description": "",
"signals": {
"consumes": [],
"emits": []
},
"runtime": {
"rendering": [
"client",
"ssr"
]
}
}
31 changes: 31 additions & 0 deletions widgets/subscription/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<body>

<subscription-widget></subscription-widget>

<script type="module">
import { mount } from "./api/runtime-shadow-widget.tsx";
import { ResourceLoader } from "@reactedge/framework/contract";

const loader = new ResourceLoader();

const contract = await loader.loadContract("default.json");
const runtime = await loader.loadRuntime();
const bootstrap = undefined

const element = document.querySelector("subscription-widget");

if (!element) {
throw new Error(
"subscription-widget element not found"
);
}

requestAnimationFrame(() => {
mount(element, contract, bootstrap, runtime);
});
</script>

</body>
</html>
39 changes: 39 additions & 0 deletions widgets/subscription/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "widget-subscription",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"build:ssr": "vite build -c vite.npm.config.ts",
"analyze": "ANALYZE=true vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"zod": "^4.4.3"
},
"devDependencies": {
"@eslint/js": "^9.39.1",
"@types/node": "^24.10.1",
"@types/react": "^19.2.5",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react-swc": "^4.2.2",
"eslint": "^9.39.1",
"eslint-plugin-react-hooks": "^7.0.1",
"eslint-plugin-react-refresh": "^0.4.24",
"globals": "^16.5.0",
"postcss": "^8.5.6",
"postcss-prefix-selector": "^2.1.1",
"rollup-plugin-visualizer": "^7.0.1",
"tsx": "^4.21.0",
"typescript": "~5.9.3",
"typescript-eslint": "^8.46.4",
"vite": "^8.3.0"
},
"peerDependencies": {
"react": "^19.2.0",
"react-dom": "^19.2.0"
}
}
27 changes: 27 additions & 0 deletions widgets/subscription/public/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"data": {
"title": "Subscribe & Save",
"isCustomerLoggedIn": true,
"plans": [
{
"id": "monthly",
"label": "Monthly delivery",
"frequency": "Every month",
"discount": "Save 10%",
"termsBlockId": "subscription-monthly-terms"
},
{
"id": "quarterly",
"label": "Quarterly delivery",
"frequency": "Every 3 months",
"discount": "Save 15%"
}
]
},
"settings": {
"colour": "#222222"
},
"integration": {
"requires": ["magentoGraphql"]
}
}
125 changes: 125 additions & 0 deletions widgets/subscription/src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {parseConfig, type SchemaWidgetConfig} from "./ConfigSchema.ts";
import type {WidgetActivity} from "@reactedge/framework/activity";
import {parseRuntimeConfig, type SchemaRuntimeConfig} from "./ConfigSchemaRuntime.ts";

export interface WidgetConfig {
readonly data: {
title: string;
plans: SubscriptionPlan[];
isCustomerLoggedIn: boolean;
loginUrl?: string | undefined;
}

readonly settings: {
colour: string;
};

readonly runtime: RuntimeConfig
readonly integrations: ResolvedConfigIntegrations
}

export interface ProductData {
id: string;
sku: string
name: string
}

export interface SubscriptionPlan {
id: string;
label: string;
frequency: string;
discount?: string | undefined;
termsBlockId?: string | undefined;
}

export interface RuntimeConfig {
storeCode: string;
sku: string;
termsUrl: string;
}

export interface ReactEdgeRuntimeConfig {
readonly integrations: ReactEdgeRuntimeIntegrations;
readonly context: Omit<RuntimeConfig, 'termsUrl'> & { termsUrl?: string }
}

export interface ReactEdgeRuntimeIntegrations {
readonly magentoGraphql: {
readonly api: string
};
}

export interface ResolvedConfigIntegrations {
readonly magentoGraphql: {
readonly api: string
};
}

export const WIDGET_ID = 'subscription';

/**
* Validates and resolves the Contact Us widget configuration.
*
* Both the widget contract and the runtime configuration are treated
* as untrusted input. Once validated, the configuration is normalized,
* resolved and frozen before being exposed to the React application.
*
* This function represents the trust boundary between the ReactEdge
* runtime and the widget implementation.
*
* The resolved configuration includes the Cloudflare integration
* required to render the captcha.
*
* @param contract - Widget contract supplied by the host platform.
* @param runtime - Runtime services supplied by the orchestrator.
* @param activity - Activity logger for bootstrap events.
* @returns An immutable Contact Us configuration.
* @throws When either configuration is invalid.
*/
export function readWidgetConfig(
contract: unknown,
runtime: unknown,
activity?: WidgetActivity
): WidgetConfig {
try {
const parsedContract = parseConfig(contract);
const parsedRuntime = parseRuntimeConfig(runtime)
const resolved = resolveConfig(parsedContract, parsedRuntime);

activity?.log(
'bootstrap',
'Config resolved',
resolved
);

return Object.freeze(resolved);

} catch (e) {
activity?.log(
'bootstrap',
'Invalid widget contract',
e instanceof Error? e.message: e,
'error'
);

throw e;
}
}

export function resolveConfig(
widget: SchemaWidgetConfig,
runtime: SchemaRuntimeConfig
): WidgetConfig {
return {
data: widget.data,
settings: widget.settings,
runtime: {
storeCode: runtime.context.storeCode,
sku: runtime.context.sku,
termsUrl: runtime.context.termsUrl ?? '/subscribe/plan/terms'
},
integrations: {
magentoGraphql: runtime.integrations?.magentoGraphql
}
};
}
Loading