@govflanders/vl-widget-global-header-types 0.0.2 → 0.0.4

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/README.md ADDED
@@ -0,0 +1,156 @@
1
+ # Global Header Widget types
2
+
3
+ The **`@govflanders/vl-widget-global-header-types`** package provides TypeScript types and interfaces for interacting with the **Global Header Widget**. This widget is embedded in customer web pages and exposes a client object (`window.globalHeaderClient`) that allows for dynamic configuration and interaction with the global header.
4
+
5
+ By using the types defined in this package, developers can ensure type-safe interactions with the widget while improving their development experience (DX) by utilizing autocompletion and built-in documentation.
6
+
7
+ ## Installation
8
+
9
+ To install the `global-header-types` package, use the following command:
10
+
11
+ ```bash
12
+ npm install -D @govflanders/vl-widget-global-header-types
13
+ ```
14
+ or
15
+ ```bash
16
+ yarn add -D @govflanders/vl-widget-global-header-types
17
+ ```
18
+
19
+ ## Usage
20
+ This package provides all the types required for working with the Global Header Widget. The widget is available on the global window object as window.globalHeaderClient. You can import the types from global-header-types to use them in your project.
21
+
22
+ Example of using the types to interact with the widget:
23
+ ```typescript
24
+ import { GlobalHeaderClient, MainLink } from '@govflanders/vl-widget-global-header-types';
25
+
26
+ const client: GlobalHeaderClient = window.globalHeaderClient;
27
+
28
+ const contactLink: MainLink = {
29
+ title: 'Contact',
30
+ href: '/contact',
31
+ };
32
+
33
+ const feedbackLink: MainLink = {
34
+ title: 'Feedback',
35
+ href: 'https://example.com/feedback',
36
+ isExternal: true,
37
+ target: '_blank',
38
+ };
39
+
40
+ client.setMainLinks([
41
+ contactLink,
42
+ feedbackLink,
43
+ ]);
44
+ ```
45
+
46
+ ## API
47
+ The window.globalHeaderClient object offers several methods for interacting with the widget. Each method is typed and returns promises, making asynchronous operations easier to handle. Below is a detailed breakdown of each method, its parameters, and the expected return values.
48
+
49
+ ### getConfig(): Promise\<Config\>
50
+ Retrieve the current widget configuration.
51
+
52
+ ### setServicePoints(servicePoints: Translatable\<ServicePoints\>): Promise\<Translatable\<ServicePoints\>\>
53
+ Update the service points displayed in the widget.
54
+
55
+ ### getServicePoints(): Promise\<EnrichedServicePoints\>
56
+ Fetch the current service points from the widget.
57
+
58
+ ### resetServicePoints(): Promise\<boolean\>
59
+ Reset the service points to the initial values defined in the widget’s configuration.
60
+
61
+ ### setProfile(profileConfig: Partial\<ProfileConfig\>): Promise\<boolean\>
62
+ Set or update the profile configuration for the widget.
63
+
64
+ ### setMainLinks(mainLinks: MainLink[]): Promise\<boolean\>
65
+ Set the main links that are displayed in the widget.
66
+
67
+ ### addApplicationMenuLink(link: ApplicationMenuLink): Promise\<ApplicationMenuLink[]\>
68
+ Add a single link to the application menu.
69
+
70
+ ### addApplicationMenuLinks(links: ApplicationMenuLink[]): Promise\<ApplicationMenuLink[]\>
71
+ Add multiple links to the application menu.
72
+
73
+ ### setApplicationMenuLinks(links: ApplicationMenuLink[]): Promise\<ApplicationMenuLink[]\>
74
+ Replace all application menu links.
75
+
76
+ ### mount(element?: HTMLElement): Promise\<boolean\>
77
+
78
+ The `mount` method allows you to control where the **Global Header Widget** is rendered on the page. There are two main ways to integrate the widget, depending on whether you want to manually control its placement or let it render automatically:
79
+
80
+ 1. **Using the 'entry' script**:
81
+ When you include the widget via the following script:
82
+
83
+ ```html
84
+ <script src="https://prod.widgets.burgerprofiel.vlaanderen.be/api/v1/widget/__global_header_id__/entry"></script>
85
+ ```
86
+
87
+ The widget is **not automatically rendered** on the page. Instead, you must call the `mount` method from the `window.globalHeaderClient` object to display it. This gives you control over where and when the widget is added. You can either:
88
+
89
+ - **Mount to a specific element**:
90
+ Pass an HTML element to the `mount` method to render the widget inside that element.
91
+
92
+ ```typescript
93
+ const element = document.getElementById('customHeader');
94
+ window.globalHeaderClient.mount(element);
95
+ ```
96
+
97
+ - **Mount without specifying an element**:
98
+ If no element is provided, the widget will automatically render at the top of the page.
99
+
100
+ ```typescript
101
+ window.globalHeaderClient.mount();
102
+ ```
103
+
104
+ 2. **Using the 'embed' script**:
105
+ If you use this script instead:
106
+
107
+ ```html
108
+ <script src="https://prod.widgets.burgerprofiel.vlaanderen.be/api/v1/widget/__global_header_id__/embed"></script>
109
+ ```
110
+
111
+ The widget will **automatically render** at the location where the script is placed in the HTML. No additional action is needed to mount the widget in this case.
112
+
113
+ #### Return Value
114
+ The `mount` method returns a promise that resolves to `true` if the widget was successfully mounted, or `false` otherwise.
115
+
116
+ ## Notes
117
+
118
+ ### Translatable Type
119
+
120
+ The `Translatable<T>` type allows an object to either be a single value or a language-specific dictionary. This is useful when content needs to be available in multiple languages.
121
+
122
+ #### Example
123
+
124
+ For example, `setMainLinks` can accept single-language or multi-language links:
125
+
126
+ ```typescript
127
+ const singleLanguageLink: Translatable<MainLink> = {
128
+ title: 'Home',
129
+ href: 'https://www.example.com',
130
+ };
131
+
132
+ const multiLanguageLink: Translatable<MainLink> = {
133
+ nl: {
134
+ title: 'Startpagina',
135
+ href: 'https://www.example.com/nl',
136
+ },
137
+ en: {
138
+ title: 'Home',
139
+ href: 'https://www.example.com/en',
140
+ },
141
+ fr: {
142
+ title: 'Accueil',
143
+ href: 'https://www.example.com/fr',
144
+ },
145
+ de: {
146
+ title: 'Startseite',
147
+ href: 'https://www.example.com/de',
148
+ },
149
+ };
150
+
151
+ client.setMainLinks([singleLanguageLink, multiLanguageLink]);
152
+ ```
153
+
154
+
155
+ ## License
156
+ This package is licensed under the MIT License.
package/dist/client.d.ts CHANGED
@@ -36,13 +36,13 @@ export type Handlers = {
36
36
  * @param profileConfig The profile configuration to set
37
37
  * @returns A promise that resolves to true if the profile configuration was set, false otherwise
38
38
  */
39
- setProfile: Handler<ProfileConfig, boolean>;
39
+ setProfile: Handler<Partial<ProfileConfig>, boolean>;
40
40
  /**
41
41
  * Set the main links
42
42
  * @param mainLinks The main links to set
43
43
  * @returns A promise that resolves to true if the main links were set, false otherwise
44
44
  */
45
- setMainLinks: Handler<MainLink[], boolean>;
45
+ setMainLinks: Handler<Translatable<MainLink>[], boolean>;
46
46
  /**
47
47
  * Add an application menu link
48
48
  * @param link The link to add
@@ -78,7 +78,7 @@ type ReturnTypes = {
78
78
  [HandlersKey in keyof Handlers]: Handlers[HandlersKey] extends Handler<any, infer Return> ? Return : never;
79
79
  };
80
80
  export type GlobalHeaderClient = {
81
- [K in keyof Handlers]: (args: ArgumentTypes[K]) => Promise<ReturnTypes[K]>;
81
+ [K in keyof Handlers]: ArgumentTypes[K] extends undefined ? () => Promise<ReturnTypes[K]> : (args: ArgumentTypes[K]) => Promise<ReturnTypes[K]>;
82
82
  };
83
83
  export type QueueItem<Key extends keyof Handlers> = {
84
84
  [K in keyof Handlers]: {
package/dist/i18n.d.ts CHANGED
@@ -14,7 +14,7 @@ export interface ResolveOptions {
14
14
  }
15
15
  export interface I18n {
16
16
  default: Language;
17
- translations: Partial<Record<Language, RecursiveRecord>>;
17
+ translations: Translatable<RecursiveRecord>;
18
18
  }
19
19
  export type Translatable<T> = T | Record<Language, T>;
20
20
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@govflanders/vl-widget-global-header-types",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "TypeScript definitions for GlobalHeaderClient",
5
5
  "main": "",
6
6
  "types": "dist/index.d.ts",
@@ -22,6 +22,9 @@
22
22
  },
23
23
  "scripts": {
24
24
  "build": "tsc -p tsconfig.json",
25
+ "watch": "tsc -w",
26
+ "prepublishOnly": "npm run build",
27
+ "release": "yarn build && yarn version --patch --no-git-tag-version && git add . && git commit -m \"Release global-header-types $(node -p \"require('./package.json').version\")\" && git tag \"global-header-types-$(node -p \"require('./package.json').version\")\" && git push && git push --tags",
25
28
  "publish": "npm_config_registry=https://registry.npmjs.org/ npm publish --access public"
26
29
  }
27
30
  }