@happydesigns/ui 0.18.0 → 0.19.1

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.
@@ -1,67 +1,68 @@
1
- import type { PageCollections, SQLOperator } from '@nuxt/content'
2
- import type { H3Event } from 'h3'
3
- import type { SearchCollectionConfig, SearchConfig } from '../../app/types/config'
4
-
5
- interface ConfigurableSearchQuery {
6
- where: (field: string, operator: SQLOperator, value?: unknown) => unknown
7
- order: (field: string, direction: 'ASC' | 'DESC') => unknown
8
- }
9
-
10
- type SearchCacheConfig = NonNullable<SearchConfig['cache']>
11
-
12
- const DEFAULT_SHARED_MAX_AGE = 300
13
- const DEFAULT_STALE_WHILE_REVALIDATE = 3600
14
-
15
- function normalizedSeconds(value: number | undefined, fallback: number) {
16
- if (value === undefined || !Number.isFinite(value))
17
- return fallback
18
-
19
- return Math.max(0, Math.floor(value))
20
- }
21
-
22
- export function createSearchCacheControl(config: SearchCacheConfig = {}, isDevelopment = false) {
23
- if (config === false || isDevelopment)
24
- return 'no-store'
25
-
26
- const sharedMaxAge = normalizedSeconds(config.sharedMaxAge, DEFAULT_SHARED_MAX_AGE)
27
- const staleWhileRevalidate = normalizedSeconds(
28
- config.staleWhileRevalidate,
29
- DEFAULT_STALE_WHILE_REVALIDATE,
30
- )
31
-
32
- return `public, max-age=0, s-maxage=${sharedMaxAge}, stale-while-revalidate=${staleWhileRevalidate}`
33
- }
34
-
35
- export function setSearchCacheHeaders(event: H3Event) {
36
- const cache = (useAppConfig().app?.search?.cache ?? {}) as SearchCacheConfig
37
- setResponseHeader(event, 'Cache-Control', createSearchCacheControl(cache, import.meta.dev))
38
- }
39
-
40
- export function applySearchCollectionConfig<T>(query: T, config: SearchCollectionConfig): T {
41
- let configuredQuery = query as ConfigurableSearchQuery
42
-
43
- for (const condition of config.where ?? []) {
44
- configuredQuery = configuredQuery.where(
45
- condition.field,
46
- condition.operator,
47
- condition.value,
48
- ) as ConfigurableSearchQuery
49
- }
50
-
51
- if (config.order) {
52
- configuredQuery = configuredQuery.order(
53
- config.order.field,
54
- config.order.direction,
55
- ) as ConfigurableSearchQuery
56
- }
57
-
58
- return configuredQuery as T
59
- }
60
-
61
- export function getSearchCollections(): SearchCollectionConfig[] {
62
- return (useAppConfig().app?.search?.collections ?? []) as SearchCollectionConfig[]
63
- }
64
-
65
- export function asPageCollection(name: keyof PageCollections): keyof PageCollections {
66
- return name
67
- }
1
+ import type { PageCollections, SQLOperator } from '@nuxt/content'
2
+ import type { H3Event } from 'h3'
3
+ import type { SearchCollectionConfig, SearchConfig } from '../../app/types/config'
4
+ import type { PageCollectionName } from '../../app/types/content'
5
+
6
+ interface ConfigurableSearchQuery {
7
+ where: (field: string, operator: SQLOperator, value?: unknown) => unknown
8
+ order: (field: string, direction: 'ASC' | 'DESC') => unknown
9
+ }
10
+
11
+ type SearchCacheConfig = NonNullable<SearchConfig['cache']>
12
+
13
+ const DEFAULT_SHARED_MAX_AGE = 300
14
+ const DEFAULT_STALE_WHILE_REVALIDATE = 3600
15
+
16
+ function normalizedSeconds(value: number | undefined, fallback: number) {
17
+ if (value === undefined || !Number.isFinite(value))
18
+ return fallback
19
+
20
+ return Math.max(0, Math.floor(value))
21
+ }
22
+
23
+ export function createSearchCacheControl(config: SearchCacheConfig = {}, isDevelopment = false) {
24
+ if (config === false || isDevelopment)
25
+ return 'no-store'
26
+
27
+ const sharedMaxAge = normalizedSeconds(config.sharedMaxAge, DEFAULT_SHARED_MAX_AGE)
28
+ const staleWhileRevalidate = normalizedSeconds(
29
+ config.staleWhileRevalidate,
30
+ DEFAULT_STALE_WHILE_REVALIDATE,
31
+ )
32
+
33
+ return `public, max-age=0, s-maxage=${sharedMaxAge}, stale-while-revalidate=${staleWhileRevalidate}`
34
+ }
35
+
36
+ export function setSearchCacheHeaders(event: H3Event) {
37
+ const cache = (useAppConfig().app?.search?.cache ?? {}) as SearchCacheConfig
38
+ setResponseHeader(event, 'Cache-Control', createSearchCacheControl(cache, import.meta.dev))
39
+ }
40
+
41
+ export function applySearchCollectionConfig<T>(query: T, config: SearchCollectionConfig): T {
42
+ let configuredQuery = query as ConfigurableSearchQuery
43
+
44
+ for (const condition of config.where ?? []) {
45
+ configuredQuery = configuredQuery.where(
46
+ condition.field,
47
+ condition.operator,
48
+ condition.value,
49
+ ) as ConfigurableSearchQuery
50
+ }
51
+
52
+ if (config.order) {
53
+ configuredQuery = configuredQuery.order(
54
+ config.order.field,
55
+ config.order.direction,
56
+ ) as ConfigurableSearchQuery
57
+ }
58
+
59
+ return configuredQuery as T
60
+ }
61
+
62
+ export function getSearchCollections(): SearchCollectionConfig[] {
63
+ return (useAppConfig().app?.search?.collections ?? []) as SearchCollectionConfig[]
64
+ }
65
+
66
+ export function asPageCollection(name: PageCollectionName): keyof PageCollections {
67
+ return name as keyof PageCollections
68
+ }
package/content.config.ts DELETED
@@ -1,6 +0,0 @@
1
- import { defineContentConfig } from '@nuxt/content'
2
- import { collections } from './schemas'
3
-
4
- export default defineContentConfig({
5
- collections,
6
- })