lucid-extension-sdk 0.0.45 → 0.0.46

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucid-extension-sdk",
3
- "version": "0.0.45",
3
+ "version": "0.0.46",
4
4
  "description": "Utility classes for writing Lucid Software editor extensions",
5
5
  "main": "sdk/index.js",
6
6
  "types": "sdk/index.d.ts",
@@ -99,3 +99,14 @@ export declare function isEmptyOrNullishObject(val: unknown): val is {};
99
99
  export declare function isAny(val: unknown): val is any;
100
100
  export declare function isUnknown(val: unknown): val is unknown;
101
101
  export declare function isPromise(val: unknown): val is Promise<unknown>;
102
+ export declare function isLiteral<T extends string | number | symbol>(t: T): (x: unknown) => x is T;
103
+ declare type AbstractConstructor<T> = Function & {
104
+ prototype: T;
105
+ };
106
+ /**
107
+ * Since `instanceof` should generally be avoided, this function should generally be avoided as well.
108
+ *
109
+ * A valid use case is for native types, like Uint8Array.
110
+ */
111
+ export declare function isInstanceOf<T>(klass: AbstractConstructor<T>): (x: unknown) => x is T;
112
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPromise = exports.isUnknown = exports.isAny = exports.isEmptyOrNullishObject = exports.isTypedArray = exports.isArray = exports.isRecord = exports.isObject = exports.isFunction = exports.isInt = exports.isNumber = exports.isBoolean = exports.isString = exports.isNullish = exports.isDefAndNotNull = exports.isUndefined = exports.isNull = exports.isDef = void 0;
3
+ exports.isInstanceOf = exports.isLiteral = exports.isPromise = exports.isUnknown = exports.isAny = exports.isEmptyOrNullishObject = exports.isTypedArray = exports.isArray = exports.isRecord = exports.isObject = exports.isFunction = exports.isInt = exports.isNumber = exports.isBoolean = exports.isString = exports.isNullish = exports.isDefAndNotNull = exports.isUndefined = exports.isNull = exports.isDef = void 0;
4
4
  /**
5
5
  * Returns true if the specified value is not undefined.
6
6
  *
@@ -163,3 +163,16 @@ function isPromise(val) {
163
163
  return isObject(val) && isFunction(val['then']) && isFunction(val['catch']);
164
164
  }
165
165
  exports.isPromise = isPromise;
166
+ function isLiteral(t) {
167
+ return (x) => x === t;
168
+ }
169
+ exports.isLiteral = isLiteral;
170
+ /**
171
+ * Since `instanceof` should generally be avoided, this function should generally be avoided as well.
172
+ *
173
+ * A valid use case is for native types, like Uint8Array.
174
+ */
175
+ function isInstanceOf(klass) {
176
+ return (x) => x instanceof klass;
177
+ }
178
+ exports.isInstanceOf = isInstanceOf;
@@ -0,0 +1,49 @@
1
+ import { SendXHRResponseFormat } from '../commandtypes';
2
+ import { Validator } from './guards';
3
+ export interface XHRRequest {
4
+ /** URL to request */
5
+ url: string;
6
+ /** HTTP method, e.g. "POST". If omitted, GET is used */
7
+ method?: string;
8
+ /** The data to send as the body of the request */
9
+ data?: string;
10
+ /** Headers to send with the request. If specifying an array, multiple headers with the same name will be sent */
11
+ headers?: {
12
+ [key: string]: string | string[];
13
+ };
14
+ /** If specified, this request should time out after the given number of milliseconds */
15
+ timeoutMs?: number;
16
+ /**
17
+ * The desired format for the returned response body. Defaults to 'utf8'.
18
+ *
19
+ * - If 'utf8', the response body will be returned as a string.
20
+ * - If 'binary', the response body will be returned as a Uint8Array.
21
+ */
22
+ responseFormat?: SendXHRResponseFormat;
23
+ }
24
+ export interface BaseXHRResponse {
25
+ /** URL of the final response, after any redirects */
26
+ url: string;
27
+ /** HTTP status, e.g. 200 or 404 */
28
+ status: number;
29
+ /** Headers sent by the server in the response */
30
+ headers: {
31
+ [key: string]: string;
32
+ };
33
+ /** True if this request failed due to a timeout */
34
+ timeout?: boolean;
35
+ }
36
+ export interface TextXHRResponse extends BaseXHRResponse {
37
+ responseFormat: 'utf8';
38
+ /** Plain text of the response body */
39
+ responseText: string;
40
+ }
41
+ export interface BinaryXHRResponse extends BaseXHRResponse {
42
+ responseFormat: 'binary';
43
+ /** Contents of the response body */
44
+ responseData: Uint8Array;
45
+ }
46
+ export declare type XHRResponse = TextXHRResponse | BinaryXHRResponse;
47
+ export declare const isTextXHRResponse: Validator<TextXHRResponse>;
48
+ export declare const isBinaryXHRResponse: Validator<BinaryXHRResponse>;
49
+ export declare const isXHRResponse: Validator<XHRResponse>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isXHRResponse = exports.isBinaryXHRResponse = exports.isTextXHRResponse = void 0;
4
+ const checks_1 = require("./checks");
5
+ const validators_1 = require("./validators/validators");
6
+ const isBaseXHRResponse = (0, validators_1.objectValidator)({
7
+ url: checks_1.isString,
8
+ status: checks_1.isNumber,
9
+ headers: (0, validators_1.objectOfValidator)(checks_1.isString),
10
+ timeout: (0, validators_1.option)(checks_1.isBoolean),
11
+ });
12
+ exports.isTextXHRResponse = (0, validators_1.both)(isBaseXHRResponse, (0, validators_1.objectValidator)({
13
+ responseFormat: (0, checks_1.isLiteral)('utf8'),
14
+ responseText: checks_1.isString,
15
+ }));
16
+ exports.isBinaryXHRResponse = (0, validators_1.both)(isBaseXHRResponse, (0, validators_1.objectValidator)({
17
+ responseFormat: (0, checks_1.isLiteral)('binary'),
18
+ responseData: (0, checks_1.isInstanceOf)(Uint8Array),
19
+ }));
20
+ exports.isXHRResponse = (0, validators_1.either)(exports.isTextXHRResponse, exports.isBinaryXHRResponse);
@@ -1,6 +1,7 @@
1
- import { CommandArgs, CommandName, SendXHRResponseFormat, UnionToIntersection } from './commandtypes';
1
+ import { CommandArgs, CommandName, UnionToIntersection } from './commandtypes';
2
2
  import { JsonSerializable } from './core/jsonserializable';
