@prezly/theme-kit-nextjs 1.3.1 → 1.4.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.
- package/README.md +15 -11
- package/build/adapter-nextjs/getNewsroomServerSideProps.d.ts +6 -0
- package/build/adapter-nextjs/getNewsroomServerSideProps.js +5 -1
- package/build/adapter-nextjs/getNewsroomServerSideProps.js.map +1 -1
- package/build/adapter-nextjs/processRequest.d.ts +10 -1
- package/build/adapter-nextjs/processRequest.js +9 -0
- package/build/adapter-nextjs/processRequest.js.map +1 -1
- package/build/data-fetching/api/PrezlyApi.d.ts +12 -4
- package/build/data-fetching/api/PrezlyApi.js +13 -2
- package/build/data-fetching/api/PrezlyApi.js.map +1 -1
- package/build/data-fetching/api/getPrezlyApi.d.ts +1 -1
- package/build/data-fetching/api/getPrezlyApi.js +5 -5
- package/build/data-fetching/api/getPrezlyApi.js.map +1 -1
- package/build/data-fetching/api/languages.d.ts +9 -0
- package/build/data-fetching/api/languages.js +9 -0
- package/build/data-fetching/api/languages.js.map +1 -1
- package/build/data-fetching/api/queries.d.ts +5 -5
- package/build/data-fetching/api/queries.js +22 -16
- package/build/data-fetching/api/queries.js.map +1 -1
- package/build/data-fetching/getAssetsUrl.d.ts +4 -0
- package/build/data-fetching/getAssetsUrl.js +4 -0
- package/build/data-fetching/getAssetsUrl.js.map +1 -1
- package/build/data-fetching/getLocalizedCategoryData.d.ts +2 -2
- package/build/data-fetching/getPrivacyPortalUrl.d.ts +1 -1
- package/build/data-fetching/getStoryPublicationDate.d.ts +1 -1
- package/build/data-fetching/getUploadcareGroupUrl.d.ts +7 -0
- package/build/data-fetching/getUploadcareGroupUrl.js +7 -0
- package/build/data-fetching/getUploadcareGroupUrl.js.map +1 -1
- package/build/data-fetching/lib/getAlgoliaSettings.d.ts +2 -2
- package/build/data-fetching/lib/getAlgoliaSettings.js +3 -3
- package/build/data-fetching/lib/getAlgoliaSettings.js.map +1 -1
- package/build/data-fetching/lib/getEnvVariables.d.ts +1 -1
- package/build/data-fetching/lib/getEnvVariables.js +7 -7
- package/build/data-fetching/lib/getEnvVariables.js.map +1 -1
- package/build/data-fetching/types.d.ts +1 -0
- package/build/intl/lang.d.ts +5 -3
- package/build/intl/lang.js +4 -2
- package/build/intl/lang.js.map +1 -1
- package/build/intl/locale.d.ts +5 -1
- package/build/intl/locale.js +9 -0
- package/build/intl/locale.js.map +1 -1
- package/build/intl/localeConfig.js +7 -2
- package/build/intl/localeConfig.js.map +1 -1
- package/build/intl/localeObject.d.ts +23 -0
- package/build/intl/localeObject.js +23 -0
- package/build/intl/localeObject.js.map +1 -1
- package/build/newsroom-context/context.d.ts +40 -3
- package/build/newsroom-context/context.js +6 -2
- package/build/newsroom-context/context.js.map +1 -1
- package/build/newsroom-context/hooks/useAlgoliaSettings.d.ts +1 -1
- package/build/newsroom-context/hooks/useAlgoliaSettings.js +2 -2
- package/build/newsroom-context/hooks/useAlgoliaSettings.js.map +1 -1
- package/build/newsroom-context/hooks/useCategories.d.ts +1 -1
- package/build/newsroom-context/hooks/useCategories.js +2 -2
- package/build/newsroom-context/hooks/useCategories.js.map +1 -1
- package/build/newsroom-context/hooks/useCompanyInformation.d.ts +1 -1
- package/build/newsroom-context/hooks/useCompanyInformation.js +2 -2
- package/build/newsroom-context/hooks/useCompanyInformation.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentCategory.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentCategory.js +2 -2
- package/build/newsroom-context/hooks/useCurrentCategory.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentLocale.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentLocale.js +2 -2
- package/build/newsroom-context/hooks/useCurrentLocale.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentStory.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentStory.js +2 -2
- package/build/newsroom-context/hooks/useCurrentStory.js.map +1 -1
- package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js +6 -1
- package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js.map +1 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.d.ts +5 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.js +10 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -1
- package/build/newsroom-context/hooks/useLanguages.d.ts +1 -1
- package/build/newsroom-context/hooks/useLanguages.js +2 -2
- package/build/newsroom-context/hooks/useLanguages.js.map +1 -1
- package/build/newsroom-context/hooks/useNewsroom.d.ts +1 -1
- package/build/newsroom-context/hooks/useNewsroom.js +2 -2
- package/build/newsroom-context/hooks/useNewsroom.js.map +1 -1
- package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -2
- package/build/newsroom-context/hooks/useNewsroomContext.js +2 -2
- package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -1
- package/build/types.d.ts +14 -1
- package/package.json +2 -10
- package/src/adapter-nextjs/getNewsroomServerSideProps.ts +7 -1
- package/src/adapter-nextjs/processRequest.ts +10 -1
- package/src/data-fetching/api/PrezlyApi.ts +19 -4
- package/src/data-fetching/api/getPrezlyApi.ts +5 -5
- package/src/data-fetching/api/languages.ts +9 -0
- package/src/data-fetching/api/queries.ts +25 -18
- package/src/data-fetching/getAssetsUrl.ts +4 -0
- package/src/data-fetching/getLocalizedCategoryData.ts +2 -2
- package/src/data-fetching/getPrivacyPortalUrl.ts +1 -1
- package/src/data-fetching/getStoryPublicationDate.tsx +1 -1
- package/src/data-fetching/getUploadcareGroupUrl.ts +7 -0
- package/src/data-fetching/lib/getAlgoliaSettings.ts +4 -4
- package/src/data-fetching/lib/getEnvVariables.ts +7 -7
- package/src/data-fetching/types.ts +1 -0
- package/src/intl/lang.ts +5 -3
- package/src/intl/locale.ts +11 -1
- package/src/intl/localeConfig.js +7 -2
- package/src/intl/localeObject.ts +23 -0
- package/src/newsroom-context/context.tsx +43 -5
- package/src/newsroom-context/hooks/useAlgoliaSettings.ts +2 -2
- package/src/newsroom-context/hooks/useCategories.ts +2 -2
- package/src/newsroom-context/hooks/useCompanyInformation.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentCategory.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentLocale.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentStory.ts +2 -2
- package/src/newsroom-context/hooks/useGetLinkLocaleSlug.ts +6 -1
- package/src/newsroom-context/hooks/useGetTranslationUrl.ts +11 -2
- package/src/newsroom-context/hooks/useLanguages.ts +2 -2
- package/src/newsroom-context/hooks/useNewsroom.ts +2 -2
- package/src/newsroom-context/hooks/useNewsroomContext.ts +4 -3
- package/src/types.ts +14 -2
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useLanguages = void 0;
|
|
4
4
|
var useNewsroomContext_1 = require("./useNewsroomContext");
|
|
5
|
-
|
|
5
|
+
function useLanguages() {
|
|
6
6
|
var context = (0, useNewsroomContext_1.useNewsroomContext)();
|
|
7
7
|
return context.languages;
|
|
8
|
-
}
|
|
8
|
+
}
|
|
9
9
|
exports.useLanguages = useLanguages;
|
|
10
10
|
//# sourceMappingURL=useLanguages.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLanguages.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useLanguages.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;
|
|
1
|
+
{"version":3,"file":"useLanguages.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useLanguages.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;AAE1D,SAAgB,YAAY;IACxB,IAAM,OAAO,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAErC,OAAO,OAAO,CAAC,SAAS,CAAC;AAC7B,CAAC;AAJD,oCAIC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare
|
|
1
|
+
export declare function useNewsroom(): import("@prezly/sdk").Newsroom;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useNewsroom = void 0;
|
|
4
4
|
var useNewsroomContext_1 = require("./useNewsroomContext");
|
|
5
|
-
|
|
5
|
+
function useNewsroom() {
|
|
6
6
|
var context = (0, useNewsroomContext_1.useNewsroomContext)();
|
|
7
7
|
return context.newsroom;
|
|
8
|
-
}
|
|
8
|
+
}
|
|
9
9
|
exports.useNewsroom = useNewsroom;
|
|
10
10
|
//# sourceMappingURL=useNewsroom.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useNewsroom.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroom.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;
|
|
1
|
+
{"version":3,"file":"useNewsroom.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroom.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;AAE1D,SAAgB,WAAW;IACvB,IAAM,OAAO,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAErC,OAAO,OAAO,CAAC,QAAQ,CAAC;AAC5B,CAAC;AAJD,kCAIC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { NewsroomContextType } from '../context';
|
|
2
|
-
export declare
|
|
1
|
+
import type { NewsroomContextType } from '../context';
|
|
2
|
+
export declare function useNewsroomContext(): NewsroomContextType;
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useNewsroomContext = void 0;
|
|
4
4
|
var react_1 = require("react");
|
|
5
5
|
var context_1 = require("../context");
|
|
6
|
-
|
|
6
|
+
function useNewsroomContext() {
|
|
7
7
|
// TS Compiler is having a hard time figuring this type implicitly, so we have to set it explicitly
|
|
8
8
|
// See https://github.com/microsoft/TypeScript/issues/5711#issuecomment-340283439
|
|
9
9
|
var newsroomContext = (0, react_1.useContext)(context_1.NewsroomContext);
|
|
@@ -11,6 +11,6 @@ var useNewsroomContext = function () {
|
|
|
11
11
|
throw new Error('No `NewsroomContextProvider` found when calling `useNewsroomContext`');
|
|
12
12
|
}
|
|
13
13
|
return newsroomContext;
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
exports.useNewsroomContext = useNewsroomContext;
|
|
16
16
|
//# sourceMappingURL=useNewsroomContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useNewsroomContext.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroomContext.ts"],"names":[],"mappings":";;;AAAA,+BAAmC;
|
|
1
|
+
{"version":3,"file":"useNewsroomContext.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroomContext.ts"],"names":[],"mappings":";;;AAAA,+BAAmC;AAGnC,sCAA6C;AAE7C,SAAgB,kBAAkB;IAC9B,mGAAmG;IACnG,iFAAiF;IACjF,IAAM,eAAe,GAAoC,IAAA,kBAAU,EAAC,yBAAe,CAAC,CAAC;IACrF,IAAI,CAAC,eAAe,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;KAC3F;IAED,OAAO,eAAe,CAAC;AAC3B,CAAC;AATD,gDASC"}
|
package/build/types.d.ts
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
import type { CultureRef, ExtraStoryFields, Story } from '@prezly/sdk';
|
|
2
|
-
import { NewsroomContextProps } from './newsroom-context';
|
|
2
|
+
import type { NewsroomContextProps } from './newsroom-context';
|
|
3
3
|
export interface ServerSidePageProps {
|
|
4
|
+
/**
|
|
5
|
+
* Indicates whether the locale requested by the Next application was correctly resolved to an active newsroom locale.
|
|
6
|
+
* This is used by `processRequest` method to redirect to the default locale if the property is `false`.
|
|
7
|
+
*/
|
|
4
8
|
localeResolved: boolean;
|
|
5
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* These are the props returned by the `processRequest` method that are meant to be passed to your `_app` to be injected into the `NewsroomContextProvider`.
|
|
12
|
+
*/
|
|
6
13
|
export interface PageProps {
|
|
7
14
|
newsroomContextProps: NewsroomContextProps;
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Categories in Algolia index are stored in a simplified format, with `slug` and `name` properties already saved in the correct locale for the story search result.
|
|
18
|
+
*/
|
|
9
19
|
export interface AlgoliaCategoryRef {
|
|
10
20
|
id: number;
|
|
11
21
|
name: string;
|
|
12
22
|
slug: string;
|
|
13
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Stories in Algolia index are stored in a simplified format, with only minimal subset of fields.
|
|
26
|
+
*/
|
|
14
27
|
export declare type AlgoliaStory = Pick<Story, 'uuid' | 'slug' | 'title' | 'subtitle'> & Pick<ExtraStoryFields, 'thumbnail_image'> & {
|
|
15
28
|
content_text: string;
|
|
16
29
|
updated_at: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prezly/theme-kit-nextjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Data layer and utility library for developing Prezly themes with NextJS",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -52,20 +52,12 @@
|
|
|
52
52
|
"parse-data-url": "^4.0.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
+
"@prezly/eslint-config": "^4.0.0",
|
|
55
56
|
"@types/node": "^17.0.13",
|
|
56
57
|
"@types/parse-data-url": "^3.0.0",
|
|
57
58
|
"@types/react": "^17.0.38",
|
|
58
59
|
"@types/react-dom": "^17.0.11",
|
|
59
60
|
"eslint": "^8.8.0",
|
|
60
|
-
"eslint-config-airbnb": "^19.0.4",
|
|
61
|
-
"eslint-config-airbnb-typescript": "^16.1.0",
|
|
62
|
-
"eslint-config-prettier": "^8.3.0",
|
|
63
|
-
"eslint-import-resolver-typescript": "^2.5.0",
|
|
64
|
-
"eslint-plugin-import": "^2.25.4",
|
|
65
|
-
"eslint-plugin-prettier": "^4.0.0",
|
|
66
|
-
"eslint-plugin-react": "^7.28.0",
|
|
67
|
-
"eslint-plugin-react-hooks": "^4.3.0",
|
|
68
|
-
"eslint-plugin-sort-export-all": "^1.2.1",
|
|
69
61
|
"np": "^7.6.0",
|
|
70
62
|
"prettier": "^2.5.1",
|
|
71
63
|
"rimraf": "^3.0.2",
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import type { Story } from '@prezly/sdk';
|
|
1
2
|
import type { GetServerSidePropsContext } from 'next';
|
|
2
3
|
|
|
3
4
|
import { getPrezlyApi } from '../data-fetching';
|
|
4
5
|
|
|
5
6
|
interface Options {
|
|
6
7
|
loadHomepageContacts?: boolean;
|
|
8
|
+
story?: Story;
|
|
7
9
|
}
|
|
8
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Use this function in your `getServerSideProps` page methods to retrieve the data necessary for the NewsroomContextProvider and `processRequest` function.
|
|
13
|
+
* See README for usage examples.
|
|
14
|
+
*/
|
|
9
15
|
export async function getNewsroomServerSideProps(
|
|
10
16
|
context: GetServerSidePropsContext,
|
|
11
17
|
options: Options = {},
|
|
@@ -13,7 +19,7 @@ export async function getNewsroomServerSideProps(
|
|
|
13
19
|
const { req: request, locale } = context;
|
|
14
20
|
|
|
15
21
|
const api = getPrezlyApi(request);
|
|
16
|
-
const serverSideProps = await api.getNewsroomServerSideProps(request, locale);
|
|
22
|
+
const serverSideProps = await api.getNewsroomServerSideProps(request, locale, options.story);
|
|
17
23
|
|
|
18
24
|
if (options.loadHomepageContacts) {
|
|
19
25
|
serverSideProps.newsroomContextProps.contacts = await api.getNewsroomContacts();
|
|
@@ -2,8 +2,17 @@ import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
|
|
|
2
2
|
|
|
3
3
|
import { getShortestLocaleCode } from '../data-fetching';
|
|
4
4
|
import { getRedirectToCanonicalLocale, LocaleObject } from '../intl';
|
|
5
|
-
import { PageProps, ServerSidePageProps } from '../types';
|
|
5
|
+
import type { PageProps, ServerSidePageProps } from '../types';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* This function combines the props returned from `getNewsroomServerSideProps` and your optional custom props for the page.
|
|
9
|
+
* It also handles locale redirects, if `canonicalUrl` parameter is provided.
|
|
10
|
+
* See README for usage examples.
|
|
11
|
+
*
|
|
12
|
+
* @param context Next.js context passed to the `getServerSideProps` method of the page
|
|
13
|
+
* @param props Object containing the return of `getNewsroomServerSideProps` and your optional custom props.
|
|
14
|
+
* @param canonicalUrl Optional: a canonical URL for the current page without locale part. If provided, locale redirects will be handled by the function.
|
|
15
|
+
*/
|
|
7
16
|
export function processRequest<Props>(
|
|
8
17
|
context: GetServerSidePropsContext,
|
|
9
18
|
props: Props & PageProps & ServerSidePageProps,
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type {
|
|
2
2
|
Category,
|
|
3
3
|
ExtraStoryFields,
|
|
4
4
|
Newsroom,
|
|
5
5
|
NewsroomLanguageSettings,
|
|
6
6
|
Story,
|
|
7
7
|
} from '@prezly/sdk';
|
|
8
|
+
import PrezlySDK from '@prezly/sdk';
|
|
8
9
|
import type { IncomingMessage } from 'http';
|
|
9
10
|
|
|
10
11
|
import { LocaleObject } from '../../intl';
|
|
11
|
-
import { PageProps, ServerSidePageProps } from '../../types';
|
|
12
|
+
import type { PageProps, ServerSidePageProps } from '../../types';
|
|
12
13
|
import { DEFAULT_PAGE_SIZE } from '../constants';
|
|
13
14
|
import { getAlgoliaSettings } from '../lib/getAlgoliaSettings';
|
|
14
15
|
|
|
@@ -48,14 +49,18 @@ export class PrezlyApi {
|
|
|
48
49
|
|
|
49
50
|
private readonly newsroomUuid: Newsroom['uuid'];
|
|
50
51
|
|
|
51
|
-
|
|
52
|
+
private readonly themeUuid: string | undefined;
|
|
53
|
+
|
|
54
|
+
constructor(accessToken: string, newsroomUuid: Newsroom['uuid'], themeUuid?: string) {
|
|
52
55
|
const baseUrl = process.env.API_BASE_URL_OVERRIDE ?? undefined;
|
|
53
56
|
this.sdk = new PrezlySDK({
|
|
54
57
|
accessToken,
|
|
55
58
|
baseUrl,
|
|
59
|
+
// This returns stories created by legacy version of the editor in a format that can be displayed by the Prezly Content Renderer.
|
|
56
60
|
headers: { 'X-Convert-v1-To-v3': 'true' },
|
|
57
61
|
});
|
|
58
62
|
this.newsroomUuid = newsroomUuid;
|
|
63
|
+
this.themeUuid = themeUuid;
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
getStory(uuid: string) {
|
|
@@ -76,6 +81,9 @@ export class PrezlyApi {
|
|
|
76
81
|
return (await this.sdk.newsroomLanguages.list(this.newsroomUuid)).languages;
|
|
77
82
|
}
|
|
78
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Note: this method returns ALL stories from the newsroom. It's intended to be used for sitemaps and not to display actual content.
|
|
86
|
+
*/
|
|
79
87
|
async getAllStories(order: SortOrder = DEFAULT_SORT_ORDER) {
|
|
80
88
|
const sortOrder = getSortByPublishedDate(order);
|
|
81
89
|
const newsroom = await this.getNewsroom();
|
|
@@ -194,8 +202,15 @@ export class PrezlyApi {
|
|
|
194
202
|
return this.sdk.newsroomGalleries.get(this.newsroomUuid, uuid);
|
|
195
203
|
}
|
|
196
204
|
|
|
205
|
+
/**
|
|
206
|
+
* In order to prevent issues with theme preview, we only load the theme preset for a specified theme, and not the currently active one.
|
|
207
|
+
*/
|
|
197
208
|
async getThemePreset() {
|
|
198
|
-
|
|
209
|
+
if (this.themeUuid) {
|
|
210
|
+
return this.sdk.newsroomThemes.get(this.newsroomUuid, this.themeUuid);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return null;
|
|
199
214
|
}
|
|
200
215
|
|
|
201
216
|
async getNewsroomServerSideProps(
|
|
@@ -4,13 +4,13 @@ import { getEnvVariables } from '../lib/getEnvVariables';
|
|
|
4
4
|
|
|
5
5
|
import { PrezlyApi } from './PrezlyApi';
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
if (
|
|
7
|
+
export function getPrezlyApi(req?: IncomingMessage): PrezlyApi {
|
|
8
|
+
if (typeof window !== 'undefined') {
|
|
9
9
|
throw new Error('"getPrezlyApi" should only be used on back-end side.');
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
// `getEnvVariables` handles both cases for envs parsing - .env and request headers
|
|
13
|
-
const { PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID } = getEnvVariables(req);
|
|
13
|
+
const { PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID, PREZLY_THEME_UUID } = getEnvVariables(req);
|
|
14
14
|
|
|
15
15
|
if (!PREZLY_ACCESS_TOKEN) {
|
|
16
16
|
throw new Error('"PREZLY_ACCESS_TOKEN" is not set in env variables.');
|
|
@@ -20,5 +20,5 @@ export const getPrezlyApi = (req?: IncomingMessage): PrezlyApi => {
|
|
|
20
20
|
throw new Error('"PREZLY_NEWSROOM_UUID" is not set in env variables.');
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
return new PrezlyApi(PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID);
|
|
24
|
-
}
|
|
23
|
+
return new PrezlyApi(PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID, PREZLY_THEME_UUID);
|
|
24
|
+
}
|
|
@@ -2,10 +2,16 @@ import type { NewsroomLanguageSettings, Story } from '@prezly/sdk';
|
|
|
2
2
|
|
|
3
3
|
import { DUMMY_DEFAULT_LOCALE, LocaleObject } from '../../intl';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @returns Language set as default in the Newsroom Settings.
|
|
7
|
+
*/
|
|
5
8
|
export function getDefaultLanguage(languages: NewsroomLanguageSettings[]) {
|
|
6
9
|
return languages.find(({ is_default }) => is_default) || languages[0];
|
|
7
10
|
}
|
|
8
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @returns Only languages having at least one published story
|
|
14
|
+
*/
|
|
9
15
|
export function getUsedLanguages(languages: NewsroomLanguageSettings[]) {
|
|
10
16
|
return languages.filter((language) => language.public_stories_count > 0);
|
|
11
17
|
}
|
|
@@ -110,6 +116,9 @@ export function getLanguageFromStory(languages: NewsroomLanguageSettings[], stor
|
|
|
110
116
|
return languages.find(({ code }) => code === storyLocaleCode);
|
|
111
117
|
}
|
|
112
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Extracts company information for the selected locale
|
|
121
|
+
*/
|
|
113
122
|
export function getCompanyInformation(languages: NewsroomLanguageSettings[], locale: LocaleObject) {
|
|
114
123
|
const currentLanguage =
|
|
115
124
|
getLanguageByExactLocaleCode(languages, locale) || getDefaultLanguage(languages);
|
|
@@ -8,15 +8,17 @@ const publishedAndPublic = [
|
|
|
8
8
|
{ visibility: { $in: ['public'] } },
|
|
9
9
|
];
|
|
10
10
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export function getSlugQuery(newsroomUuid: string, slug: string) {
|
|
12
|
+
return {
|
|
13
|
+
$and: [
|
|
14
|
+
{ 'newsroom.uuid': { $in: [newsroomUuid] } },
|
|
15
|
+
{ slug: { $eq: slug } },
|
|
16
|
+
...publishedAndAccessible,
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getStoriesQuery(newsroomUuid: string, categoryId?: number, localeCode?: string) {
|
|
20
22
|
const query: any = {
|
|
21
23
|
$and: [...publishedAndPublic, { 'newsroom.uuid': { $in: [newsroomUuid] } }],
|
|
22
24
|
};
|
|
@@ -30,15 +32,20 @@ export const getStoriesQuery = (newsroomUuid: string, categoryId?: number, local
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
return query;
|
|
33
|
-
}
|
|
35
|
+
}
|
|
34
36
|
|
|
35
|
-
export
|
|
36
|
-
order === 'desc' ? '-published_at' : 'published_at';
|
|
37
|
+
export function getSortByPublishedDate(order: 'asc' | 'desc') {
|
|
38
|
+
return order === 'desc' ? '-published_at' : 'published_at';
|
|
39
|
+
}
|
|
37
40
|
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
+
export function getGalleriesQuery() {
|
|
42
|
+
return {
|
|
43
|
+
$and: [{ status: { $eq: 'public' } }, { images_number: { $gt: 0 } }],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
41
46
|
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
}
|
|
47
|
+
export function getContactsQuery() {
|
|
48
|
+
return {
|
|
49
|
+
$and: [{ is_featured: { $eq: true } }],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { ASSETS_URL } from './constants';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @param uuid UUID of the assets hosted on Uploadcare
|
|
5
|
+
* @returns Full asset URL that can be used as a source for <img>, <video> and <a> tags.
|
|
6
|
+
*/
|
|
3
7
|
export function getAssetsUrl(uuid: string) {
|
|
4
8
|
return `${ASSETS_URL}/${uuid}/`;
|
|
5
9
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Category } from '@prezly/sdk';
|
|
2
2
|
|
|
3
|
-
import { LocaleObject } from '../intl';
|
|
4
|
-
import { AlgoliaCategoryRef } from '../types';
|
|
3
|
+
import type { LocaleObject } from '../intl';
|
|
4
|
+
import type { AlgoliaCategoryRef } from '../types';
|
|
5
5
|
|
|
6
6
|
function isAlgoliaCategory(
|
|
7
7
|
category: Category | AlgoliaCategoryRef,
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { ASSETS_URL } from './constants';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* This method constructs a URL to download all images in the Gallery as a single archive.
|
|
5
|
+
*
|
|
6
|
+
* @param uuid Gallery UUID
|
|
7
|
+
* @param title Filename for the saved archive. Usually it is the Gallery title.
|
|
8
|
+
* @returns URL to an archive containing all of the images in the Gallery.
|
|
9
|
+
*/
|
|
3
10
|
export function getUploadcareGroupUrl(uuid: string, title: string) {
|
|
4
11
|
return `${ASSETS_URL}/${uuid}/archive/zip/${encodeURI(title)}.zip`;
|
|
5
12
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'http';
|
|
2
2
|
|
|
3
|
-
import { AlgoliaSettings } from '../types';
|
|
3
|
+
import type { AlgoliaSettings } from '../types';
|
|
4
4
|
|
|
5
5
|
import { getEnvVariables } from './getEnvVariables';
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
if (
|
|
7
|
+
export function getAlgoliaSettings(req?: IncomingMessage): AlgoliaSettings {
|
|
8
|
+
if (typeof window !== 'undefined') {
|
|
9
9
|
throw new Error('"getAlgoliaSettings" should only be used on back-end side.');
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -26,4 +26,4 @@ export const getAlgoliaSettings = (req?: IncomingMessage): AlgoliaSettings => {
|
|
|
26
26
|
ALGOLIA_APP_ID,
|
|
27
27
|
ALGOLIA_INDEX,
|
|
28
28
|
};
|
|
29
|
-
}
|
|
29
|
+
}
|
|
@@ -3,7 +3,7 @@ import parseDataUrl from 'parse-data-url';
|
|
|
3
3
|
|
|
4
4
|
import type { PrezlyEnv } from '../types';
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function decodeJson(json: string): Record<string, any> {
|
|
7
7
|
try {
|
|
8
8
|
const decoded = JSON.parse(json);
|
|
9
9
|
if (decoded && typeof decoded === 'object') {
|
|
@@ -13,9 +13,9 @@ const decodeJson = (json: string): Record<string, any> => {
|
|
|
13
13
|
// passthru
|
|
14
14
|
}
|
|
15
15
|
return {};
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
function decodeHttpEnv(header: string): Record<string, any> {
|
|
19
19
|
if (header.startsWith('data:')) {
|
|
20
20
|
const parsed = parseDataUrl(header);
|
|
21
21
|
if (parsed && parsed.contentType === 'application/json') {
|
|
@@ -25,10 +25,10 @@ const decodeHttpEnv = (header: string): Record<string, any> => {
|
|
|
25
25
|
return {}; // unsupported data-uri
|
|
26
26
|
}
|
|
27
27
|
return decodeJson(header);
|
|
28
|
-
}
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
export
|
|
31
|
-
if (
|
|
30
|
+
export function getEnvVariables(req?: IncomingMessage): PrezlyEnv {
|
|
31
|
+
if (typeof window !== 'undefined') {
|
|
32
32
|
throw new Error('"getEnvVariables" should only be used on back-end side.');
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -41,4 +41,4 @@ export const getEnvVariables = (req?: IncomingMessage): PrezlyEnv => {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
return { ...process.env };
|
|
44
|
-
}
|
|
44
|
+
}
|
package/src/intl/lang.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CultureRef, NewsroomLanguageSettings } from '@prezly/sdk';
|
|
1
|
+
import type { CultureRef, NewsroomLanguageSettings } from '@prezly/sdk';
|
|
2
2
|
|
|
3
3
|
function isOnlyCulture(culture: CultureRef, languages: NewsroomLanguageSettings[]): boolean {
|
|
4
4
|
const numberOfLanguages = languages.filter(
|
|
@@ -9,14 +9,16 @@ function isOnlyCulture(culture: CultureRef, languages: NewsroomLanguageSettings[
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
+
* @returns the display name of the locale in its native language
|
|
13
|
+
*
|
|
12
14
|
* If there's only one culture used in a specific language,
|
|
13
15
|
* we strip the culture name completely.
|
|
14
16
|
*
|
|
15
17
|
* Examples:
|
|
16
18
|
* - English (Global), Spanish (Spain)
|
|
17
|
-
*
|
|
19
|
+
* - -> English, Spanish
|
|
18
20
|
* - English (Global), English (UK), Spanish (Spain)
|
|
19
|
-
*
|
|
21
|
+
* - -> English (Global), English (UK), Spanish
|
|
20
22
|
*/
|
|
21
23
|
export function getLanguageDisplayName(
|
|
22
24
|
language: NewsroomLanguageSettings,
|
package/src/intl/locale.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import type { Redirect } from 'next';
|
|
2
|
-
import { ParsedUrlQuery
|
|
2
|
+
import type { ParsedUrlQuery } from 'querystring';
|
|
3
|
+
import { stringify } from 'querystring';
|
|
3
4
|
|
|
4
5
|
import { LocaleObject } from './localeObject';
|
|
5
6
|
|
|
6
7
|
export const DEFAULT_LOCALE = 'en';
|
|
8
|
+
// We use pseudo locale used for localization testing, to reliably determine if we need to fallback to the default newsroom language
|
|
7
9
|
export const DUMMY_DEFAULT_LOCALE = 'qps-ploc';
|
|
8
10
|
|
|
11
|
+
/**
|
|
12
|
+
* This a list of locales that Prezly has translations for. Each code represents a language file in the `@prezly/themes-intl-messages` package.
|
|
13
|
+
* See https://github.com/prezly/themes-intl-messages for more info.
|
|
14
|
+
*/
|
|
9
15
|
const SUPPORTED_LOCALES = [
|
|
10
16
|
'af',
|
|
11
17
|
'ar',
|
|
@@ -55,6 +61,10 @@ const SUPPORTED_LOCALES = [
|
|
|
55
61
|
'zh-TW',
|
|
56
62
|
];
|
|
57
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Use this function to determine which translations file you should load from `@prezly/themes-intl-mesages` package.
|
|
66
|
+
* See https://github.com/prezly/theme-nextjs-bea/blob/main/utils/lang.ts for a usage example.
|
|
67
|
+
*/
|
|
58
68
|
export function getSupportedLocaleIsoCode(locale: LocaleObject): string {
|
|
59
69
|
const localeIsoCode = locale.toHyphenCode();
|
|
60
70
|
|
package/src/intl/localeConfig.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This list is pulled from the main Prezly application. The underscores are replaced with dashes.
|
|
3
|
+
* Together with permutation performed lower in the code,
|
|
4
|
+
* the exported list represents all of the possible locale codes that Next.js theme application might accept.
|
|
5
|
+
*/
|
|
2
6
|
const supportedLocales = [
|
|
3
7
|
'af-ZA',
|
|
4
8
|
'ar',
|
|
@@ -33,6 +37,7 @@ const supportedLocales = [
|
|
|
33
37
|
'en-IE',
|
|
34
38
|
'en-IL',
|
|
35
39
|
'en-IN',
|
|
40
|
+
'en-JM',
|
|
36
41
|
'en-JP',
|
|
37
42
|
'en-KH',
|
|
38
43
|
'en-KR',
|
|
@@ -104,7 +109,7 @@ const supportedLocales = [
|
|
|
104
109
|
|
|
105
110
|
const lowercaseLocales = supportedLocales.map((l) => l.toLowerCase());
|
|
106
111
|
|
|
107
|
-
// Get all permutations for the shorter locale codes supported by Prezly (e.g. short region codes)
|
|
112
|
+
// Get all permutations for the shorter locale codes supported by Prezly (e.g. short region codes and neutral language codes)
|
|
108
113
|
const lowercaseLocalPermutations = Array.from(
|
|
109
114
|
new Set([
|
|
110
115
|
...lowercaseLocales,
|
package/src/intl/localeObject.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import localeConfig from './localeConfig';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* This class is created to simplify working with locales and transforming between different locale code formats.
|
|
5
|
+
* Prezly API uses underscore-codes, while most frontend application tend to use hyphen-codes or slugs.
|
|
6
|
+
* Note that `fromAnyCode` constructor only accepts locales supported by Prezly backend. See `localeConfig` for reference.
|
|
7
|
+
*/
|
|
3
8
|
export class LocaleObject {
|
|
4
9
|
/**
|
|
5
10
|
* Hyphen-separated locale code.
|
|
@@ -39,24 +44,39 @@ export class LocaleObject {
|
|
|
39
44
|
return new LocaleObject(foundCode);
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
/**
|
|
48
|
+
* @returns Code formatted into hyphen format: `nl-BE`, `en-US`, `fr`, `fil`
|
|
49
|
+
*/
|
|
42
50
|
public toHyphenCode(): string {
|
|
43
51
|
return this.localeCode;
|
|
44
52
|
}
|
|
45
53
|
|
|
54
|
+
/**
|
|
55
|
+
* @returns Code formatted into underscore format, as expected by Prezly API: `nl_BE`, `en_US`, `fr`, `fil`
|
|
56
|
+
*/
|
|
46
57
|
public toUnderscoreCode(): string {
|
|
47
58
|
return this.localeCode.replace('-', '_');
|
|
48
59
|
}
|
|
49
60
|
|
|
61
|
+
/**
|
|
62
|
+
* @returns Code formatted to a URL-friendly lowercase slug format: `nl-be`, `en-us`, `fr`, `fil`
|
|
63
|
+
*/
|
|
50
64
|
public toUrlSlug(): string {
|
|
51
65
|
return this.localeCode.toLowerCase();
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
/**
|
|
69
|
+
* @returns Only the language part of the code without culture/region part: `nl`, `en`, `fr`
|
|
70
|
+
*/
|
|
54
71
|
public toNeutralLanguageCode(): string {
|
|
55
72
|
const [language] = this.localeCode.split('-');
|
|
56
73
|
|
|
57
74
|
return language;
|
|
58
75
|
}
|
|
59
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @returns Only the culture/regtion part without the language part: `be`, `us`, `fr`
|
|
79
|
+
*/
|
|
60
80
|
public toRegionCode(): string {
|
|
61
81
|
const [language, region] = this.localeCode.split('-');
|
|
62
82
|
|
|
@@ -67,6 +87,9 @@ export class LocaleObject {
|
|
|
67
87
|
return region.toUpperCase();
|
|
68
88
|
}
|
|
69
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Check if two LocaleObject instances are representing the same locale code.
|
|
92
|
+
*/
|
|
70
93
|
public isEqual(anotherLocale: LocaleObject) {
|
|
71
94
|
return anotherLocale.toHyphenCode() === this.localeCode;
|
|
72
95
|
}
|