Skip to content

Commit

Permalink
Merge pull request #4 from managed-components/fail_gracefully
Browse files Browse the repository at this point in the history
Ignore empty endpoints
  • Loading branch information
simonabadoiu authored Nov 15, 2023
2 parents cd7a2cd + 5cc2078 commit 0c89ba6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Manager, MCEvent } from '@managed-components/types'
import { flattenKeys } from './utils'

function handleEvents(manager: Manager, event: MCEvent) {
if (event.payload.method && event.payload.method.startsWith('post')) {
const { endpoint, method } = event.payload
if (!endpoint) return
if (method?.startsWith('post')) {
sendPostRequest(manager, event)
} else {
sendGetRequest(manager, event)
Expand All @@ -21,13 +23,17 @@ export function createRequestBody(event: MCEvent) {
}

export function sendPostRequest(manager: Manager, event: MCEvent) {
manager.fetch(`${event.payload.endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(createRequestBody(event)),
})
try {
manager.fetch(`${event.payload.endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(createRequestBody(event)),
})
} catch {
// empty
}
}

// Functions related to get requests
Expand All @@ -43,7 +49,11 @@ export function constructGetRequestUrl(event: MCEvent) {
}

export function sendGetRequest(manager: Manager, event: MCEvent) {
manager.fetch(`${constructGetRequestUrl(event)}`)
try {
manager.fetch(`${constructGetRequestUrl(event)}`)
} catch {
// empty
}
}

// Event listeners
Expand Down

0 comments on commit 0c89ba6

Please sign in to comment.