@nordcraft/runtime 1.0.7 → 1.0.9

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.
package/package.json CHANGED
@@ -4,8 +4,8 @@
4
4
  "type": "module",
5
5
  "homepage": "https://github.com/nordcraftengine/nordcraft",
6
6
  "dependencies": {
7
- "@nordcraft/core": "1.0.7",
8
- "@nordcraft/std-lib": "1.0.7",
7
+ "@nordcraft/core": "1.0.9",
8
+ "@nordcraft/std-lib": "1.0.9",
9
9
  "fast-deep-equal": "3.1.3",
10
10
  "path-to-regexp": "6.3.0"
11
11
  },
@@ -21,5 +21,5 @@
21
21
  "files": ["dist", "src"],
22
22
  "main": "dist/page.main.js",
23
23
  "types": "dist/page.main.d.ts",
24
- "version": "1.0.7"
24
+ "version": "1.0.9"
25
25
  }
@@ -31,6 +31,7 @@ import {
31
31
  sortObjectEntries,
32
32
  } from '@nordcraft/core/dist/utils/collections'
33
33
  import { PROXY_URL_HEADER, validateUrl } from '@nordcraft/core/dist/utils/url'
34
+ import { isDefined } from '@nordcraft/core/dist/utils/util'
34
35
  import { handleAction } from '../events/handleAction'
35
36
  import type { Signal } from '../signal/signal'
36
37
  import type { ComponentContext, ContextApi } from '../types'
@@ -748,71 +749,72 @@ export function createAPI(
748
749
  }
749
750
  })
750
751
  payloadSignal.subscribe(async (_) => {
751
- if (api.autoFetch && applyFormula(api.autoFetch, getFormulaContext(api))) {
752
- // Ensure we only use caching if the page is currently loading
753
- if ((window?.__toddle?.isPageLoaded ?? false) === false) {
754
- const { url, requestSettings } = constructRequest(api)
755
- const cacheKey = requestHash(url, requestSettings)
756
- const cacheMatch = ctx.toddle.pageState.Apis?.[cacheKey] as ApiStatus
757
- if (cacheMatch) {
758
- if (cacheMatch.error) {
759
- apiError(
760
- api,
761
- {
762
- body: cacheMatch.error,
763
- status: cacheMatch.response?.status,
764
- headers: cacheMatch.response?.headers ?? undefined,
765
- },
766
- {
767
- requestStart:
768
- cacheMatch.response?.performance?.requestStart ?? null,
769
- responseStart:
770
- cacheMatch.response?.performance?.responseStart ?? null,
771
- responseEnd:
772
- cacheMatch.response?.performance?.responseEnd ?? null,
773
- },
774
- )
775
- } else {
776
- apiSuccess(
777
- api,
778
- {
779
- body: cacheMatch.data,
780
- status: cacheMatch.response?.status,
781
- headers: cacheMatch.response?.headers ?? undefined,
782
- },
783
- {
784
- requestStart:
785
- cacheMatch.response?.performance?.requestStart ?? null,
786
- responseStart:
787
- cacheMatch.response?.performance?.responseStart ?? null,
788
- responseEnd:
789
- cacheMatch.response?.performance?.responseEnd ?? null,
790
- },
791
- )
792
- }
793
- } else {
794
- // Execute will set the initial status of the api in the dataSignal
795
- await execute(api, url, requestSettings)
796
- }
752
+ const { url, requestSettings } = constructRequest(api)
753
+ // Ensure we only use caching if the page is currently loading
754
+ const cacheMatch =
755
+ // We lookup the API from cache as long as autofetch is defined (and not statically falsy)
756
+ // since the autofetch formula could've evaluated to true during SSR
757
+ isDefined(api.autoFetch) &&
758
+ (api.autoFetch.type !== 'value' || api.autoFetch.value === true) &&
759
+ (window?.__toddle?.isPageLoaded ?? false) === false
760
+ ? (ctx.toddle.pageState.Apis?.[
761
+ requestHash(url, requestSettings)
762
+ ] as ApiStatus)
763
+ : undefined
764
+
765
+ if (cacheMatch) {
766
+ if (cacheMatch.error) {
767
+ apiError(
768
+ api,
769
+ {
770
+ body: cacheMatch.error,
771
+ status: cacheMatch.response?.status,
772
+ headers: cacheMatch.response?.headers ?? undefined,
773
+ },
774
+ {
775
+ requestStart:
776
+ cacheMatch.response?.performance?.requestStart ?? null,
777
+ responseStart:
778
+ cacheMatch.response?.performance?.responseStart ?? null,
779
+ responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
780
+ },
781
+ )
797
782
  } else {
798
- // Execute will set the initial status of the api in the dataSignal
799
- const { url, requestSettings } = constructRequest(api)
800
- await execute(api, url, requestSettings)
783
+ apiSuccess(
784
+ api,
785
+ {
786
+ body: cacheMatch.data,
787
+ status: cacheMatch.response?.status,
788
+ headers: cacheMatch.response?.headers ?? undefined,
789
+ },
790
+ {
791
+ requestStart:
792
+ cacheMatch.response?.performance?.requestStart ?? null,
793
+ responseStart:
794
+ cacheMatch.response?.performance?.responseStart ?? null,
795
+ responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
796
+ },
797
+ )
801
798
  }
802
799
  } else {
803
- ctx.dataSignal.update((data) => {
804
- return {
805
- ...data,
806
- Apis: {
807
- ...(data.Apis ?? {}),
808
- [api.name]: {
809
- isLoading: false,
810
- data: null,
811
- error: null,
800
+ if (applyFormula(api.autoFetch, getFormulaContext(api))) {
801
+ // Execute will set the initial status of the api in the dataSignal
802
+ await execute(api, url, requestSettings)
803
+ } else {
804
+ ctx.dataSignal.update((data) => {
805
+ return {
806
+ ...data,
807
+ Apis: {
808
+ ...(data.Apis ?? {}),
809
+ [api.name]: {
810
+ isLoading: false,
811
+ data: null,
812
+ error: null,
813
+ },
812
814
  },
813
- },
814
- }
815
- })
815
+ }
816
+ })
817
+ }
816
818
  }
817
819
  })
818
820