@proef/common-ui 2.6.48 → 2.6.49

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/dist/esm/index.js CHANGED
@@ -120,7 +120,7 @@ import { Kpi } from "./v1/primitives/display/Kpi/Kpi.js";
120
120
  import { LazyLoader } from "./legacy/components/organisms/LazyLoader/LazyLoader.js";
121
121
  import { MonthCalendar } from "./v1/components/data/MonthCalendar/MonthCalendar.js";
122
122
  import { Notice } from "./v1/primitives/display/Notice/Notice.js";
123
- import { OUTBOX_SAVED_MESSAGE, OutboxSavedError, connectOutbox, isOutboxSaved, watchOutbox } from "./v1/capabilities/onepoint-rn/connectOutbox.js";
123
+ import { OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE, OUTBOX_SAVED_MESSAGE, OutboxDependenciesUnsupportedError, OutboxSavedError, connectOutbox, isOutboxSaved, supportsOutboxDependencies, watchOutbox } from "./v1/capabilities/onepoint-rn/connectOutbox.js";
124
124
  import { Pagination } from "./v1/components/data/Pagination/Pagination.js";
125
125
  import { PeopleFilter } from "./v1/primitives/display/PeopleFilter/PeopleFilter.js";
126
126
  import { Person } from "./v1/primitives/display/Person/Person.js";
