From 799e766ecc032dfe0f8f770b8fcd5ae14f8ecbbd Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 20 Nov 2024 17:09:17 +0000 Subject: [PATCH 1/3] refactor: :recycle: refactor @bugsnag/delivery-xml-http-request --- .../delivery-xml-http-request/delivery.d.ts | 6 ---- .../delivery-xml-http-request/package.json | 15 +++++++- .../rollup.config.npm.mjs | 6 ++++ .../{delivery.js => src/delivery.ts} | 36 +++++++++++-------- .../delivery-xml-http-request/tsconfig.json | 4 +++ 5 files changed, 45 insertions(+), 22 deletions(-) delete mode 100644 packages/delivery-xml-http-request/delivery.d.ts create mode 100644 packages/delivery-xml-http-request/rollup.config.npm.mjs rename packages/delivery-xml-http-request/{delivery.js => src/delivery.ts} (55%) create mode 100644 packages/delivery-xml-http-request/tsconfig.json diff --git a/packages/delivery-xml-http-request/delivery.d.ts b/packages/delivery-xml-http-request/delivery.d.ts deleted file mode 100644 index 05552de3af..0000000000 --- a/packages/delivery-xml-http-request/delivery.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Delivery } from '@bugsnag/core/client' -import { Client } from '@bugsnag/core' - -declare const delivery: (client: Client, window?: Window) => Delivery - -export default delivery diff --git a/packages/delivery-xml-http-request/package.json b/packages/delivery-xml-http-request/package.json index c2306b2a33..aa36a3ce45 100644 --- a/packages/delivery-xml-http-request/package.json +++ b/packages/delivery-xml-http-request/package.json @@ -1,7 +1,15 @@ { "name": "@bugsnag/delivery-xml-http-request", "version": "8.1.1", - "main": "delivery.js", + "main": "dist/delivery.js", + "types": "dist/types/delivery.d.ts", + "exports": { + ".": { + "types": "./dist/types/delivery.d.ts", + "default": "./dist/delivery.js", + "import": "./dist/delivery.mjs" + } + }, "description": "@bugsnag/js delivery mechanism for most browsers", "homepage": "https://www.bugsnag.com/", "repository": { @@ -16,6 +24,11 @@ ], "author": "Bugsnag", "license": "MIT", + "scripts": { + "build": "npm run build:npm", + "build:npm": "rollup --config rollup.config.npm.mjs", + "clean": "rm -rf dist/*" + }, "devDependencies": { "@bugsnag/core": "^8.1.1" }, diff --git a/packages/delivery-xml-http-request/rollup.config.npm.mjs b/packages/delivery-xml-http-request/rollup.config.npm.mjs new file mode 100644 index 0000000000..5a55749227 --- /dev/null +++ b/packages/delivery-xml-http-request/rollup.config.npm.mjs @@ -0,0 +1,6 @@ +import createRollupConfig from "../../.rollup/index.mjs"; + +export default createRollupConfig({ + input: "src/delivery.ts", + external: ['@bugsnag/core/lib/json-payload'] +}); diff --git a/packages/delivery-xml-http-request/delivery.js b/packages/delivery-xml-http-request/src/delivery.ts similarity index 55% rename from packages/delivery-xml-http-request/delivery.js rename to packages/delivery-xml-http-request/src/delivery.ts index 49f6c5c4e8..b9f1e687b0 100644 --- a/packages/delivery-xml-http-request/delivery.js +++ b/packages/delivery-xml-http-request/src/delivery.ts @@ -1,24 +1,28 @@ -const payload = require('@bugsnag/core/lib/json-payload') +import type { Client, Config, Session } from '@bugsnag/core' -module.exports = (client, win = window) => ({ +import payload from '@bugsnag/core/lib/json-payload' +import { Event } from 'packages/core' +import ClientWithInternals, { Delivery } from 'packages/core/client' + +const delivery = (client: Client, win = window): Delivery => ({ sendEvent: (event, cb = () => {}) => { try { - const url = client._config.endpoints.notify + const url = (client as ClientWithInternals>)._config.endpoints.notify if (url === null) { const err = new Error('Event not sent due to incomplete endpoint configuration') return cb(err) } const req = new win.XMLHttpRequest() - const body = payload.event(event, client._config.redactedKeys) + const body = payload.event(event as unknown as Event, (client as ClientWithInternals>)._config.redactedKeys) req.onreadystatechange = function () { if (req.readyState === win.XMLHttpRequest.DONE) { const status = req.status if (status === 0 || status >= 400) { - const err = new Error(`Request failed with status ${status}`) - client._logger.error('Event failed to send…', err) + const err = new Error(`Request failed with status ${status}`); + (client as ClientWithInternals)._logger.error('Event failed to send…', err) if (body.length > 10e5) { - client._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`) + (client as ClientWithInternals)._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`) } cb(err) } else { @@ -29,17 +33,17 @@ module.exports = (client, win = window) => ({ req.open('POST', url) req.setRequestHeader('Content-Type', 'application/json') - req.setRequestHeader('Bugsnag-Api-Key', event.apiKey || client._config.apiKey) + req.setRequestHeader('Bugsnag-Api-Key', event.apiKey || (client as ClientWithInternals)._config.apiKey) req.setRequestHeader('Bugsnag-Payload-Version', '4') req.setRequestHeader('Bugsnag-Sent-At', (new Date()).toISOString()) req.send(body) } catch (e) { - client._logger.error(e) + (client as ClientWithInternals)._logger.error(e) } }, sendSession: (session, cb = () => {}) => { try { - const url = client._config.endpoints.sessions + const url = (client as ClientWithInternals>)._config.endpoints.sessions if (url === null) { const err = new Error('Session not sent due to incomplete endpoint configuration') return cb(err) @@ -50,8 +54,8 @@ module.exports = (client, win = window) => ({ if (req.readyState === win.XMLHttpRequest.DONE) { const status = req.status if (status === 0 || status >= 400) { - const err = new Error(`Request failed with status ${status}`) - client._logger.error('Session failed to send…', err) + const err = new Error(`Request failed with status ${status}`); + (client as ClientWithInternals)._logger.error('Session failed to send…', err) cb(err) } else { cb(null) @@ -61,12 +65,14 @@ module.exports = (client, win = window) => ({ req.open('POST', url) req.setRequestHeader('Content-Type', 'application/json') - req.setRequestHeader('Bugsnag-Api-Key', client._config.apiKey) + req.setRequestHeader('Bugsnag-Api-Key', (client as ClientWithInternals)._config.apiKey) req.setRequestHeader('Bugsnag-Payload-Version', '1') req.setRequestHeader('Bugsnag-Sent-At', (new Date()).toISOString()) - req.send(payload.session(session, client._config.redactedKeys)) + req.send(payload.session(session as Session, (client as ClientWithInternals>)._config.redactedKeys)) } catch (e) { - client._logger.error(e) + (client as ClientWithInternals)._logger.error(e) } } }) + +export default delivery diff --git a/packages/delivery-xml-http-request/tsconfig.json b/packages/delivery-xml-http-request/tsconfig.json new file mode 100644 index 0000000000..a5cb75c562 --- /dev/null +++ b/packages/delivery-xml-http-request/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"] +} From 60c158627e364e6a68d9a667ab0a9e3f7aa1dad4 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 21 Nov 2024 14:40:35 +0000 Subject: [PATCH 2/3] remove delivery-xml-http-request from root tsconfig --- tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 4d8eb8966a..c91817bd35 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -63,7 +63,6 @@ "packages/core", "packages/delivery-node", "packages/delivery-react-native", - "packages/delivery-xml-http-request", "packages/in-flight", "packages/plugin-aws-lambda", "packages/plugin-contextualize", From fb59b53bd8deb79894a66408562912e30234a195 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Thu, 21 Nov 2024 17:22:50 +0000 Subject: [PATCH 3/3] update files list --- packages/delivery-xml-http-request/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/delivery-xml-http-request/package.json b/packages/delivery-xml-http-request/package.json index aa36a3ce45..525db25923 100644 --- a/packages/delivery-xml-http-request/package.json +++ b/packages/delivery-xml-http-request/package.json @@ -20,7 +20,7 @@ "access": "public" }, "files": [ - "*.js" + "dist" ], "author": "Bugsnag", "license": "MIT",