@websolutespa/bom-next 4.0.0-next.6 → 4.0.0-next.8
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/CHANGELOG.md +17 -0
- package/package.json +9 -12
- package/src/exports/server.ts +0 -1
- package/src/lib/payload.config.ts +0 -12
- package/src/server/mock/category.ts +0 -39
- package/src/server/mock/collection.ts +0 -28
- package/src/server/mock/page.ts +0 -77
- package/src/server/mock/route.ts +0 -50
- package/src/server/mock/store.ts +0 -168
- package/src/server/session/session.ts +0 -106
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @websolutespa/bom-next
|
|
2
2
|
|
|
3
|
+
## 4.0.0-next.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Cleanup: unused dependencies, peers and unreachable code removed.
|
|
8
|
+
|
|
9
|
+
- `payload-utils`: the `jsdom` peer accepts `^26.1.0 || ^30.1.3`, and `htmlToLexical` imports it on use, so routes that never convert html don't load it. jsdom 30 fails with `ERR_REQUIRE_ESM` on Vercel's Node runtime; projects can stay on jsdom 26 and drop a `jsdom` override.
|
|
10
|
+
- Peers that nothing imports are gone: `uuid` (search, seo, thron), `@payloadcms/ui` (seed), `react-error-boundary` (mixer-ui, mixer-emails), `@websolutespa/payload-utils` (live-preview).
|
|
11
|
+
- `bom-mixer-hooks` declares `@types/html-to-text`, which it got from the web apps before.
|
|
12
|
+
- `bom-mixer-llm` publishes only the default mocks.
|
|
13
|
+
- Source files that no export reached are removed: mixer-ui carousel, navbar/menubar, media-image-next, useThemeForm, lazy-loader-client; mixer-llm card and product cta, Background, scroller-lenis, cors, useBlock, useComponent.
|
|
14
|
+
- `bom-cli`: nodemon is a dev dependency (it was installed in every project); no theme or storybook apps.
|
|
15
|
+
- `bom-next`: `jose` and `transliteration` dropped with the unused mock and session code.
|
|
16
|
+
- `eslint-config` is the legacy ESLint 10 config; projects on oxlint and oxfmt use `@websolutespa/oxc`.
|
|
17
|
+
|
|
18
|
+
## 4.0.0-next.7
|
|
19
|
+
|
|
3
20
|
## 4.0.0-next.6
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@websolutespa/bom-next",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.8",
|
|
4
4
|
"description": "Next module of the BOM Repository",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bom",
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"html-to-text": "10.0.1",
|
|
16
|
-
"
|
|
17
|
-
"server-only": "0.0.1",
|
|
18
|
-
"transliteration": "2.3.5"
|
|
16
|
+
"server-only": "0.0.1"
|
|
19
17
|
},
|
|
20
18
|
"peerDependencies": {
|
|
21
19
|
"@sentry/nextjs": "^10.75.0",
|
|
@@ -42,13 +40,13 @@
|
|
|
42
40
|
"@types/node": "24.19.2",
|
|
43
41
|
"@types/react": "19.2.18",
|
|
44
42
|
"@types/react-dom": "19.2.7",
|
|
45
|
-
"@websolutespa/bom-cli": "^4.0.0-next.
|
|
46
|
-
"@websolutespa/bom-core": "^4.0.0-next.
|
|
47
|
-
"@websolutespa/
|
|
48
|
-
"@websolutespa/payload-plugin-bowl": "^4.0.0-next.
|
|
49
|
-
"@websolutespa/payload-utils": "^4.0.0-next.
|
|
50
|
-
"@websolutespa/test": "^4.0.0-next.
|
|
51
|
-
"@websolutespa/tsconfig": "^4.0.0-next.
|
|
43
|
+
"@websolutespa/bom-cli": "^4.0.0-next.8",
|
|
44
|
+
"@websolutespa/bom-core": "^4.0.0-next.8",
|
|
45
|
+
"@websolutespa/oxc": "^4.0.0-next.8",
|
|
46
|
+
"@websolutespa/payload-plugin-bowl": "^4.0.0-next.8",
|
|
47
|
+
"@websolutespa/payload-utils": "^4.0.0-next.8",
|
|
48
|
+
"@websolutespa/test": "^4.0.0-next.8",
|
|
49
|
+
"@websolutespa/tsconfig": "^4.0.0-next.8",
|
|
52
50
|
"jsdom": "30.1.3",
|
|
53
51
|
"next": "16.4.2",
|
|
54
52
|
"npm-run-all": "4.1.5",
|
|
@@ -57,7 +55,6 @@
|
|
|
57
55
|
"payload": "3.91.0",
|
|
58
56
|
"react": "19.2.9",
|
|
59
57
|
"react-dom": "19.2.9",
|
|
60
|
-
"react-error-boundary": "^6.1.6",
|
|
61
58
|
"schema-dts": "2.1.0",
|
|
62
59
|
"tsdown": "0.21.0",
|
|
63
60
|
"typescript": "6.0.3",
|
package/src/exports/server.ts
CHANGED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ICollectionDescription,
|
|
3
|
-
IEntity,
|
|
4
|
-
ILocalizedString,
|
|
5
|
-
ISerializedCollection,
|
|
6
|
-
} from '@websolutespa/bom-core';
|
|
7
|
-
import { textToSlug } from './route';
|
|
8
|
-
|
|
9
|
-
export function toCategory(
|
|
10
|
-
description: ICollectionDescription,
|
|
11
|
-
collection: (IEntity & { category?: string })[]
|
|
12
|
-
): ISerializedCollection {
|
|
13
|
-
const mapToCategory = (x: IEntity & { category?: string }, i: number) => {
|
|
14
|
-
// !!! loose
|
|
15
|
-
if (typeof x.slug !== 'string' && x.category != null) {
|
|
16
|
-
if (x.title != null) {
|
|
17
|
-
if (typeof x.title === 'string') {
|
|
18
|
-
x.slug = textToSlug(x.title);
|
|
19
|
-
} else {
|
|
20
|
-
const slug: ILocalizedString = { ...x.title };
|
|
21
|
-
x.slug = Object.fromEntries(Object.entries(slug).map(([k, v]) => [k, textToSlug(v)]));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
x.slug = '';
|
|
26
|
-
}
|
|
27
|
-
const data = {
|
|
28
|
-
...x,
|
|
29
|
-
};
|
|
30
|
-
if (!data.schema) {
|
|
31
|
-
data.schema = description.singularName;
|
|
32
|
-
}
|
|
33
|
-
return data;
|
|
34
|
-
};
|
|
35
|
-
return {
|
|
36
|
-
...description,
|
|
37
|
-
items: collection.map((x, i) => mapToCategory(x, i)),
|
|
38
|
-
};
|
|
39
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ICollectionDescription, IEntity, ISerializedCollection } from '@websolutespa/bom-core';
|
|
2
|
-
|
|
3
|
-
export function toCollection(
|
|
4
|
-
description: ICollectionDescription,
|
|
5
|
-
collection: IEntity[],
|
|
6
|
-
createdAt?: string | Date,
|
|
7
|
-
updatedAt?: string | Date
|
|
8
|
-
): ISerializedCollection {
|
|
9
|
-
const mapToCollection = (x: IEntity, i: number) => {
|
|
10
|
-
const data = {
|
|
11
|
-
...(createdAt && createdAt
|
|
12
|
-
? {
|
|
13
|
-
createdAt,
|
|
14
|
-
updatedAt,
|
|
15
|
-
}
|
|
16
|
-
: {}),
|
|
17
|
-
...x,
|
|
18
|
-
};
|
|
19
|
-
if (!data.schema) {
|
|
20
|
-
data.schema = description.singularName;
|
|
21
|
-
}
|
|
22
|
-
return data;
|
|
23
|
-
};
|
|
24
|
-
return {
|
|
25
|
-
...description,
|
|
26
|
-
items: collection.map((x, i) => mapToCollection(x, i)),
|
|
27
|
-
};
|
|
28
|
-
}
|
package/src/server/mock/page.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getCollectionDescription,
|
|
3
|
-
ICategorized,
|
|
4
|
-
ICollectionDescription,
|
|
5
|
-
IEntity,
|
|
6
|
-
ILocalizable,
|
|
7
|
-
ILocalizedString,
|
|
8
|
-
ISerializedCollection,
|
|
9
|
-
ISerializedStore,
|
|
10
|
-
} from '@websolutespa/bom-core';
|
|
11
|
-
import { textToSlug } from './route';
|
|
12
|
-
|
|
13
|
-
export function createPageService(
|
|
14
|
-
store: ISerializedStore,
|
|
15
|
-
pageKeys: string[]
|
|
16
|
-
): ISerializedCollection<ICategorized> {
|
|
17
|
-
const pages: ICategorized[] = [];
|
|
18
|
-
for (const key of pageKeys) {
|
|
19
|
-
const collection = store[key];
|
|
20
|
-
if (collection) {
|
|
21
|
-
const items = collection.items as ICategorized[];
|
|
22
|
-
pages.push(...items);
|
|
23
|
-
} else {
|
|
24
|
-
console.warn(`BomCli.createPageService.collection not found [${key}]`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
const pageDescription = getCollectionDescription('page');
|
|
28
|
-
const pageService = {
|
|
29
|
-
...pageDescription,
|
|
30
|
-
items: pages,
|
|
31
|
-
};
|
|
32
|
-
return pageService;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function toPage(
|
|
36
|
-
description: ICollectionDescription,
|
|
37
|
-
collection: IEntity[],
|
|
38
|
-
createdAt: string | Date,
|
|
39
|
-
updatedAt: string | Date
|
|
40
|
-
): ISerializedCollection {
|
|
41
|
-
const mapToPage = (x: IEntity, i: number) => {
|
|
42
|
-
if (typeof x.slug !== 'string') {
|
|
43
|
-
if (x.title != null) {
|
|
44
|
-
if (typeof x.title === 'string') {
|
|
45
|
-
x.slug = textToSlug(x.title);
|
|
46
|
-
} else {
|
|
47
|
-
const slug: ILocalizedString = { ...x.title };
|
|
48
|
-
x.slug = Object.fromEntries(Object.entries(slug).map(([k, v]) => [k, textToSlug(v)]));
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
throw `${description.singularName} error: invalid page missing title or slug`;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (!x.title) {
|
|
55
|
-
throw `${description.singularName} error: invalid page missing title`;
|
|
56
|
-
}
|
|
57
|
-
const meta: { [key: string]: ILocalizable } = x.meta || ({} as any);
|
|
58
|
-
meta.title = meta.title || (x.title as ILocalizable);
|
|
59
|
-
meta.description = meta.description || (x.abstract as ILocalizable);
|
|
60
|
-
meta.keywords = meta.keywords || 'keywords';
|
|
61
|
-
meta.robots = meta.robots || 'all';
|
|
62
|
-
x.meta = meta;
|
|
63
|
-
const page = {
|
|
64
|
-
createdAt,
|
|
65
|
-
updatedAt,
|
|
66
|
-
...x,
|
|
67
|
-
};
|
|
68
|
-
if (!page.schema) {
|
|
69
|
-
page.schema = description.singularName;
|
|
70
|
-
}
|
|
71
|
-
return page;
|
|
72
|
-
};
|
|
73
|
-
return {
|
|
74
|
-
...description,
|
|
75
|
-
items: collection.map((x, i) => mapToPage(x, i)),
|
|
76
|
-
};
|
|
77
|
-
}
|
package/src/server/mock/route.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
collectRoutes,
|
|
3
|
-
getCollectionDescription,
|
|
4
|
-
ICategorized,
|
|
5
|
-
ICategory,
|
|
6
|
-
ILocale,
|
|
7
|
-
IMarket,
|
|
8
|
-
IMemoryStore,
|
|
9
|
-
IRoute,
|
|
10
|
-
ISerializedCollection,
|
|
11
|
-
ISerializedStore,
|
|
12
|
-
} from '@websolutespa/bom-core';
|
|
13
|
-
import { slugify } from 'transliteration';
|
|
14
|
-
|
|
15
|
-
export function createRouteService(
|
|
16
|
-
store: ISerializedStore,
|
|
17
|
-
pageKeys: string[]
|
|
18
|
-
): ISerializedCollection {
|
|
19
|
-
const routes: IRoute[] = getRoutes(store, pageKeys);
|
|
20
|
-
const routeDescription = getCollectionDescription('route');
|
|
21
|
-
const routeService = {
|
|
22
|
-
...routeDescription,
|
|
23
|
-
items: routes,
|
|
24
|
-
};
|
|
25
|
-
return routeService;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function getRoutes(store: ISerializedStore, pageKeys: string[]): IRoute[] {
|
|
29
|
-
let routes: IRoute[] = [];
|
|
30
|
-
const locales = store.locale!.items as ILocale[];
|
|
31
|
-
const markets = store.market!.items as IMarket[];
|
|
32
|
-
if (locales.length > 0 && markets.length > 0) {
|
|
33
|
-
const categories = store.category!.items as ICategory[];
|
|
34
|
-
const categorized: IMemoryStore<ICategorized> = {};
|
|
35
|
-
for (const key of pageKeys) {
|
|
36
|
-
const collection = store[key];
|
|
37
|
-
if (collection) {
|
|
38
|
-
categorized[key] = collection.items as ICategorized[];
|
|
39
|
-
} else {
|
|
40
|
-
console.warn('BomCli.getRoutes', `collection not found [${key}]`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
routes = collectRoutes(categorized, categories, markets, locales);
|
|
44
|
-
}
|
|
45
|
-
return routes;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function textToSlug(value: string): string {
|
|
49
|
-
return slugify(value);
|
|
50
|
-
}
|
package/src/server/mock/store.ts
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getCollectionDescription,
|
|
3
|
-
ICollectionDescription,
|
|
4
|
-
IEntity,
|
|
5
|
-
IMemoryStore,
|
|
6
|
-
IMockStore,
|
|
7
|
-
isArray,
|
|
8
|
-
ISerializedCollection,
|
|
9
|
-
ISerializedStore,
|
|
10
|
-
isProduction,
|
|
11
|
-
isValidCollectionDescription,
|
|
12
|
-
} from '@websolutespa/bom-core';
|
|
13
|
-
import { toCategory } from './category';
|
|
14
|
-
import { toCollection } from './collection';
|
|
15
|
-
import { toPage } from './page';
|
|
16
|
-
import { createRouteService } from './route';
|
|
17
|
-
|
|
18
|
-
// storeStrategy
|
|
19
|
-
export enum StoreStrategy {
|
|
20
|
-
Local = 'local',
|
|
21
|
-
Api = 'api',
|
|
22
|
-
DecoratedApi = 'decoratedApi',
|
|
23
|
-
Data = 'data',
|
|
24
|
-
Mock = 'mock',
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getStoreStrategy(): StoreStrategy {
|
|
28
|
-
let storeStrategy = StoreStrategy.Mock;
|
|
29
|
-
if (typeof process !== 'undefined' && process.env.STORE_STRATEGY) {
|
|
30
|
-
storeStrategy = process.env.STORE_STRATEGY as StoreStrategy;
|
|
31
|
-
}
|
|
32
|
-
return storeStrategy;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const storeStrategy: StoreStrategy = getStoreStrategy();
|
|
36
|
-
|
|
37
|
-
export function resolveMockStore(data: IMockStore): IMemoryStore {
|
|
38
|
-
const mocks = { ...data.collections, ...data.pages };
|
|
39
|
-
const collectionKeys = Object.keys(data.collections);
|
|
40
|
-
const pageKeys = Object.keys(data.pages);
|
|
41
|
-
const keys = collectionKeys.concat(pageKeys);
|
|
42
|
-
const store: ISerializedStore = {};
|
|
43
|
-
const createdAt = new Date();
|
|
44
|
-
const updatedAt = new Date();
|
|
45
|
-
const collections: ICollectionDescription[] = keys.map((key) => getCollectionDescription(key));
|
|
46
|
-
collections.forEach((description) => {
|
|
47
|
-
if (!isValidCollectionDescription(description)) {
|
|
48
|
-
throw `collection error: invalid singular key ${description.singularName}`;
|
|
49
|
-
}
|
|
50
|
-
const mockItems = (mocks[description.singularName as keyof typeof mocks] || []) as IEntity[];
|
|
51
|
-
const items = mockData(mockItems);
|
|
52
|
-
let collectionService: ISerializedCollection;
|
|
53
|
-
if (description.singularName === 'category') {
|
|
54
|
-
// console.log('store.buildStore', 'categories');
|
|
55
|
-
collectionService = toCategory(description, items);
|
|
56
|
-
} else if (pageKeys.includes(description.singularName)) {
|
|
57
|
-
// console.log('store.buildStore', 'pages');
|
|
58
|
-
collectionService = toPage(description, items, createdAt, updatedAt);
|
|
59
|
-
} else {
|
|
60
|
-
// console.log('store.buildStore', 'data');
|
|
61
|
-
collectionService = toCollection(description, items, createdAt, updatedAt);
|
|
62
|
-
}
|
|
63
|
-
store[description.singularName] = collectionService;
|
|
64
|
-
});
|
|
65
|
-
if (!store.category) {
|
|
66
|
-
store.category = toCategory(getCollectionDescription('category'), []);
|
|
67
|
-
}
|
|
68
|
-
if (!store.menu) {
|
|
69
|
-
store.menu = toCollection(getCollectionDescription('menu'), []);
|
|
70
|
-
}
|
|
71
|
-
if (!store.redirect) {
|
|
72
|
-
store.redirect = toCollection(getCollectionDescription('redirect'), []);
|
|
73
|
-
}
|
|
74
|
-
const routeService = createRouteService(store, pageKeys);
|
|
75
|
-
store.route = routeService;
|
|
76
|
-
// const serializedCollections: ISerializedCollection[] = Object.keys(store).map((key) => store[key]!);
|
|
77
|
-
// console.log('serializedCollections', serializedCollections);
|
|
78
|
-
const mockStore: IMemoryStore = {};
|
|
79
|
-
Object.keys(store).forEach((key) => {
|
|
80
|
-
mockStore[key] = store[key]!.items;
|
|
81
|
-
});
|
|
82
|
-
console.log('[mock] resolveMockStore');
|
|
83
|
-
return mockStore;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function serializeMockStore(mockStore: IMockStore) {
|
|
87
|
-
if (!isProduction && storeStrategy === StoreStrategy.Mock) {
|
|
88
|
-
const store = resolveMockStore(mockStore);
|
|
89
|
-
return JSON.stringify(store);
|
|
90
|
-
} else {
|
|
91
|
-
return JSON.stringify({});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function mockEnv(mockStore: IMockStore) {
|
|
96
|
-
if (storeStrategy === StoreStrategy.Mock) {
|
|
97
|
-
return {
|
|
98
|
-
COLLECTIONS: [...Object.keys(mockStore.collections), ...Object.keys(mockStore.pages)].join(
|
|
99
|
-
','
|
|
100
|
-
),
|
|
101
|
-
PAGES: [...Object.keys(mockStore.pages)].join(','),
|
|
102
|
-
STORE: serializeMockStore(mockStore),
|
|
103
|
-
};
|
|
104
|
-
} else {
|
|
105
|
-
return {
|
|
106
|
-
COLLECTIONS: [
|
|
107
|
-
'category',
|
|
108
|
-
'label',
|
|
109
|
-
'locale',
|
|
110
|
-
'market',
|
|
111
|
-
'menu',
|
|
112
|
-
'redirect',
|
|
113
|
-
'route',
|
|
114
|
-
'template',
|
|
115
|
-
].join(','),
|
|
116
|
-
PAGES: [].join(','),
|
|
117
|
-
STORE: JSON.stringify({
|
|
118
|
-
category: [],
|
|
119
|
-
label: [],
|
|
120
|
-
locale: [],
|
|
121
|
-
market: [],
|
|
122
|
-
menu: [],
|
|
123
|
-
redirect: [],
|
|
124
|
-
route: [],
|
|
125
|
-
template: [],
|
|
126
|
-
}),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function mockData(items: IEntity[]): IEntity[] {
|
|
132
|
-
items.forEach((item, i) => {
|
|
133
|
-
if (item.id == null) {
|
|
134
|
-
item.id = i + 1;
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
if (items.length > 0) {
|
|
138
|
-
const inherit: boolean =
|
|
139
|
-
items.find((x) => x.$inherit && (x.$inherit === true || isArray(x.$inherit))) !== undefined;
|
|
140
|
-
if (inherit) {
|
|
141
|
-
const forbiddenProperties = ['slug', 'meta', 'order', 'isDefault', 'isHidden', 'useSplat'];
|
|
142
|
-
let lastItem: IEntity | undefined = undefined;
|
|
143
|
-
const parsedItems = items.map(({ $inherit, ...item }) => {
|
|
144
|
-
let inheritedItem = item;
|
|
145
|
-
if (lastItem) {
|
|
146
|
-
if ($inherit === true) {
|
|
147
|
-
inheritedItem = { ...lastItem, ...item };
|
|
148
|
-
} else if (isArray($inherit)) {
|
|
149
|
-
inheritedItem = { ...pickProps(lastItem, ...($inherit as string[])), ...item };
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
lastItem = Object.fromEntries(
|
|
153
|
-
Object.entries(inheritedItem).filter(([k, v]) => !forbiddenProperties.includes(k))
|
|
154
|
-
) as IEntity;
|
|
155
|
-
return inheritedItem;
|
|
156
|
-
});
|
|
157
|
-
return parsedItems;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return items;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function pickProps(
|
|
164
|
-
item: Record<string, unknown>,
|
|
165
|
-
...props: string[]
|
|
166
|
-
): Record<string, unknown> {
|
|
167
|
-
return Object.fromEntries(Object.entries(item).filter(([k, v]) => props.includes(k)));
|
|
168
|
-
}
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
'server-only';
|
|
2
|
-
|
|
3
|
-
import { SignJWT, jwtVerify } from 'jose';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Minimal cookie store accepted by the session helpers: the object returned by
|
|
7
|
-
* `await cookies()` from `next/headers`. The template reads it and passes it
|
|
8
|
-
* down, so these helpers never read request data themselves.
|
|
9
|
-
*/
|
|
10
|
-
export type SessionCookies = {
|
|
11
|
-
get(name: string): { value: string } | undefined;
|
|
12
|
-
set?(name: string, value: string, options?: Record<string, unknown>): unknown;
|
|
13
|
-
delete?(name: string): unknown;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type ISession = {
|
|
17
|
-
token: string;
|
|
18
|
-
expiresAt: Date;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
function getSessionSecret() {
|
|
22
|
-
const secret = process.env.MIXER_SECRET || '';
|
|
23
|
-
if (!secret) {
|
|
24
|
-
throw 'getSessionSecret MIXER_SECRET not defined';
|
|
25
|
-
}
|
|
26
|
-
return secret;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function getSessionKey() {
|
|
30
|
-
const secret = getSessionSecret();
|
|
31
|
-
const sessionKey = new TextEncoder().encode(secret);
|
|
32
|
-
return sessionKey;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function encrypt(session: ISession) {
|
|
36
|
-
try {
|
|
37
|
-
const sessionKey = getSessionKey();
|
|
38
|
-
return new SignJWT(session)
|
|
39
|
-
.setProtectedHeader({ alg: 'HS256' })
|
|
40
|
-
.setIssuedAt()
|
|
41
|
-
.setExpirationTime('7d')
|
|
42
|
-
.sign(sessionKey);
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.log('encrypt.error', 'Failed to encrypt session');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function decrypt(session: string | undefined = '') {
|
|
49
|
-
try {
|
|
50
|
-
const sessionKey = getSessionKey();
|
|
51
|
-
const { payload } = await jwtVerify(session, sessionKey, {
|
|
52
|
-
algorithms: ['HS256'],
|
|
53
|
-
});
|
|
54
|
-
return payload;
|
|
55
|
-
} catch (error) {
|
|
56
|
-
console.log('decrypt.error', 'Failed to decrypt session');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export async function createSession(token: string, cookies: SessionCookies) {
|
|
61
|
-
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
|
62
|
-
const session = await encrypt({ token, expiresAt });
|
|
63
|
-
if (session) {
|
|
64
|
-
cookies.set?.('session', session, {
|
|
65
|
-
httpOnly: true,
|
|
66
|
-
secure: true,
|
|
67
|
-
expires: expiresAt,
|
|
68
|
-
sameSite: 'lax',
|
|
69
|
-
path: '/',
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export async function getSession(cookies: SessionCookies) {
|
|
75
|
-
const encriptedSession = cookies.get('session')?.value;
|
|
76
|
-
const session = await decrypt(encriptedSession);
|
|
77
|
-
return session as ISession | undefined;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export async function hasValidSession(cookies: SessionCookies) {
|
|
81
|
-
const session = await getSession(cookies);
|
|
82
|
-
return session?.token && new Date(session?.expiresAt) > new Date();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export async function updateSession(cookies: SessionCookies) {
|
|
86
|
-
const session = cookies.get('session')?.value;
|
|
87
|
-
if (!session) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const payload = await decrypt(session);
|
|
91
|
-
if (!payload) {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
|
95
|
-
cookies.set?.('session', session, {
|
|
96
|
-
httpOnly: true,
|
|
97
|
-
secure: true,
|
|
98
|
-
expires: expires,
|
|
99
|
-
sameSite: 'lax',
|
|
100
|
-
path: '/',
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export async function deleteSession(cookies: SessionCookies) {
|
|
105
|
-
cookies.delete?.('session');
|
|
106
|
-
}
|