@akinon/next 1.45.0-rc.5 → 1.46.0

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 (53) hide show
  1. package/CHANGELOG.md +5 -316
  2. package/api/client.ts +9 -39
  3. package/assets/styles/index.scss +26 -50
  4. package/components/index.ts +0 -1
  5. package/components/input.tsx +7 -21
  6. package/components/link.tsx +13 -17
  7. package/components/pagination.tsx +2 -1
  8. package/components/price.tsx +4 -11
  9. package/components/selected-payment-option-view.tsx +38 -26
  10. package/data/client/account.ts +9 -10
  11. package/data/client/address.ts +8 -32
  12. package/data/client/api.ts +2 -2
  13. package/data/client/b2b.ts +2 -35
  14. package/data/client/basket.ts +5 -6
  15. package/data/client/checkout.ts +4 -47
  16. package/data/client/wishlist.ts +2 -70
  17. package/data/server/category.ts +2 -2
  18. package/data/server/list.ts +2 -2
  19. package/data/server/product.ts +13 -15
  20. package/data/server/special-page.ts +2 -2
  21. package/data/urls.ts +3 -17
  22. package/hooks/index.ts +1 -2
  23. package/hooks/use-payment-options.ts +1 -2
  24. package/lib/cache.ts +6 -18
  25. package/middlewares/default.ts +2 -50
  26. package/middlewares/locale.ts +30 -32
  27. package/middlewares/pretty-url.ts +0 -4
  28. package/middlewares/url-redirection.ts +0 -4
  29. package/package.json +4 -5
  30. package/plugins.d.ts +0 -1
  31. package/redux/middlewares/checkout.ts +11 -70
  32. package/redux/reducers/checkout.ts +5 -24
  33. package/redux/reducers/config.ts +0 -2
  34. package/types/commerce/account.ts +0 -1
  35. package/types/commerce/address.ts +1 -1
  36. package/types/commerce/b2b.ts +2 -12
  37. package/types/commerce/checkout.ts +0 -30
  38. package/types/commerce/misc.ts +0 -2
  39. package/types/commerce/order.ts +0 -12
  40. package/types/index.ts +2 -30
  41. package/utils/app-fetch.ts +1 -1
  42. package/utils/generate-commerce-search-params.ts +2 -6
  43. package/utils/index.ts +6 -27
  44. package/utils/menu-generator.ts +2 -2
  45. package/utils/server-translation.ts +1 -5
  46. package/with-pz-config.js +1 -11
  47. package/assets/styles/index.css +0 -49
  48. package/assets/styles/index.css.map +0 -1
  49. package/components/file-input.tsx +0 -8
  50. package/hooks/use-message-listener.ts +0 -24
  51. package/lib/cache-handler.mjs +0 -33
  52. package/routes/pretty-url.tsx +0 -194
  53. package/utils/redirection-iframe.ts +0 -85
package/types/index.ts CHANGED
@@ -2,7 +2,7 @@ import { LocaleUrlStrategy } from '../localization';
2
2
  import { PzNextRequest } from '../middlewares';
3
3
  import { Control, FieldError } from 'react-hook-form';
4
4
  import { ReactNode } from 'react';
