mockaton 10.6.2 → 10.6.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Dashboard.js +49 -52
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "HTTP Mock Server",
4
4
  "type": "module",
5
- "version": "10.6.2",
5
+ "version": "10.6.3",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Dashboard.js CHANGED
@@ -69,6 +69,10 @@ const state = /** @type {State} */ {
69
69
  return Boolean(state.proxyFallback)
70
70
  },
71
71
 
72
+ fileFor(method, urlMask) {
73
+ return state.brokersByMethod[method]?.[urlMask]?.currentMock.file
74
+ },
75
+
72
76
  leftSideWidth: window.innerWidth / 2,
73
77
 
74
78
  groupByMethod: initPreference('groupByMethod'),
@@ -95,7 +99,7 @@ async function updateState() {
95
99
  const response = await mockaton.getState()
96
100
  if (!response.ok)
97
101
  throw response.status
98
-
102
+
99
103
  Object.assign(state, await response.json())
100
104
 
101
105
  const focusedElem = selectorFor(document.activeElement)
@@ -335,6 +339,7 @@ function SaveProxiedCheckbox(ref) {
335
339
 
336
340
  function ResetButton() {
337
341
  function onClick() {
342
+ state.setChosenLink('', '')
338
343
  mockaton.reset()
339
344
  .then(parseError)
340
345
  .then(updateState)
@@ -702,31 +707,22 @@ function Resizer() {
702
707
  /** # Payload Preview */
703
708
 
704
709
  const payloadViewerTitleRef = useRef()
705
- const payloadViewerRef = useRef()
706
- const SPINNER_DELAY = 80
710
+ const payloadViewerCodeRef = useRef()
707
711
 
708
712
  function PayloadViewer() {
709
713
  return (
710
714
  r('div', className(CSS.PayloadViewer),
711
715
  r('h2', { ref: payloadViewerTitleRef }, t`Preview`),
712
716
  r('pre', null,
713
- r('code', { ref: payloadViewerRef }, t`Click a link to preview it`))))
714
- }
715
-
716
- function PayloadViewerProgressBar() {
717
- return (
718
- r('div', className(CSS.ProgressBar),
719
- r('div', { style: { animationDuration: state.delay - SPINNER_DELAY + 'ms' } })))
717
+ r('code', { ref: payloadViewerCodeRef }, t`Click a link to preview it`))))
720
718
  }
721
719
 
722
720
  function PayloadViewerTitle({ file, statusText }) {
723
- const tokens = file.split('.')
724
- const ext = tokens.pop()
725
- const status = tokens.pop()
726
- const urlAndMethod = tokens.join('.') + '.'
721
+ const { method, status, ext } = parseFilename(file)
722
+ const fileNameWithComments = file.split('.').slice(0, -3).join('.')
727
723
  return (
728
724
  r('span', null,
729
- urlAndMethod,
725
+ fileNameWithComments + '.' + method + '.',
730
726
  r('abbr', { title: statusText }, status),
731
727
  '.' + ext))
732
728
  }
@@ -741,13 +737,20 @@ function PayloadViewerTitleWhenProxied({ mime, status, statusText, gatewayIsBad
741
737
  ' ' + mime))
742
738
  }
743
739
 
740
+ const SPINNER_DELAY = 80
741
+ function PayloadViewerProgressBar() {
742
+ return (
743
+ r('div', className(CSS.ProgressBar),
744
+ r('div', { style: { animationDuration: state.delay - SPINNER_DELAY + 'ms' } })))
745
+ }
746
+
744
747
  async function previewMock(method, urlMask) {
745
748
  previewMock.controller?.abort()
746
749
  previewMock.controller = new AbortController
747
750
 
748
751
  const spinnerTimer = setTimeout(() => {
749
752
  payloadViewerTitleRef.current.replaceChildren(t`Fetching…`)
750
- payloadViewerRef.current.replaceChildren(PayloadViewerProgressBar())
753
+ payloadViewerCodeRef.current.replaceChildren(PayloadViewerProgressBar())
751
754
  }, SPINNER_DELAY)
752
755
 
753
756
  try {
@@ -756,48 +759,48 @@ async function previewMock(method, urlMask) {
756
759
  signal: previewMock.controller.signal
757
760
  })
758
761
  clearTimeout(spinnerTimer)
759
- await updatePayloadViewer(method, urlMask, response)
762
+ const file = state.fileFor(method, urlMask)
763
+ if (file === '')
764
+ await updatePayloadViewer(STR_PROXIED, response)
765
+ else if (file)
766
+ await updatePayloadViewer(file, response)
767
+ else {/* e.g. selected was deleted */}
760
768
  }
761
769
  catch (err) {
762
770
  onError(err)
763
- payloadViewerRef.current.replaceChildren()
771
+ payloadViewerCodeRef.current.replaceChildren()
764
772
  }
765
773
  }
