@getuserfeedback/protocol 0.6.1 → 0.7.2

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 (48) hide show
  1. package/dist/app-event.d.ts +659 -17
  2. package/dist/app-event.d.ts.map +1 -1
  3. package/dist/app-event.js +71 -8
  4. package/dist/flow-assignments.d.ts +4 -4
  5. package/dist/host/constants.d.ts +2 -2
  6. package/dist/host/constants.d.ts.map +1 -1
  7. package/dist/host/constants.js +3 -1
  8. package/dist/host/sdk-types.d.ts +73 -28
  9. package/dist/host/sdk-types.d.ts.map +1 -1
  10. package/dist/host/sdk.d.ts.map +1 -1
  11. package/dist/identity-type.d.ts +4 -2
  12. package/dist/identity-type.d.ts.map +1 -1
  13. package/dist/identity-type.js +4 -2
  14. package/dist/index.d.ts +2 -86
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -6
  17. package/dist/widget-commands.d.ts +102 -3
  18. package/dist/widget-commands.d.ts.map +1 -1
  19. package/dist/widget-commands.js +5 -0
  20. package/dist/widget-config.d.ts +5 -5
  21. package/package.json +22 -2
  22. package/src/app-event.ts +211 -0
  23. package/src/client-meta.ts +25 -0
  24. package/src/defaults.ts +4 -0
  25. package/src/errors.ts +58 -0
  26. package/src/flow-assignments.test.ts +63 -0
  27. package/src/flow-assignments.ts +125 -0
  28. package/src/host/command-dispatch.ts +59 -0
  29. package/src/host/command-envelope.ts +41 -0
  30. package/src/host/command-settlement.ts +121 -0
  31. package/src/host/constants.ts +103 -0
  32. package/src/host/host-event-contract.ts +277 -0
  33. package/src/host/index.ts +13 -0
  34. package/src/host/lazy-handle-client.ts +207 -0
  35. package/src/host/request-id.ts +3 -0
  36. package/src/host/sdk-types.ts +506 -0
  37. package/src/host/sdk.ts +43 -0
  38. package/src/host/unique-id.ts +17 -0
  39. package/src/identity-type.ts +102 -0
  40. package/src/index.ts +136 -0
  41. package/src/protocol-root.test.ts +476 -0
  42. package/src/public-grant-scope.ts +25 -0
  43. package/src/runtime-endpoints.ts +69 -0
  44. package/src/scopes.ts +79 -0
  45. package/src/trpc-envelope.ts +9 -0
  46. package/src/version-resolution.ts +18 -0
  47. package/src/widget-commands.ts +152 -0
  48. package/src/widget-config.ts +157 -0
