@resconet/qp-bridge 0.0.1-alpha.5

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.
@@ -0,0 +1,157 @@
1
+ import { z } from 'zod';
2
+ export declare const qpBridgeMessageSchema: z.ZodObject<{
3
+ type: z.ZodLiteral<"qp-bridge">;
4
+ id: z.ZodOptional<z.ZodString>;
5
+ }, "strip", z.ZodTypeAny, {
6
+ type: "qp-bridge";
7
+ id?: string | undefined;
8
+ }, {
9
+ type: "qp-bridge";
10
+ id?: string | undefined;
11
+ }>;
12
+ export declare const onAnswerChangeMessageSchema: z.ZodObject<z.objectUtil.extendShape<{
13
+ type: z.ZodLiteral<"qp-bridge">;
14
+ id: z.ZodOptional<z.ZodString>;
15
+ }, {
16
+ status: z.ZodLiteral<"answerChanged">;
17
+ question: z.ZodString;
18
+ answer: z.ZodUnknown;
19
+ }>, "strip", z.ZodTypeAny, {
20
+ type: "qp-bridge";
21
+ status: "answerChanged";
22
+ question: string;
23
+ id?: string | undefined;
24
+ answer?: unknown;
25
+ }, {
26
+ type: "qp-bridge";
27
+ status: "answerChanged";
28
+ question: string;
29
+ id?: string | undefined;
30
+ answer?: unknown;
31
+ }>;
32
+ export declare const responseSuccessMessageSchema: z.ZodObject<z.objectUtil.extendShape<{
33
+ type: z.ZodLiteral<"qp-bridge">;
34
+ id: z.ZodOptional<z.ZodString>;
35
+ }, {
36
+ status: z.ZodLiteral<"success">;
37
+ }>, "strip", z.ZodTypeAny, {
38
+ type: "qp-bridge";
39
+ status: "success";
40
+ id?: string | undefined;
41
+ }, {
42
+ type: "qp-bridge";
43
+ status: "success";
44
+ id?: string | undefined;
45
+ }>;
46
+ export declare const responseErrorMessageSchema: z.ZodObject<z.objectUtil.extendShape<{
47
+ type: z.ZodLiteral<"qp-bridge">;
48
+ id: z.ZodOptional<z.ZodString>;
49
+ }, {
50
+ status: z.ZodLiteral<"error">;
51
+ message: z.ZodString;
52
+ }>, "strip", z.ZodTypeAny, {
53
+ type: "qp-bridge";
54
+ status: "error";
55
+ message: string;
56
+ id?: string | undefined;
57
+ }, {
58
+ type: "qp-bridge";
59
+ status: "error";
60
+ message: string;
61
+ id?: string | undefined;
62
+ }>;
63
+ export declare const questionSchema: z.ZodObject<{
64
+ label: z.ZodString;
65
+ name: z.ZodString;
66
+ }, "strip", z.ZodTypeAny, {
67
+ label: string;
68
+ name: string;
69
+ }, {
70
+ label: string;
71
+ name: string;
72
+ }>;
73
+ export declare const getQuestionRequestMessageSchema: z.ZodObject<z.objectUtil.extendShape<{
74
+ type: z.ZodLiteral<"qp-bridge">;
75
+ id: z.ZodOptional<z.ZodString>;
76
+ }, {
77
+ action: z.ZodLiteral<"getQuestion">;
78
+ question: z.ZodString;
79
+ }>, "strip", z.ZodTypeAny, {
80
+ type: "qp-bridge";
81
+ question: string;
82
+ action: "getQuestion";
83
+ id?: string | undefined;
84
+ }, {
85
+ type: "qp-bridge";
86
+ question: string;
87
+ action: "getQuestion";
88
+ id?: string | undefined;
89
+ }>;
90
+ export declare const getQuestionSuccessResponseMessageSchema: z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<{
91
+ type: z.ZodLiteral<"qp-bridge">;
92
+ id: z.ZodOptional<z.ZodString>;
93
+ }, {
94
+ status: z.ZodLiteral<"success">;
95
+ }>, {
96
+ question: z.ZodObject<{
97
+ label: z.ZodString;
98
+ name: z.ZodString;
99
+ }, "strip", z.ZodTypeAny, {
100
+ label: string;
101
+ name: string;
102
+ }, {
103
+ label: string;
104
+ name: string;
105
+ }>;
106
+ }>, "strip", z.ZodTypeAny, {
107
+ type: "qp-bridge";
108
+ status: "success";
109
+ question: {
110
+ label: string;
111
+ name: string;
112
+ };
113
+ id?: string | undefined;
114
+ }, {
115
+ type: "qp-bridge";
116
+ status: "success";
117
+ question: {
118
+ label: string;
119
+ name: string;
120
+ };
121
+ id?: string | undefined;
122
+ }>;
123
+ export declare const setAnswerRequestMessageSchema: z.ZodObject<z.objectUtil.extendShape<{
124
+ type: z.ZodLiteral<"qp-bridge">;
125
+ id: z.ZodOptional<z.ZodString>;
126
+ }, {
127
+ action: z.ZodLiteral<"setAnswer">;
128
+ question: z.ZodString;
129
+ answer: z.ZodUnknown;
130
+ }>, "strip", z.ZodTypeAny, {
131
+ type: "qp-bridge";
132
+ question: string;
133
+ action: "setAnswer";
134
+ id?: string | undefined;
135
+ answer?: unknown;
136
+ }, {
137
+ type: "qp-bridge";
138
+ question: string;
139
+ action: "setAnswer";
140
+ id?: string | undefined;
141
+ answer?: unknown;
142
+ }>;
143
+ export type QpBridgeMessage = z.infer<typeof qpBridgeMessageSchema>;
144
+ export type ResponseSuccessMessage = z.infer<typeof responseSuccessMessageSchema>;
145
+ export type ResponseErrorMessage = z.infer<typeof responseErrorMessageSchema>;
146
+ export type Question = z.infer<typeof questionSchema>;
147
+ export type OnAnswerChangeMessage = z.infer<typeof onAnswerChangeMessageSchema>;
148
+ export type GetQuestionSuccessResponseMessage = z.infer<typeof getQuestionSuccessResponseMessageSchema>;
149
+ export type SetAnswerRequestMessage = z.infer<typeof setAnswerRequestMessageSchema>;
150
+ export type GetQuestionRequestMessage = z.infer<typeof getQuestionRequestMessageSchema>;
151
+ export declare function isOnAnswerChangeMessage(data: unknown): data is OnAnswerChangeMessage;
152
+ export declare function isQpBridgeMessage(data: unknown): data is QpBridgeMessage;
153
+ export declare function isSuccessMessage(data: unknown): data is ResponseSuccessMessage;
154
+ export declare function isErrorMessage(data: unknown): data is ResponseErrorMessage;
155
+ export declare function isGetQuestionRequestMessage(data: unknown): data is GetQuestionRequestMessage;
156
+ export declare function isGetQuestionSuccessResponseMessage(data: unknown): data is GetQuestionSuccessResponseMessage;
157
+ export declare function isSetAnswerRequestMessage(data: unknown): data is SetAnswerRequestMessage;
@@ -0,0 +1,48 @@
1
+ import { Question } from './qp-bridge-types';
2
+ /**
3
+ * Sets the answer for a specific question in the questionnaire.
4
+ *
5
+ * @param {string} questionName - Name of the question for which the answer is being set.
6
+ * @param {*} answer - The answer to set for the question. Can be any type.
7
+ * @returns {Promise<void>} A promise that resolves when the answer has been successfully set.
8
+ * @throws {Error} Throws if the message from the parent window indicates a failure.
9
+ * @example
10
+ * ```typescript
11
+ * await setAnswer('favoriteColor', 'blue');
12
+ * ```
13
+ * @see onAnswerChange
14
+ */
15
+ export declare function setAnswer(questionName: string, answer: unknown): Promise<void>;
16
+ /**
17
+ * Retrieves a question by its name from the questionnaire.
18
+ *
19
+ * @param {string} name - The name of the question to retrieve.
20
+ * @returns {Promise<Question>} A promise that resolves to the question object.
21
+ * @throws {Error} Throws if the message from the parent window indicates a failure.
22
+ * @example
23
+ * ```typescript
24
+ * const question = await getQuestion('favoriteColor');
25
+ * console.log(question.label); // Outputs the label of the question
26
+ * ```
27
+ */
28
+ export declare function getQuestion(name: string): Promise<Question>;
29
+ /**
30
+ * Subscribes to changes in answers for questions.
31
+ * This function listens for messages from the parent window
32
+ * and invokes the provided listener whenever an answer changes.
33
+ * @param listener - A function that will be called with the question name and the new answer whenever an answer changes.
34
+ * @return An object with a `cancelSubscription` method to stop listening for changes.
35
+ * @example
36
+ * ```typescript
37
+ * import { onAnswerChange } from 'qp-bridge';
38
+ * const { cancelSubsctiprion } = onAnswerChange((question, answer) => {
39
+ * console.log(`Answer for ${question} changed to:`, answer);
40
+ * });
41
+ *
42
+ * // To stop listening for changes:
43
+ * cancelSubscription();
44
+ * ```
45
+ **/
46
+ export declare function onAnswerChange(listener: (question: string, answer: unknown) => void): {
47
+ cancelSubscription: () => void;
48
+ };
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@resconet/qp-bridge",
3
+ "version": "0.0.1-alpha.5",
4
+ "dependencies": {
5
+ "zod": "^3.23.8"
6
+ },
7
+ "main": "./index.js",
8
+ "module": "./index.mjs",
9
+ "typings": "./index.d.ts"
10
+ }