@proef/common-ui 2.6.32 → 2.6.33
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.
|
@@ -52,7 +52,7 @@ async function replyOf(carga) {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
function expiryOf(config) {
|
|
55
|
-
const expiresAt = config.outbox?.expiresAt;
|
|
55
|
+
const expiresAt = config.outbox === true ? void 0 : config.outbox?.expiresAt;
|
|
56
56
|
return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt;
|
|
57
57
|
}
|
|
58
58
|
function outboxAdapter(api, outboxId) {
|
|
@@ -88,10 +88,16 @@ function outboxAdapter(api, outboxId) {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
function goesThroughOutbox(config) {
|
|
91
|
-
|
|
91
|
+
if (!config.outbox) return false;
|
|
92
|
+
const method = (config.method ?? "get").toLowerCase();
|
|
93
|
+
if (!WRITE_METHODS.includes(method)) {
|
|
94
|
+
throw new Error(`connectOutbox: only writes go through the Outbox, and this ${method.toUpperCase()} is marked with outbox`);
|
|
95
|
+
}
|
|
92
96
|
const data = config.data;
|
|
93
|
-
|
|
94
|
-
|
|
97
|
+
if (data instanceof FormData || data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
98
|
+
throw new Error("connectOutbox: files do not go through the Outbox yet; send this write without outbox");
|
|
99
|
+
}
|
|
100
|
+
return dentroDaApp();
|
|
95
101
|
}
|
|
96
102
|
const WATCH_WAIT_MS = 1e4;
|
|
97
103
|
function statusFor(carga, outboxId) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connectOutbox.js","sources":["../../../../../src/v1/capabilities/onepoint-rn/connectOutbox.ts"],"sourcesContent":["import { AxiosError, AxiosHeaders } from 'axios'\nimport type { AxiosAdapter, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'\nimport { ErroBridge, onNativeEvent, pedirAoNativo } from './bridge'\nimport { dentroDaApp } from './dentroDaApp'\n\nexport type PedidoOutbox = 'outbox.send' | 'outbox.watch'\n\nconst OUTBOX_ID_FORMAT = /^[a-z0-9]+(-[a-z0-9]+)*$/\nconst WRITE_METHODS = ['post', 'put', 'patch', 'delete']\nconst BRIDGE_WAIT_MS = 30_000\n\nexport const OUTBOX_SAVED_MESSAGE = 'Guardado no telemóvel. Será enviado quando a ligação voltar.'\n\nexport type OutboxOptions = { outboxId: string | undefined }\n\nexport type OutboxRequestOptions = { expiresAt?: string | Date }\n\ndeclare module 'axios' {\n interface AxiosRequestConfig {\n outbox?: OutboxRequestOptions\n }\n}\n\ntype OutboxReply =\n | { outcome: 'delivered' | 'rejected'; status: number; body: unknown }\n | { outcome: 'saved'; id: string }\n\nexport class OutboxSavedError extends Error {\n readonly outboxActionId: string | null\n\n constructor(outboxActionId: string | null) {\n super(OUTBOX_SAVED_MESSAGE)\n this.name = 'OutboxSavedError'\n this.outboxActionId = outboxActionId\n }\n}\n\nexport function isOutboxSaved(error: unknown): error is OutboxSavedError {\n return error instanceof OutboxSavedError\n}\n\nfunction checkedOutboxId(outboxId: string | undefined): string {\n if (outboxId && OUTBOX_ID_FORMAT.test(outboxId)) return outboxId\n throw new Error(\n `connectOutbox: outboxId \"${String(outboxId)}\" is not valid. Declare one per portal and environment in the portal's .env, ` +\n 'using lowercase letters, digits and hyphens: production agendamentoedp, DEV agendamentoedp-dev. ' +\n 'The OnePoint app ties it to the portal\\'s Entra app and refuses it from any other portal.',\n )\n}\n\nfunction absolute(url: string): string {\n return new URL(url, window.location.href).href\n}\n\nfunction withoutTrailingSlash(url: string): string {\n return url.replace(/\\/+$/, '')\n}\n\nfunction addressAndPath(api: AxiosInstance, config: InternalAxiosRequestConfig): { address: string; path: string } {\n const full = absolute(api.getUri(config))\n const base = withoutTrailingSlash(absolute(config.baseURL ?? window.location.origin))\n if (full === base || full.startsWith(`${base}/`) || full.startsWith(`${base}?`)) {\n return { address: base, path: full.slice(base.length) }\n }\n const origin = new URL(full).origin\n return { address: origin, path: full.slice(origin.length) }\n}\n\nfunction stringHeaders(config: InternalAxiosRequestConfig): Record<string, string> {\n const headers: Record<string, string> = {}\n for (const [name, value] of Object.entries(AxiosHeaders.from(config.headers).toJSON())) {\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') headers[name] = String(value)\n }\n return headers\n}\n\nasync function replyOf(carga: unknown): Promise<OutboxReply> {\n try {\n return await pedirAoNativo<OutboxReply>('outbox.send', carga, BRIDGE_WAIT_MS)\n } catch (error) {\n if (error instanceof ErroBridge && error.codigo === 'expirou') throw new OutboxSavedError(null)\n throw error\n }\n}\n\nfunction expiryOf(config: InternalAxiosRequestConfig): string | undefined {\n const expiresAt = config.outbox?.expiresAt\n return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt\n}\n\nfunction outboxAdapter(api: AxiosInstance, outboxId: string): AxiosAdapter {\n return async (config) => {\n const { address, path } = addressAndPath(api, config)\n const reply = await replyOf({\n outboxId,\n address,\n method: config.method,\n path,\n headers: stringHeaders(config),\n body: config.data,\n expiresAt: expiryOf(config),\n })\n if (reply.outcome === 'saved') throw new OutboxSavedError(reply.id)\n\n const response: AxiosResponse = {\n data: reply.body,\n status: reply.status,\n statusText: '',\n headers: {},\n config,\n request: {},\n }\n const accepted = config.validateStatus ? config.validateStatus(reply.status) : reply.outcome === 'delivered'\n if (accepted) return response\n throw new AxiosError(\n `Request failed with status code ${reply.status}`,\n reply.status >= 500 ? AxiosError.ERR_BAD_RESPONSE : AxiosError.ERR_BAD_REQUEST,\n config,\n response.request,\n response,\n )\n }\n}\n\nfunction goesThroughOutbox(config: InternalAxiosRequestConfig): boolean {\n const
|
|
1
|
+
{"version":3,"file":"connectOutbox.js","sources":["../../../../../src/v1/capabilities/onepoint-rn/connectOutbox.ts"],"sourcesContent":["import { AxiosError, AxiosHeaders } from 'axios'\nimport type { AxiosAdapter, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'\nimport { ErroBridge, onNativeEvent, pedirAoNativo } from './bridge'\nimport { dentroDaApp } from './dentroDaApp'\n\nexport type PedidoOutbox = 'outbox.send' | 'outbox.watch'\n\nconst OUTBOX_ID_FORMAT = /^[a-z0-9]+(-[a-z0-9]+)*$/\nconst WRITE_METHODS = ['post', 'put', 'patch', 'delete']\nconst BRIDGE_WAIT_MS = 30_000\n\nexport const OUTBOX_SAVED_MESSAGE = 'Guardado no telemóvel. Será enviado quando a ligação voltar.'\n\nexport type OutboxOptions = { outboxId: string | undefined }\n\nexport type OutboxRequestOptions = { expiresAt?: string | Date }\n\ndeclare module 'axios' {\n interface AxiosRequestConfig {\n outbox?: true | OutboxRequestOptions\n }\n}\n\ntype OutboxReply =\n | { outcome: 'delivered' | 'rejected'; status: number; body: unknown }\n | { outcome: 'saved'; id: string }\n\nexport class OutboxSavedError extends Error {\n readonly outboxActionId: string | null\n\n constructor(outboxActionId: string | null) {\n super(OUTBOX_SAVED_MESSAGE)\n this.name = 'OutboxSavedError'\n this.outboxActionId = outboxActionId\n }\n}\n\nexport function isOutboxSaved(error: unknown): error is OutboxSavedError {\n return error instanceof OutboxSavedError\n}\n\nfunction checkedOutboxId(outboxId: string | undefined): string {\n if (outboxId && OUTBOX_ID_FORMAT.test(outboxId)) return outboxId\n throw new Error(\n `connectOutbox: outboxId \"${String(outboxId)}\" is not valid. Declare one per portal and environment in the portal's .env, ` +\n 'using lowercase letters, digits and hyphens: production agendamentoedp, DEV agendamentoedp-dev. ' +\n 'The OnePoint app ties it to the portal\\'s Entra app and refuses it from any other portal.',\n )\n}\n\nfunction absolute(url: string): string {\n return new URL(url, window.location.href).href\n}\n\nfunction withoutTrailingSlash(url: string): string {\n return url.replace(/\\/+$/, '')\n}\n\nfunction addressAndPath(api: AxiosInstance, config: InternalAxiosRequestConfig): { address: string; path: string } {\n const full = absolute(api.getUri(config))\n const base = withoutTrailingSlash(absolute(config.baseURL ?? window.location.origin))\n if (full === base || full.startsWith(`${base}/`) || full.startsWith(`${base}?`)) {\n return { address: base, path: full.slice(base.length) }\n }\n const origin = new URL(full).origin\n return { address: origin, path: full.slice(origin.length) }\n}\n\nfunction stringHeaders(config: InternalAxiosRequestConfig): Record<string, string> {\n const headers: Record<string, string> = {}\n for (const [name, value] of Object.entries(AxiosHeaders.from(config.headers).toJSON())) {\n if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') headers[name] = String(value)\n }\n return headers\n}\n\nasync function replyOf(carga: unknown): Promise<OutboxReply> {\n try {\n return await pedirAoNativo<OutboxReply>('outbox.send', carga, BRIDGE_WAIT_MS)\n } catch (error) {\n if (error instanceof ErroBridge && error.codigo === 'expirou') throw new OutboxSavedError(null)\n throw error\n }\n}\n\nfunction expiryOf(config: InternalAxiosRequestConfig): string | undefined {\n const expiresAt = config.outbox === true ? undefined : config.outbox?.expiresAt\n return expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt\n}\n\nfunction outboxAdapter(api: AxiosInstance, outboxId: string): AxiosAdapter {\n return async (config) => {\n const { address, path } = addressAndPath(api, config)\n const reply = await replyOf({\n outboxId,\n address,\n method: config.method,\n path,\n headers: stringHeaders(config),\n body: config.data,\n expiresAt: expiryOf(config),\n })\n if (reply.outcome === 'saved') throw new OutboxSavedError(reply.id)\n\n const response: AxiosResponse = {\n data: reply.body,\n status: reply.status,\n statusText: '',\n headers: {},\n config,\n request: {},\n }\n const accepted = config.validateStatus ? config.validateStatus(reply.status) : reply.outcome === 'delivered'\n if (accepted) return response\n throw new AxiosError(\n `Request failed with status code ${reply.status}`,\n reply.status >= 500 ? AxiosError.ERR_BAD_RESPONSE : AxiosError.ERR_BAD_REQUEST,\n config,\n response.request,\n response,\n )\n }\n}\n\nfunction goesThroughOutbox(config: InternalAxiosRequestConfig): boolean {\n if (!config.outbox) return false\n const method = (config.method ?? 'get').toLowerCase()\n if (!WRITE_METHODS.includes(method)) {\n throw new Error(`connectOutbox: only writes go through the Outbox, and this ${method.toUpperCase()} is marked with outbox`)\n }\n const data: unknown = config.data\n if (data instanceof FormData || data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\n throw new Error('connectOutbox: files do not go through the Outbox yet; send this write without outbox')\n }\n return dentroDaApp()\n}\n\nexport type OutboxState = 'pending' | 'sending' | 'delivered' | 'rejected' | 'expired' | 'deleted'\n\nexport type OutboxActionStatus = { id: string; state: OutboxState; createdAt?: string; reason?: string }\n\nexport type OutboxWatch = { actions: OutboxActionStatus[]; stop: () => void }\n\nconst WATCH_WAIT_MS = 10_000\n\nfunction statusFor(carga: unknown, outboxId: string): OutboxActionStatus | null {\n if (typeof carga !== 'object' || carga === null) return null\n const { outboxId: of, ...status } = carga as OutboxActionStatus & { outboxId?: string }\n return of === outboxId ? status : null\n}\n\nexport async function watchOutbox(options: OutboxOptions, onChange: (status: OutboxActionStatus) => void): Promise<OutboxWatch> {\n const outboxId = checkedOutboxId(options.outboxId)\n if (!dentroDaApp()) return { actions: [], stop: () => undefined }\n const stop = onNativeEvent('outbox', (carga) => {\n const status = statusFor(carga, outboxId)\n if (status) onChange(status)\n })\n try {\n const actions = await pedirAoNativo<OutboxActionStatus[]>('outbox.watch', { outboxId }, WATCH_WAIT_MS)\n return { actions, stop }\n } catch (error) {\n stop()\n throw error\n }\n}\n\nexport function connectOutbox(api: AxiosInstance, options: OutboxOptions): void {\n const outboxId = checkedOutboxId(options.outboxId)\n const adapter = outboxAdapter(api, outboxId)\n api.interceptors.request.use((config) => {\n if (goesThroughOutbox(config)) config.adapter = adapter\n return config\n })\n}\n"],"names":[],"mappings":";;;AAOA,MAAM,mBAAmB;AACzB,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,OAA2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