@@ -0,0 +1,43 @@
1
+ /**
2
+ * SDK-facing protocol surface: types (from sdk-types) and values only.
3
+ * Does not re-export schema modules so SDK dist stays zod-free.
4
+ */
5
+
6
+ export { createCommandDispatch } from "./command-dispatch.js";
7
+ export {
8
+ COMMAND_SETTLEMENT_TIMEOUT_CODE,
9
+ CommandSettlementError,
10
+ CommandSettlementTimeoutError,
11
+ toCommandSettlementError,
12
+ waitForCommandSettlement,
13
+ } from "./command-settlement.js";
14
+ export type {
15
+ FlowHandleFromSettlement,
16
+ FlowState,
17
+ } from "./host-event-contract.js";
18
+ export {
19
+ getFlowHandleFromSettlementDetail,
20
+ isFlowStateChangedDetail,
21
+ isHandleInvalidatedDetail,
22
+ isInstanceFlowStateChangedDetail,
23
+ } from "./host-event-contract.js";
24
+ export type {
25
+ AppEventCapability,
26
+ AppEventFlag,
27
+ AppEventFlagProvider,
28
+ AppEventFlagValue,
29
+ AppEventPayload,
30
+ CapabilitiesInput,
31
+ ClientMeta,
32
+ ClientOptions,
33
+ Command,
34
+ CommandEnvelope,
35
+ ConfigureOptions,
36
+ ContainerPolicy,
37
+ FlagsInput,
38
+ InitOptions,
39
+ PublicCommandPayload,
40
+ ResponseMetadataInput,
41
+ // Re-export name stays TelemetryEvent until the migration on TelemetryEvent in sdk-types completes.
42
+ TelemetryEvent,
43
+ } from "./sdk-types.js";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Generates a unique string ID. Uses crypto.randomUUID when available,
3
+ * otherwise falls back to a timestamp + random string.
4
+ *
5
+ * @param prefix - Optional prefix for the fallback format (e.g. "req", "err").
6
+ * Only affects fallback; UUIDs have no prefix.
7
+ */
8
+ export function createUniqueId(prefix?: string): string {
9
+ if (
10
+ typeof globalThis.crypto !== "undefined" &&
11
+ typeof globalThis.crypto.randomUUID === "function"
12
+ ) {
13
+ return globalThis.crypto.randomUUID();
14
+ }
15
+ const fallback = `${Date.now()}_${Math.random().toString(36).slice(2)}`;
16
+ return prefix ? `${prefix}_${fallback}` : fallback;
17
+ }
@@ -0,0 +1,102 @@
1
+ import * as z from "zod/mini";
2
+
3
+ export const IDENTITY_TYPE_VALUES = [
4
+ "anonymousId",
5
+ "userId",
6
+ "traits.email",
7
+ "traits.phone",
8
+ "context.device.id",
9
+ "context.device.advertisingId",
10
+ "context.device.token",
11
+ ] as const;
12
+
13
+ export type StandardIdentityTypeValue = (typeof IDENTITY_TYPE_VALUES)[number];
14
+ export type CustomIdentityTypeValue = `custom.${string}`;
15
+ export type IdentityTypeValue =
16
+ | StandardIdentityTypeValue
17
+ | CustomIdentityTypeValue;
18
+
19
+ const CUSTOM_IDENTITY_TYPE_PATTERN = /^custom\.[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
20
+
21
+ const IDENTITY_TYPE_ALIAS_SUGGESTIONS: Record<
22
+ string,
23
+ StandardIdentityTypeValue
24
+ > = {
25
+ advertising_id: "context.device.advertisingId",
26
+ anonymous_id: "anonymousId",
27
+ device_id: "context.device.id",
28
+ device_token: "context.device.token",
29
+ email: "traits.email",
30
+ phone: "traits.phone",
31
+ phone_number: "traits.phone",
32
+ user_id: "userId",
33
+ };
34
+
35
+ const COMMON_IDENTITY_TYPE_MAPPINGS = [
36
+ ["email", "traits.email"],
37
+ ["phone", "traits.phone"],
38
+ ["user_id", "userId"],
39
+ ] as const;
40
+
41
+ const normalizeIdentityTypeAliasKey = (value: string): string =>
42
+ value.trim().toLowerCase().replace(/-/g, "_");
43
+
44
+ export const isIdentityTypeValue = (
45
+ value: string,
46
+ ): value is IdentityTypeValue =>
47
+ IDENTITY_TYPE_VALUES.some((candidate) => candidate === value) ||
48
+ CUSTOM_IDENTITY_TYPE_PATTERN.test(value);
49
+
50
+ const parseIdentityTypeValue = (value: string): IdentityTypeValue | null =>
51
+ isIdentityTypeValue(value) ? value : null;
52
+
53
+ export const getIdentityTypeSuggestion = (
54
+ value: string,
55
+ ): IdentityTypeValue | null =>
56
+ IDENTITY_TYPE_ALIAS_SUGGESTIONS[normalizeIdentityTypeAliasKey(value)] ?? null;
57
+
58
+ export const buildIdentityTypeGuidanceMessage = (value: string): string => {
59
+ const normalizedValue = value.trim();
60
+ const displayValue =
61
+ normalizedValue.length > 0 ? `"${normalizedValue}"` : "that value";
62
+ const suggestion = getIdentityTypeSuggestion(value);
63
+
64
+ if (suggestion) {
65
+ return `Unsupported identity type ${displayValue}. Use "${suggestion}" instead.`;
66
+ }
67
+
68
+ const mappingExamples = COMMON_IDENTITY_TYPE_MAPPINGS.map(
69
+ ([from, to]) => `${from} -> ${to}`,
70
+ ).join(", ");
71
+
72
+ return `Unsupported identity type ${displayValue}. Use one of: ${IDENTITY_TYPE_VALUES.join(", ")}, or a custom identity type like "custom.shopifyCustomerId". Common mappings: ${mappingExamples}.`;
73
+ };
74
+
75
+ const identityTypeInputSchema = z.string().check(
76
+ z.trim(),
77
+ z.minLength(1, { error: "Identity type is required." }),
78
+ z.check<string>((ctx) => {
79
+ if (isIdentityTypeValue(ctx.value)) {
80
+ return;
81
+ }
82
+ ctx.issues.push({
83
+ code: "custom",
84
+ input: ctx.value,
85
+ message: buildIdentityTypeGuidanceMessage(ctx.value),
86
+ continue: false,
87
+ });
88
+ }),
89
+ );
90
+
91
+ export const appEventIdentityTypeSchema = z.pipe(
92
+ identityTypeInputSchema,
93
+ z.transform((value): IdentityTypeValue => {
94
+ const parsed = parseIdentityTypeValue(value);
95
+ if (parsed === null) {
96
+ throw new Error(
97
+ `Identity type should have been validated before transform: ${value}`,
98
+ );
99
+ }
100
+ return parsed;
101
+ }),
102
+ );
package/src/index.ts ADDED
@@ -0,0 +1,136 @@
1
+ export {
2
+ type AppEventCapability,
3
+ type AppEventFlag,
4
+ type AppEventJsonValue,
5
+ type AppEventPayload,
6
+ type AppEventTrackInput,
7
+ appEventBaseSchema,
8
+ appEventCapabilitySchema,
9
+ appEventContextSchema,
10
+ appEventCustomerTrackSchema,
11
+ appEventFlagProviderSchema,
12
+ appEventFlagSchema,
13
+ appEventFlowDismissedSchema,
14
+ appEventIdentitySchema,
15
+ appEventIdentityTypeSchema,
16
+ appEventPayloadSchema,
17
+ appEventSurveyViewedSchema,
18
+ appEventSystemBaseSchema,
19
+ appEventSystemTrackSchema,
20
+ appEventTrackSchema,
21
+ reservedSystemAppEventNames,
22
+ } from "./app-event.js";
23
+ export {
24
+ type ClientMeta,
25
+ clientMetaSchema,
26
+ } from "./client-meta.js";
27
+ export { DEFAULT_AUTO_DETECT_COLOR_SCHEME_ATTRS } from "./defaults.js";
28
+ export type { ErrorEvent, ProtocolErrorCode } from "./errors.js";
29
+ export {
30
+ ErrorCategory,
31
+ ErrorCategorySchema,
32
+ ErrorEventSchema,
33
+ ProtocolError,
34
+ } from "./errors.js";
35
+ export {
36
+ type ClaimPendingFlowAssignmentsRequest,
37
+ type ClaimPendingFlowAssignmentsResponse,
38
+ claimPendingFlowAssignmentsRequestSchema,
39
+ claimPendingFlowAssignmentsResponseSchema,
40
+ type FlowAssignmentRecipientRequest,
41
+ type FlowAssignmentRecord,
42
+ type FlowAssignmentStatus,
43
+ type FlowAssignmentTargetingContext,
44
+ flowAssignmentRecipientRequestSchema,
45
+ flowAssignmentRecordSchema,
46
+ flowAssignmentStatusSchema,
47
+ flowAssignmentTargetingContextSchema,
48
+ type ListPendingFlowAssignmentsResponse,
49
+ listPendingFlowAssignmentsResponseSchema,
50
+ nullableUpdateFlowAssignmentLifecycleResponseSchema,
51
+ type ReleaseFlowAssignmentClaimRequest,
52
+ type ReleaseFlowAssignmentClaimResponse,
53
+ releaseFlowAssignmentClaimRequestSchema,
54
+ releaseFlowAssignmentClaimResponseSchema,
55
+ type UpdateFlowAssignmentLifecycleRequest,
56
+ type UpdateFlowAssignmentLifecycleResponse,
57
+ updateFlowAssignmentLifecycleRequestSchema,
58
+ updateFlowAssignmentLifecycleResponseSchema,
59
+ } from "./flow-assignments.js";
60
+ export {
61
+ buildIdentityTypeGuidanceMessage,
62
+ type CustomIdentityTypeValue,
63
+ getIdentityTypeSuggestion,
64
+ IDENTITY_TYPE_VALUES,
65
+ type IdentityTypeValue,
66
+ isIdentityTypeValue,
67
+ type StandardIdentityTypeValue,
68
+ } from "./identity-type.js";
69
+ export {
70
+ isPublicGrantScope,
71
+ type PublicGrantScope,
72
+ publicGrantScopeIdSchema,
73
+ } from "./public-grant-scope.js";
74
+ export {
75
+ type RuntimeEndpoints,
76
+ resolveApiBaseUrl,
77
+ resolveCoreUrl,
78
+ resolveTrpcBaseUrl,
79
+ runtimeEndpointsSchema,
80
+ } from "./runtime-endpoints.js";
81
+ export {
82
+ getScopeMeta,
83
+ isScope,
84
+ listScopes,
85
+ SCOPE_IDS,
86
+ type Scope,
87
+ scopeIdSchema,
88
+ } from "./scopes.js";
89
+ export {
90
+ type TrpcSuccessResponse,
91
+ trpcSuccessResponseSchema,
92
+ } from "./trpc-envelope.js";
93
+ export {
94
+ type FlowVersionResolution,
95
+ flowVersionResolutionSchema,
96
+ type ThemeVersionResolution,
97
+ themeVersionResolutionSchema,
98
+ } from "./version-resolution.js";
99
+ export {
100
+ type PublicCommandPayload,
101
+ parseConfigureOptions,
102
+ parseInitOptions,
103
+ parsePublicCommand,
104
+ publicCommandPayloadSchema,
105
+ publicWidgetCommandKindSchema,
106
+ } from "./widget-commands.js";
107
+ export {
108
+ type AuthConfigInput,
109
+ type AuthJwtConfigInput,
110
+ authConfigInputSchema,
111
+ authJwtConfigInputSchema,
112
+ type ColorSchemeConfigInput,
113
+ type ConfigureOptions,
114
+ type ConsentConfigInput,
115
+ type CoreInstanceConfig,
116
+ type CoreInstanceConfigInput,
117
+ colorSchemeConfigInputSchema,
118
+ configureOptionsSchema,
119
+ consentConfigInputSchema,
120
+ coreInstanceConfigSchema,
121
+ type DebugConfigInput,
122
+ debugInputSchema,
123
+ type FlagsConfigInput,
124
+ flagsConfigInputSchema,
125
+ type HostCapabilitiesConfigInput,
126
+ hostCapabilitiesConfigInputSchema,
127
+ type IdentityConfigInput,
128
+ INSTANCE_CONFIG_UPDATE_KEYS,
129
+ type InitOptions,
130
+ type InstanceUpdateableConfig,
131
+ identityConfigInputSchema,
132
+ initOptionsSchema,
133
+ instanceUpdateableConfigSchema,
134
+ type TelemetryConfigInput,
135
+ telemetryConfigInputSchema,
136
+ } from "./widget-config.js";