@appsemble/sdk 0.20.10 → 0.20.13

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 CHANGED
@@ -1,10 +1,10 @@
1
- # ![](https://gitlab.com/appsemble/appsemble/-/raw/0.18.23/config/assets/logo.svg) Appsemble SDK
1
+ # ![](https://gitlab.com/appsemble/appsemble/-/raw/0.20.13/config/assets/logo.svg) Appsemble SDK
2
2
 
3
3
  > Build your own blocks
4
4
 
5
5
  [![npm](https://img.shields.io/npm/v/@appsemble/sdk)](https://www.npmjs.com/package/@appsemble/sdk)
6
- [![GitLab CI](https://gitlab.com/appsemble/appsemble/badges/0.18.23/pipeline.svg)](https://gitlab.com/appsemble/appsemble/-/releases/0.18.23)
7
- [![Code coverage](https://codecov.io/gl/appsemble/appsemble/branch/0.18.23/graph/badge.svg)](https://codecov.io/gl/appsemble/appsemble)
6
+ [![GitLab CI](https://gitlab.com/appsemble/appsemble/badges/0.20.13/pipeline.svg)](https://gitlab.com/appsemble/appsemble/-/releases/0.20.13)
7
+ [![Code coverage](https://codecov.io/gl/appsemble/appsemble/branch/0.20.13/graph/badge.svg)](https://codecov.io/gl/appsemble/appsemble)
8
8
  [![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io)
9
9
 
10
10
  ## Installation
@@ -144,5 +144,5 @@ declare module '@appsemble/sdk' {
144
144
 
145
145
  ## License
146
146
 
147
- [LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.18.23/LICENSE.md) ©
147
+ [LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.20.13/LICENSE.md) ©
148
148
  [Appsemble](https://appsemble.com)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -138,9 +138,9 @@ export interface Utils {
138
138
  /**
139
139
  * Remap data based in a user defined remapper function.
140
140
  *
141
- * @param remapper - The user defined remapper function.
142
- * @param data - The data to remap.
143
- * @param context - The context to retrieve contextual data from.
141
+ * @param remapper The user defined remapper function.
142
+ * @param data The data to remap.
143
+ * @param context The context to retrieve contextual data from.
144
144
  * @returns The result of the remapped data.
145
145
  */
146
146
  remap: (remapper: Remapper, data: unknown, context?: Record<string, unknown>) => unknown;
@@ -155,20 +155,20 @@ export interface Utils {
155
155
  /**
156
156
  * Get a URL serving an asset for the given asset id.
157
157
  *
158
- * @param assetId - The id of the asset whose URL to get.
158
+ * @param assetId The id of the asset whose URL to get.
159
159
  * @returns The URL that matches the given asset id.
160
160
  */
161
161
  asset: (assetId: string) => string;
162
162
  /**
163
163
  * Convert a FontAwesome icon name into a valid FontAwesome CSS class.
164
164
  *
165
- * @param icon - The FontAwesome icon.
165
+ * @param icon The FontAwesome icon.
166
166
  * @returns String containing the FontAwesome classes for the icon.
167
167
  */
168
168
  fa: (icon: IconName) => string;
169
169
  /**
170
- * @param items - The list of menu items to display.
171
- * @param header - A header that can be used to separate from other block menus.
170
+ * @param items The list of menu items to display.
171
+ * @param header A header that can be used to separate from other block menus.
172
172
  */
173
173
  menu: (items: MenuItem[], header?: string) => void;
174
174
  }
@@ -176,8 +176,8 @@ export interface Events {
176
176
  /**
177
177
  * Emit an Appsemble event.
178
178
  *
179
- * @param type - The type of event to emit.
180
- * @param data - Data to emit with the event.
179
+ * @param type The type of event to emit.
180
+ * @param data Data to emit with the event.
181
181
  * @returns Boolean indicating whether an emitter is implemented or not.
182
182
  */
183
183
  emit: {
@@ -186,8 +186,8 @@ export interface Events {
186
186
  /**
187
187
  * Remove an event listener for an Appsemble event.
188
188
  *
189
- * @param type - The type of event to listen remove the listener from.
190
- * @param callback - The callback to remove.
189
+ * @param type The type of event to listen remove the listener from.
190
+ * @param callback The callback to remove.
191
191
  * @returns Boolean indicating whether a listener is implemented or not.
192
192
  */
193
193
  off: {
@@ -196,8 +196,8 @@ export interface Events {
196
196
  /**
197
197
  * Add an event listener for an Appsemble event.
198
198
  *
199
- * @param type - The type of event to listen on.
200
- * @param callback - A callback to register for the event.
199
+ * @param type The type of event to listen on.
200
+ * @param callback A callback to register for the event.
201
201
  * @returns Boolean indicating whether a listener is implemented or not.
202
202
  */
203
203
  on: {
@@ -266,7 +266,7 @@ export interface AppsembleBootstrapEvent extends CustomEvent {
266
266
  /**
267
267
  * Register a boostrap function.
268
268
  *
269
- * @param fn - The bootstrap function to register
269
+ * @param fn The bootstrap function to register
270
270
  *
271
271
  * If the function returns an element, it’s appended to the shadow root.
272
272
  */
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Register a boostrap function.
3
3
  *
4
- * @param fn - The bootstrap function to register
4
+ * @param fn The bootstrap function to register
5
5
  *
6
6
  * If the function returns an element, it’s appended to the shadow root.
7
7
  */
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAiTA;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,EAAqB;IAC7C,MAAM,KAAK,GAA4B,IAAI,WAAW,CAAC,oBAAoB,EAAE;QAC3E,MAAM,EAAE;YACN,EAAE;YACF,QAAQ;SACT;KACF,CAAC,CAAC;IACH,IAAI,QAAQ,CAAC,aAAa,EAAE;QAC1B,QAAQ,CAAC,aAAa,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;KAC7C;AACH,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
package/index.test.js ADDED
@@ -0,0 +1,31 @@
1
+ import { bootstrap } from '.';
2
+ let event;
3
+ let originalCurrentScript;
4
+ beforeEach(() => {
5
+ originalCurrentScript = document.currentScript;
6
+ Object.defineProperty(document, 'currentScript', {
7
+ value: {
8
+ dispatchEvent: jest.fn((e) => {
9
+ event = e;
10
+ }),
11
+ },
12
+ writable: true,
13
+ });
14
+ });
15
+ afterEach(() => {
16
+ Object.defineProperty(document, 'currentScript', {
17
+ value: originalCurrentScript,
18
+ writable: true,
19
+ });
20
+ originalCurrentScript = undefined;
21
+ });
22
+ describe('bootstrap', () => {
23
+ it('should dispatch the AppsembleBoostrap event', () => {
24
+ const fn = jest.fn();
25
+ bootstrap(fn);
26
+ expect(document.currentScript.dispatchEvent).toHaveBeenCalledWith(new CustomEvent(''));
27
+ expect(event.type).toBe('AppsembleBootstrap');
28
+ expect(event.detail).toStrictEqual({ fn, document });
29
+ });
30
+ });
31
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,CAAC;AAE9B,IAAI,KAAkB,CAAC;AACvB,IAAI,qBAA6C,CAAC;AAElD,UAAU,CAAC,GAAG,EAAE;IACd,qBAAqB,GAAG,QAAQ,CAAC,aAAa,CAAC;IAC/C,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,eAAe,EAAE;QAC/C,KAAK,EAAE;YACL,aAAa,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE;gBAC3B,KAAK,GAAG,CAAC,CAAC;YACZ,CAAC,CAAC;SACH;QACD,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,SAAS,CAAC,GAAG,EAAE;IACb,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,eAAe,EAAE;QAC/C,KAAK,EAAE,qBAAqB;QAC5B,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IACH,qBAAqB,GAAG,SAAS,CAAC;AACpC,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;QACrB,SAAS,CAAC,EAAE,CAAC,CAAC;QACd,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,oBAAoB,CAAC,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;QACvF,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAC9C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/index.test.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { bootstrap } from '.';
2
+
3
+ let event: CustomEvent;
4
+ let originalCurrentScript: HTMLOrSVGScriptElement;
5
+
6
+ beforeEach(() => {
7
+ originalCurrentScript = document.currentScript;
8
+ Object.defineProperty(document, 'currentScript', {
9
+ value: {
10
+ dispatchEvent: jest.fn((e) => {
11
+ event = e;
12
+ }),
13
+ },
14
+ writable: true,
15
+ });
16
+ });
17
+
18
+ afterEach(() => {
19
+ Object.defineProperty(document, 'currentScript', {
20
+ value: originalCurrentScript,
21
+ writable: true,
22
+ });
23
+ originalCurrentScript = undefined;
24
+ });
25
+
26
+ describe('bootstrap', () => {
27
+ it('should dispatch the AppsembleBoostrap event', () => {
28
+ const fn = jest.fn();
29
+ bootstrap(fn);
30
+ expect(document.currentScript.dispatchEvent).toHaveBeenCalledWith(new CustomEvent(''));
31
+ expect(event.type).toBe('AppsembleBootstrap');
32
+ expect(event.detail).toStrictEqual({ fn, document });
33
+ });
34
+ });
package/index.ts ADDED
@@ -0,0 +1,323 @@
1
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
2
+ import type { Action, BaseMessage, BulmaColor, BulmaSize, Theme } from '@appsemble/types';
3
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
4
+ import type { IconName } from '@fortawesome/fontawesome-common-types';
5
+ import { Promisable } from 'type-fest';
6
+
7
+ export { IconName };
8
+ export { Action, BulmaColor, BulmaSize, Theme };
9
+
10
+ /**
11
+ * A user defined remapper function.
12
+ */
13
+ export type Remapper = object[] | boolean | number | object | string;
14
+
15
+ /**
16
+ * Actions defined on a block.
17
+ *
18
+ * If a block uses actions, extend this interface using module augmentation. The keys are the names
19
+ * of the events the block supports.
20
+ *
21
+ * @example
22
+ * declare module '<at>appsemble/sdk' {
23
+ * interface Actions {
24
+ * onClick: {}
25
+ * }
26
+ * }
27
+ */
28
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
29
+ export interface Actions {}
30
+
31
+ /**
32
+ * Messages defined on a block.
33
+ *
34
+ * If a block uses messages, extend this interface using module augmentation.
35
+ * The keys are the names of the messages.
36
+ * The types are either `never` if the message doesn’t support parameters
37
+ * or an object containing the keys of values and the type of the value.
38
+ *
39
+ * @example
40
+ * declare module '<at>appsemble/sdk' {
41
+ * interface Messages {
42
+ * exampleMessage: never;
43
+ * hello: { person: string };
44
+ * }
45
+ * }
46
+ */
47
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
48
+ export interface Messages {}
49
+
50
+ /**
51
+ * Event emitters defined on a block.
52
+ *
53
+ * If a block emits events, extend this interface using module augmentation. The keys are the names
54
+ * of the events the block can emit.
55
+ *
56
+ * @example
57
+ * declare module '<at>appsemble/sdk' {
58
+ * interface EventEmitters {
59
+ * data: {}
60
+ * }
61
+ * }
62
+ */
63
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
64
+ export interface EventEmitters {}
65
+
66
+ /**
67
+ * Event listeners defined on a block.
68
+ *
69
+ * If a block listens on events, extend this interface using module augmentation. The keys are the
70
+ * names of the events the block can emit.
71
+ *
72
+ * @example
73
+ * declare module '<at>appsemble/sdk' {
74
+ * interface EventListeners {
75
+ * data: {}
76
+ * }
77
+ * }
78
+ */
79
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
80
+ export interface EventListeners {}
81
+
82
+ /**
83
+ * Custom free form parameters defined on a block.
84
+ *
85
+ * If a block listens on events, extend this interface using module augmentation. The keys are the
86
+ * names of the events the block can emit.
87
+ *
88
+ * @example
89
+ * declare module '<at>appsemble/sdk' {
90
+ * interface Parameters {
91
+ * param1: string;
92
+ * param2: number;
93
+ * param3: {
94
+ * nested: boolean;
95
+ * }
96
+ * }
97
+ * }
98
+ */
99
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
100
+ export interface Parameters {}
101
+
102
+ export type PageParameters = Record<string, string>;
103
+
104
+ /**
105
+ * A menu item that can be displayed in addition to the normal app menu.
106
+ */
107
+ export interface MenuItem {
108
+ /**
109
+ * The title of the menu item.
110
+ */
111
+ title: string;
112
+
113
+ /**
114
+ * The icon to display next to the title.
115
+ */
116
+ icon?: IconName;
117
+
118
+ /**
119
+ * The color to use for the icon.
120
+ */
121
+ iconColor?: BulmaColor;
122
+
123
+ /**
124
+ * The click handler for when the menu item is clicked.
125
+ */
126
+ onClick: () => void;
127
+
128
+ /**
129
+ * The submenu items associated with this menu item.
130
+ */
131
+ submenu?: Omit<MenuItem, 'submenu'>[];
132
+
133
+ /**
134
+ * Whether the item is considered active.
135
+ */
136
+ active?: boolean;
137
+ }
138
+
139
+ export interface Message extends BaseMessage {
140
+ /**
141
+ * The content of the message to display.
142
+ */
143
+ body: string;
144
+ }
145
+
146
+ /**
147
+ * A set of utility functions provided by the Appsemble SDK.
148
+ */
149
+ export interface Utils {
150
+ /**
151
+ * Register a function that should be called when the block is being removed.
152
+ *
153
+ * Use this to clean up resouces that would otherwise stay in memory, e.g. object URLs.
154
+ */
155
+ addCleanup: (fn: () => void) => void;
156
+
157
+ /**
158
+ * Remap data based in a user defined remapper function.
159
+ *
160
+ * @param remapper The user defined remapper function.
161
+ * @param data The data to remap.
162
+ * @param context The context to retrieve contextual data from.
163
+ * @returns The result of the remapped data.
164
+ */
165
+ remap: (remapper: Remapper, data: unknown, context?: Record<string, unknown>) => unknown;
166
+
167
+ /**
168
+ * Show a bulma style message.
169
+ */
170
+ showMessage: (message: Message | string) => void;
171
+
172
+ /**
173
+ * Formats a message using ICU syntax.
174
+ */
175
+ formatMessage: <T extends keyof Messages>(
176
+ message: T,
177
+ ...args: Messages[T] extends never ? [] : [Messages[T]]
178
+ ) => string;
179
+
180
+ /**
181
+ * Get a URL serving an asset for the given asset id.
182
+ *
183
+ * @param assetId The id of the asset whose URL to get.
184
+ * @returns The URL that matches the given asset id.
185
+ */
186
+ asset: (assetId: string) => string;
187
+
188
+ /**
189
+ * Convert a FontAwesome icon name into a valid FontAwesome CSS class.
190
+ *
191
+ * @param icon The FontAwesome icon.
192
+ * @returns String containing the FontAwesome classes for the icon.
193
+ */
194
+ fa: (icon: IconName) => string;
195
+
196
+ /**
197
+ * @param items The list of menu items to display.
198
+ * @param header A header that can be used to separate from other block menus.
199
+ */
200
+ menu: (items: MenuItem[], header?: string) => void;
201
+ }
202
+
203
+ export interface Events {
204
+ /**
205
+ * Emit an Appsemble event.
206
+ *
207
+ * @param type The type of event to emit.
208
+ * @param data Data to emit with the event.
209
+ * @returns Boolean indicating whether an emitter is implemented or not.
210
+ */
211
+ emit: { [K in keyof EventEmitters]: (data: unknown, error?: string) => Promise<boolean> };
212
+
213
+ /**
214
+ * Remove an event listener for an Appsemble event.
215
+ *
216
+ * @param type The type of event to listen remove the listener from.
217
+ * @param callback The callback to remove.
218
+ * @returns Boolean indicating whether a listener is implemented or not.
219
+ */
220
+ off: {
221
+ [K in keyof EventListeners]: <T>(callback: (data: T, error?: string) => void) => boolean;
222
+ };
223
+
224
+ /**
225
+ * Add an event listener for an Appsemble event.
226
+ *
227
+ * @param type The type of event to listen on.
228
+ * @param callback A callback to register for the event.
229
+ * @returns Boolean indicating whether a listener is implemented or not.
230
+ */
231
+ on: {
232
+ [K in keyof EventListeners]: <T>(callback: (data: T, error?: string) => void) => boolean;
233
+ };
234
+ }
235
+
236
+ /**
237
+ * The parameters that get passed to the bootstrap function.
238
+ */
239
+ export interface BootstrapParams {
240
+ /**
241
+ * The actions that may be dispatched by the block.
242
+ */
243
+ actions: { [K in keyof Actions]: Action };
244
+
245
+ /**
246
+ * The parameters as they are defined in the app definition.
247
+ */
248
+ parameters: Parameters;
249
+
250
+ /**
251
+ * Any kind of data that has been passed in by some context.
252
+ */
253
+ data: unknown;
254
+
255
+ /**
256
+ * Event related functions and constants.
257
+ */
258
+ events: Events;
259
+
260
+ /**
261
+ * URL parameters of the current route.
262
+ *
263
+ * If the page on which the block is rendered, has parameters specified on a page level, the
264
+ * parameter keys and values of the page will be extracted and set as this object.
265
+ */
266
+ pageParameters?: PageParameters;
267
+
268
+ /**
269
+ * The path of the block as it is being rendered on the page using the page’s normalized name.
270
+ */
271
+ path: string;
272
+
273
+ /**
274
+ * The path of the block as it is being rendered on the page using the page’s index.
275
+ */
276
+ pathIndex: string;
277
+
278
+ /**
279
+ * The theme of the block given the context.
280
+ *
281
+ * This is based on a combination of the base theme, app theme, page theme and block theme.
282
+ */
283
+ theme: Theme;
284
+
285
+ /**
286
+ * The shadow root to which DOM elements may be appended.
287
+ */
288
+ shadowRoot: ShadowRoot;
289
+
290
+ /**
291
+ * Some utility functions provided by the Appsemble app framework.
292
+ */
293
+ utils: Utils;
294
+ }
295
+
296
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
297
+ export type BootstrapFunction = (params: BootstrapParams) => Promisable<Element | void>;
298
+
299
+ export interface AppsembleBootstrapEvent extends CustomEvent {
300
+ detail: {
301
+ fn: BootstrapFunction;
302
+ document: Document;
303
+ };
304
+ }
305
+
306
+ /**
307
+ * Register a boostrap function.
308
+ *
309
+ * @param fn The bootstrap function to register
310
+ *
311
+ * If the function returns an element, it’s appended to the shadow root.
312
+ */
313
+ export function bootstrap(fn: BootstrapFunction): void {
314
+ const event: AppsembleBootstrapEvent = new CustomEvent('AppsembleBootstrap', {
315
+ detail: {
316
+ fn,
317
+ document,
318
+ },
319
+ });
320
+ if (document.currentScript) {
321
+ document.currentScript.dispatchEvent(event);
322
+ }
323
+ }
package/jest.config.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('../../config/jest/config')(__dirname);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appsemble/sdk",
3
- "version": "0.20.10",
3
+ "version": "0.20.13",
4
4
  "description": "Build your own blocks",
5
5
  "keywords": [
6
6
  "app",
@@ -23,16 +23,17 @@
23
23
  "license": "LGPL-3.0-only",
24
24
  "author": "Appsemble <info@appsemble.com> (https://appsemble.com)",
25
25
  "sideEffects": false,
26
- "main": "dist",
27
26
  "files": [
28
- "dist"
27
+ "**/*.d.ts",
28
+ "**/*.js",
29
+ "!*.config.js"
29
30
  ],
30
31
  "scripts": {
31
- "prepack": "tsc -p tsconfig.build.json",
32
+ "prepack": "tsc --noEmit false",
32
33
  "test": "jest"
33
34
  },
34
35
  "dependencies": {
35
- "@appsemble/types": "0.20.10",
36
+ "@appsemble/types": "0.20.13",
36
37
  "@fortawesome/fontawesome-common-types": "^6.0.0",
37
38
  "type-fest": "^2.0.0"
38
39
  }
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig",
3
+ "compilerOptions": {
4
+ "lib": ["dom", "dom.iterable", "esnext"]
5
+ }
6
+ }
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiTA;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,EAAqB;IAC7C,MAAM,KAAK,GAA4B,IAAI,WAAW,CAAC,oBAAoB,EAAE;QAC3E,MAAM,EAAE;YACN,EAAE;YACF,QAAQ;SACT;KACF,CAAC,CAAC;IACH,IAAI,QAAQ,CAAC,aAAa,EAAE;QAC1B,QAAQ,CAAC,aAAa,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;KAC7C;AACH,CAAC"}