5
- import { UsePaginationType } from '../hooks/use-pagination';
5
+
6
6
  declare global {
7
7
  interface Window {
8
8
  // we did it like this because declare types needs to be identical, if not it will fail
@@ -68,17 +68,9 @@ export interface Currency {
68
68
  * @see https://en.wikipedia.org/wiki/ISO_4217
69
69
  */
70
70
  code: string;
71
- /**
72
- * Number of decimal places to display.
73
- *
74
- * @example
75
- * decimalScale: 3
76
- */
77
- decimalScale?: number;
78
71
  }
79
72
 
80
73
  export interface Settings {
81
- usePrettyUrlRoute?: boolean;
82
74
  commerceUrl: string;
83
75
  redis: {
84
76
  defaultExpirationTime: number;
@@ -224,7 +216,7 @@ export type Translations = { [key: string]: object };
224
216
 
225
217
  export interface PageProps<T = any> {
226
218
  params: T;
227
- searchParams: { [key: string]: string | string[] | undefined };
219
+ searchParams: URLSearchParams;
228
220
  }
229
221
 
230
222
  export interface LayoutProps<T = any> extends PageProps<T> {
@@ -258,8 +250,6 @@ export interface ButtonProps
258
250
  appearance?: 'filled' | 'outlined' | 'ghost';
259
251
  }
260
252
 
261
- export type FileInputProps = React.HTMLProps<HTMLInputElement>;
262
-
263
253
  export interface PriceProps {
264
254
  currencyCode?: string;
265
255
  useCurrencySymbol?: boolean;
@@ -293,21 +283,3 @@ export interface AccordionProps {
293
283
  export interface PluginModuleComponentProps {
294
284
  settings?: Record<string, any>;
295
285
  }
296
-
297
- export interface PaginationProps {
298
- total: number | undefined;
299
- limit?: number | undefined;
300
- currentPage: number | undefined;
301
- numberOfPages?: number | undefined;
302
- containerClassName?: string;
303
- moreButtonClassName?: string;
304
- prevClassName?: string;
305
- nextClassName?: string;
306
- pageClassName?: string;
307
- threshold?: number | undefined;
308
- delta?: number | undefined;
309
- render?: (pagination: UsePaginationType) => ReactNode;
310
- type?: 'infinite' | 'list' | 'more';
311
- onPageChange?: (page: number) => void;
312
- direction?: 'next' | 'prev';
313
- }
@@ -41,7 +41,7 @@ const appFetch = async <T>(
41
41
  };
42
42
 
43
43
  init.next = {
44
- revalidate: Settings.usePrettyUrlRoute ? 0 : 60
44
+ revalidate: 60
45
45
  };
46
46
 
47
47
  logger.debug(`FETCH START ${url}`, { requestURL, init, ip });
@@ -1,15 +1,11 @@
1
1
  import logger from './log';
2
2
 
3
- export const generateCommerceSearchParams = (searchParams?: {
4
- [key: string]: string | string[] | undefined;
5
- }) => {
3
+ export const generateCommerceSearchParams = (searchParams?: URLSearchParams) => {
6
4
  if (!searchParams) {
7
5
  return null;
8
6
  }
9
7
 
10
- const urlSerchParams = new URLSearchParams(
11
- searchParams as Record<string, string>
12
- );
8
+ const urlSerchParams = new URLSearchParams(searchParams);
13
9
 
14
10
  Object.entries(searchParams).forEach(([key, value]) => {
15
11
  if (typeof value === 'object') {
package/utils/index.ts CHANGED
@@ -63,33 +63,18 @@ export function getTranslateFn(path: string, translations: any) {
63
63
 
64
64
  export function buildClientRequestUrl(
65
65
  path: string,
66
- options?: ClientRequestOptions & { cache?: boolean }
66
+ options?: ClientRequestOptions
67
67
  ) {
68
68
  let url = `/api/client${path}`;
69
69
 
70
- let hasQuery = url.includes('?');
71
-
72
70
  if (options) {
73
- const { cache, ...otherOptions } = options;
74
-
75
- if (Object.keys(otherOptions).length > 0) {
76
- if (hasQuery) {
77
- url += '&';
78
- } else {
79
- url += '?';
80
- hasQuery = true;
81
- }
82
- url += `options=${encodeURIComponent(JSON.stringify(otherOptions))}`;
71
+ if (url.includes('?')) {
72
+ url += '&';
73
+ } else {
74
+ url += '?';
83
75
  }
84
76
 
85
- if (cache === false) {
86
- if (hasQuery) {
87
- url += '&';
88
- } else {
89
- url += '?';
90
- }
91
- url += `t=${Date.now()}`;
92
- }
77
+ url += `options=${encodeURIComponent(JSON.stringify(options))}`;
93
78
  }
94
79
 
95
80
  return url;
@@ -117,12 +102,6 @@ export function buildCDNUrl(url: string, config?: CDNOptions) {
117
102
  ''
118
103
  );
119
104
 
120
- const noOptionFileExtension = url?.split('.').pop()?.toLowerCase() ?? '';
121
-
122
- if (noOptionFileExtension === 'svg' || noOptionFileExtension === 'gif') {
123
- return url;
124
- }
125
-
126
105
  let options = '';
127
106
 
128
107
  if (config) {
@@ -13,8 +13,8 @@ export const menuGenerator = (arr: MenuItemType[]) => {
13
13
  });
14
14
 
15
15
  Object.values(data).forEach((item) => {
16
- if (item.parent_pk) {
17
- data[item.parent_pk].children.push(item);
16
+ if (item.parent) {
17
+ data[item.parent.pk].children.push(item);
18
18
  } else {
19
19
  tree.push(item);
20
20
  }
@@ -5,10 +5,10 @@ import { getTranslateFn } from '../utils';
5
5
  import { Translations } from '../types';
6
6
  import settings from 'settings';
7
7
  import logger from './log';
8
- import { unstable_cache } from 'next/cache';
9
8
 
10
9
  export const translations: Translations = {};
11
10
 
11
+ // TODO: Read translations from cache
12
12
  export async function getTranslations(locale_: string) {
13
13
  try {
14
14
  const locale = settings.localization.locales.find(
@@ -52,10 +52,6 @@ export async function getTranslations(locale_: string) {
52
52
  return translations;
53
53
  }
54
54
 
55
- export const getCachedTranslations = unstable_cache(async (locale: string) =>
56
- getTranslations(locale)
57
- );
58
-
59
55
  export function t(path: string) {
60
56
  return getTranslateFn(path, translations);
61
57
  }
package/with-pz-config.js CHANGED
@@ -8,7 +8,6 @@ const defaultConfig = {
8
8
  transpilePackages: ['@akinon/next', ...pzPlugins.map((p) => `@akinon/${p}`)],
9
9
  skipTrailingSlashRedirect: true,
10
10
  poweredByHeader: false,
11
- cacheMaxMemorySize: 0,
12
11
  env: {
13
12
  NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN
14
13
  },
@@ -66,12 +65,7 @@ const defaultConfig = {
66
65
  }
67
66
  };
68
67
 
69
- const withPzConfig = (
70
- myNextConfig = {},
71
- options = {
72
- useCacheHandler: false
73
- }
74
- ) => {
68
+ const withPzConfig = (myNextConfig = {}) => {
75
69
  let extendedConfig = deepMerge({}, defaultConfig, myNextConfig);
76
70
 
77
71
  const originalPzHeadersFunction = defaultConfig.headers;
@@ -97,10 +91,6 @@ const withPzConfig = (
97
91
  return [...pzRewrites, ...myRewrites];
98
92
  };
99
93
 
100
- if (options.useCacheHandler && process.env.CACHE_HOST) {
101
- extendedConfig.cacheHandler = require.resolve('./lib/cache-handler.mjs');
102
- }
103
-
104
94
  return extendedConfig;
105
95
  };
106
96
 
@@ -1,49 +0,0 @@
1
- .checkout-payment-iframe-wrapper {
2
- position: fixed;
3
- top: 0;
4
- left: 0;
5
- width: 100%;
6
- height: 100%;
7
- border: none;
8
- z-index: 1000;
9
- background-color: white;
10
- }
11
- .checkout-payment-iframe-wrapper iframe {
12
- width: 100%;
13
- height: 100%;
14
- border: none;
15
- background-color: white;
16
- }
17
- .checkout-payment-iframe-wrapper .close-button {
18
- position: fixed;
19
- top: 16px;
20
- right: 16px;
21
- width: 32px;
22
- height: 32px;
23
- display: flex;
24
- align-items: center;
25
- justify-content: center;
26
- z-index: 1001;
27
- }
28
-
29
- .checkout-payment-redirection-iframe-wrapper {
30
- width: 100%;
31
- position: relative;
32
- }
33
- .checkout-payment-redirection-iframe-wrapper iframe {
34
- width: 100%;
35
- height: 100%;
36
- border: none;
37
- background-color: white;
38
- }
39
- .checkout-payment-redirection-iframe-wrapper .close-button {
40
- position: absolute;
41
- top: 16px;
42
- right: 16px;
43
- width: 32px;
44
- height: 32px;
45
- display: flex;
46
- align-items: center;
47
- justify-content: center;
48
- z-index: 1001;
49
- }/*# sourceMappingURL=index.css.map */
@@ -1 +0,0 @@
1
- {"version":3,"sources":["index.scss","index.css"],"names":[],"mappings":"AAAA;EACE,eAAA;EACA,MAAA;EACA,OAAA;EACA,WAAA;EACA,YAAA;EACA,YAAA;EACA,aAAA;EACA,uBAAA;ACCF;ADCE;EACE,WAAA;EACA,YAAA;EACA,YAAA;EACA,uBAAA;ACCJ;ADEE;EACE,eAAA;EACA,SAAA;EACA,WAAA;EACA,WAAA;EACA,YAAA;EACA,aAAA;EACA,mBAAA;EACA,uBAAA;EACA,aAAA;ACAJ;;ADIA;EACE,WAAA;EACA,kBAAA;ACDF;ADGE;EACE,WAAA;EACA,YAAA;EACA,YAAA;EACA,uBAAA;ACDJ;ADIE;EACE,kBAAA;EACA,SAAA;EACA,WAAA;EACA,WAAA;EACA,YAAA;EACA,aAAA;EACA,mBAAA;EACA,uBAAA;EACA,aAAA;ACFJ","file":"index.css"}
@@ -1,8 +0,0 @@
1
- import { forwardRef } from 'react';
2
- import { FileInputProps } from '../types/index';
3
-
4
- export const FileInput = forwardRef<HTMLInputElement, FileInputProps>(
5
- function fileInput(props, ref) {
6
- return <input type="file" {...props} ref={ref} />;
7
- }
8
- );
@@ -1,24 +0,0 @@
1
- import { useEffect } from 'react';
2
-
3
- export const useMessageListener = () => {
4
- useEffect(() => {
5
- const handleMessage = (event: MessageEvent) => {
6
- if (event.origin !== window.location.origin) {
7
- return;
8
- }
9
-
10
- if (event.data && typeof event.data === 'string') {
11
- const messageData = JSON.parse(event.data);
12
- if (messageData?.url) {
13
- window.location.href = messageData.url;
14
- }
15
- }
16
- };
17
-
18
- window.addEventListener('message', handleMessage);
19
-
20
- return () => {
21
- window.removeEventListener('message', handleMessage);
22
- };
23
- }, []);
24
- };
@@ -1,33 +0,0 @@
1
- import { CacheHandler } from '@neshca/cache-handler';
2
- import createLruHandler from '@neshca/cache-handler/local-lru';
3
- import createRedisHandler from '@neshca/cache-handler/redis-stack';
4
- import { createClient } from 'redis';
5
-
6
- CacheHandler.onCreation(async () => {
7
- const redisUrl = `redis://${process.env.CACHE_HOST}:${
8
- process.env.CACHE_PORT
9
- }/${process.env.CACHE_BUCKET ?? '0'}`;
10
-
11
- const client = createClient({
12
- url: redisUrl
13
- });
14
-
15
- client.on('error', (error) => {
16
- console.error('Redis client error', { redisUrl, error });
17
- });
18
-
19
- await client.connect();
20
-
21
- const redisHandler = await createRedisHandler({
22
- client,
23
- timeoutMs: 5000
24
- });
25
-
26
- const localHandler = createLruHandler();
27
-
28
- return {
29
- handlers: [redisHandler, localHandler]
30
- };
31
- });
32
-
33
- export default CacheHandler;
@@ -1,194 +0,0 @@
1
- import { URLS } from '@akinon/next/data/urls';
2
- import { Metadata, PageProps } from '@akinon/next/types';
3
- import logger from '@akinon/next/utils/log';
4
- import { notFound } from 'next/navigation';
5
-
6
- type PrettyUrlResult = {
7
- matched: boolean;
8
- path?: string;
9
- pk?: number;
10
- };
11
-
12
- const resolvePrettyUrlHandler =
13
- (pathname: string, ip: string | null) => async () => {
14
- let results = [] as { old_path: string }[];
15
- let prettyUrlResult: PrettyUrlResult = {
16
- matched: false
17
- };
18
-
19
- try {
20
- const requestUrl = URLS.misc.prettyUrls(`/${pathname}/`);
21
-
22
- logger.debug(`Resolving pretty url`, { pathname, requestUrl, ip });
23
-
24
- const start = Date.now();
25
- const apiResponse = await fetch(requestUrl, {
26
- next: {
27
- revalidate: 0
28
- }
29
- });
30
- const data = await apiResponse.json();
31
- ({ results } = data);
32
- const end = Date.now();
33
- console.warn('Pretty url response time', end - start, requestUrl);
34
-
35
- const matched = results.length > 0;
36
- const [{ old_path: path } = { old_path: '' }] = results;
37
- let pk;
38
-
39
- if (matched) {
40
- const pkRegex = /\/(\d+)\/$/;
41
- const match = path.match(pkRegex);
42
- pk = match ? parseInt(match[1]) : undefined;
43
- }
44
-
45
- prettyUrlResult = {
46
- matched,
47
- path,
48
- pk
49
- };
50
-
51
- logger.trace('Pretty url result', { prettyUrlResult, ip });
52
- } catch (error) {
53
- logger.error('Error resolving pretty url', { error, pathname, ip });
54
- }
55
-
56
- return prettyUrlResult;
57
- };
58
-
59
- export async function generateMetadata({ params }: PageProps) {
60
- let result: Metadata = {};
61
- const { prettyurl } = params;
62
- const pageSlug = prettyurl
63
- .filter((x) => !x.startsWith('searchparams'))
64
- .join('/');
65
-
66
- const searchParams = Object.fromEntries(
67
- new URLSearchParams(
68
- decodeURIComponent(
69
- prettyurl
70
- .find((x) => x.startsWith('searchparams'))
71
- ?.replace('searchparams%7C', '')
72
- ) ?? {}
73
- ).entries()
74
- );
75
-
76
- const prettyUrlResult = await resolvePrettyUrlHandler(pageSlug, null)();
77
-
78
- if (!prettyUrlResult.matched) {
79
- return notFound();
80
- }
81
-
82
- const commonProps = {
83
- params: {
84
- ...params,
85
- pk: prettyUrlResult.pk
86
- },
87
- searchParams
88
- };
89
-
90
- try {
91
- if (prettyUrlResult.path.startsWith('/product/')) {
92
- await import('@product/[pk]/page').then(async (module) => {
93
- result = await module['generateMetadata']?.(commonProps);
94
- });
95
- }
96
-
97
- if (prettyUrlResult.path.startsWith('/group-product/')) {
98
- await import('@group-product/[pk]/page').then(async (module) => {
99
- result = await module['generateMetadata']?.(commonProps);
100
- });
101
- }
102
-
103
- if (prettyUrlResult.path.startsWith('/category/')) {
104
- await import('@category/[pk]/page').then(async (module) => {
105
- result = await module['generateMetadata']?.(commonProps);
106
- });
107
- }
108
-
109
- if (prettyUrlResult.path.startsWith('/special-page/')) {
110
- await import('@special-page/[pk]/page').then(async (module) => {
111
- result = await module['generateMetadata']?.(commonProps);
112
- });
113
- }
114
-
115
- if (prettyUrlResult.path.startsWith('/flat-page/')) {
116
- await import('@flat-page/[pk]/page').then(async (module) => {
117
- result = await module['generateMetadata']?.(commonProps);
118
- });
119
- }
120
- // eslint-disable-next-line no-empty
121
- } catch (error) {}
122
-
123
- return result;
124
- }
125
-
126
- export const dynamic = 'force-static';
127
- export const revalidate = 300;
128
-
129
- export default async function Page({ params }) {
130
- const { prettyurl } = params;
131
- const pageSlug = prettyurl
132
- .filter((x) => !x.startsWith('searchparams'))
133
- .join('/');
134
-
135
- const urlSearchParams = new URLSearchParams(
136
- decodeURIComponent(
137
- prettyurl
138
- .find((x) => x.startsWith('searchparams'))
139
- ?.replace('searchparams%7C', '')
140
- ) ?? {}
141
- );
142
-
143
- const searchParams = {};
144
-
145
- for (const [key, value] of urlSearchParams.entries() as unknown as Array<
146
- [string, string]
147
- >) {
148
- if (!searchParams[key]) {
149
- searchParams[key] = [];
150
- }
151
- searchParams[key].push(value);
152
- }
153
-
154
- const result = await resolvePrettyUrlHandler(pageSlug, null)();
155
-
156
- if (!result.matched) {
157
- return notFound();
158
- }
159
-
160
- const commonProps = {
161
- params: {
162
- ...params,
163
- pk: result.pk
164
- },
165
- searchParams
166
- };
167
-
168
- if (result.path.startsWith('/category/')) {
169
- const CategoryPage = (await import('@category/[pk]/page')).default;
170
- return <CategoryPage {...commonProps} />;
171
- }
172
-
173
- if (result.path.startsWith('/product/')) {
174
- const ProductPage = (await import('@product/[pk]/page')).default;
175
- return <ProductPage {...commonProps} />;
176
- }
177
-
178
- if (result.path.startsWith('/group-product/')) {
179
- const GroupProduct = (await import('@group-product/[pk]/page')).default;
180
- return <GroupProduct {...commonProps} />;
181
- }
182
-
183
- if (result.path.startsWith('/special-page/')) {
184
- const SpecialPage = (await import('@special-page/[pk]/page')).default;
185
- return <SpecialPage {...commonProps} />;
186
- }
187
-
188
- if (result.path.startsWith('/flat-page/')) {
189
- const FlatPage = (await import('@flat-page/[pk]/page')).default;
190
- return <FlatPage {...commonProps} />;
191
- }
192
-
193
- return null;
194
- }
@@ -1,85 +0,0 @@
1
- const iframeURLChange = (iframe, callback) => {
2
- iframe.addEventListener('load', () => {
3
- setTimeout(() => {
4
- if (iframe?.contentWindow?.location) {
5
- callback(iframe.contentWindow.location);
6
- }
7
- }, 0);
8
- });
9
- };
10
-
11
- const removeIframe = async () => {
12
- const iframeSelector = document.querySelector(
13
- '.checkout-payment-redirection-iframe-wrapper'
14
- );
15
-
16
- const redirectionPaymentWrapper = document.querySelector(
17
- '.checkout-redirection-payment-wrapper'
18
- );
19
-
20
- const form = redirectionPaymentWrapper.querySelector('form') as HTMLElement;
21
-
22
- if (!iframeSelector) {
23
- return;
24
- }
25
-
26
- iframeSelector.remove();
27
-
28
- if (form) {
29
- form.style.display = 'block';
30
- }
31
-
32
- location.reload();
33
- };
34
-
35
- export const showRedirectionIframe = (redirectUrl: string) => {
36
- const iframeWrapper = document.createElement('div');
37
- const iframe = document.createElement('iframe');
38
- const closeButton = document.createElement('div');
39
- const redirectionPaymentWrapper = document.querySelector(
40
- '.checkout-redirection-payment-wrapper'
41
- );
42
- const form = redirectionPaymentWrapper.querySelector('form') as HTMLElement;
43
-
44
- iframeWrapper.className = 'checkout-payment-redirection-iframe-wrapper';
45
- closeButton.className = 'close-button';
46
-
47
- iframe.setAttribute('src', redirectUrl);
48
- closeButton.innerHTML = '&#x2715';
49
- closeButton.addEventListener('click', removeIframe);
50
-
51
- iframeWrapper.append(iframe, closeButton);
52
-
53
- if (form) {
54
- form.style.display = 'none';
55
- }
56
-
57
- redirectionPaymentWrapper.appendChild(iframeWrapper);
58
-
59
- iframeURLChange(iframe, (location) => {
60
- if (location.origin !== window.location.origin) {
61
- return false;
62
- }
63
-
64
- const searchParams = new URLSearchParams(location.search);
65
- const isOrderCompleted = location.href.includes('/orders/completed');
66
-
67
- if (isOrderCompleted) {
68
- (window.parent as any)?.postMessage?.(
69
- JSON.stringify({
70
- url: location.pathname
71
- })
72
- );
73
- }
74
-
75
- if (
76
- searchParams.has('success') ||
77
- isOrderCompleted ||
78
- location.href.includes('/orders/checkout')
79
- ) {
80
- setTimeout(() => {
81
- removeIframe();
82
- }, 0);
83
- }
84
- });
85
- };