3
3
  import { UnfurlCallbacks } from './core/unfurl/unfurlcallbacks';
4
+ import { BinaryXHRResponse, TextXHRResponse, XHRRequest, XHRResponse } from './core/xhr';
4
5
  import { CollectionProxy } from './data/collectionproxy';
5
6
  import { BlockDefinition } from './document/blockdefinition';
6
7
  import { BlockProxy } from './document/blockproxy';
@@ -9,50 +10,6 @@ import { GroupProxy } from './document/groupproxy';
9
10
  import { LineProxy } from './document/lineproxy';
10
11
  import { PageProxy } from './document/pageproxy';
11
12
  import { FileUploadData } from './ui/menu';
12
- export interface XHRRequest {
13
- /** URL to request */
14
- url: string;
15
- /** HTTP method, e.g. "POST". If omitted, GET is used */
16
- method?: string;
17
- /** The data to send as the body of the request */
18
- data?: string;
19
- /** Headers to send with the request. If specifying an array, multiple headers with the same name will be sent */
20
- headers?: {
21
- [key: string]: string | string[];
22
- };
23
- /** If specified, this request should time out after the given number of milliseconds */
24
- timeoutMs?: number;
25
- /**
26
- * The desired format for the returned response body. Defaults to 'utf8'.
27
- *
28
- * - If 'utf8', the response body will be returned as a string.
29
- * - If 'binary', the response body will be returned as a Uint8Array.
30
- */
31
- responseFormat?: SendXHRResponseFormat;
32
- }
33
- export interface BaseXHRResponse {
34
- /** URL of the final response, after any redirects */
35
- url: string;
36
- /** HTTP status, e.g. 200 or 404 */
37
- status: number;
38
- /** Headers sent by the server in the response */
39
- headers: {
40
- [key: string]: string;
41
- };
42
- /** True if this request failed due to a timeout */
43
- timeout?: boolean;
44
- }
45
- export interface TextXHRResponse extends BaseXHRResponse {
46
- responseFormat: 'utf8';
47
- /** Plain text of the response body */
48
- responseText: string;
49
- }
50
- export interface BinaryXHRResponse extends BaseXHRResponse {
51
- responseFormat: 'binary';
52
- /** Contents of the response body */
53
- responseData: Uint8Array;
54
- }
55
- export declare type XHRResponse = TextXHRResponse | BinaryXHRResponse;
56
13
  export declare type DataActionResult = {
57
14
  /** The HTTP Status Code from the Extension Data Sync endpoint */
58
15
  'status': number;
@@ -16,7 +16,7 @@ const elementproxy_1 = require("./document/elementproxy");
16
16
  const groupproxy_1 = require("./document/groupproxy");
17
17
  const lineproxy_1 = require("./document/lineproxy");
18
18
  const pageproxy_1 = require("./document/pageproxy");
19
- const registerunfurlmessage_1 = require("./message/registerunfurlmessage");
19
+ const unfurleventmessage_1 = require("./message/unfurleventmessage");
20
20
  function parseRawXHRResponse(responseFormat, raw) {
21
21
  var _a, _b, _c, _d;
22
22
  return Object.assign({ url: (_a = raw === null || raw === void 0 ? void 0 : raw['url']) !== null && _a !== void 0 ? _a : '', status: (_b = raw === null || raw === void 0 ? void 0 : raw['s']) !== null && _b !== void 0 ? _b : 0, headers: (_c = raw === null || raw === void 0 ? void 0 : raw['h']) !== null && _c !== void 0 ? _c : {}, timeout: raw === null || raw === void 0 ? void 0 : raw['to'] }, (responseFormat === 'utf8'
@@ -207,12 +207,17 @@ class EditorClient {
207
207
  const action = this.getUniqueActionName();
208
208
  this.registerAction(action, async (rawMsg) => {
209
209
  var _a;
210
- const msg = (0, registerunfurlmessage_1.deserializeRegisterUnfurlMessage)(rawMsg);
210
+ const msg = (0, unfurleventmessage_1.deserializeUnfurlEventMessage)(rawMsg);
211
211
  switch (msg.unfurlCallbackType) {
212
212
  case unfurlcallbacks_1.UnfurlCallbackType.Unfurl: {
213
- const result = await callbacks.unfurlCallback(msg.url);
214
- if (result && !(0, unfurlrefresherrortype_1.unfurlRefreshErrorTypeValidator)(result)) {
215
- return (0, unfurldetails_1.serializeUnfurlDetails)(result);
213
+ try {
214
+ const result = await callbacks.unfurlCallback(msg.url);
215
+ if (result && !(0, unfurlrefresherrortype_1.unfurlRefreshErrorTypeValidator)(result)) {
216
+ return (0, unfurldetails_1.serializeUnfurlDetails)(result);
217
+ }
218
+ }
219
+ catch (err) {
220
+ return unfurlrefresherrortype_1.UnfurlRefreshErrorType.GenericFailure;
216
221
  }
217
222
  break;
218
223
  }
package/sdk/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './core/offsettype';
5
5
  export * from './core/serializeddataerror';
6
6
  export * from './core/shapedatainheritance';
7
7
  export * from './core/unfurl/unfurldetails';
8
+ export * from './core/xhr';
8
9
  export * from './data/collectionproxy';
9
10
  export * from './data/dataerror';
10
11
  export * from './data/dataitemproxy';
package/sdk/index.js CHANGED
@@ -17,6 +17,7 @@ __exportStar(require("./core/offsettype"), exports);
17
17
  __exportStar(require("./core/serializeddataerror"), exports);
18
18
  __exportStar(require("./core/shapedatainheritance"), exports);
19
19
  __exportStar(require("./core/unfurl/unfurldetails"), exports);
20
+ __exportStar(require("./core/xhr"), exports);
20
21
  __exportStar(require("./data/collectionproxy"), exports);
21
22
  __exportStar(require("./data/dataerror"), exports);
22
23
  __exportStar(require("./data/dataitemproxy"), exports);
@@ -0,0 +1,24 @@
1
+ import { JsonObject } from '../core/jsonserializable';
2
+ import { UnfurlCallbackType } from '../core/unfurl/unfurlcallbacks';
3
+ /** @ignore */
4
+ export interface UnfurlEventMessage {
5
+ /** @ignore */
6
+ id: string;
7
+ /** @ignore */
8
+ url: string;
9
+ /** @ignore */
10
+ unfurlCallbackType: UnfurlCallbackType;
11
+ /** @ignore */
12
+ blockId?: string;
13
+ }
14
+ /** @ignore */
15
+ export interface SerializedUnfurlEventMessage extends JsonObject {
16
+ 'id': string;
17
+ 'u': string;
18
+ 't': UnfurlCallbackType;
19
+ 'b'?: string;
20
+ }
21
+ /** @ignore */
22
+ export declare function deserializeUnfurlEventMessage(raw: SerializedUnfurlEventMessage): UnfurlEventMessage;
23
+ /** @ignore */
24
+ export declare function serializeUnfurlEventMessage(concrete: UnfurlEventMessage): SerializedUnfurlEventMessage;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeUnfurlEventMessage = exports.deserializeUnfurlEventMessage = void 0;
4
+ /** @ignore */
5
+ function deserializeUnfurlEventMessage(raw) {
6
+ return {
7
+ id: raw['id'],
8
+ url: raw['u'],
9
+ unfurlCallbackType: raw['t'],
10
+ blockId: raw['b'],
11
+ };
12
+ }
13
+ exports.deserializeUnfurlEventMessage = deserializeUnfurlEventMessage;
14
+ /** @ignore */
15
+ function serializeUnfurlEventMessage(concrete) {
16
+ return {
17
+ 'id': concrete.id,
18
+ 'u': concrete.url,
19
+ 't': concrete.unfurlCallbackType,
20
+ 'b': concrete.blockId,
21
+ };
22
+ }
23
+ exports.serializeUnfurlEventMessage = serializeUnfurlEventMessage;
@@ -1,32 +0,0 @@
1
- import { JsonObject } from '../../lucid-extension-sdk';
2
- import { isString } from '../core/checks';
3
- import { UnfurlCallbackType } from '../core/unfurl/unfurlcallbacks';
4
- /** @ignore */
5
- export interface RegisterUnfurlMessage {
6
- /** @ignore */
7
- id: string;
8
- /** @ignore */
9
- url: string;
10
- /** @ignore */
11
- unfurlCallbackType: UnfurlCallbackType;
12
- /** @ignore */
13
- blockId?: string;
14
- }
15
- /** @ignore */
16
- export interface SerializedRegisterUnfurlMessage extends JsonObject {
17
- 'id': string;
18
- 'u': string;
19
- 't': UnfurlCallbackType;
20
- 'b'?: string;
21
- }
22
- /** @ignore */
23
- export declare const isValidRegisterUnfurlMessage: (subject: unknown) => subject is import("../core/guards").DestructureGuardedTypeObj<{
24
- id: typeof isString;
25
- u: typeof isString;
26
- t: (x: unknown) => x is UnfurlCallbackType;
27
- b: (x: unknown) => x is string | undefined;
28
- }>;
29
- /** @ignore */
30
- export declare function deserializeRegisterUnfurlMessage(raw: SerializedRegisterUnfurlMessage): RegisterUnfurlMessage;
31
- /** @ignore */
32
- export declare function serializeRegisterUnfurlMessage(concrete: RegisterUnfurlMessage): SerializedRegisterUnfurlMessage;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.serializeRegisterUnfurlMessage = exports.deserializeRegisterUnfurlMessage = exports.isValidRegisterUnfurlMessage = void 0;
4
- const checks_1 = require("../core/checks");
5
- const unfurlcallbacks_1 = require("../core/unfurl/unfurlcallbacks");
6
- const validators_1 = require("../core/validators/validators");
7
- /** @ignore */
8
- exports.isValidRegisterUnfurlMessage = (0, validators_1.objectValidator)({
9
- 'id': checks_1.isString,
10
- 'u': checks_1.isString,
11
- 't': (0, validators_1.stringEnumValidator)(unfurlcallbacks_1.UnfurlCallbackType),
12
- 'b': (0, validators_1.option)(checks_1.isString),
13
- });
14
- /** @ignore */
15
- function deserializeRegisterUnfurlMessage(raw) {
16
- return {
17
- id: raw['id'],
18
- url: raw['u'],
19
- unfurlCallbackType: raw['t'],
20
- blockId: raw['b'],
21
- };
22
- }
23
- exports.deserializeRegisterUnfurlMessage = deserializeRegisterUnfurlMessage;
24
- /** @ignore */
25
- function serializeRegisterUnfurlMessage(concrete) {
26
- return {
27
- 'id': concrete.id,
28
- 'u': concrete.url,
29
- 't': concrete.unfurlCallbackType,
30
- 'b': concrete.blockId,
31
- };
32
- }
33
- exports.serializeRegisterUnfurlMessage = serializeRegisterUnfurlMessage;