766
774
 
767
-
768
- async function updatePayloadViewer(method, urlMask, response) {
775
+ async function updatePayloadViewer(file, response) {
769
776
  const mime = response.headers.get('content-type') || ''
770
777
 
771
- const file = mockSelectorFor(method, urlMask)?.value
772
- if (!file)
773
- return // e.g. selected was deleted
774
- if (file === STR_PROXIED)
775
- payloadViewerTitleRef.current.replaceChildren(PayloadViewerTitleWhenProxied({
776
- status: response.status,
777
- statusText: response.statusText,
778
- mime,
779
- gatewayIsBad: response.headers.get(HEADER_FOR_502)
780
- }))
781
- else
782
- payloadViewerTitleRef.current.replaceChildren(PayloadViewerTitle({
783
- statusText: response.statusText,
784
- file
785
- }))
778
+ payloadViewerTitleRef.current.replaceChildren(
779
+ file === STR_PROXIED
780
+ ? PayloadViewerTitleWhenProxied({
781
+ mime,
782
+ status: response.status,
783
+ statusText: response.statusText,
784
+ gatewayIsBad: response.headers.get(HEADER_FOR_502)
785
+ })
786
+ : PayloadViewerTitle({
787
+ file,
788
+ statusText: response.statusText
789
+ }))
786
790
 
787
- if (mime.startsWith('image/')) { // Naively assumes GET.200
788
- payloadViewerRef.current.replaceChildren(
791
+ if (mime.startsWith('image/')) // Naively assumes GET.200
792
+ payloadViewerCodeRef.current.replaceChildren(
789
793
  r('img', {
790
794
  src: URL.createObjectURL(await response.blob())
791
795
  }))
792
- }
793
796
  else {
794
797
  const body = await response.text() || t`/* Empty Response Body */`
795
798
  if (mime === 'application/json')
796
- payloadViewerRef.current.replaceChildren(r('span', className(CSS.json), syntaxJSON(body)))
799
+ payloadViewerCodeRef.current.replaceChildren(r('span', className(CSS.json), syntaxJSON(body)))
797
800
  else if (isXML(mime))
798
- payloadViewerRef.current.replaceChildren(syntaxXML(body))
801
+ payloadViewerCodeRef.current.replaceChildren(syntaxXML(body))
799
802
  else
800
- payloadViewerRef.current.textContent = body
803
+ payloadViewerCodeRef.current.textContent = body
801
804
  }
802
805
  }
803
806
 
@@ -807,12 +810,6 @@ function isXML(mime) {
807
810
  }
808
811
 
809
812
 
810
- function mockSelectorFor(method, urlMask) {
811
- const tr = document.querySelector(`tr[key="${method}::${urlMask}"]`)
812
- return tr?.querySelector(`.${CSS.MockSelector}`)
813
- }
814
-
815
-
816
813
  function initKeyboardNavigation() {
817
814
  addEventListener('keydown', onKeyDown)
818
815
 
@@ -1013,19 +1010,19 @@ function selectorFor(elem) {
1013
1010
 
1014
1011
  const path = []
1015
1012
  while (elem) {
1016
- let mod = ''
1013
+ let qualifier = ''
1017
1014
  if (elem.hasAttribute('key'))
1018
- mod = `[key="${elem.getAttribute('key')}"]`
1015
+ qualifier = `[key="${elem.getAttribute('key')}"]`
1019
1016
  else {
1020
1017
  let i = 0
1021
1018
  let sib = elem
1022
1019
  while ((sib = sib.previousElementSibling))
1023
- if (sib.nodeName === elem.nodeName)
1020
+ if (sib.tagName === elem.tagName)
1024
1021
  i++
1025
1022
  if (i)
1026
- mod = `:nth-of-type(${i + 1})`
1023
+ qualifier = `:nth-of-type(${i + 1})`
1027
1024
  }
1028
- path.push(elem.nodeName + mod)
1025
+ path.push(elem.tagName + qualifier)
1029
1026
  elem = elem.parentElement
1030
1027
  }
1031
1028
  return path.reverse().join('>')