@simpleplatform/sdk 3.0.1 → 3.0.3

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
@@ -358,6 +358,9 @@ while `simple.records.current()` rejects with `SpaceProtocolError` code
358
358
  In an older host, `records.current()` can remain available through the `record`
359
359
  capability while `records.open()` requires the newer `records` capability.
360
360
 
361
+ A Space has one connection per page. The first `connect()` opens it, and every
362
+ later call returns the same client. A call with a different `targetOrigin` is rejected.
363
+
361
364
  When `connect()` runs in the Space document, it disables vertical root
362
365
  overscroll bounce. Normal scrolling inside the Space remains enabled.
363
366
 
@@ -209,21 +209,22 @@ function createDataRequestId() {
209
209
  return `space-data-${Math.random().toString(36).slice(2)}`;
210
210
  }
211
211
  function readGraphQLErrorMessage(error, errors) {
212
+ const firstError = Array.isArray(errors) ? errors[0] : undefined;
213
+ const details = firstError && typeof firstError === 'object'
214
+ ? firstError
215
+ : undefined;
216
+ // The host always sends `error` beside `errors`, and `error` holds only the
217
+ // top-level messages. The text a person can act on is under `extensions`,
218
+ // so it is read first.
219
+ const issue = details?.extensions?.issues?.[0]?.message;
220
+ if (typeof issue === 'string' && issue)
221
+ return issue;
222
+ const detail = details?.extensions?.details?.message;
223
+ if (typeof detail === 'string' && detail)
224
+ return detail;
212
225
  if (typeof error === 'string' && error)
213
226
  return error;
214
- if (Array.isArray(errors)) {
215
- const firstError = errors[0];
216
- if (firstError && typeof firstError === 'object') {
217
- const details = firstError;
218
- const issue = details.extensions?.issues?.[0]?.message;
219
- if (typeof issue === 'string' && issue)
220
- return issue;
221
- const message = details.extensions?.details?.message;
222
- if (typeof message === 'string' && message)
223
- return message;
224
- if (typeof details.message === 'string' && details.message)
225
- return details.message;
226
- }
227
- }
227
+ if (typeof details?.message === 'string' && details.message)
228
+ return details.message;
228
229
  return 'GraphQL request failed.';
229
230
  }
@@ -2,6 +2,7 @@ import { createMessagePortTransport } from './browser-transport.js';
2
2
  import { createSimpleClient, HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, isSpaceContext, MANAGED_RECORD_FORM_PROTOCOL_VERSION, PROTOCOL_VERSION, RECORDS_PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION, } from './core.js';
3
3
  import { isObjectRecord } from './protocol.js';
4
4
  export { HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, PROTOCOL_VERSION, RECORDS_PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION };
5
+ const connections = new WeakMap();
5
6
  /** Connects this Space to its host using the explicitly supplied host origin. */
6
7
  export function connect({ targetOrigin }) {
7
8
  if (!isOrigin(targetOrigin)) {
@@ -17,9 +18,18 @@ export function connect({ targetOrigin }) {
17
18
  message: 'The Space SDK requires a browser window.',
18
19
  }));
19
20
  }
21
+ const existing = connections.get(window);
22
+ if (existing) {
23
+ if (existing.targetOrigin === targetOrigin)
24
+ return existing.promise;
25
+ return Promise.reject(new SpaceProtocolError({
26
+ code: 'invalid_request',
27
+ message: 'connect() already opened this Space\'s connection to a different targetOrigin.',
28
+ }));
29
+ }
20
30
  // The host cannot style a cross-origin frame's viewport, where edge bounce occurs.
21
31
  window.document.documentElement.style.overscrollBehaviorY = 'none';
22
- return new Promise((resolve, reject) => {
32
+ const promise = new Promise((resolve, reject) => {
23
33
  const onMessage = (event) => {
24
34
  if (event.origin !== targetOrigin || !isInitializationMessage(event.data))
25
35
  return;
@@ -78,7 +88,12 @@ export function connect({ targetOrigin }) {
78
88
  },
79
89
  type: 'SPACE_READY',
80
90
  }, targetOrigin);
91
+ }).catch((error) => {
92
+ connections.delete(window);
93
+ throw error;
81
94
  });
95
+ connections.set(window, { promise, targetOrigin });
96
+ return promise;
82
97
  }
83
98
  function isOrigin(targetOrigin) {
84
99
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleplatform/sdk",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "Simple Platform Typescript SDK",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://docs.simple.dev",