From 3514f900d9844448ebdf68a7894efdaf49292dfa Mon Sep 17 00:00:00 2001 From: MD4 Date: Fri, 11 Oct 2024 15:58:29 +0200 Subject: [PATCH] chore(*): updated types for messages hooks & commands --- package-lock.json | 7 - .../screeb-sdk-angular/docs/classes/Screeb.md | 6 +- .../docs/classes/ScreebConfig.md | 2 +- .../src/lib/screeb-config.ts | 4 +- .../screeb-sdk-angular/src/lib/screeb.ts | 6 +- packages/screeb-sdk-browser/docs/README.md | 219 ++++++++++++++++-- .../screeb-sdk-browser/src/hooks.types.ts | 129 ++++++++++- packages/screeb-sdk-browser/src/index.ts | 10 +- packages/screeb-sdk-browser/src/types.ts | 28 --- packages/screeb-sdk-react/docs/README.md | 14 +- packages/screeb-sdk-react/package.json | 3 +- packages/screeb-sdk-react/src/provider.tsx | 6 +- packages/screeb-sdk-react/src/types.ts | 12 +- 13 files changed, 349 insertions(+), 97 deletions(-) diff --git a/package-lock.json b/package-lock.json index 51356d1..7f66968 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24092,7 +24092,6 @@ }, "devDependencies": { "@screeb/eslint-config": "^0.1.6", - "@screeb/sdk-browser": "^0.1.6", "@screeb/typescript-config": "^0.1.10", "@types/jest": "^29.5.5", "@types/node": "^20.8.4", @@ -24143,12 +24142,6 @@ "vite": "^4.4.11" } }, - "packages/screeb-sdk-react/node_modules/@screeb/sdk-browser": { - "version": "0.1.18", - "resolved": "https://registry.npmjs.org/@screeb/sdk-browser/-/sdk-browser-0.1.18.tgz", - "integrity": "sha512-X5+ggzCYqubxUXyNf/grmkw7wf5xglMwvrhv607+Tvs6xWeHEyRmN4/90E/L5SEmnB6Wy+/LWj2nLvKh2e3VQw==", - "dev": true - }, "packages/screeb-sdk-react/node_modules/@types/node": { "version": "20.16.10", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.10.tgz", diff --git a/packages/screeb-sdk-angular/docs/classes/Screeb.md b/packages/screeb-sdk-angular/docs/classes/Screeb.md index 51ddf01..5392020 100644 --- a/packages/screeb-sdk-angular/docs/classes/Screeb.md +++ b/packages/screeb-sdk-angular/docs/classes/Screeb.md @@ -370,7 +370,7 @@ Initializes Screeb tag. | `websiteId` | `string` | Your website/channel id. | | `userId?` | `string` | The unique identifier of your user. | | `userProperties?` | `PropertyRecord` | The properties of your user. ```text Requirements: - Property names must be limited to 128 characters - No more than 1000 attributes - Supported types for values: string, number, boolean and Date ``` | -| `hooks?` | `Hooks` | - | +| `hooks?` | `HooksInit` | - | | `language?` | `string` | Force a specific language for the tag. eg: 'en'. default: browser language. | #### Returns @@ -454,7 +454,7 @@ Starts a message by its ID. | :------ | :------ | | `messageId` | `string` | | `hiddenFields` | `PropertyRecord` | -| `hooks?` | `Hooks` | +| `hooks?` | `HooksMessageStart` | | `language?` | `string` | #### Returns @@ -513,7 +513,7 @@ Starts a survey by its ID. | `distributionId` | `string` | | `allowMultipleResponses` | `boolean` | | `hiddenFields` | `PropertyRecord` | -| `hooks?` | `Hooks` | +| `hooks?` | `HooksSurveyStart` | | `language?` | `string` | #### Returns diff --git a/packages/screeb-sdk-angular/docs/classes/ScreebConfig.md b/packages/screeb-sdk-angular/docs/classes/ScreebConfig.md index 59b0d8e..30ae4a8 100644 --- a/packages/screeb-sdk-angular/docs/classes/ScreebConfig.md +++ b/packages/screeb-sdk-angular/docs/classes/ScreebConfig.md @@ -46,7 +46,7 @@ ___ ### hooks -• `Optional` **hooks**: `Hooks` +• `Optional` **hooks**: `HooksInit` Hooks to define callback for various event diff --git a/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb-config.ts b/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb-config.ts index 78f6cc4..339208b 100644 --- a/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb-config.ts +++ b/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb-config.ts @@ -1,5 +1,5 @@ import { Injectable } from "@angular/core"; -import { Hooks, PropertyRecord, ScreebOptions } from "@screeb/sdk-browser"; +import { HooksInit, PropertyRecord, ScreebOptions } from "@screeb/sdk-browser"; /** Configuration of Screeb module */ @Injectable({ providedIn: "root" }) @@ -11,7 +11,7 @@ export class ScreebConfig { /** The properties of your user. */ userProperties?: PropertyRecord; /** Hooks to define callback for various event */ - hooks?: Hooks; + hooks?: HooksInit; /** * Indicates if Screeb should be automatically loaded. * This will ping to the Screeb servers. diff --git a/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb.ts b/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb.ts index eab0d3e..27ae9ef 100644 --- a/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb.ts +++ b/packages/screeb-sdk-angular/projects/screeb-sdk-angular/src/lib/screeb.ts @@ -325,7 +325,7 @@ export class Screeb { distributionId: string, allowMultipleResponses: boolean, hiddenFields: _Screeb.PropertyRecord, - hooks?: _Screeb.Hooks, + hooks?: _Screeb.HooksSurveyStart, language?: string ) { await this.ensureScreeb("surveyStart"); @@ -377,7 +377,7 @@ export class Screeb { public async messageStart( messageId: string, hiddenFields: _Screeb.PropertyRecord, - hooks?: _Screeb.Hooks, + hooks?: _Screeb.HooksMessageStart, language?: string ) { await this.ensureScreeb("messageStart"); @@ -464,7 +464,7 @@ export class Screeb { websiteId: string, userId?: string, userProperties?: _Screeb.PropertyRecord, - hooks?: _Screeb.Hooks, + hooks?: _Screeb.HooksInit, language?: string ) { await this.ensureScreeb("init", true); diff --git a/packages/screeb-sdk-browser/docs/README.md b/packages/screeb-sdk-browser/docs/README.md index 47cdf27..9d07129 100644 --- a/packages/screeb-sdk-browser/docs/README.md +++ b/packages/screeb-sdk-browser/docs/README.md @@ -9,13 +9,24 @@ - [Channel](README.md#channel) - [ChannelType](README.md#channeltype) - [HookCommonProperties](README.md#hookcommonproperties) +- [HookCommonPropertiesMessage](README.md#hookcommonpropertiesmessage) +- [HookCommonPropertiesSurvey](README.md#hookcommonpropertiessurvey) +- [HookOnMessageCompleted](README.md#hookonmessagecompleted) +- [HookOnMessageDisplayAllowed](README.md#hookonmessagedisplayallowed) +- [HookOnMessageHidden](README.md#hookonmessagehidden) +- [HookOnMessageShowed](README.md#hookonmessageshowed) +- [HookOnMessageStarted](README.md#hookonmessagestarted) - [HookOnQuestionReplied](README.md#hookonquestionreplied) - [HookOnReady](README.md#hookonready) - [HookOnSurveyCompleted](README.md#hookonsurveycompleted) +- [HookOnSurveyDisplayAllowed](README.md#hookonsurveydisplayallowed) - [HookOnSurveyHidden](README.md#hookonsurveyhidden) - [HookOnSurveyShowed](README.md#hookonsurveyshowed) - [HookOnSurveyStarted](README.md#hookonsurveystarted) -- [Hooks](README.md#hooks) +- [HooksInit](README.md#hooksinit) +- [HooksMessageStart](README.md#hooksmessagestart) +- [HooksSurveyStart](README.md#hookssurveystart) +- [Message](README.md#message) - [PropertyRecord](README.md#propertyrecord) - [PropertyType](README.md#propertytype) - [ResponseItem](README.md#responseitem) @@ -83,14 +94,125 @@ ___ | Name | Type | | :------ | :------ | | `channel` | [`Channel`](README.md#channel) | -| `survey` | [`Survey`](README.md#survey) | | `user` | [`User`](README.md#user) | ___ +### HookCommonPropertiesMessage + +Ƭ **HookCommonPropertiesMessage**: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `message`: [`Message`](README.md#message) } + +___ + +### HookCommonPropertiesSurvey + +Ƭ **HookCommonPropertiesSurvey**: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `survey`: [`Survey`](README.md#survey) } + +___ + +### HookOnMessageCompleted + +Ƭ **HookOnMessageCompleted**: (`data`: [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` + +#### Type declaration + +▸ (`data`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | + +##### Returns + +`void` + +___ + +### HookOnMessageDisplayAllowed + +Ƭ **HookOnMessageDisplayAllowed**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey)) => `boolean` + +#### Type declaration + +▸ (`data`): `boolean` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) | + +##### Returns + +`boolean` + +___ + +### HookOnMessageHidden + +Ƭ **HookOnMessageHidden**: (`data`: [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` + +#### Type declaration + +▸ (`data`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | + +##### Returns + +`void` + +___ + +### HookOnMessageShowed + +Ƭ **HookOnMessageShowed**: (`data`: [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` + +#### Type declaration + +▸ (`data`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | + +##### Returns + +`void` + +___ + +### HookOnMessageStarted + +Ƭ **HookOnMessageStarted**: (`data`: [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` } }) => `void` + +#### Type declaration + +▸ (`data`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesMessage`](README.md#hookcommonpropertiesmessage) & \{ `response`: \{ `id`: `string` } } | + +##### Returns + +`void` + +___ + ### HookOnQuestionReplied -Ƭ **HookOnQuestionReplied**: (`data`: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `answer`: [`ResponseItemAnswer`](README.md#responseitemanswer) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] ; `question`: [`ResponseItemQuestion`](README.md#responseitemquestion) ; `status`: ``null`` } }) => `void` +Ƭ **HookOnQuestionReplied**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `answer`: [`ResponseItemAnswer`](README.md#responseitemanswer) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] ; `question`: [`ResponseItemQuestion`](README.md#responseitemquestion) ; `status`: ``null`` } }) => `void` #### Type declaration @@ -100,7 +222,7 @@ ___ | Name | Type | | :------ | :------ | -| `data` | [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `answer`: [`ResponseItemAnswer`](README.md#responseitemanswer) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] ; `question`: [`ResponseItemQuestion`](README.md#responseitemquestion) ; `status`: ``null`` } } | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `answer`: [`ResponseItemAnswer`](README.md#responseitemanswer) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] ; `question`: [`ResponseItemQuestion`](README.md#responseitemquestion) ; `status`: ``null`` } } | ##### Returns @@ -132,7 +254,7 @@ ___ ### HookOnSurveyCompleted -Ƭ **HookOnSurveyCompleted**: (`data`: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` +Ƭ **HookOnSurveyCompleted**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` #### Type declaration @@ -142,7 +264,7 @@ ___ | Name | Type | | :------ | :------ | -| `data` | [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | ##### Returns @@ -150,9 +272,29 @@ ___ ___ +### HookOnSurveyDisplayAllowed + +Ƭ **HookOnSurveyDisplayAllowed**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey)) => `boolean` + +#### Type declaration + +▸ (`data`): `boolean` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) | + +##### Returns + +`boolean` + +___ + ### HookOnSurveyHidden -Ƭ **HookOnSurveyHidden**: (`data`: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` +Ƭ **HookOnSurveyHidden**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` #### Type declaration @@ -162,7 +304,7 @@ ___ | Name | Type | | :------ | :------ | -| `data` | [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `hide_reason`: [`ResponseStatus`](README.md#responsestatus) ; `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | ##### Returns @@ -172,7 +314,7 @@ ___ ### HookOnSurveyShowed -Ƭ **HookOnSurveyShowed**: (`data`: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` +Ƭ **HookOnSurveyShowed**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } }) => `void` #### Type declaration @@ -182,7 +324,7 @@ ___ | Name | Type | | :------ | :------ | -| `data` | [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` ; `items`: [`ResponseItem`](README.md#responseitem)[] } } | ##### Returns @@ -192,7 +334,7 @@ ___ ### HookOnSurveyStarted -Ƭ **HookOnSurveyStarted**: (`data`: [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` } }) => `void` +Ƭ **HookOnSurveyStarted**: (`data`: [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` } }) => `void` #### Type declaration @@ -202,7 +344,7 @@ ___ | Name | Type | | :------ | :------ | -| `data` | [`HookCommonProperties`](README.md#hookcommonproperties) & \{ `response`: \{ `id`: `string` } } | +| `data` | [`HookCommonPropertiesSurvey`](README.md#hookcommonpropertiessurvey) & \{ `response`: \{ `id`: `string` } } | ##### Returns @@ -210,23 +352,60 @@ ___ ___ -### Hooks +### HooksInit -Ƭ **Hooks**: `Object` +Ƭ **HooksInit**: [`HooksSurveyStart`](README.md#hookssurveystart) & [`HooksMessageStart`](README.md#hooksmessagestart) & \{ `onMessageDisplayAllowed?`: [`HookOnMessageDisplayAllowed`](README.md#hookonmessagedisplayallowed) ; `onReady?`: [`HookOnReady`](README.md#hookonready) ; `onSurveyDisplayAllowed?`: [`HookOnSurveyDisplayAllowed`](README.md#hookonsurveydisplayallowed) ; `version`: `string` } -This is the Screeb tag hooks object. +This is the Screeb tag hooks object available on `init` command. + +___ + +### HooksMessageStart + +Ƭ **HooksMessageStart**: `Object` + +This is the Screeb tag hooks object available on `message.start` command. + +#### Type declaration + +| Name | Type | Description | +| :------ | :------ | :------ | +| `onMessageCompleted?` | [`HookOnMessageCompleted`](README.md#hookonmessagecompleted) | This hook is triggered when a message is completed | +| `onMessageHidden?` | [`HookOnMessageHidden`](README.md#hookonmessagehidden) | This hook is triggered when a message is hidden | +| `onMessageShowed?` | [`HookOnMessageShowed`](README.md#hookonmessageshowed) | This hook is triggered when a message is displayed on screen (also triggered when page is reloaded) | +| `onMessageStarted?` | [`HookOnMessageStarted`](README.md#hookonmessagestarted) | This hook is triggered when a message is started | +| `onQuestionReplied?` | [`HookOnQuestionReplied`](README.md#hookonquestionreplied) | This hook is triggered when a question is answered | + +___ + +### HooksSurveyStart + +Ƭ **HooksSurveyStart**: `Object` + +This is the Screeb tag hooks object available on `survey.start` command. #### Type declaration | Name | Type | Description | | :------ | :------ | :------ | | `onQuestionReplied?` | [`HookOnQuestionReplied`](README.md#hookonquestionreplied) | This hook is triggered when a question is answered | -| `onReady?` | [`HookOnReady`](README.md#hookonready) | This hook is triggered when Screeb SD is loaded, initialized and ready to rock | | `onSurveyCompleted?` | [`HookOnSurveyCompleted`](README.md#hookonsurveycompleted) | This hook is triggered when a survey is completed | | `onSurveyHidden?` | [`HookOnSurveyHidden`](README.md#hookonsurveyhidden) | This hook is triggered when a survey is hidden | | `onSurveyShowed?` | [`HookOnSurveyShowed`](README.md#hookonsurveyshowed) | This hook is triggered when a survey is displayed on screen (also triggered when page is reloaded) | | `onSurveyStarted?` | [`HookOnSurveyStarted`](README.md#hookonsurveystarted) | This hook is triggered when a survey is started | -| `version` | `string` | This defines the version of hooks and their data | + +___ + +### Message + +Ƭ **Message**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `id` | `string` | +| `messagey_size` | ``100`` | ___ @@ -715,7 +894,7 @@ Initializes Screeb tag. | `websiteId` | `string` | Your website/channel id. | | `userId?` | `string` | The unique identifier of your user. | | `userProperties?` | [`PropertyRecord`](README.md#propertyrecord) | The properties of your user. ```text Requirements: - Property names must be limited to 128 characters - No more than 1000 attributes - Supported types for values: string, number, boolean and Date ``` | -| `hooks?` | [`Hooks`](README.md#hooks) | Hooks to be called when SDK is ready or a survey is showed, started, completed, hidden or when a question is replied. | +| `hooks?` | [`HooksInit`](README.md#hooksinit) | Hooks to be called when SDK is ready or a survey is showed, started, completed, hidden or when a question is replied. | | `language?` | `string` | Force a specific language for the tag. eg: 'en'. default: browser language. | #### Returns @@ -827,7 +1006,7 @@ Starts a message by its ID. | :------ | :------ | | `messageId` | `string` | | `hiddenFields` | [`PropertyRecord`](README.md#propertyrecord) | -| `hooks?` | [`Hooks`](README.md#hooks) | +| `hooks?` | [`HooksMessageStart`](README.md#hooksmessagestart) | | `language?` | `string` | #### Returns @@ -890,7 +1069,7 @@ Starts a survey by its ID. | `distributionId` | `string` | `undefined` | | `allowMultipleResponses` | `boolean` | `true` | | `hiddenFields` | [`PropertyRecord`](README.md#propertyrecord) | `{}` | -| `hooks?` | [`Hooks`](README.md#hooks) | `undefined` | +| `hooks?` | [`HooksSurveyStart`](README.md#hookssurveystart) | `undefined` | | `language?` | `string` | `undefined` | #### Returns diff --git a/packages/screeb-sdk-browser/src/hooks.types.ts b/packages/screeb-sdk-browser/src/hooks.types.ts index 2e7588f..ce4d4f2 100644 --- a/packages/screeb-sdk-browser/src/hooks.types.ts +++ b/packages/screeb-sdk-browser/src/hooks.types.ts @@ -29,6 +29,11 @@ export type Survey = { survey_size: 100; }; +export type Message = { + id: string; + messagey_size: 100; +}; + export type ResponseStatus = | "displayed" | "started" @@ -62,15 +67,32 @@ export type ResponseItem = { export type HookCommonProperties = { channel: Channel; user: User; +}; + +export type HookCommonPropertiesSurvey = HookCommonProperties & { survey: Survey; }; +export type HookCommonPropertiesMessage = HookCommonProperties & { + message: Message; +}; + // eslint-disable-next-line no-unused-vars export type HookOnReady = (data: { channel: Channel; user: User }) => void; +export type HookOnSurveyDisplayAllowed = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesSurvey, +) => boolean; + +export type HookOnMessageDisplayAllowed = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesSurvey, +) => boolean; + export type HookOnSurveyShowed = ( // eslint-disable-next-line no-unused-vars - data: HookCommonProperties & { + data: HookCommonPropertiesSurvey & { response: { id: string; items: ResponseItem[]; @@ -80,39 +102,66 @@ export type HookOnSurveyShowed = ( export type HookOnSurveyStarted = ( // eslint-disable-next-line no-unused-vars - data: HookCommonProperties & { + data: HookCommonPropertiesSurvey & { response: { id: string; }; }, ) => void; -export type HookOnQuestionReplied = ( +export type HookOnSurveyCompleted = ( // eslint-disable-next-line no-unused-vars - data: HookCommonProperties & { + data: HookCommonPropertiesSurvey & { response: { id: string; - status: null; - question: ResponseItemQuestion; - answer: ResponseItemAnswer; items: ResponseItem[]; }; }, ) => void; -export type HookOnSurveyCompleted = ( +export type HookOnSurveyHidden = ( // eslint-disable-next-line no-unused-vars - data: HookCommonProperties & { + data: HookCommonPropertiesSurvey & { response: { id: string; items: ResponseItem[]; + hide_reason: ResponseStatus; }; }, ) => void; -export type HookOnSurveyHidden = ( +export type HookOnMessageShowed = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesMessage & { + response: { + id: string; + items: ResponseItem[]; + }; + }, +) => void; + +export type HookOnMessageStarted = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesMessage & { + response: { + id: string; + }; + }, +) => void; + +export type HookOnMessageCompleted = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesMessage & { + response: { + id: string; + items: ResponseItem[]; + }; + }, +) => void; + +export type HookOnMessageHidden = ( // eslint-disable-next-line no-unused-vars - data: HookCommonProperties & { + data: HookCommonPropertiesMessage & { response: { id: string; items: ResponseItem[]; @@ -120,3 +169,61 @@ export type HookOnSurveyHidden = ( }; }, ) => void; + +export type HookOnQuestionReplied = ( + // eslint-disable-next-line no-unused-vars + data: HookCommonPropertiesSurvey & { + response: { + id: string; + status: null; + question: ResponseItemQuestion; + answer: ResponseItemAnswer; + items: ResponseItem[]; + }; + }, +) => void; + +/** This is the Screeb tag hooks object available on `survey.start` command. */ +export type HooksSurveyStart = { + /** This hook is triggered when a survey is displayed on screen (also triggered when page is reloaded) */ + onSurveyShowed?: HookOnSurveyShowed; + /** This hook is triggered when a survey is started */ + onSurveyStarted?: HookOnSurveyStarted; + /** This hook is triggered when a survey is completed */ + onSurveyCompleted?: HookOnSurveyCompleted; + /** This hook is triggered when a survey is hidden */ + onSurveyHidden?: HookOnSurveyHidden; + + /** This hook is triggered when a question is answered */ + onQuestionReplied?: HookOnQuestionReplied; +}; + +/** This is the Screeb tag hooks object available on `message.start` command. */ +export type HooksMessageStart = { + /** This hook is triggered when a message is displayed on screen (also triggered when page is reloaded) */ + onMessageShowed?: HookOnMessageShowed; + /** This hook is triggered when a message is started */ + onMessageStarted?: HookOnMessageStarted; + /** This hook is triggered when a message is completed */ + onMessageCompleted?: HookOnMessageCompleted; + /** This hook is triggered when a message is hidden */ + onMessageHidden?: HookOnMessageHidden; + + /** This hook is triggered when a question is answered */ + onQuestionReplied?: HookOnQuestionReplied; +}; + +/** This is the Screeb tag hooks object available on `init` command. */ +export type HooksInit = HooksSurveyStart & + HooksMessageStart & { + /** This defines the version of hooks and their data */ + version: string; + + /** This hook is triggered when Screeb SD is loaded, initialized and ready to rock */ + onReady?: HookOnReady; + + /** This hook is triggered before a survey displays to allow it or not. */ + onSurveyDisplayAllowed?: HookOnSurveyDisplayAllowed; + /** This hook is triggered before a message displays to allow it or not. */ + onMessageDisplayAllowed?: HookOnMessageDisplayAllowed; + }; diff --git a/packages/screeb-sdk-browser/src/index.ts b/packages/screeb-sdk-browser/src/index.ts index e196aa0..dbcae5f 100644 --- a/packages/screeb-sdk-browser/src/index.ts +++ b/packages/screeb-sdk-browser/src/index.ts @@ -1,5 +1,5 @@ +import { HooksInit, HooksMessageStart, HooksSurveyStart } from "./hooks.types"; import { - Hooks, PropertyRecord, ScreebFunction, ScreebIdentityGetReturn, @@ -128,12 +128,12 @@ export const init = ( websiteId: string, userId?: string, userProperties?: PropertyRecord, - hooks?: Hooks, + hooks?: HooksInit, language?: string, ) => { let identityObject: | { - hooks?: Hooks; + hooks?: HooksInit; identity?: { id?: string; properties?: PropertyRecord }; language?: string; } @@ -450,7 +450,7 @@ export const surveyStart = ( distributionId: string, allowMultipleResponses = true, hiddenFields: PropertyRecord = {}, - hooks?: Hooks, + hooks?: HooksSurveyStart, language?: string, ) => callScreebCommand("survey.start", surveyId, { @@ -498,7 +498,7 @@ export const messageClose = () => callScreebCommand("message.close"); export const messageStart = ( messageId: string, hiddenFields: PropertyRecord = {}, - hooks?: Hooks, + hooks?: HooksMessageStart, language?: string, ) => callScreebCommand("message.start", messageId, { diff --git a/packages/screeb-sdk-browser/src/types.ts b/packages/screeb-sdk-browser/src/types.ts index de0fff2..d3ff7d8 100644 --- a/packages/screeb-sdk-browser/src/types.ts +++ b/packages/screeb-sdk-browser/src/types.ts @@ -1,12 +1,3 @@ -import { - HookOnQuestionReplied, - HookOnReady, - HookOnSurveyCompleted, - HookOnSurveyHidden, - HookOnSurveyShowed, - HookOnSurveyStarted, -} from "./hooks.types"; - /** This is property types that are supported by Screeb. */ export type PropertyType = number | boolean | string | Date | PropertyRecord; @@ -64,22 +55,3 @@ export type ScreebIdentityGetReturn = { /** `true` if the tag is loaded, initialized and ready to rock */ is_ready: boolean; }; - -/** This is the Screeb tag hooks object. */ -export type Hooks = { - /** This defines the version of hooks and their data */ - version: string; - - /** This hook is triggered when Screeb SD is loaded, initialized and ready to rock */ - onReady?: HookOnReady; - /** This hook is triggered when a survey is displayed on screen (also triggered when page is reloaded) */ - onSurveyShowed?: HookOnSurveyShowed; - /** This hook is triggered when a survey is started */ - onSurveyStarted?: HookOnSurveyStarted; - /** This hook is triggered when a question is answered */ - onQuestionReplied?: HookOnQuestionReplied; - /** This hook is triggered when a survey is completed */ - onSurveyCompleted?: HookOnSurveyCompleted; - /** This hook is triggered when a survey is hidden */ - onSurveyHidden?: HookOnSurveyHidden; -}; diff --git a/packages/screeb-sdk-react/docs/README.md b/packages/screeb-sdk-react/docs/README.md index 512995d..11b5205 100644 --- a/packages/screeb-sdk-react/docs/README.md +++ b/packages/screeb-sdk-react/docs/README.md @@ -366,7 +366,7 @@ ___ ### InitFunction -Ƭ **InitFunction**: (`websiteId`: `string`, `userId?`: `string`, `userProperties?`: `PropertyRecord`, `hooks?`: `Hooks`, `language?`: `string`) => `Promise`\<`void`\> +Ƭ **InitFunction**: (`websiteId`: `string`, `userId?`: `string`, `userProperties?`: `PropertyRecord`, `hooks?`: `HooksInit`, `language?`: `string`) => `Promise`\<`void`\> Initializes Screeb tag. @@ -400,7 +400,7 @@ init( | `websiteId` | `string` | Your website/channel id. | | `userId?` | `string` | The unique identifier of your user. | | `userProperties?` | `PropertyRecord` | The properties of your user. ```text Requirements: - Property names must be limited to 128 characters - No more than 1000 attributes - Supported types for values: string, number, boolean and Date ``` | -| `hooks?` | `Hooks` | - | +| `hooks?` | `HooksInit` | - | | `language?` | `string` | Force a specific language for the tag. eg: 'en'. default: browser language. | ##### Returns @@ -465,7 +465,7 @@ ___ ### MessageStartFunction -Ƭ **MessageStartFunction**: (`messageId`: `string`, `hiddenFields`: `PropertyRecord`, `hooks?`: `Hooks`, `language?`: `string`) => `Promise`\<`unknown`\> +Ƭ **MessageStartFunction**: (`messageId`: `string`, `hiddenFields`: `PropertyRecord`, `hooks?`: `HooksMessageStart`, `language?`: `string`) => `Promise`\<`unknown`\> Starts a message by its ID. @@ -494,7 +494,7 @@ messageStart( | :------ | :------ | | `messageId` | `string` | | `hiddenFields` | `PropertyRecord` | -| `hooks?` | `Hooks` | +| `hooks?` | `HooksMessageStart` | | `language?` | `string` | ##### Returns @@ -543,7 +543,7 @@ Properties of Screeb provider | Name | Type | Description | | :------ | :------ | :------ | -| `hooks?` | `Hooks` | Hooks to define callback for various event | +| `hooks?` | `HooksInit` | Hooks to define callback for various event | | `language?` | `string` | The language you want to force | | `userId?` | `string` | The unique identifier of your user. | | `userProperties?` | `PropertyRecord` | The properties of your user. | @@ -585,7 +585,7 @@ ___ ### SurveyStartFunction -Ƭ **SurveyStartFunction**: (`surveyId`: `string`, `distributionId`: `string`, `allowMultipleResponses`: `boolean`, `hiddenFields`: `PropertyRecord`, `hooks?`: `Hooks`, `language?`: `string`) => `Promise`\<`unknown`\> +Ƭ **SurveyStartFunction**: (`surveyId`: `string`, `distributionId`: `string`, `allowMultipleResponses`: `boolean`, `hiddenFields`: `PropertyRecord`, `hooks?`: `HooksSurveyStart`, `language?`: `string`) => `Promise`\<`unknown`\> Starts a survey by its ID. @@ -617,7 +617,7 @@ surveyStart( | `distributionId` | `string` | | `allowMultipleResponses` | `boolean` | | `hiddenFields` | `PropertyRecord` | -| `hooks?` | `Hooks` | +| `hooks?` | `HooksSurveyStart` | | `language?` | `string` | ##### Returns diff --git a/packages/screeb-sdk-react/package.json b/packages/screeb-sdk-react/package.json index 3d2c329..257e65b 100644 --- a/packages/screeb-sdk-react/package.json +++ b/packages/screeb-sdk-react/package.json @@ -45,7 +45,6 @@ }, "devDependencies": { "@screeb/eslint-config": "^0.1.6", - "@screeb/sdk-browser": "^0.1.6", "@screeb/typescript-config": "^0.1.10", "@types/jest": "^29.5.5", "@types/node": "^20.8.4", @@ -72,4 +71,4 @@ "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/packages/screeb-sdk-react/src/provider.tsx b/packages/screeb-sdk-react/src/provider.tsx index ebbc028..a69cbe4 100644 --- a/packages/screeb-sdk-react/src/provider.tsx +++ b/packages/screeb-sdk-react/src/provider.tsx @@ -145,7 +145,7 @@ export const ScreebProvider: React.FC< websiteId: string, userId?: string, userProperties?: Screeb.PropertyRecord, - hooks?: Screeb.Hooks, + hooks?: Screeb.HooksInit, language?: string, ) => { await ensureScreeb( @@ -200,7 +200,7 @@ export const ScreebProvider: React.FC< distributionId: string, allowMultipleResponses: boolean, hiddenFields: Screeb.PropertyRecord, - hooks?: Screeb.Hooks, + hooks?: Screeb.HooksSurveyStart, language?: string, ) => await ensureScreeb("surveyStart", () => @@ -225,7 +225,7 @@ export const ScreebProvider: React.FC< async ( messageId: string, hiddenFields: Screeb.PropertyRecord, - hooks?: Screeb.Hooks, + hooks?: Screeb.HooksMessageStart, language?: string, ) => await ensureScreeb("messageStart", () => diff --git a/packages/screeb-sdk-react/src/types.ts b/packages/screeb-sdk-react/src/types.ts index 910f05e..085957e 100644 --- a/packages/screeb-sdk-react/src/types.ts +++ b/packages/screeb-sdk-react/src/types.ts @@ -1,6 +1,8 @@ /* eslint-disable no-unused-vars */ import { - Hooks, + HooksInit, + HooksMessageStart, + HooksSurveyStart, PropertyRecord, ScreebIdentityGetReturn, ScreebOptions, @@ -15,7 +17,7 @@ export type ScreebProps = { /** The properties of your user. */ userProperties?: PropertyRecord; /** Hooks to define callback for various event */ - hooks?: Hooks; + hooks?: HooksInit; /** The language you want to force */ language?: string; }; @@ -292,7 +294,7 @@ export type InitFunction = ( websiteId: string, userId?: string, userProperties?: PropertyRecord, - hooks?: Hooks, + hooks?: HooksInit, language?: string, ) => Promise; @@ -347,7 +349,7 @@ export type SurveyStartFunction = ( distributionId: string, allowMultipleResponses: boolean, hiddenFields: PropertyRecord, - hooks?: Hooks, + hooks?: HooksSurveyStart, language?: string, ) => Promise; @@ -383,7 +385,7 @@ export type MessageCloseFunction = () => Promise; export type MessageStartFunction = ( messageId: string, hiddenFields: PropertyRecord, - hooks?: Hooks, + hooks?: HooksMessageStart, language?: string, ) => Promise;