@c1m-public/dash-site-kit 0.1.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +145 -0
  3. package/dist/format.d.ts +11 -0
  4. package/dist/format.js +16 -0
  5. package/dist/image-policy.d.ts +6 -0
  6. package/dist/image-policy.js +28 -0
  7. package/dist/index.d.ts +30 -0
  8. package/dist/index.js +6 -0
  9. package/dist/models/bootstrap.d.ts +33 -0
  10. package/dist/models/bootstrap.js +1 -0
  11. package/dist/models/directory.d.ts +37 -0
  12. package/dist/models/directory.js +1 -0
  13. package/dist/models/lead-portal.d.ts +56 -0
  14. package/dist/models/lead-portal.js +6 -0
  15. package/dist/models/leads.d.ts +19 -0
  16. package/dist/models/leads.js +1 -0
  17. package/dist/models/plans.d.ts +31 -0
  18. package/dist/models/plans.js +1 -0
  19. package/dist/models/profile.d.ts +25 -0
  20. package/dist/models/profile.js +1 -0
  21. package/dist/next/assets.d.ts +4 -0
  22. package/dist/next/assets.js +6 -0
  23. package/dist/next/cache.d.ts +7 -0
  24. package/dist/next/cache.js +7 -0
  25. package/dist/next/client.d.ts +24 -0
  26. package/dist/next/client.js +84 -0
  27. package/dist/next/data-layer.d.ts +48 -0
  28. package/dist/next/data-layer.js +57 -0
  29. package/dist/next/delivery.d.ts +2 -0
  30. package/dist/next/delivery.js +7 -0
  31. package/dist/next/env.d.ts +12 -0
  32. package/dist/next/env.js +38 -0
  33. package/dist/next/normalize.d.ts +23 -0
  34. package/dist/next/normalize.js +142 -0
  35. package/dist/next/observability.d.ts +11 -0
  36. package/dist/next/observability.js +18 -0
  37. package/dist/next/providers/bootstrap.d.ts +7 -0
  38. package/dist/next/providers/bootstrap.js +23 -0
  39. package/dist/next/providers/directory.d.ts +7 -0
  40. package/dist/next/providers/directory.js +39 -0
  41. package/dist/next/providers/lead-portal.d.ts +7 -0
  42. package/dist/next/providers/lead-portal.js +65 -0
  43. package/dist/next/providers/leads.d.ts +13 -0
  44. package/dist/next/providers/leads.js +38 -0
  45. package/dist/next/providers/plans.d.ts +7 -0
  46. package/dist/next/providers/plans.js +17 -0
  47. package/dist/next/providers/profile.d.ts +15 -0
  48. package/dist/next/providers/profile.js +33 -0
  49. package/dist/next.d.ts +23 -0
  50. package/dist/next.js +21 -0
  51. package/dist/ports/bootstrap.d.ts +6 -0
  52. package/dist/ports/bootstrap.js +1 -0
  53. package/dist/ports/directory.d.ts +7 -0
  54. package/dist/ports/directory.js +1 -0
  55. package/dist/ports/lead-portal.d.ts +6 -0
  56. package/dist/ports/lead-portal.js +1 -0
  57. package/dist/ports/leads.d.ts +9 -0
  58. package/dist/ports/leads.js +1 -0
  59. package/dist/ports/plans.d.ts +5 -0
  60. package/dist/ports/plans.js +1 -0
  61. package/dist/ports/profile.d.ts +9 -0
  62. package/dist/ports/profile.js +1 -0
  63. package/dist/site-config.d.ts +70 -0
  64. package/dist/site-config.js +87 -0
  65. package/dist/ui/error-fallback.d.ts +6 -0
  66. package/dist/ui/error-fallback.js +12 -0
  67. package/dist/ui/primitives.d.ts +25 -0
  68. package/dist/ui/primitives.js +23 -0
  69. package/dist/ui/site-image.d.ts +7 -0
  70. package/dist/ui/site-image.js +9 -0
  71. package/dist/ui.d.ts +12 -0
  72. package/dist/ui.js +12 -0
  73. package/dist/url-policy.d.ts +3 -0
  74. package/dist/url-policy.js +46 -0
  75. package/dist/validation/directory.d.ts +2 -0
  76. package/dist/validation/directory.js +15 -0
  77. package/dist/validation/leads.d.ts +21 -0
  78. package/dist/validation/leads.js +59 -0
  79. package/package.json +67 -0
