Skip to content

Commit

Permalink
Read into callback array
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-lippert committed Nov 8, 2023
1 parent 6276783 commit ecc2f8b
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ export class State {
this.service.onTransition(async (state) => {
this.serviceState = state
if (this.machineState === state.value) return
this.machineState = state.value
await this.state.storage.put('machineState', this.machineState)
await this.state.storage.put('machineState', (this.machineState = state.value))
const meta = Object.values(state.meta)[0]
const callback = meta?.callback || state.configuration.flatMap((c) => c.config).reduce((acc, c) => ({ ...acc, ...c }), {}).callback
if (callback) {
const callbacks = Array.isArray(callback) ? callback : [callback]
for (let i = 0; i < callbacks.length; i++) {
const url = typeof callbacks[i] === 'string' || callbacks[i] instanceof String ? callbacks[i] : callbacks[i].url
const init = callbacks[i].init || meta?.init || {}
init.headers = { 'content-type': 'application/json', ...(meta?.headers || init.headers) }
init.method = meta?.method || init.method || 'POST'
init.body = JSON.stringify(meta?.body || state.event)
init.headers = { 'content-type': 'application/json', ...(callbacks[i].headers || meta?.headers || init.headers) }
init.method = callbacks[i].method || meta?.method || init.method || 'POST'
init.body = JSON.stringify(callbacks[i].body || meta?.body || state.event)
console.log({ url, init, state })
const data = await fetch(url, init)
// Escape special regex characters and replace x with \d to check if the callback status code matches an event (e.g. 2xx)
Expand Down

0 comments on commit ecc2f8b

Please sign in to comment.