@@ -283,9 +283,11 @@ export {
283
283
  default52 as MultiTagPicker,
284
284
  default53 as NotFoundPage,
285
285
  Notice,
286
+ OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE,
286
287
  OUTBOX_SAVED_MESSAGE,
287
288
  OfflineBoxes,
288
289
  OfflineBoxesIcon,
290
+ OutboxDependenciesUnsupportedError,
289
291
  OutboxSavedError,
290
292
  PALETA,
291
293
  default54 as Page,
@@ -389,6 +391,7 @@ export {
389
391
  scanQRCodes,
390
392
  scheduleParts,
391
393
  shortDateLabel,
394
+ supportsOutboxDependencies,
392
395
  syncInbox,
393
396
  terminarSessao,
394
397
  termosDaPesquisa,
@@ -1,11 +1,12 @@
1
1
  import { AxiosError, AxiosHeaders } from "axios";
2
- import { onNativeEvent, pedirAoNativo, ErroBridge } from "./bridge.js";
2
+ import { pedirAoNativo, onNativeEvent, ErroBridge } from "./bridge.js";
3
3
  import { dentroDaApp } from "./dentroDaApp.js";
4
4
  import { addressAndPath } from "./requestAddress.js";
5
5
  import { checkedStorageId } from "./storageId.js";
6
6
  const WRITE_METHODS = ["post", "put", "patch", "delete"];
7
7
  const BRIDGE_WAIT_MS = 3e4;
8
8
  const OUTBOX_SAVED_MESSAGE = "Guardado no telemóvel. Será enviado quando a ligação voltar.";
9
+ const OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE = "Esta versão da app não consegue guardar este passo sem ligação. Tente com ligação ou atualize a app.";
9
10
  class OutboxSavedError extends Error {
10
11
  constructor(outboxActionId) {
11
12
  super(OUTBOX_SAVED_MESSAGE);
@@ -35,9 +36,31 @@ function expiryOf(config) {
35
36
  const expiresAt = config.outbox === true ? void 0 : config.outbox?.expiresAt;
36
37
  return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt;
37
38
  }
39
+ function dependenciesOf(config) {
40
+ const dependsOn = config.outbox === true ? void 0 : config.outbox?.dependsOn;
41
+ return dependsOn?.length ? dependsOn : void 0;
42
+ }
43
+ class OutboxDependenciesUnsupportedError extends Error {
44
+ constructor() {
45
+ super(OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE);
46
+ this.name = "OutboxDependenciesUnsupportedError";
47
+ }
48
+ }
49
+ const FEATURES_WAIT_MS = 5e3;
50
+ let features = null;
51
+ async function supportsOutboxDependencies() {
52
+ if (!dentroDaApp()) return false;
53
+ features ??= pedirAoNativo("outbox.features", {}, FEATURES_WAIT_MS).catch(() => {
54
+ features = null;
55
+ return {};
56
+ });
57
+ return (await features).dependsOn === true;
58
+ }
38
59
  function outboxAdapter(api, storageId) {
39
60
  return async (config) => {
40
61
  const { address, path } = addressAndPath(api, config);
62
+ const dependsOn = dependenciesOf(config);
63
+ if (dependsOn && !await supportsOutboxDependencies()) throw new OutboxDependenciesUnsupportedError();
41
64
  const reply = await replyOf({
42
65
  storageId,
43
66
  address,
@@ -45,7 +68,8 @@ function outboxAdapter(api, storageId) {
45
68
  path,
46
69
  headers: stringHeaders(config),
47
70
  body: config.data,
48
- expiresAt: expiryOf(config)
71
+ expiresAt: expiryOf(config),
72
+ dependsOn
49
73
  });
50
74
  if (reply.outcome === "saved") throw new OutboxSavedError(reply.id);
51
75
  const response = {
@@ -109,10 +133,13 @@ function connectOutbox(api, options) {
109
133
  });
110
134
  }
111
135
  export {
136
+ OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE,
112
137
  OUTBOX_SAVED_MESSAGE,
138
+ OutboxDependenciesUnsupportedError,
113
139
  OutboxSavedError,
114
140
  connectOutbox,
115
141
  isOutboxSaved,
142
+ supportsOutboxDependencies,
116
143
  watchOutbox
117
144
  };
118
145
  //# sourceMappingURL=connectOutbox.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"connectOutbox.js","sources":["../../../../../src/v1/capabilities/onepoint-rn/connectOutbox.ts"],"sourcesContent":["import { AxiosError, AxiosHeaders } from 'axios'\r\nimport type { AxiosAdapter, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'\r\nimport { ErroBridge, onNativeEvent, pedirAoNativo } from './bridge'\r\nimport { dentroDaApp } from './dentroDaApp'\r\nimport { addressAndPath } from './requestAddress'\r\nimport { checkedStorageId } from './storageId'\r\n\r\nexport type PedidoOutbox = 'outbox.send' | 'outbox.watch'\r\n\r\nconst WRITE_METHODS = ['post', 'put', 'patch', 'delete']\r\nconst BRIDGE_WAIT_MS = 30_000\r\n\r\nexport const OUTBOX_SAVED_MESSAGE = 'Guardado no telemóvel. Será enviado quando a ligação voltar.'\r\n\r\nexport type OutboxOptions = { storageId: string | undefined }\r\n\r\nexport type OutboxRequestOptions = { expiresAt?: string | Date }\r\n\r\ndeclare module 'axios' {\r\n interface AxiosRequestConfig {\r\n outbox?: true | OutboxRequestOptions\r\n }\r\n}\r\n\r\ntype OutboxReply =\r\n | { outcome: 'delivered' | 'rejected'; status: number; body: unknown }\r\n | { outcome: 'saved'; id: string }\r\n\r\nexport class OutboxSavedError extends Error {\r\n readonly outboxActionId: string | null\r\n\r\n constructor(outboxActionId: string | null) {\r\n super(OUTBOX_SAVED_MESSAGE)\r\n this.name = 'OutboxSavedError'\r\n this.outboxActionId = outboxActionId\r\n }\r\n}\r\n\r\nexport function isOutboxSaved(error: unknown): error is OutboxSavedError {\r\n return error instanceof OutboxSavedError\r\n}\r\n\r\nfunction stringHeaders(config: InternalAxiosRequestConfig): Record<string, string> {\r\n const headers: Record<string, string> = {}\r\n for (const [name, value] of Object.entries(AxiosHeaders.from(config.headers).toJSON())) {\r\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') headers[name] = String(value)\r\n }\r\n return headers\r\n}\r\n\r\nasync function replyOf(carga: unknown): Promise<OutboxReply> {\r\n try {\r\n return await pedirAoNativo<OutboxReply>('outbox.send', carga, BRIDGE_WAIT_MS)\r\n } catch (error) {\r\n if (error instanceof ErroBridge && error.codigo === 'expirou') throw new OutboxSavedError(null)\r\n throw error\r\n }\r\n}\r\n\r\nfunction expiryOf(config: InternalAxiosRequestConfig): string | undefined {\r\n const expiresAt = config.outbox === true ? undefined : config.outbox?.expiresAt\r\n return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt\r\n}\r\n\r\nfunction outboxAdapter(api: AxiosInstance, storageId: string): AxiosAdapter {\r\n return async (config) => {\r\n const { address, path } = addressAndPath(api, config)\r\n const reply = await replyOf({\r\n storageId,\r\n address,\r\n method: config.method,\r\n path,\r\n headers: stringHeaders(config),\r\n body: config.data,\r\n expiresAt: expiryOf(config),\r\n })\r\n if (reply.outcome === 'saved') throw new OutboxSavedError(reply.id)\r\n\r\n const response: AxiosResponse = {\r\n data: reply.body,\r\n status: reply.status,\r\n statusText: '',\r\n headers: {},\r\n config,\r\n request: {},\r\n }\r\n const accepted = config.validateStatus ? config.validateStatus(reply.status) : reply.outcome === 'delivered'\r\n if (accepted) return response\r\n throw new AxiosError(\r\n `Request failed with status code ${reply.status}`,\r\n reply.status >= 500 ? AxiosError.ERR_BAD_RESPONSE : AxiosError.ERR_BAD_REQUEST,\r\n config,\r\n response.request,\r\n response,\r\n )\r\n }\r\n}\r\n\r\nfunction goesThroughOutbox(config: InternalAxiosRequestConfig): boolean {\r\n if (!config.outbox) return false\r\n const method = (config.method ?? 'get').toLowerCase()\r\n if (!WRITE_METHODS.includes(method)) {\r\n throw new Error(`connectOutbox: only writes go through the Outbox, and this ${method.toUpperCase()} is marked with outbox`)\r\n }\r\n const data: unknown = config.data\r\n if (data instanceof FormData || data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\r\n throw new Error('connectOutbox: files do not go through the Outbox yet; send this write without outbox')\r\n }\r\n return dentroDaApp()\r\n}\r\n\r\nexport type OutboxState = 'pending' | 'sending' | 'delivered' | 'rejected' | 'expired' | 'deleted'\r\n\r\nexport type OutboxActionStatus = {\r\n id: string\r\n state: OutboxState\r\n createdAt?: string\r\n reason?: string\r\n method?: string\r\n path?: string\r\n /** Os cabeçalhos guardados com a escrita (nunca o token). */\r\n headers?: Record<string, string>\r\n /** O que vai ser enviado. */\r\n body?: unknown\r\n}\r\n\r\nexport type OutboxWatch = { actions: OutboxActionStatus[]; stop: () => void }\r\n\r\nconst WATCH_WAIT_MS = 10_000\r\n\r\nfunction statusFor(carga: unknown, storageId: string): OutboxActionStatus | null {\r\n if (typeof carga !== 'object' || carga === null) return null\r\n const { storageId: of, ...status } = carga as OutboxActionStatus & { storageId?: string }\r\n return of === storageId ? status : null\r\n}\r\n\r\nexport async function watchOutbox(options: OutboxOptions, onChange: (status: OutboxActionStatus) => void): Promise<OutboxWatch> {\r\n const storageId = checkedStorageId(options.storageId, 'connectOutbox')\r\n if (!dentroDaApp()) return { actions: [], stop: () => undefined }\r\n const stop = onNativeEvent('outbox', (carga) => {\r\n const status = statusFor(carga, storageId)\r\n if (status) onChange(status)\r\n })\r\n try {\r\n const actions = await pedirAoNativo<OutboxActionStatus[]>('outbox.watch', { storageId }, WATCH_WAIT_MS)\r\n return { actions, stop }\r\n } catch (error) {\r\n stop()\r\n throw error\r\n }\r\n}\r\n\r\nexport function connectOutbox(api: AxiosInstance, options: OutboxOptions): void {\r\n const storageId = checkedStorageId(options.storageId, 'connectOutbox')\r\n const adapter = outboxAdapter(api, storageId)\r\n api.interceptors.request.use((config) => {\r\n if (goesThroughOutbox(config)) config.adapter = adapter\r\n return config\r\n })\r\n}\r\n"],"names":[],"mappings":";;;;;AASA,MAAM,gBAAgB,CAAC,QAAQ,OAAO,SAAS,QAAQ;AACvD,MAAM,iBAAiB;AAEhB,MAAM,uBAAuB;AAgB7B,MAAM,yBAAyB,MAAM;AAAA,EAG1C,YAAY,gBAA+B;AACzC,UAAM,oBAAoB;AAC1B,SAAK,OAAO;AACZ,SAAK,iBAAiB;AAAA,EACxB;AACF;AAEO,SAAS,cAAc,OAA2C;AACvE,SAAO,iBAAiB;AAC1B;AAEA,SAAS,cAAc,QAA4D;AACjF,QAAM,UAAkC,CAAA;AACxC,aAAW,CAAC,MAAM,KAAK,KAAK,OAAO,QAAQ,aAAa,KAAK,OAAO,OAAO,EAAE,OAAA,CAAQ,GAAG;AACtF,QAAI,OAAO,UAAU,YAAY,OAAO,UAAU,YAAY,OAAO,UAAU,UAAW,SAAQ,IAAI,IAAI,OAAO,KAAK;AAAA,EACxH;AACA,SAAO;AACT;AAEA,eAAe,QAAQ,OAAsC;AAC3D,MAAI;AACF,WAAO,MAAM,cAA2B,eAAe,OAAO,cAAc;AAAA,EAC9E,SAAS,OAAO;AACd,QAAI,iBAAiB,cAAc,MAAM,WAAW,UAAW,OAAM,IAAI,iBAAiB,IAAI;AAC9F,UAAM;AAAA,EACR;AACF;AAEA,SAAS,SAAS,QAAwD;AACxE,QAAM,YAAY,OAAO,WAAW,OAAO,SAAY,OAAO,QAAQ;AACtE,SAAO,qBAAqB,OAAO,UAAU,YAAA,IAAgB;AAC/D;AAEA,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C;AAC/E,MAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;AACxD,QAAM,EAAE,WAAW,IAAI,GAAG,WAAW;AACrC,SAAO,OAAO,YAAY,SAAS;AACrC;AAEA,eAAsB,YAAY,SAAwB,UAAsE;AAC9H,QAAM,YAAY,iBAAiB,QAAQ,WAAW,eAAe;AACrE,MAAI,CAAC,YAAA,EAAe,QAAO,EAAE,SAAS,IAAI,MAAM,MAAM,OAAA;AACtD,QAAM,OAAO,cAAc,UAAU,CAAC,UAAU;AAC9C,UAAM,SAAS,UAAU,OAAO,SAAS;AACzC,QAAI,iBAAiB,MAAM;AAAA,EAC7B,CAAC;AACD,MAAI;AACF,UAAM,UAAU,MAAM,cAAoC,gBAAgB,EAAE,UAAA,GAAa,aAAa;AACtG,WAAO,EAAE,SAAS,KAAA;AAAA,EACpB,SAAS,OAAO;AACd,SAAA;AACA,UAAM;AAAA,EACR;AACF;AAEO,SAAS,cAAc,KAAoB,SAA8B;AAC9E,QAAM,YAAY,iBAAiB,QAAQ,WAAW,eAAe;AACrE,QAAM,UAAU,cAAc,KAAK,SAAS;AAC5C,MAAI,aAAa,QAAQ,IAAI,CAAC,WAAW;AACvC,QAAI,kBAAkB,MAAM,EAAG,QAAO,UAAU;AAChD,WAAO;AAAA,EACT,CAAC;AACH;"}
1
+ {"version":3,"file":"connectOutbox.js","sources":["../../../../../src/v1/capabilities/onepoint-rn/connectOutbox.ts"],"sourcesContent":["import { AxiosError, AxiosHeaders } from 'axios'\r\nimport type { AxiosAdapter, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'\r\nimport { ErroBridge, onNativeEvent, pedirAoNativo } from './bridge'\r\nimport { dentroDaApp } from './dentroDaApp'\r\nimport { addressAndPath } from './requestAddress'\r\nimport { checkedStorageId } from './storageId'\r\n\r\nexport type PedidoOutbox = 'outbox.send' | 'outbox.watch' | 'outbox.features'\r\n\r\nconst WRITE_METHODS = ['post', 'put', 'patch', 'delete']\r\nconst BRIDGE_WAIT_MS = 30_000\r\n\r\nexport const OUTBOX_SAVED_MESSAGE = 'Guardado no telemóvel. Será enviado quando a ligação voltar.'\r\nexport const OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE =\r\n 'Esta versão da app não consegue guardar este passo sem ligação. Tente com ligação ou atualize a app.'\r\n\r\nexport type OutboxOptions = { storageId: string | undefined }\r\n\r\nexport type OutboxRequestOptions = {\r\n expiresAt?: string | Date\r\n /**\r\n * Ids de escritas ainda por enviar (`OutboxSavedError.outboxActionId`) que têm de chegar antes desta.\r\n * A app só a envia depois de todas entregues; se alguma for recusada, expirar ou for apagada, esta é recusada também.\r\n * Uma escrita já entregue pode ficar de fora, ou ser indicada: a app lembra-se das entregues nos últimos 30 dias.\r\n */\r\n dependsOn?: string[]\r\n}\r\n\r\ndeclare module 'axios' {\r\n interface AxiosRequestConfig {\r\n outbox?: true | OutboxRequestOptions\r\n }\r\n}\r\n\r\ntype OutboxReply =\r\n | { outcome: 'delivered' | 'rejected'; status: number; body: unknown }\r\n | { outcome: 'saved'; id: string }\r\n\r\nexport class OutboxSavedError extends Error {\r\n readonly outboxActionId: string | null\r\n\r\n constructor(outboxActionId: string | null) {\r\n super(OUTBOX_SAVED_MESSAGE)\r\n this.name = 'OutboxSavedError'\r\n this.outboxActionId = outboxActionId\r\n }\r\n}\r\n\r\nexport function isOutboxSaved(error: unknown): error is OutboxSavedError {\r\n return error instanceof OutboxSavedError\r\n}\r\n\r\nfunction stringHeaders(config: InternalAxiosRequestConfig): Record<string, string> {\r\n const headers: Record<string, string> = {}\r\n for (const [name, value] of Object.entries(AxiosHeaders.from(config.headers).toJSON())) {\r\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') headers[name] = String(value)\r\n }\r\n return headers\r\n}\r\n\r\nasync function replyOf(carga: unknown): Promise<OutboxReply> {\r\n try {\r\n return await pedirAoNativo<OutboxReply>('outbox.send', carga, BRIDGE_WAIT_MS)\r\n } catch (error) {\r\n if (error instanceof ErroBridge && error.codigo === 'expirou') throw new OutboxSavedError(null)\r\n throw error\r\n }\r\n}\r\n\r\nfunction expiryOf(config: InternalAxiosRequestConfig): string | undefined {\r\n const expiresAt = config.outbox === true ? undefined : config.outbox?.expiresAt\r\n return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt\r\n}\r\n\r\nfunction dependenciesOf(config: InternalAxiosRequestConfig): string[] | undefined {\r\n const dependsOn = config.outbox === true ? undefined : config.outbox?.dependsOn\r\n return dependsOn?.length ? dependsOn : undefined\r\n}\r\n\r\nexport class OutboxDependenciesUnsupportedError extends Error {\r\n constructor() {\r\n super(OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE)\r\n this.name = 'OutboxDependenciesUnsupportedError'\r\n }\r\n}\r\n\r\ntype OutboxFeatures = { dependsOn?: boolean }\r\nconst FEATURES_WAIT_MS = 5_000\r\nlet features: Promise<OutboxFeatures> | null = null\r\n\r\n/**\r\n * Se a app guarda escritas que dependem de outras (`outbox.dependsOn`). Uma app mais antiga não conhece o pedido\r\n * e ignoraria o campo, enviando a escrita fora de ordem: por isso pergunta-se antes. Fora da app é sempre `false`.\r\n */\r\nexport async function supportsOutboxDependencies(): Promise<boolean> {\r\n if (!dentroDaApp()) return false\r\n features ??= pedirAoNativo<OutboxFeatures>('outbox.features', {}, FEATURES_WAIT_MS).catch(() => {\r\n features = null // sem resposta não é \"não sabe\": volta a perguntar da próxima vez\r\n return {}\r\n })\r\n return (await features).dependsOn === true\r\n}\r\n\r\n/** Só para testes: esquece o que a app disse que sabe fazer. */\r\nexport function forgetOutboxFeatures(): void {\r\n features = null\r\n}\r\n\r\nfunction outboxAdapter(api: AxiosInstance, storageId: string): AxiosAdapter {\r\n return async (config) => {\r\n const { address, path } = addressAndPath(api, config)\r\n const dependsOn = dependenciesOf(config)\r\n if (dependsOn && !(await supportsOutboxDependencies())) throw new OutboxDependenciesUnsupportedError()\r\n const reply = await replyOf({\r\n storageId,\r\n address,\r\n method: config.method,\r\n path,\r\n headers: stringHeaders(config),\r\n body: config.data,\r\n expiresAt: expiryOf(config),\r\n dependsOn,\r\n })\r\n if (reply.outcome === 'saved') throw new OutboxSavedError(reply.id)\r\n\r\n const response: AxiosResponse = {\r\n data: reply.body,\r\n status: reply.status,\r\n statusText: '',\r\n headers: {},\r\n config,\r\n request: {},\r\n }\r\n const accepted = config.validateStatus ? config.validateStatus(reply.status) : reply.outcome === 'delivered'\r\n if (accepted) return response\r\n throw new AxiosError(\r\n `Request failed with status code ${reply.status}`,\r\n reply.status >= 500 ? AxiosError.ERR_BAD_RESPONSE : AxiosError.ERR_BAD_REQUEST,\r\n config,\r\n response.request,\r\n response,\r\n )\r\n }\r\n}\r\n\r\nfunction goesThroughOutbox(config: InternalAxiosRequestConfig): boolean {\r\n if (!config.outbox) return false\r\n const method = (config.method ?? 'get').toLowerCase()\r\n if (!WRITE_METHODS.includes(method)) {\r\n throw new Error(`connectOutbox: only writes go through the Outbox, and this ${method.toUpperCase()} is marked with outbox`)\r\n }\r\n const data: unknown = config.data\r\n if (data instanceof FormData || data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\r\n throw new Error('connectOutbox: files do not go through the Outbox yet; send this write without outbox')\r\n }\r\n return dentroDaApp()\r\n}\r\n\r\nexport type OutboxState = 'pending' | 'sending' | 'delivered' | 'rejected' | 'expired' | 'deleted'\r\n\r\nexport type OutboxActionStatus = {\r\n id: string\r\n state: OutboxState\r\n createdAt?: string\r\n reason?: string\r\n method?: string\r\n path?: string\r\n /** Os cabeçalhos guardados com a escrita (nunca o token). */\r\n headers?: Record<string, string>\r\n /** O que vai ser enviado. */\r\n body?: unknown\r\n /** Escritas por que ainda espera antes de ser enviada. */\r\n dependsOn?: string[]\r\n /** A escrita cuja falha fez recusar esta, por depender dela. */\r\n blockedBy?: string\r\n}\r\n\r\nexport type OutboxWatch = { actions: OutboxActionStatus[]; stop: () => void }\r\n\r\nconst WATCH_WAIT_MS = 10_000\r\n\r\nfunction statusFor(carga: unknown, storageId: string): OutboxActionStatus | null {\r\n if (typeof carga !== 'object' || carga === null) return null\r\n const { storageId: of, ...status } = carga as OutboxActionStatus & { storageId?: string }\r\n return of === storageId ? status : null\r\n}\r\n\r\nexport async function watchOutbox(options: OutboxOptions, onChange: (status: OutboxActionStatus) => void): Promise<OutboxWatch> {\r\n const storageId = checkedStorageId(options.storageId, 'connectOutbox')\r\n if (!dentroDaApp()) return { actions: [], stop: () => undefined }\r\n const stop = onNativeEvent('outbox', (carga) => {\r\n const status = statusFor(carga, storageId)\r\n if (status) onChange(status)\r\n })\r\n try {\r\n const actions = await pedirAoNativo<OutboxActionStatus[]>('outbox.watch', { storageId }, WATCH_WAIT_MS)\r\n return { actions, stop }\r\n } catch (error) {\r\n stop()\r\n throw error\r\n }\r\n}\r\n\r\nexport function connectOutbox(api: AxiosInstance, options: OutboxOptions): void {\r\n const storageId = checkedStorageId(options.storageId, 'connectOutbox')\r\n const adapter = outboxAdapter(api, storageId)\r\n api.interceptors.request.use((config) => {\r\n if (goesThroughOutbox(config)) config.adapter = adapter\r\n return config\r\n })\r\n}\r\n"],"names":[],"mappings":";;;;;AASA,MAAM,gBAAgB,CAAC,QAAQ,OAAO,SAAS,QAAQ;AACvD,MAAM,iBAAiB;AAEhB,MAAM,uBAAuB;AAC7B,MAAM,0CACX;AAwBK,MAAM,yBAAyB,MAAM;AAAA,EAG1C,YAAY,gBAA+B;AACzC,UAAM,oBAAoB;AAC1B,SAAK,OAAO;AACZ,SAAK,iBAAiB;AAAA,EACxB;AACF;AAEO,SAAS,cAAc,OAA2C;AACvE,SAAO,iBAAiB;AAC1B;AAEA,SAAS,cAAc,QAA4D;AACjF,QAAM,UAAkC,CAAA;AACxC,aAAW,CAAC,MAAM,KAAK,KAAK,OAAO,QAAQ,aAAa,KAAK,OAAO,OAAO,EAAE,OAAA,CAAQ,GAAG;AACtF,QAAI,OAAO,UAAU,YAAY,OAAO,UAAU,YAAY,OAAO,UAAU,UAAW,SAAQ,IAAI,IAAI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A;AACtD,QAAM,OAAO,cAAc,UAAU,CAAC,UAAU;AAC9C,UAAM,SAAS,UAAU,OAAO,SAAS;AACzC,QAAI,iBAAiB,MAAM;AAAA,EAC7B,CAAC;AACD,MAAI;AACF,UAAM,UAAU,MAAM,cAAoC,gBAAgB,EAAE,UAAA,GAAa,aAAa;AACtG,WAAO,EAAE,SAAS,KAAA;AAAA,EACpB,SAAS,OAAO;AACd,SAAA;AACA,UAAM;AAAA,EACR;AACF;AAEO,SAAS,cAAc,KAAoB,SAA8B;AAC9E,QAAM,YAAY,iBAAiB,QAAQ,WAAW,eAAe;AACrE,QAAM,UAAU,cAAc,KAAK,SAAS;AAC5C,MAAI,aAAa,QAAQ,IAAI,CAAC,WAAW;AACvC,QAAI,kBAAkB,MAAM,EAAG,QAAO,UAAU;AAChD,WAAO;AAAA,EACT,CAAC;AACH;"}
@@ -1,11 +1,18 @@
1
1
  import type { AxiosInstance } from 'axios';
2
- export type PedidoOutbox = 'outbox.send' | 'outbox.watch';
2
+ export type PedidoOutbox = 'outbox.send' | 'outbox.watch' | 'outbox.features';
3
3
  export declare const OUTBOX_SAVED_MESSAGE = "Guardado no telem\u00F3vel. Ser\u00E1 enviado quando a liga\u00E7\u00E3o voltar.";
4
+ export declare const OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE = "Esta vers\u00E3o da app n\u00E3o consegue guardar este passo sem liga\u00E7\u00E3o. Tente com liga\u00E7\u00E3o ou atualize a app.";
4
5
  export type OutboxOptions = {
5
6
  storageId: string | undefined;
6
7
  };
7
8
  export type OutboxRequestOptions = {
8
9
  expiresAt?: string | Date;
10
+ /**
11
+ * Ids de escritas ainda por enviar (`OutboxSavedError.outboxActionId`) que têm de chegar antes desta.
12
+ * A app só a envia depois de todas entregues; se alguma for recusada, expirar ou for apagada, esta é recusada também.
13
+ * Uma escrita já entregue pode ficar de fora, ou ser indicada: a app lembra-se das entregues nos últimos 30 dias.
14
+ */
15
+ dependsOn?: string[];
9
16
  };
10
17
  declare module 'axios' {
11
18
  interface AxiosRequestConfig {
@@ -17,6 +24,16 @@ export declare class OutboxSavedError extends Error {
17
24
  constructor(outboxActionId: string | null);
18
25
  }
19
26
  export declare function isOutboxSaved(error: unknown): error is OutboxSavedError;
27
+ export declare class OutboxDependenciesUnsupportedError extends Error {
28
+ constructor();
29
+ }
30
+ /**
31
+ * Se a app guarda escritas que dependem de outras (`outbox.dependsOn`). Uma app mais antiga não conhece o pedido
32
+ * e ignoraria o campo, enviando a escrita fora de ordem: por isso pergunta-se antes. Fora da app é sempre `false`.
33
+ */
34
+ export declare function supportsOutboxDependencies(): Promise<boolean>;
35
+ /** Só para testes: esquece o que a app disse que sabe fazer. */
36
+ export declare function forgetOutboxFeatures(): void;
20
37
  export type OutboxState = 'pending' | 'sending' | 'delivered' | 'rejected' | 'expired' | 'deleted';
21
38
  export type OutboxActionStatus = {
22
39
  id: string;
@@ -29,6 +46,10 @@ export type OutboxActionStatus = {
29
46
  headers?: Record<string, string>;
30
47
  /** O que vai ser enviado. */
31
48
  body?: unknown;
49
+ /** Escritas por que ainda espera antes de ser enviada. */
50
+ dependsOn?: string[];
51
+ /** A escrita cuja falha fez recusar esta, por depender dela. */
52
+ blockedBy?: string;
32
53
  };
33
54
  export type OutboxWatch = {
34
55
  actions: OutboxActionStatus[];
@@ -15,7 +15,7 @@ export { ErroBridge, pedirAoNativo, type CodigoErroBridge, type TipoPedido } fro
15
15
  export { nativo } from './capabilities/onepoint-rn/nativo';
16
16
  export { type Coordenadas, type RespostaFicheiro } from './capabilities/onepoint-rn/device';
17
17
  export { type OrientacaoEcra } from './capabilities/onepoint-rn/frame';
18
- export { connectOutbox, isOutboxSaved, OutboxSavedError, OUTBOX_SAVED_MESSAGE, type OutboxOptions, type OutboxRequestOptions, watchOutbox, type OutboxActionStatus, type OutboxState, type OutboxWatch, } from './capabilities/onepoint-rn/connectOutbox';
18
+ export { connectOutbox, isOutboxSaved, OutboxSavedError, OUTBOX_SAVED_MESSAGE, OutboxDependenciesUnsupportedError, OUTBOX_DEPENDENCIES_UNSUPPORTED_MESSAGE, supportsOutboxDependencies, type OutboxOptions, type OutboxRequestOptions, watchOutbox, type OutboxActionStatus, type OutboxState, type OutboxWatch, } from './capabilities/onepoint-rn/connectOutbox';
19
19
  export { connectInbox, INBOX_SAVED_AT_HEADER, inboxSavedAt, inboxStatus, readInbox, syncInbox, watchInbox, type InboxBundle, type InboxEvent, type InboxConnectOptions, type InboxEach, type InboxGet, type InboxOptions, type InboxProgress, type InboxRead, type InboxStatus, type InboxSyncOptions, type InboxTier, } from './capabilities/onepoint-rn/connectInbox';
20
20
  export { useOfflineBoxes, type OfflineBoxesOptions, type OfflineBoxesState } from './capabilities/onepoint-rn/useOfflineBoxes';
21
21
  export { default as OfflineBoxes, DEFAULT_OFFLINE_BOXES_LABELS } from './components/feedback/OfflineBoxes/OfflineBoxes';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proef/common-ui",
3
- "version": "2.6.48",
3
+ "version": "2.6.49",
4
4
  "description": "Biblioteca de componentes e tema partilhada pelos portais Proef.",
5
5
  "repository": {
6
6
  "type": "git",