@@ -0,0 +1,48 @@
1
+ import "server-only";
2
+ import type { DashBootstrap } from "../models/bootstrap.js";
3
+ import type { DirectoryEntry, DirectoryFilter, DirectoryProfileResult, DirectoryRegion } from "../models/directory.js";
4
+ import type { LeadInput, LeadOutcome } from "../models/leads.js";
5
+ import type { RequestLinkOutcome, ViewOutcome } from "../models/lead-portal.js";
6
+ import type { PlanCatalog } from "../models/plans.js";
7
+ import type { DashProfile } from "../models/profile.js";
8
+ /**
9
+ * A credential-free local preview. When `enabled()` is true the layer answers
10
+ * from these fixtures and never touches the channel, so `npm run dev` works
11
+ * without a token.
12
+ *
13
+ * This is the seam that used to force every site to hand-write its own
14
+ * lib/{bootstrap,directory,profile}/index.ts. Those files were the single
15
+ * largest source of drift between sites that were otherwise identical, because
16
+ * the preview branch had to be re-threaded through each composed loader by
17
+ * hand. Declaring it once, here, is the point.
18
+ */
19
+ export interface DashPreview {
20
+ enabled(): boolean;
21
+ bootstrap: DashBootstrap;
22
+ profile: DashProfile;
23
+ }
24
+ export interface DashDataLayerOptions {
25
+ /** Recorded on every captured lead. Normally `siteConfig.leadSource`. */
26
+ leadSource: string;
27
+ preview?: DashPreview;
28
+ }
29
+ export interface DashDataLayer {
30
+ loadDashBootstrap(): Promise<DashBootstrap>;
31
+ loadDashProfile(): Promise<DashProfile>;
32
+ listDirectoryRegions(): Promise<DirectoryRegion[]>;
33
+ getDirectoryRegion(code: string): Promise<DirectoryRegion | undefined>;
34
+ listDirectory(filter?: DirectoryFilter): Promise<DirectoryEntry[]>;
35
+ getDirectoryProfile(idOrSlug: string): Promise<DirectoryProfileResult>;
36
+ loadDashPlans(): Promise<PlanCatalog>;
37
+ submitLead(input: LeadInput, attemptId: string): Promise<LeadOutcome>;
38
+ requestPortalLink(email: string): Promise<RequestLinkOutcome>;
39
+ viewPortal(token: string): Promise<ViewOutcome>;
40
+ }
41
+ /**
42
+ * Builds the whole logic layer for a dash site in one call.
43
+ *
44
+ * Every read is wrapped in React `cache`, so a page that asks for the profile
45
+ * and the shell that asks for the bootstrap share one request-scoped fetch
46
+ * rather than racing each other to the same door.
47
+ */
48
+ export declare function createDashDataLayer(options: DashDataLayerOptions): DashDataLayer;
@@ -0,0 +1,57 @@
1
+ import "server-only";
2
+ import { cache } from "react";
3
+ import { validateDirectoryKey, validateRegionFilter } from "../validation/directory.js";
4
+ import { createC1MBootstrapProvider } from "./providers/bootstrap.js";
5
+ import { createC1MDirectoryProvider } from "./providers/directory.js";
6
+ import { createC1MLeadPortalProvider } from "./providers/lead-portal.js";
7
+ import { createC1MLeadsProvider } from "./providers/leads.js";
8
+ import { createC1MPlansProvider } from "./providers/plans.js";
9
+ import { createC1MProfileProvider } from "./providers/profile.js";
10
+ /**
11
+ * Builds the whole logic layer for a dash site in one call.
12
+ *
13
+ * Every read is wrapped in React `cache`, so a page that asks for the profile
14
+ * and the shell that asks for the bootstrap share one request-scoped fetch
15
+ * rather than racing each other to the same door.
16
+ */
17
+ export function createDashDataLayer(options) {
18
+ const { preview } = options;
19
+ const inPreview = () => preview?.enabled() === true;
20
+ const bootstrapProvider = createC1MBootstrapProvider();
21
+ const loadDashBootstrap = cache(() => inPreview() ? Promise.resolve(preview.bootstrap) : bootstrapProvider.loadBootstrap());
22
+ const profileProvider = createC1MProfileProvider(loadDashBootstrap);
23
+ const directoryProvider = createC1MDirectoryProvider();
24
+ const plansProvider = createC1MPlansProvider();
25
+ const leadsProvider = createC1MLeadsProvider(options.leadSource);
26
+ const leadPortalProvider = createC1MLeadPortalProvider();
27
+ const loadDashProfile = cache(() => inPreview() ? Promise.resolve(preview.profile) : profileProvider.loadProfile());
28
+ const listDirectoryRegions = cache(() => inPreview() ? Promise.resolve([]) : directoryProvider.listRegions());
29
+ const listDirectory = cache((filter) => inPreview() ? Promise.resolve([]) : directoryProvider.list(filter));
30
+ return {
31
+ loadDashBootstrap,
32
+ loadDashProfile,
33
+ listDirectoryRegions,
34
+ listDirectory,
35
+ async getDirectoryRegion(code) {
36
+ const key = validateRegionFilter(code);
37
+ if (!key)
38
+ return undefined;
39
+ const regions = await listDirectoryRegions();
40
+ return regions.find((region) => region.code === key);
41
+ },
42
+ async getDirectoryProfile(idOrSlug) {
43
+ // Validate before the hop: an unusable key is a 404 here, not a
44
+ // round-trip that lets caller input reach the door unchecked.
45
+ const key = validateDirectoryKey(idOrSlug);
46
+ if (!key)
47
+ return { state: "missing" };
48
+ if (inPreview())
49
+ return { state: "missing" };
50
+ return directoryProvider.get(key);
51
+ },
52
+ loadDashPlans: cache(() => plansProvider.loadCatalog()),
53
+ submitLead: (input, attemptId) => leadsProvider.create(input, attemptId),
54
+ requestPortalLink: (email) => leadPortalProvider.requestLink(email),
55
+ viewPortal: (token) => leadPortalProvider.view(token),
56
+ };
57
+ }
@@ -0,0 +1,2 @@
1
+ import "server-only";
2
+ export declare function getChannelAttestation(challenge: string): Promise<string | undefined>;
@@ -0,0 +1,7 @@
1
+ import "server-only";
2
+ import { getChannelClient, requireDashScope } from "./client.js";
3
+ export async function getChannelAttestation(challenge) {
4
+ const identity = await getChannelClient().ping(challenge);
5
+ requireDashScope(identity.scope);
6
+ return identity.attestation;
7
+ }
@@ -0,0 +1,12 @@
1
+ import "server-only";
2
+ export interface ChannelEnvironment {
3
+ token: string;
4
+ /** Set only for local development or a self-hosted SaaSDash API. */
5
+ apiUrl?: string;
6
+ }
7
+ export declare class ChannelConfigurationError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ type ChannelProcessEnvironment = Readonly<Record<string, string | undefined>>;
11
+ export declare function resolveChannelEnvironment(environment?: ChannelProcessEnvironment): ChannelEnvironment;
12
+ export {};
@@ -0,0 +1,38 @@
1
+ import "server-only";
2
+ export class ChannelConfigurationError extends Error {
3
+ constructor(message) {
4
+ super(message);
5
+ this.name = "ChannelConfigurationError";
6
+ }
7
+ }
8
+ export function resolveChannelEnvironment(environment = process.env) {
9
+ if (environment.NEXT_PUBLIC_SAASDASH_CHANNEL_TOKEN || environment.NEXT_PUBLIC_C1M_CHANNEL_TOKEN) {
10
+ throw new ChannelConfigurationError("SAASDASH_CHANNEL_TOKEN must not use a NEXT_PUBLIC_* variable");
11
+ }
12
+ if (environment.NEXT_PUBLIC_SAASDASH_API_URL || environment.NEXT_PUBLIC_C1M_API_URL) {
13
+ throw new ChannelConfigurationError("SAASDASH_API_URL must not use a NEXT_PUBLIC_* variable");
14
+ }
15
+ const preferredToken = environment.SAASDASH_CHANNEL_TOKEN?.trim();
16
+ const legacyToken = environment.C1M_CHANNEL_TOKEN?.trim();
17
+ if (preferredToken && legacyToken && preferredToken !== legacyToken) {
18
+ throw new ChannelConfigurationError("SAASDASH_CHANNEL_TOKEN and legacy C1M_CHANNEL_TOKEN must match during migration");
19
+ }
20
+ const token = preferredToken || legacyToken;
21
+ if (!token)
22
+ throw new ChannelConfigurationError("SAASDASH_CHANNEL_TOKEN is required");
23
+ if (!token.startsWith("sk_chan_")) {
24
+ throw new ChannelConfigurationError("SAASDASH_CHANNEL_TOKEN must start with sk_chan_");
25
+ }
26
+ // The token is the only REQUIRED setting — the client knows the managed API
27
+ // endpoint. The override exists for local smoke tests and self-hosting.
28
+ const preferredApiUrl = environment.SAASDASH_API_URL?.trim();
29
+ const legacyApiUrl = environment.C1M_API_URL?.trim();
30
+ if (preferredApiUrl && legacyApiUrl && preferredApiUrl !== legacyApiUrl) {
31
+ throw new ChannelConfigurationError("SAASDASH_API_URL and legacy C1M_API_URL must match during migration");
32
+ }
33
+ const apiUrl = preferredApiUrl || legacyApiUrl;
34
+ if (apiUrl && !/^https?:\/\//.test(apiUrl)) {
35
+ throw new ChannelConfigurationError("SAASDASH_API_URL must be an http(s) URL");
36
+ }
37
+ return apiUrl ? { token, apiUrl } : { token };
38
+ }
@@ -0,0 +1,23 @@
1
+ import type { DashDirectoryEntry, DashDirectoryProfile, DashDirectoryRegion, DashInitResponse, DashPlansResponse, DashProfileResponse } from "@c1m-public/channel-client";
2
+ import type { DashProfile } from "../models/profile.js";
3
+ import type { DashBootstrap } from "../models/bootstrap.js";
4
+ import type { DirectoryEntry, DirectoryProfile, DirectoryRegion } from "../models/directory.js";
5
+ import type { PlanCatalog } from "../models/plans.js";
6
+ /** One flat shell object from ONE wire read: `dash.init`. */
7
+ export declare function normalizeDashBootstrap(init: DashInitResponse): DashBootstrap;
8
+ /**
9
+ * One flat object from the public page read (`dash.profile`) — everything a
10
+ * dash owner chose to publish. `fallbackName` is the tenant's display name
11
+ * from the shell bootstrap so a dash with an unedited profile still renders
12
+ * under its real name without a second identity read.
13
+ */
14
+ export declare function normalizeDashProfile(fallbackName: string, response: DashProfileResponse): DashProfile;
15
+ export declare function normalizeDirectoryRegion(region: DashDirectoryRegion): DirectoryRegion;
16
+ export declare function normalizeDirectoryEntry(entry: DashDirectoryEntry): DirectoryEntry;
17
+ export declare function normalizeDirectoryProfile(profile: DashDirectoryProfile): DirectoryProfile;
18
+ /**
19
+ * Pricing keys are region codes; each value carries its own currency. Prices
20
+ * stay in minor units end to end — `lib/format.ts` is the only place they
21
+ * become display strings.
22
+ */
23
+ export declare function normalizePlanCatalog(response: DashPlansResponse): PlanCatalog;
@@ -0,0 +1,142 @@
1
+ function text(value) {
2
+ const trimmed = value?.trim();
3
+ return trimmed || undefined;
4
+ }
5
+ // --- bootstrap ------------------------------------------------------------
6
+ /** One flat shell object from ONE wire read: `dash.init`. */
7
+ export function normalizeDashBootstrap(init) {
8
+ const name = text(init.tenant.display_name) ?? init.tenant.id;
9
+ const siteUrl = text(init.site?.url)?.replace(/\/+$/, "") ?? null;
10
+ return {
11
+ id: init.tenant.id,
12
+ name,
13
+ description: text(init.seo?.description) ?? text(init.public_profile.short_description),
14
+ branding: {
15
+ primaryColor: text(init.branding?.primary_color),
16
+ logoUrl: text(init.branding?.logo_url) ?? text(init.public_profile.logo_url),
17
+ logoSquareUrl: text(init.branding?.logo_square_url),
18
+ faviconUrl: text(init.branding?.favicon_url),
19
+ coverUrl: text(init.branding?.cover_url) ?? text(init.public_profile.header_media_url),
20
+ },
21
+ locale: {
22
+ region: text(init.locale?.region),
23
+ currency: text(init.locale?.currency),
24
+ locale: text(init.locale?.locale),
25
+ timezone: text(init.locale?.timezone),
26
+ },
27
+ site: { url: siteUrl, source: siteUrl ? (init.site?.source ?? null) : null },
28
+ seo: {
29
+ title: text(init.seo?.title) ?? name,
30
+ description: text(init.seo?.description) ?? text(init.public_profile.headline) ?? text(init.public_profile.short_description),
31
+ },
32
+ };
33
+ }
34
+ // --- profile --------------------------------------------------------------
35
+ /**
36
+ * One flat object from the public page read (`dash.profile`) — everything a
37
+ * dash owner chose to publish. `fallbackName` is the tenant's display name
38
+ * from the shell bootstrap so a dash with an unedited profile still renders
39
+ * under its real name without a second identity read.
40
+ */
41
+ export function normalizeDashProfile(fallbackName, response) {
42
+ const page = response.page;
43
+ const profile = page.profile;
44
+ const gallery = (page.media ?? []).map((item) => {
45
+ const altText = text(item.alt_text);
46
+ const caption = text(item.caption);
47
+ return {
48
+ url: item.url,
49
+ kind: item.kind,
50
+ ...(altText ? { altText } : {}),
51
+ ...(caption ? { caption } : {}),
52
+ };
53
+ });
54
+ return {
55
+ name: text(profile.display_name) ?? text(fallbackName) ?? profile.tenant_id,
56
+ headline: text(profile.headline),
57
+ shortDescription: text(profile.short_description),
58
+ about: text(profile.about),
59
+ category: text(profile.category),
60
+ pricingSummary: text(profile.pricing_summary),
61
+ logoUrl: text(profile.logo_url),
62
+ // The landing hero renders this. cover_url is the listing/rail card after
63
+ // the 000130 media rename, so the about hero wins and the card is the
64
+ // fallback. The OpenGraph image is a separate field (bootstrap.branding
65
+ // .coverUrl) and deliberately still prefers the card.
66
+ coverUrl: text(profile.header_media_url) ?? text(profile.cover_url),
67
+ gallery,
68
+ ownerName: text(page.owner_name),
69
+ userCount: typeof page.user_count === "number" ? page.user_count : 0,
70
+ };
71
+ }
72
+ // --- directory ------------------------------------------------------------
73
+ export function normalizeDirectoryRegion(region) {
74
+ return {
75
+ code: region.code,
76
+ label: text(region.label) ?? region.code,
77
+ count: region.count,
78
+ };
79
+ }
80
+ export function normalizeDirectoryEntry(entry) {
81
+ return {
82
+ id: entry.workspace_id,
83
+ slug: entry.slug,
84
+ name: entry.name,
85
+ tagline: text(entry.tagline),
86
+ category: text(entry.category),
87
+ logoUrl: text(entry.logo_url) ?? text(entry.logo_square_url),
88
+ reviewCount: entry.review_count,
89
+ averageRating: entry.average_rating,
90
+ verified: entry.verified === true,
91
+ regionCode: text(entry.major_region_code),
92
+ regionLabel: text(entry.major_region_label),
93
+ };
94
+ }
95
+ export function normalizeDirectoryProfile(profile) {
96
+ const entry = normalizeDirectoryEntry(profile);
97
+ return {
98
+ ...entry,
99
+ name: text(profile.display_name) ?? entry.name,
100
+ description: text(profile.description),
101
+ createdAt: text(profile.created_at),
102
+ };
103
+ }
104
+ // --- plans ------------------------------------------------------------------
105
+ /**
106
+ * Pricing keys are region codes; each value carries its own currency. Prices
107
+ * stay in minor units end to end — `lib/format.ts` is the only place they
108
+ * become display strings.
109
+ */
110
+ export function normalizePlanCatalog(response) {
111
+ const plans = (response.plans ?? [])
112
+ .map((plan) => {
113
+ const pricing = {};
114
+ for (const [regionCode, price] of Object.entries(plan.pricing ?? {})) {
115
+ pricing[regionCode] = {
116
+ currency: price.currency,
117
+ monthlyCents: price.monthly_price,
118
+ yearlyCents: price.yearly_price,
119
+ };
120
+ }
121
+ return {
122
+ id: plan.id,
123
+ name: plan.name,
124
+ description: text(plan.description),
125
+ level: plan.level,
126
+ isFree: plan.is_free === true,
127
+ trialDays: plan.trial_days,
128
+ highlights: plan.highlights ?? [],
129
+ pricing,
130
+ };
131
+ })
132
+ .sort((a, b) => a.level - b.level);
133
+ return {
134
+ plans,
135
+ regions: (response.regions ?? []).map((region) => ({
136
+ code: region.code,
137
+ name: text(region.name) ?? region.code,
138
+ currency: region.currency,
139
+ })),
140
+ defaultRegion: response.default_region,
141
+ };
142
+ }
@@ -0,0 +1,11 @@
1
+ import "server-only";
2
+ import type { ChannelRequestObservation } from "@c1m-public/channel-client";
3
+ export interface ChannelRequestLog extends ChannelRequestObservation {
4
+ event: "c1m.channel.request";
5
+ }
6
+ /**
7
+ * The single outbound seam for Channel API telemetry. Vercel captures these
8
+ * structured server logs today; a Sentry/OpenTelemetry sink can replace the
9
+ * console transport here without changing clients or providers.
10
+ */
11
+ export declare function observeChannelRequest(observation: ChannelRequestObservation): void;
@@ -0,0 +1,18 @@
1
+ import "server-only";
2
+ /**
3
+ * The single outbound seam for Channel API telemetry. Vercel captures these
4
+ * structured server logs today; a Sentry/OpenTelemetry sink can replace the
5
+ * console transport here without changing clients or providers.
6
+ */
7
+ export function observeChannelRequest(observation) {
8
+ const entry = { event: "c1m.channel.request", ...observation };
9
+ if (observation.outcome === "success") {
10
+ console.info(entry);
11
+ return;
12
+ }
13
+ if (observation.outcome === "api_error" && observation.status < 500) {
14
+ console.warn(entry);
15
+ return;
16
+ }
17
+ console.error(entry);
18
+ }
@@ -0,0 +1,7 @@
1
+ import "server-only";
2
+ import type { BootstrapProvider } from "../../ports/bootstrap.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ export declare function createC1MBootstrapProvider(resolveClient?: ChannelClientResolver): BootstrapProvider;
7
+ export {};
@@ -0,0 +1,23 @@
1
+ import "server-only";
2
+ import { resolveC1MAsset } from "../assets.js";
3
+ import { getChannelClient, getDashChannelClient } from "../client.js";
4
+ import { normalizeDashBootstrap } from "../normalize.js";
5
+ export function createC1MBootstrapProvider(resolveClient = getDashChannelClient) {
6
+ return {
7
+ async loadBootstrap() {
8
+ const client = await resolveClient();
9
+ const bootstrap = normalizeDashBootstrap(await client.dash.init());
10
+ const asset = (value) => (value ? resolveC1MAsset(client, value) : undefined);
11
+ return {
12
+ ...bootstrap,
13
+ branding: {
14
+ ...bootstrap.branding,
15
+ logoUrl: asset(bootstrap.branding.logoUrl),
16
+ logoSquareUrl: asset(bootstrap.branding.logoSquareUrl),
17
+ faviconUrl: asset(bootstrap.branding.faviconUrl),
18
+ coverUrl: asset(bootstrap.branding.coverUrl),
19
+ },
20
+ };
21
+ },
22
+ };
23
+ }
@@ -0,0 +1,7 @@
1
+ import "server-only";
2
+ import type { DirectoryProvider } from "../../ports/directory.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ export declare function createC1MDirectoryProvider(resolveClient?: ChannelClientResolver): DirectoryProvider;
7
+ export {};
@@ -0,0 +1,39 @@
1
+ import "server-only";
2
+ import { isChannelAPIError } from "@c1m-public/channel-client";
3
+ import { resolveC1MAsset } from "../assets.js";
4
+ import { getChannelClient, getDashChannelClient } from "../client.js";
5
+ import { normalizeDirectoryEntry, normalizeDirectoryProfile, normalizeDirectoryRegion } from "../normalize.js";
6
+ function resolveEntryAsset(client, entry) {
7
+ return { ...entry, logoUrl: resolveC1MAsset(client, entry.logoUrl) };
8
+ }
9
+ export function createC1MDirectoryProvider(resolveClient = getDashChannelClient) {
10
+ return {
11
+ async listRegions() {
12
+ const response = await (await resolveClient()).dash.directory.regions();
13
+ return (response.items ?? []).map(normalizeDirectoryRegion);
14
+ },
15
+ async list(filter = {}) {
16
+ const client = await resolveClient();
17
+ const response = await client.dash.directory.list({
18
+ ...(filter.region ? { region: filter.region } : {}),
19
+ ...(filter.category ? { category: filter.category } : {}),
20
+ });
21
+ return (response.items ?? []).map(normalizeDirectoryEntry).map((entry) => resolveEntryAsset(client, entry));
22
+ },
23
+ async get(idOrSlug) {
24
+ try {
25
+ const client = await resolveClient();
26
+ const response = await client.dash.directory.get(idOrSlug);
27
+ const profile = resolveEntryAsset(client, normalizeDirectoryProfile(response.profile));
28
+ return { state: "resolved", profile };
29
+ }
30
+ catch (error) {
31
+ // An unknown listing is a 404 page, not a crash; anything else is a
32
+ // real failure and is rethrown.
33
+ if (isChannelAPIError(error) && error.status === 404)
34
+ return { state: "missing" };
35
+ throw error;
36
+ }
37
+ },
38
+ };
39
+ }
@@ -0,0 +1,7 @@
1
+ import "server-only";
2
+ import type { LeadPortalProvider } from "../../ports/lead-portal.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ export declare function createC1MLeadPortalProvider(resolveClient?: ChannelClientResolver): LeadPortalProvider;
7
+ export {};
@@ -0,0 +1,65 @@
1
+ import "server-only";
2
+ import { isChannelAPIError } from "@c1m-public/channel-client";
3
+ import { getChannelClient, getVisitorDashChannelClient } from "../client.js";
4
+ /** Wire → domain. The only place the portal's wire shape is known. */
5
+ function normalizeView(view) {
6
+ return {
7
+ email: view.email,
8
+ requests: (view.requests ?? []).map((request) => ({
9
+ reference: request.reference,
10
+ submittedAt: request.submittedAt,
11
+ status: request.status,
12
+ summary: {
13
+ ...(request.summary?.from ? { from: request.summary.from } : {}),
14
+ ...(request.summary?.to ? { to: request.summary.to } : {}),
15
+ ...(request.summary?.moveDate ? { moveDate: request.summary.moveDate } : {}),
16
+ ...(request.summary?.size ? { size: request.summary.size } : {}),
17
+ },
18
+ matches: (request.matches ?? []).map((match) => ({
19
+ workspaceId: match.workspaceId,
20
+ name: match.name,
21
+ ...(match.category ? { category: match.category } : {}),
22
+ ...(match.logoUrl ? { logoUrl: match.logoUrl } : {}),
23
+ ...(match.coverageArea ? { coverageArea: match.coverageArea } : {}),
24
+ verified: match.verified === true,
25
+ ...(typeof match.rating === "number" ? { rating: match.rating } : {}),
26
+ ...(typeof match.reviewCount === "number" ? { reviewCount: match.reviewCount } : {}),
27
+ responded: match.responded === true,
28
+ })),
29
+ })),
30
+ };
31
+ }
32
+ export function createC1MLeadPortalProvider(resolveClient = getVisitorDashChannelClient) {
33
+ return {
34
+ async requestLink(email) {
35
+ try {
36
+ const client = await resolveClient();
37
+ await client.dash.leadPortal.requestLink({ email });
38
+ return { state: "requested" };
39
+ }
40
+ catch (error) {
41
+ // A 4xx is the door rejecting THIS input (malformed email). Everything
42
+ // else is transient; the observability seam has already logged it.
43
+ if (isChannelAPIError(error) && error.status >= 400 && error.status < 500) {
44
+ return { state: "invalid" };
45
+ }
46
+ return { state: "error" };
47
+ }
48
+ },
49
+ async view(token) {
50
+ try {
51
+ const client = await resolveClient();
52
+ const view = await client.dash.leadPortal.view(token);
53
+ return { state: "ok", view: normalizeView(view) };
54
+ }
55
+ catch (error) {
56
+ // The door answers 401 invalid_link for an expired, malformed or
57
+ // revoked token. That is a visitor-fixable state: request a new link.
58
+ if (isChannelAPIError(error) && error.status === 401) {
59
+ return { state: "invalid_link" };
60
+ }
61
+ return { state: "unavailable" };
62
+ }
63
+ },
64
+ };
65
+ }
@@ -0,0 +1,13 @@
1
+ import "server-only";
2
+ import type { LeadsProvider } from "../../ports/leads.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ /**
7
+ * `leadSource` is recorded on every capture so a dash's CRM can tell its
8
+ * client sites apart. It is a REQUIRED argument rather than a module-level
9
+ * import: this package has no site.defaults.json to read, and a wrong or
10
+ * defaulted source silently mis-attributes real leads.
11
+ */
12
+ export declare function createC1MLeadsProvider(leadSource: string, resolveClient?: ChannelClientResolver): LeadsProvider;
13
+ export {};
@@ -0,0 +1,38 @@
1
+ import "server-only";
2
+ import { isChannelAPIError } from "@c1m-public/channel-client";
3
+ import { getChannelClient, getVisitorDashChannelClient } from "../client.js";
4
+ /**
5
+ * `leadSource` is recorded on every capture so a dash's CRM can tell its
6
+ * client sites apart. It is a REQUIRED argument rather than a module-level
7
+ * import: this package has no site.defaults.json to read, and a wrong or
8
+ * defaulted source silently mis-attributes real leads.
9
+ */
10
+ export function createC1MLeadsProvider(leadSource, resolveClient = getVisitorDashChannelClient) {
11
+ const source = leadSource.trim();
12
+ if (!source)
13
+ throw new Error("createC1MLeadsProvider requires a non-empty leadSource");
14
+ return {
15
+ async create(input, attemptId) {
16
+ try {
17
+ const client = await resolveClient();
18
+ const { lead } = await client.dash.leads.create({
19
+ email: input.email,
20
+ ...(input.name ? { name: input.name } : {}),
21
+ ...(input.phone ? { phone: input.phone } : {}),
22
+ ...(input.message ? { message: input.message } : {}),
23
+ source,
24
+ }, { idempotencyKey: attemptId });
25
+ return { state: "created", id: lead.id };
26
+ }
27
+ catch (error) {
28
+ // A 4xx is the door rejecting THIS submission (bad email, missing
29
+ // field) — the visitor can fix it. Everything else is transient; the
30
+ // observability seam has already logged it.
31
+ if (isChannelAPIError(error) && error.status >= 400 && error.status < 500) {
32
+ return { state: "invalid" };
33
+ }
34
+ return { state: "error" };
35
+ }
36
+ },
37
+ };
38
+ }
@@ -0,0 +1,7 @@
1
+ import "server-only";
2
+ import type { PlansProvider } from "../../ports/plans.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ export declare function createC1MPlansProvider(resolveClient?: ChannelClientResolver): PlansProvider;
7
+ export {};
@@ -0,0 +1,17 @@
1
+ import "server-only";
2
+ import { getChannelClient, getVisitorDashChannelClient } from "../client.js";
3
+ import { normalizePlanCatalog } from "../normalize.js";
4
+ export function createC1MPlansProvider(resolveClient = getVisitorDashChannelClient) {
5
+ return {
6
+ async loadCatalog() {
7
+ const client = await resolveClient();
8
+ const [response, geo] = await Promise.all([
9
+ client.dash.plans(),
10
+ client.geo.detect().catch(() => undefined),
11
+ ]);
12
+ const catalog = normalizePlanCatalog(response);
13
+ const detectedRegion = geo ? catalog.regions.find((region) => region.code.toUpperCase() === geo.region.code.toUpperCase()) : undefined;
14
+ return detectedRegion ? { ...catalog, defaultRegion: detectedRegion.code } : catalog;
15
+ },
16
+ };
17
+ }
@@ -0,0 +1,15 @@
1
+ import "server-only";
2
+ import type { ProfileProvider } from "../../ports/profile.js";
3
+ import { getChannelClient } from "../client.js";
4
+ type ChannelClient = ReturnType<typeof getChannelClient>;
5
+ type ChannelClientResolver = () => ChannelClient | Promise<ChannelClient>;
6
+ /**
7
+ * `resolveBootstrap` is required: the profile name falls back to the dash
8
+ * bootstrap, and in a site that layer may be wrapped (local preview), so the
9
+ * package must be handed the site's composed loader rather than reaching for
10
+ * one itself.
11
+ */
12
+ export declare function createC1MProfileProvider(resolveBootstrap: () => Promise<{
13
+ name: string;
14
+ }>, resolveClient?: ChannelClientResolver): ProfileProvider;
15
+ export {};