@getuserfeedback/flow 0.0.0-stage → 0.1.0-beta.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.
- package/LICENSE +21 -0
- package/README.md +27 -2
- package/dist/index.d.ts +192 -0
- package/dist/index.js +1 -0
- package/package.json +25 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 getuserfeedback.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# getuserfeedback.com Flow SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@getuserfeedback/flow` runs a compiled getuserfeedback.com questionnaire in your own interface. It manages answers, branching, validation, and submission state. Your app renders the questionnaire and sends the response through the transport you choose.
|
|
4
|
+
|
|
5
|
+
The SDK is in beta and its API may change between prereleases. Install it with:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @getuserfeedback/flow@beta
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createFlow, type FlowDefinition } from "@getuserfeedback/flow";
|
|
13
|
+
|
|
14
|
+
export function createQuestionnaire(
|
|
15
|
+
definition: FlowDefinition,
|
|
16
|
+
submit: Parameters<typeof createFlow>[0]["onSubmit"],
|
|
17
|
+
) {
|
|
18
|
+
return createFlow({ flow: definition, onSubmit: submit });
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Pass a compiled `questionnaire-program@1` definition. Use `getSnapshot()` to read the current page and form state, and `subscribe()` to update your UI when it changes. Forward input changes to `setValue()`, the back action to `back()`, and the primary action to `continue()`. The SDK calls `onSubmit` with answers from the eligible path when the response is ready.
|
|
23
|
+
|
|
24
|
+
The package requires no UI framework and makes no network requests on its own.
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
type ValidationRule = {
|
|
2
|
+
type: "string" | "number" | "enum" | "minLength" | "maxLength" | "minimum" | "maximum" | "pattern" | "format";
|
|
3
|
+
params?: {
|
|
4
|
+
minLength?: number;
|
|
5
|
+
maxLength?: number;
|
|
6
|
+
minimum?: number;
|
|
7
|
+
maximum?: number;
|
|
8
|
+
pattern?: string;
|
|
9
|
+
format?: string;
|
|
10
|
+
enum?: string[];
|
|
11
|
+
};
|
|
12
|
+
message: string;
|
|
13
|
+
};
|
|
14
|
+
type Question = {
|
|
15
|
+
_key: string;
|
|
16
|
+
label: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
validation: {
|
|
20
|
+
required: boolean;
|
|
21
|
+
requiredMessage?: string;
|
|
22
|
+
rules?: ValidationRule[];
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
type ChoiceOption = {
|
|
26
|
+
value: string;
|
|
27
|
+
label: string;
|
|
28
|
+
};
|
|
29
|
+
type RatingIcon = "star" | "heart" | "thumb" | "smile";
|
|
30
|
+
type TextBlock = {
|
|
31
|
+
_type: "block";
|
|
32
|
+
_key: string;
|
|
33
|
+
children: {
|
|
34
|
+
_type: "span";
|
|
35
|
+
_key: string;
|
|
36
|
+
text: string;
|
|
37
|
+
marks?: ("strong" | "em" | "strike-through" | "code" | "highlight" | "link")[];
|
|
38
|
+
}[];
|
|
39
|
+
markDefs?: {
|
|
40
|
+
_type: "link";
|
|
41
|
+
_key: string;
|
|
42
|
+
href: string;
|
|
43
|
+
blank?: boolean;
|
|
44
|
+
rel?: ("nofollow" | "noopener" | "noreferrer")[];
|
|
45
|
+
}[];
|
|
46
|
+
style?: "normal" | "h1" | "h2" | "blockquote";
|
|
47
|
+
};
|
|
48
|
+
type FlowBlock = {
|
|
49
|
+
_type: "statement";
|
|
50
|
+
_key: string;
|
|
51
|
+
content: TextBlock[];
|
|
52
|
+
} | (Question & ({
|
|
53
|
+
_type: "shortTextInput";
|
|
54
|
+
} | {
|
|
55
|
+
_type: "longTextInput";
|
|
56
|
+
} | {
|
|
57
|
+
_type: "emailInput";
|
|
58
|
+
} | {
|
|
59
|
+
_type: "urlInput";
|
|
60
|
+
} | {
|
|
61
|
+
_type: "npsInput";
|
|
62
|
+
} | {
|
|
63
|
+
_type: "multipleChoiceInput";
|
|
64
|
+
allowMultipleAnswers?: boolean;
|
|
65
|
+
allowOther?: boolean;
|
|
66
|
+
options: ChoiceOption[];
|
|
67
|
+
} | {
|
|
68
|
+
_type: "ratingInput";
|
|
69
|
+
max: number;
|
|
70
|
+
icon: RatingIcon;
|
|
71
|
+
} | {
|
|
72
|
+
_type: "scaleInput";
|
|
73
|
+
options: ChoiceOption[];
|
|
74
|
+
} | {
|
|
75
|
+
_type: "scalarInput";
|
|
76
|
+
model: {
|
|
77
|
+
primitive: "number";
|
|
78
|
+
scoreSource: "ordered-options";
|
|
79
|
+
scoreStart: number;
|
|
80
|
+
options: ChoiceOption[];
|
|
81
|
+
};
|
|
82
|
+
ui: {
|
|
83
|
+
variant: "rating" | "nps" | "scale";
|
|
84
|
+
icon?: RatingIcon;
|
|
85
|
+
};
|
|
86
|
+
}));
|
|
87
|
+
type ScalarValue = string | number | boolean;
|
|
88
|
+
type BranchExpression = {
|
|
89
|
+
kind: "comparison";
|
|
90
|
+
left: {
|
|
91
|
+
kind: "answer";
|
|
92
|
+
blockId: string;
|
|
93
|
+
};
|
|
94
|
+
op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte" | "isEmpty" | "isNotEmpty";
|
|
95
|
+
right?: ScalarValue | ScalarValue[];
|
|
96
|
+
} | {
|
|
97
|
+
kind: "group";
|
|
98
|
+
op: "and" | "or";
|
|
99
|
+
conditions: BranchExpression[];
|
|
100
|
+
} | {
|
|
101
|
+
kind: "not";
|
|
102
|
+
condition: BranchExpression;
|
|
103
|
+
};
|
|
104
|
+
type Instruction = {
|
|
105
|
+
kind: "page";
|
|
106
|
+
pageId: string;
|
|
107
|
+
blockIds: string[];
|
|
108
|
+
} | {
|
|
109
|
+
kind: "group";
|
|
110
|
+
when: BranchExpression;
|
|
111
|
+
body: Instruction[];
|
|
112
|
+
} | {
|
|
113
|
+
kind: "end";
|
|
114
|
+
outcomeKey: string;
|
|
115
|
+
};
|
|
116
|
+
type FlowEnding = Readonly<{
|
|
117
|
+
_type: "end";
|
|
118
|
+
_key: string;
|
|
119
|
+
content: readonly TextBlock[];
|
|
120
|
+
exit: Readonly<{
|
|
121
|
+
kind: "close";
|
|
122
|
+
}> | Readonly<{
|
|
123
|
+
kind: "link";
|
|
124
|
+
_key: string;
|
|
125
|
+
text: string;
|
|
126
|
+
url: string;
|
|
127
|
+
target?: "self" | "blank";
|
|
128
|
+
}>;
|
|
129
|
+
}>;
|
|
130
|
+
type FlowDefinition = {
|
|
131
|
+
pages: {
|
|
132
|
+
_type: "page";
|
|
133
|
+
_key: string;
|
|
134
|
+
title?: string;
|
|
135
|
+
description?: string;
|
|
136
|
+
blocks: FlowBlock[];
|
|
137
|
+
}[];
|
|
138
|
+
navigation: {
|
|
139
|
+
version: "questionnaire-program@1";
|
|
140
|
+
root: Instruction[];
|
|
141
|
+
};
|
|
142
|
+
postSubmit?: {
|
|
143
|
+
outcomeSelector: {
|
|
144
|
+
kind: "questionnaire";
|
|
145
|
+
};
|
|
146
|
+
outcomes: readonly FlowEnding[];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
type AnswerValue = string | readonly string[] | null;
|
|
151
|
+
type FlowSubmitResult = {
|
|
152
|
+
success: true;
|
|
153
|
+
selectedOutcomeKey?: string | null;
|
|
154
|
+
} | {
|
|
155
|
+
success: false;
|
|
156
|
+
errors: {
|
|
157
|
+
fieldErrors?: Record<string, string[]>;
|
|
158
|
+
formErrors?: string[];
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
type FlowOptions = {
|
|
162
|
+
flow: FlowDefinition;
|
|
163
|
+
initialValues?: Readonly<Record<string, AnswerValue>>;
|
|
164
|
+
onSubmit: (answers: Readonly<Record<string, AnswerValue>>) => FlowSubmitResult | Promise<FlowSubmitResult>;
|
|
165
|
+
};
|
|
166
|
+
type FlowSnapshot = Readonly<{
|
|
167
|
+
values: Readonly<Partial<Record<string, AnswerValue>>>;
|
|
168
|
+
pageId: string;
|
|
169
|
+
hasPrevious: boolean;
|
|
170
|
+
primaryAction: "next" | "submit" | null;
|
|
171
|
+
isDirty: boolean;
|
|
172
|
+
isSubmitting: boolean;
|
|
173
|
+
fieldErrors: Readonly<Partial<Record<string, string>>>;
|
|
174
|
+
formErrors: readonly string[];
|
|
175
|
+
submission: Readonly<{
|
|
176
|
+
success: true;
|
|
177
|
+
selectedOutcomeKey?: string | null;
|
|
178
|
+
/** The authored ending selected by submission. Show a generic acknowledgment when null. */
|
|
179
|
+
ending: FlowEnding | null;
|
|
180
|
+
}> | null;
|
|
181
|
+
}>;
|
|
182
|
+
type Flow = {
|
|
183
|
+
getSnapshot: () => FlowSnapshot;
|
|
184
|
+
subscribe: (listener: () => void) => () => void;
|
|
185
|
+
setValue: (key: string, value: AnswerValue) => void;
|
|
186
|
+
back: () => void;
|
|
187
|
+
continue: () => Promise<void>;
|
|
188
|
+
};
|
|
189
|
+
declare function createFlow(options: FlowOptions): Flow;
|
|
190
|
+
|
|
191
|
+
export { createFlow };
|
|
192
|
+
export type { AnswerValue, Flow, FlowDefinition, FlowEnding, FlowOptions, FlowSnapshot, FlowSubmitResult };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function H(e,r){return e==null||e===""||(r==="array-string"||r==="array-enum")&&Array.isArray(e)&&e.length===0}function Z(e,r){let t=[],n=[];if(!r)return{fieldErrors:t,globalErrors:n};for(let i of r.formErrors??[]){let o=K([i]);if(o)n.push(o)}for(let[i,o]of Object.entries(r.fieldErrors??{})){let s=K(o);if(!s)continue;let a=ke(i,e);if(a){t.push({path:a,message:s});continue}n.push(s)}return{fieldErrors:t,globalErrors:n}}function J(e){return Object.fromEntries(e.filter((r)=>r.blockType!=="statement").map((r)=>[r.key,r.kind==="array-string"||r.kind==="array-enum"?[]:null]))}function R(e,r){let t={};for(let[n,i]of Object.entries(e)){if(i===void 0)continue;let o=Oe(xe(i),r?.get(n));if(o!==null||i===null)t[n]=o}return t}function ke(e,r){let t=e.trim();if(!t)return null;return r.has(t)?t:null}function K(e){if(!e||e.length===0)return null;let t=(Array.isArray(e)?e:[e]).map((n)=>typeof n==="string"?n.trim():"").filter((n)=>n.length>0);return t.length===0?null:t.join(", ")}function xe(e){if(e===null||e===void 0)return null;if(typeof e==="string")return e;if(Ie(e))return e;return null}function Ie(e){return Array.isArray(e)&&e.every((r)=>typeof r==="string")}function Se(e,r){return e.flatMap((t)=>{if(r.has(t))return[t];let n=t.trim();if(n.length===0)return[];return[n]})}function Q(e){let r=new Map;for(let t of e)if(t.kind==="array-string"&&t.otherOptionValues)r.set(t.key,new Set(t.otherOptionValues));return r}function Oe(e,r){if(!r||!Array.isArray(e))return e;return Se(e,r)}function M(e){let r=[],t=(n)=>{if(n._type==="repeatBlock"&&"templateBlocks"in n){for(let i of n.templateBlocks)t(i);return}r.push({key:n._key,blockType:n._type,validation:"validation"in n?n.validation:void 0,...Ve(n)})};for(let n of e)t(n);return{fields:r}}function q(e){let r=e.options.map((t)=>t.value);if(e.allowOther)return{kind:e.allowMultipleAnswers?"array-string":"string",allowsCustomValues:!0,otherOptionValues:r};return{kind:e.allowMultipleAnswers?"array-enum":"enum",allowsCustomValues:!1,enumValues:F(r)}}function Ve(e){switch(e._type){case"multipleChoiceInput":return q(e);case"ratingInput":return{kind:"string",enumValues:F(Array.from({length:e.max},(r,t)=>String(t+1))),allowsCustomValues:!1};case"npsInput":return{kind:"enum",enumValues:F(Array.from({length:11},(r,t)=>String(t))),allowsCustomValues:!1};case"scalarInput":case"scaleInput":return{kind:"enum",enumValues:F((e._type==="scalarInput"?e.model.options:e.options).map((r)=>r.value)),allowsCustomValues:!1};default:return{kind:"string",allowsCustomValues:!1}}}function F(e){return e}function G(e,r=M(e)){let t=new Map;for(let n of r.fields){if(n.blockType==="statement")continue;t.set(n.key,n)}return t}function X(e,r){let t={};for(let[n,i]of Object.entries(e)){let o=r.get(n);if(!o)continue;let s=Ee(i,o);if(s!==void 0)t[n]=s}return t}function Ee(e,r){if(e===null)return null;let t=new Set(r.enumValues??[]);switch(r.kind){case"array-enum":return Array.isArray(e)?e.filter((n)=>typeof n==="string"&&t.has(n)):void 0;case"array-string":return Array.isArray(e)&&e.every((n)=>typeof n==="string")?e:void 0;case"enum":return typeof e==="string"&&t.has(e)?e:void 0;default:return typeof e==="string"&&(r.blockType!=="ratingInput"||t.has(e))?e:void 0}}function Be(e,r,t){let n=R(Ce(e)?e:{},t);return X(n,r)}var Ce=(e)=>typeof e==="object"&&e!==null;function Y(e,r){let t=M(e),n=G(e,t),i=new Set(n.keys()),o=Q(t.fields),s=J(t.fields),a=Be(r,n,o),m={...s,...a};return{answerContracts:n,answerKeys:i,fieldDefaults:s,nonBlankArrayAnswerOptionSets:o,initialAnswers:m}}function ee(e){return e._type!=="statement"&&e._type!=="repeatBlock"}var B="__branch_other__";var _e=(e)=>{if(typeof e==="object"&&e!==null&&"value"in e)return e.value;return e},ne=(e)=>{let r=_e(e);if(Array.isArray(r))return r.map((t)=>ne(t));return r},se=(e)=>e===void 0||e===null||typeof e==="string"&&e.trim()===""||Array.isArray(e)&&e.length===0;var g=(e)=>{if(typeof e==="number"&&Number.isFinite(e))return e;if(typeof e==="string"){let r=e.trim();if(r==="")return null;let t=Number(r);return Number.isFinite(t)?t:null}return null},C=(e,r)=>{if(e===r)return!0;let t=g(e),n=g(r);if(t===null||n===null)return!1;return t===n},re=(e,r)=>{for(let t of e)if(C(t,r))return!0;return!1},Re=(e)=>{let r=new Map;if(!e)return r;for(let t of e){if(t._type!=="multipleChoiceInput")continue;let{otherOptionValues:n}=q(t);if(n)r.set(t._key,new Set(n))}return r},Fe=(e)=>typeof e==="string"||typeof e==="number"||typeof e==="boolean",te=(e,r)=>{if(!Fe(e)||!r)return!1;return!r.has(String(e))},P=(e,r)=>{if(Array.isArray(e))return e.some((t)=>te(t,r));return te(e,r)},oe=(e,r,t,n,i)=>{let o=n?i?.get(n):void 0;if(e==="isEmpty")return se(r);if(e==="isNotEmpty")return!oe("isEmpty",r,t);if(e==="in"||e==="nin"){if(!Array.isArray(t))return!1;let s=t.some((l)=>l===B),a=t.filter((l)=>l!==B),m=Array.isArray(r)?r.some((l)=>re(a,l)):re(a,r),f=s&&P(r,o),b=m||f;return e==="in"?b:!b}if(e==="eq"){if(t===B)return P(r,o);if(Array.isArray(r))return r.some((s)=>C(s,t));return C(r,t)}if(e==="neq"){if(t===B)return!P(r,o);if(Array.isArray(r))return r.every((s)=>!C(s,t));return!C(r,t)}if(e==="gt"){let s=g(r),a=g(t);return s!==null&&a!==null&&s>a}if(e==="gte"){let s=g(r),a=g(t);return s!==null&&a!==null&&s>=a}if(e==="lt"){let s=g(r),a=g(t);return s!==null&&a!==null&&s<a}if(e==="lte"){let s=g(r),a=g(t);return s!==null&&a!==null&&s<=a}return!1};function ie(e){let r=Re(e);return(t,n,i)=>v(t,n,r,i)}function v(e,r,t,n){if(e.kind==="comparison"){let o=e.left.blockId;if(n&&!n.has(o))return"unknown";let s=ne(Object.prototype.hasOwnProperty.call(r,o)?r[o]:void 0);if(se(s)&&e.op!=="isEmpty"&&e.op!=="isNotEmpty")return"unknown";return oe(e.op,s,e.right,o,t)}if(e.kind==="not"){let o=v(e.condition,r,t,n);return o==="unknown"?"unknown":!o}let i=!1;for(let o of e.conditions){let s=v(o,r,t,n);if(e.op==="and"&&s===!1)return!1;if(e.op==="or"&&s===!0)return!0;i||=s==="unknown"}return i?"unknown":e.op==="and"}function ae(e,r){let t=r.flatMap((o)=>o.blocks),n=ie(t),i=new Set(t.filter(ee).map((o)=>o._key));return(o)=>{let s=Object.create(null),a={pageIds:[],eligibleBlockIds:[],eligibleAnswers:s,outcomeKey:""},m=new Set,f=(b)=>{for(let l of b){if(l.kind==="end")return a.outcomeKey=l.outcomeKey,!0;if(l.kind==="group"){if(n(l.when,s,m)===!0&&f(l.body))return!0}else{a.pageIds.push(l.pageId);for(let d of l.blockIds)if(a.eligibleBlockIds.push(d),i.has(d)){if(m.add(d),Object.prototype.hasOwnProperty.call(o,d))s[d]=o[d]}}}return!1};return f(e.root),a}}function ue(e){return e._type==="repeatBlock"}function le(e,r,t){if(r.type!=="enum"||!r.params?.enum)return e;if(t)return e;if(e==="string"||e==="enum")return"enum";return"array-enum"}class ce extends Error{ruleIndex;constructor(e){super("Invalid regular expression");this.ruleIndex=e}}function*fe(e){let r=e.kind;for(let[t,n]of(e.validation?.rules??[]).entries()){let i;if(r==="string"&&(n.type==="string"||n.type==="pattern")&&n.params?.pattern)try{i=new RegExp(n.params.pattern)}catch(s){if(!(s instanceof SyntaxError))throw s;throw new ce(t)}let o=le(r,n,e.allowsCustomValues);yield{rule:n,currentKind:r,nextKind:o,compiledPattern:i},r=o}}function me(e,r){let t=Ne(e.kind,e.enumValues,r),{kind:n,validation:i}=e;for(let o of fe(e))t=ze(t,o,r),n=o.nextKind;if(t=Me(t,n,i,r),i?.required)return Te(t,n,e.allowsCustomValues,i,r);return r.wrapOptional(t,(o)=>H(o,n))}function Me(e,r,t,n){if(r!=="array-string")return e;return n.refine(e,(i)=>{if(!Array.isArray(i))return!1;return i.every((o)=>typeof o==="string"&&o.trim().length>0)},t?.required?t.requiredMessage??"At least one selection is required":"Selections cannot be blank")}function Ne(e,r,t){switch(e){case"enum":return t.enum(pe(r));case"array-enum":return t.array(t.enum(pe(r)));case"array-string":return t.array(t.string());default:return t.string()}}function pe(e){if(!e)throw Error("Enum values are required for enum schema generation.");return e}function ze(e,{rule:r,currentKind:t,nextKind:n,compiledPattern:i},o){let s=e;switch(r.type){case"string":{if(t!=="string")break;if(r.params?.minLength!==void 0)s=o.stringMin(s,r.params.minLength,r.message);if(r.params?.maxLength!==void 0)s=o.stringMax(s,r.params.maxLength,r.message);if(i)s=o.stringRegex(s,i,r.message);break}case"minLength":{if(r.params?.minLength===void 0)break;if(t==="string")s=o.stringMin(s,r.params.minLength,r.message);else if(t==="array-string"||t==="array-enum")s=o.arrayMin(s,r.params.minLength,r.message);break}case"maxLength":{if(r.params?.maxLength===void 0)break;if(t==="string")s=o.stringMax(s,r.params.maxLength,r.message);else if(t==="array-string"||t==="array-enum")s=o.arrayMax(s,r.params.maxLength,r.message);break}case"pattern":{if(i)s=o.stringRegex(s,i,r.message);break}case"format":{if(r.params?.format&&t==="string")switch(r.params.format){case"email":s=o.stringEmail(s,r.message);break;case"url":s=o.stringUrl(s,r.message);break;default:break}break}case"enum":{if(!r.params?.enum)break;let a=r.params.enum;if(n==="enum")s=o.enum(a);else if(n==="array-enum")s=o.array(o.enum(a));break}}return s}function Te(e,r,t,n,i){let o=n.requiredMessage;if(r==="string"){if(t)return i.refine(e,(s)=>typeof s==="string"&&s.trim().length>0,o??"This field is required");return i.stringMin(e,1,o??"This field is required")}if(r==="enum"){if(o)return i.refine(e,()=>!0,o);return e}if(r==="array-string"||r==="array-enum")return i.arrayMin(e,1,o??"At least one selection is required");return e}var je=/^(?:[A-Za-z0-9_'+-]+\.)*[A-Za-z0-9_'+-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/,qe=/^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/i;function Pe(e){if(typeof e!=="string"||!e.trim())return!1;let r=e.startsWith("http://")||e.startsWith("https://")?e:`https://${e}`;try{let t=new URL(r.trim());return(t.protocol==="http:"||t.protocol==="https:")&&qe.test(t.hostname)}catch{return!1}}function I(e,r,t){return(n)=>e(n)??(r(n)?void 0:t||"Invalid input")}function N(e,r,t){return I(e,(n)=>(typeof n==="string"||Array.isArray(n))&&r(n.length),t)}var ve={string:()=>(e)=>typeof e==="string"?void 0:"Invalid input: expected string",enum:(e)=>{let r=new Set(e);return(t)=>typeof t==="string"&&r.has(t)?void 0:"Select an available option"},array:(e)=>(r)=>{if(!Array.isArray(r))return"Invalid input: expected array";for(let t of r){let n=e(t);if(n!==void 0)return n}},wrapOptional:(e,r)=>(t)=>r(t)?void 0:e(t),stringMin:(e,r,t)=>N(e,(n)=>n>=r,t||`Enter at least ${r} characters`),stringMax:(e,r,t)=>N(e,(n)=>n<=r,t||`Enter at most ${r} characters`),arrayMin:(e,r,t)=>N(e,(n)=>n>=r,t||`Select at least ${r} options`),arrayMax:(e,r,t)=>N(e,(n)=>n<=r,t||`Select at most ${r} options`),stringRegex:(e,r,t)=>I(e,(n)=>typeof n==="string"&&r.test(n),t||"Invalid format"),stringEmail:(e,r)=>I(e,(t)=>typeof t==="string"&&je.test(t),r||"Invalid email address"),stringUrl:(e,r)=>I(e,Pe,r||"Invalid URL"),refine:I};function de(){let e=new WeakMap;return(r,t)=>{let n=Object.create(null);for(let i of r){let o=e.get(i);if(!o){if(o=me(i,ve),i.blockType==="ratingInput"){let m=new Set(i.enumValues);o=I(o,(f)=>f==null||f===""||typeof f==="string"&&m.has(f),"Select an available option")}e.set(i,o)}let{key:s}=i,a=o(Object.hasOwn(t,s)?t[s]:void 0);if(a!==void 0)n[s]=a}return n}}function hr(e){let{pages:r,navigation:t,postSubmit:n}=e.flow;if(t?.version!=="questionnaire-program@1")throw Error("Flow requires questionnaire-program@1.");if(r.some(({blocks:u})=>u.some(ue)))throw Error("Questionnaire programs do not support repeat blocks.");let{initialAnswers:i,answerKeys:o,answerContracts:s,nonBlankArrayAnswerOptionSets:a}=Y(r.flatMap((u)=>u.blocks),e.initialValues),m=(u)=>u.flatMap((c)=>{let p=s.get(c);return p?[p]:[]}),f=new Map(r.map(({_key:u,blocks:c})=>[u,m(c.map((p)=>p._key))])),b=ae(t,r),l=de(),d=Object.freeze(Object.fromEntries(Object.entries(i).map(([u,c])=>[u,ye(c)]))),w=d,D=!1;function L(){return b(R(w,a))}let S=L(),O=0,A={},k=[],x=!1,V=null,z=new Set;function U(){let u=S.pageIds[O]??"",c=O+1<S.pageIds.length;return Object.freeze({values:w,pageId:u,hasPrevious:!V&&O>0,primaryAction:V?null:c?"next":"submit",isDirty:D,isSubmitting:x,fieldErrors:Object.freeze({...A}),formErrors:Object.freeze([...k]),submission:V})}let E=U();function _(){E=U();for(let u of z)try{u()}catch(c){console.error("Flow subscriber failed",c)}}function he(u,c){if(x||V||!f.get(E.pageId)?.some((p)=>p.key===u)||ge(w[u],c))return;w=Object.freeze({...w,[u]:ye(c)}),D=Object.entries(w).some(([p,T])=>!ge(d[p],T)),S=L(),delete A[u],k=[],_()}function we(){if(x||!E.hasPrevious)return;O-=1,A={},k=[],_()}async function Ae(){let{primaryAction:u,pageId:c}=E;if(x||u===null)return;let p=u==="next";A={},k=[];try{let T=p?f.get(c)??[]:m(S.eligibleBlockIds);if(A=l(T,w),Object.keys(A).length>0)return;if(p){O+=1;return}let be=Object.fromEntries(Object.entries(S.eligibleAnswers).filter(([h,y])=>!Array.isArray(y)||y.length>0||s.get(h)?.validation?.required).map(([h,y])=>[h,Array.isArray(y)?[...y]:y]));x=!0,_();let j=await e.onSubmit(be);if(j.success){let{selectedOutcomeKey:h}=j;V=Object.freeze({success:!0,...h===void 0?{}:{selectedOutcomeKey:h},ending:n?.outcomes.find(({_key:y})=>y===h)??null});return}let W=Z(o,j.errors);A=Object.fromEntries(W.fieldErrors.map(({path:h,message:y})=>[h,y])),k=W.globalErrors}catch{k=[p?"Could not validate the answers. Try again.":"Could not submit the response. Try again."]}finally{x=!1,_()}}return{getSnapshot:()=>E,subscribe:(u)=>(z.add(u),()=>{z.delete(u)}),setValue:he,back:we,continue:Ae}}function ye(e){return e===null||typeof e==="string"?e:Object.freeze([...e])}function ge(e,r){if(e===r)return!0;return Array.isArray(e)&&Array.isArray(r)&&e.length===r.length&&e.every((t,n)=>t===r[n])}export{hr as createFlow};
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getuserfeedback/flow",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Headless runtime for getuserfeedback.com questionnaires.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/getuserfeedback/getuserfeedback.git",
|
|
9
|
+
"directory": "packages/flow"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"tag": "beta"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/index.js",
|
|
25
|
+
"dist/index.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|