@prezly/theme-kit-nextjs 1.0.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 (164) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +28 -0
  3. package/build/adapter-nextjs/getBasePageProps.d.ts +9 -0
  4. package/build/adapter-nextjs/getBasePageProps.js +11 -0
  5. package/build/adapter-nextjs/getBasePageProps.js.map +1 -0
  6. package/build/adapter-nextjs/index.d.ts +2 -0
  7. package/build/adapter-nextjs/index.js +3 -0
  8. package/build/adapter-nextjs/index.js.map +1 -0
  9. package/build/adapter-nextjs/processRequest.d.ts +3 -0
  10. package/build/adapter-nextjs/processRequest.js +20 -0
  11. package/build/adapter-nextjs/processRequest.js.map +1 -0
  12. package/build/data-fetching/api/PrezlyApi.d.ts +43 -0
  13. package/build/data-fetching/api/PrezlyApi.js +139 -0
  14. package/build/data-fetching/api/PrezlyApi.js.map +1 -0
  15. package/build/data-fetching/api/getPrezlyApi.d.ts +4 -0
  16. package/build/data-fetching/api/getPrezlyApi.js +17 -0
  17. package/build/data-fetching/api/getPrezlyApi.js.map +1 -0
  18. package/build/data-fetching/api/index.d.ts +2 -0
  19. package/build/data-fetching/api/index.js +3 -0
  20. package/build/data-fetching/api/index.js.map +1 -0
  21. package/build/data-fetching/api/languages.d.ts +18 -0
  22. package/build/data-fetching/api/languages.js +117 -0
  23. package/build/data-fetching/api/languages.js.map +1 -0
  24. package/build/data-fetching/api/queries.d.ts +45 -0
  25. package/build/data-fetching/api/queries.js +35 -0
  26. package/build/data-fetching/api/queries.js.map +1 -0
  27. package/build/data-fetching/constants.d.ts +2 -0
  28. package/build/data-fetching/constants.js +3 -0
  29. package/build/data-fetching/constants.js.map +1 -0
  30. package/build/data-fetching/getAssetsUrl.d.ts +1 -0
  31. package/build/data-fetching/getAssetsUrl.js +5 -0
  32. package/build/data-fetching/getAssetsUrl.js.map +1 -0
  33. package/build/data-fetching/getLocalizedCategoryData.d.ts +10 -0
  34. package/build/data-fetching/getLocalizedCategoryData.js +31 -0
  35. package/build/data-fetching/getLocalizedCategoryData.js.map +1 -0
  36. package/build/data-fetching/getNewsroomLogoUrl.d.ts +3 -0
  37. package/build/data-fetching/getNewsroomLogoUrl.js +14 -0
  38. package/build/data-fetching/getNewsroomLogoUrl.js.map +1 -0
  39. package/build/data-fetching/getPrivacyPortalUrl.d.ts +8 -0
  40. package/build/data-fetching/getPrivacyPortalUrl.js +9 -0
  41. package/build/data-fetching/getPrivacyPortalUrl.js.map +1 -0
  42. package/build/data-fetching/getStoryPublicationDate.d.ts +3 -0
  43. package/build/data-fetching/getStoryPublicationDate.jsx +10 -0
  44. package/build/data-fetching/getStoryPublicationDate.jsx.map +1 -0
  45. package/build/data-fetching/getUploadcareGroupUrl.d.ts +1 -0
  46. package/build/data-fetching/getUploadcareGroupUrl.js +5 -0
  47. package/build/data-fetching/getUploadcareGroupUrl.js.map +1 -0
  48. package/build/data-fetching/index.d.ts +9 -0
  49. package/build/data-fetching/index.js +10 -0
  50. package/build/data-fetching/index.js.map +1 -0
  51. package/build/data-fetching/lib/getAlgoliaSettings.d.ts +4 -0
  52. package/build/data-fetching/lib/getAlgoliaSettings.js +18 -0
  53. package/build/data-fetching/lib/getAlgoliaSettings.js.map +1 -0
  54. package/build/data-fetching/lib/getEnvVariables.d.ts +4 -0
  55. package/build/data-fetching/lib/getEnvVariables.js +37 -0
  56. package/build/data-fetching/lib/getEnvVariables.js.map +1 -0
  57. package/build/data-fetching/types.d.ts +11 -0
  58. package/build/data-fetching/types.js +2 -0
  59. package/build/data-fetching/types.js.map +1 -0
  60. package/build/index.d.ts +5 -0
  61. package/build/index.js +6 -0
  62. package/build/index.js.map +1 -0
  63. package/build/intl/index.d.ts +4 -0
  64. package/build/intl/index.js +5 -0
  65. package/build/intl/index.js.map +1 -0
  66. package/build/intl/lang.d.ts +12 -0
  67. package/build/intl/lang.js +25 -0
  68. package/build/intl/lang.js.map +1 -0
  69. package/build/intl/locale.d.ts +10 -0
  70. package/build/intl/locale.js +93 -0
  71. package/build/intl/locale.js.map +1 -0
  72. package/build/intl/localeConfig.d.ts +2 -0
  73. package/build/intl/localeConfig.js +113 -0
  74. package/build/intl/localeConfig.js.map +1 -0
  75. package/build/intl/localeObject.d.ts +22 -0
  76. package/build/intl/localeObject.js +59 -0
  77. package/build/intl/localeObject.js.map +1 -0
  78. package/build/newsroom-context/context.d.ts +26 -0
  79. package/build/newsroom-context/context.jsx +22 -0
  80. package/build/newsroom-context/context.jsx.map +1 -0
  81. package/build/newsroom-context/hooks/index.d.ts +11 -0
  82. package/build/newsroom-context/hooks/index.js +12 -0
  83. package/build/newsroom-context/hooks/index.js.map +1 -0
  84. package/build/newsroom-context/hooks/useAlgoliaSettings.d.ts +1 -0
  85. package/build/newsroom-context/hooks/useAlgoliaSettings.js +6 -0
  86. package/build/newsroom-context/hooks/useAlgoliaSettings.js.map +1 -0
  87. package/build/newsroom-context/hooks/useCategories.d.ts +1 -0
  88. package/build/newsroom-context/hooks/useCategories.js +6 -0
  89. package/build/newsroom-context/hooks/useCategories.js.map +1 -0
  90. package/build/newsroom-context/hooks/useCompanyInformation.d.ts +1 -0
  91. package/build/newsroom-context/hooks/useCompanyInformation.js +6 -0
  92. package/build/newsroom-context/hooks/useCompanyInformation.js.map +1 -0
  93. package/build/newsroom-context/hooks/useCurrentLocale.d.ts +1 -0
  94. package/build/newsroom-context/hooks/useCurrentLocale.js +6 -0
  95. package/build/newsroom-context/hooks/useCurrentLocale.js.map +1 -0
  96. package/build/newsroom-context/hooks/useGetLinkLocaleSlug.d.ts +2 -0
  97. package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js +21 -0
  98. package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js.map +1 -0
  99. package/build/newsroom-context/hooks/useGetTranslationUrl.d.ts +2 -0
  100. package/build/newsroom-context/hooks/useGetTranslationUrl.js +36 -0
  101. package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -0
  102. package/build/newsroom-context/hooks/useLanguages.d.ts +1 -0
  103. package/build/newsroom-context/hooks/useLanguages.js +6 -0
  104. package/build/newsroom-context/hooks/useLanguages.js.map +1 -0
  105. package/build/newsroom-context/hooks/useNewsroom.d.ts +1 -0
  106. package/build/newsroom-context/hooks/useNewsroom.js +6 -0
  107. package/build/newsroom-context/hooks/useNewsroom.js.map +1 -0
  108. package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -0
  109. package/build/newsroom-context/hooks/useNewsroomContext.js +12 -0
  110. package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -0
  111. package/build/newsroom-context/hooks/useSelectedCategory.d.ts +1 -0
  112. package/build/newsroom-context/hooks/useSelectedCategory.js +6 -0
  113. package/build/newsroom-context/hooks/useSelectedCategory.js.map +1 -0
  114. package/build/newsroom-context/hooks/useSelectedStory.d.ts +1 -0
  115. package/build/newsroom-context/hooks/useSelectedStory.js +6 -0
  116. package/build/newsroom-context/hooks/useSelectedStory.js.map +1 -0
  117. package/build/newsroom-context/index.d.ts +2 -0
  118. package/build/newsroom-context/index.js +3 -0
  119. package/build/newsroom-context/index.js.map +1 -0
  120. package/build/types.d.ts +38 -0
  121. package/build/types.js +2 -0
  122. package/build/types.js.map +1 -0
  123. package/package.json +60 -0
  124. package/src/@types/global.d.ts +7 -0
  125. package/src/adapter-nextjs/getBasePageProps.ts +23 -0
  126. package/src/adapter-nextjs/index.ts +2 -0
  127. package/src/adapter-nextjs/processRequest.ts +31 -0
  128. package/src/data-fetching/api/PrezlyApi.ts +240 -0
  129. package/src/data-fetching/api/getPrezlyApi.ts +24 -0
  130. package/src/data-fetching/api/index.ts +2 -0
  131. package/src/data-fetching/api/languages.ts +172 -0
  132. package/src/data-fetching/api/queries.ts +44 -0
  133. package/src/data-fetching/constants.ts +2 -0
  134. package/src/data-fetching/getAssetsUrl.ts +5 -0
  135. package/src/data-fetching/getLocalizedCategoryData.ts +47 -0
  136. package/src/data-fetching/getNewsroomLogoUrl.ts +21 -0
  137. package/src/data-fetching/getPrivacyPortalUrl.ts +22 -0
  138. package/src/data-fetching/getStoryPublicationDate.tsx +15 -0
  139. package/src/data-fetching/getUploadcareGroupUrl.ts +5 -0
  140. package/src/data-fetching/index.ts +9 -0
  141. package/src/data-fetching/lib/getAlgoliaSettings.ts +29 -0
  142. package/src/data-fetching/lib/getEnvVariables.ts +44 -0
  143. package/src/data-fetching/types.ts +12 -0
  144. package/src/index.ts +5 -0
  145. package/src/intl/index.ts +4 -0
  146. package/src/intl/lang.ts +36 -0
  147. package/src/intl/locale.ts +118 -0
  148. package/src/intl/localeConfig.js +116 -0
  149. package/src/intl/localeObject.ts +73 -0
  150. package/src/newsroom-context/context.tsx +73 -0
  151. package/src/newsroom-context/hooks/index.ts +11 -0
  152. package/src/newsroom-context/hooks/useAlgoliaSettings.ts +7 -0
  153. package/src/newsroom-context/hooks/useCategories.ts +7 -0
  154. package/src/newsroom-context/hooks/useCompanyInformation.ts +7 -0
  155. package/src/newsroom-context/hooks/useCurrentLocale.ts +7 -0
  156. package/src/newsroom-context/hooks/useGetLinkLocaleSlug.ts +33 -0
  157. package/src/newsroom-context/hooks/useGetTranslationUrl.ts +61 -0
  158. package/src/newsroom-context/hooks/useLanguages.ts +7 -0
  159. package/src/newsroom-context/hooks/useNewsroom.ts +7 -0
  160. package/src/newsroom-context/hooks/useNewsroomContext.ts +14 -0
  161. package/src/newsroom-context/hooks/useSelectedCategory.ts +7 -0
  162. package/src/newsroom-context/hooks/useSelectedStory.ts +7 -0
  163. package/src/newsroom-context/index.ts +2 -0
  164. package/src/types.ts +56 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Prezly
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # NextJS Prezly Theme Kit
2
+
3
+ This library is a collection of data-fetching functions, hooks and utility functions to abstract some of the under-the-hood logic used by Prezly newsrooms and help developers create themes much faster with little boilerplate required.
4
+
5
+ ## Adding the library to your NextJS theme
6
+
7
+ ### npm
8
+
9
+ ```Shell
10
+ npm install --save @prezly/theme-kit-nextjs
11
+ ```
12
+
13
+ #### peerDependencies
14
+
15
+ This library is intended to be used with NextJS applications, so it requires `next`, `react` and `react-dom` to work. These should already be installed if you have an existing NextJS app.
16
+ If you're starting from scratch, use [`create-next-app`](https://nextjs.org/docs/api-reference/create-next-app) to quick-start the project.
17
+
18
+ To keep things fresh, we require at least NextJS 12 and React 17.
19
+
20
+ You can also install the dependencies manually
21
+ ```Shell
22
+ npm install --save next react react-dom
23
+ npm install --save-dev @types/react @types/react-dom
24
+ ```
25
+
26
+ ----
27
+
28
+ Brought to you by [Prezly](https://www.prezly.com/?utm_source=github&utm_campaign=@prezly/theme-kit-nextjs).
@@ -0,0 +1,9 @@
1
+ import type { GetServerSidePropsContext } from 'next';
2
+ interface BasePagePropsOptions {
3
+ loadHomepageContacts?: boolean;
4
+ }
5
+ export declare function getBasePageProps(context: GetServerSidePropsContext, options?: BasePagePropsOptions): Promise<{
6
+ api: import("../data-fetching/api/PrezlyApi").PrezlyApi;
7
+ basePageProps: import("..").BasePageProps;
8
+ }>;
9
+ export {};
@@ -0,0 +1,11 @@
1
+ import { getPrezlyApi } from '../data-fetching';
2
+ export async function getBasePageProps(context, options = {}) {
3
+ const { req: request, locale } = context;
4
+ const api = getPrezlyApi(request);
5
+ const basePageProps = await api.getBasePageProps(request, locale);
6
+ if (options.loadHomepageContacts) {
7
+ basePageProps.contacts = await api.getNewsroomContacts();
8
+ }
9
+ return { api, basePageProps };
10
+ }
11
+ //# sourceMappingURL=getBasePageProps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getBasePageProps.js","sourceRoot":"","sources":["../../src/adapter-nextjs/getBasePageProps.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAMhD,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAClC,OAAkC,EAClC,UAAgC,EAAE;IAElC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEzC,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAClC,MAAM,aAAa,GAAG,MAAM,GAAG,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAElE,IAAI,OAAO,CAAC,oBAAoB,EAAE;QAC9B,aAAa,CAAC,QAAQ,GAAG,MAAM,GAAG,CAAC,mBAAmB,EAAE,CAAC;KAC5D;IAED,OAAO,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC;AAClC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './getBasePageProps';
2
+ export * from './processRequest';
@@ -0,0 +1,3 @@
1
+ export * from './getBasePageProps';
2
+ export * from './processRequest';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapter-nextjs/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
2
+ import { BasePageProps } from '../types';
3
+ export declare function processRequest<Props extends BasePageProps>(context: GetServerSidePropsContext, basePageProps: BasePageProps, canonicalUrl?: string, customProps?: Omit<Props, keyof BasePageProps>): GetServerSidePropsResult<Props>;
@@ -0,0 +1,20 @@
1
+ import { getRedirectToCanonicalLocale } from '../intl';
2
+ export function processRequest(context, basePageProps, canonicalUrl, customProps) {
3
+ const { locale, query } = context;
4
+ if (!basePageProps.localeResolved) {
5
+ return { notFound: true };
6
+ }
7
+ if (canonicalUrl) {
8
+ const redirect = getRedirectToCanonicalLocale(basePageProps, locale, canonicalUrl, query);
9
+ if (redirect) {
10
+ return { redirect };
11
+ }
12
+ }
13
+ return {
14
+ props: {
15
+ ...basePageProps,
16
+ ...customProps,
17
+ },
18
+ };
19
+ }
20
+ //# sourceMappingURL=processRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processRequest.js","sourceRoot":"","sources":["../../src/adapter-nextjs/processRequest.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,4BAA4B,EAAE,MAAM,SAAS,CAAC;AAGvD,MAAM,UAAU,cAAc,CAC1B,OAAkC,EAClC,aAA4B,EAC5B,YAAqB,EACrB,WAA8C;IAE9C,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAElC,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE;QAC/B,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;KAC7B;IAED,IAAI,YAAY,EAAE;QACd,MAAM,QAAQ,GAAG,4BAA4B,CAAC,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;QAC1F,IAAI,QAAQ,EAAE;YACV,OAAO,EAAE,QAAQ,EAAE,CAAC;SACvB;KACJ;IAED,OAAO;QACH,KAAK,EAAE;YACH,GAAG,aAAa;YAChB,GAAG,WAAW;SACR;KACb,CAAC;AACN,CAAC"}
@@ -0,0 +1,43 @@
1
+ /// <reference types="node" />
2
+ import PrezlySDK, { Category, ExtraStoryFields, Newsroom, NewsroomLanguageSettings, Story } from '@prezly/sdk';
3
+ import type { IncomingMessage } from 'http';
4
+ import { BasePageProps } from '../../types';
5
+ declare type SortOrder = 'desc' | 'asc';
6
+ interface GetStoriesOptions {
7
+ page?: number;
8
+ pageSize?: number;
9
+ order?: SortOrder;
10
+ include?: (keyof ExtraStoryFields)[];
11
+ localeCode?: string;
12
+ }
13
+ interface GetGalleriesOptions {
14
+ page?: number;
15
+ pageSize: number;
16
+ }
17
+ export declare class PrezlyApi {
18
+ private readonly sdk;
19
+ private readonly newsroomUuid;
20
+ constructor(accessToken: string, newsroomUuid: Newsroom['uuid']);
21
+ getStory(uuid: string): Promise<import("@prezly/sdk").ExtendedStory>;
22
+ getNewsroom(): Promise<Newsroom>;
23
+ getNewsroomContacts(): Promise<import("@prezly/sdk").NewsroomContact[]>;
24
+ getNewsroomLanguages(): Promise<NewsroomLanguageSettings[]>;
25
+ getAllStories(order?: SortOrder): Promise<Story[]>;
26
+ getStories({ page, pageSize, order, include, localeCode, }?: GetStoriesOptions): Promise<{
27
+ stories: Story[];
28
+ storiesTotal: number;
29
+ }>;
30
+ getStoriesFromCategory(category: Category, { page, pageSize, order, include, localeCode, }?: GetStoriesOptions): Promise<{
31
+ stories: Story[];
32
+ storiesTotal: number;
33
+ }>;
34
+ getStoryBySlug(slug: string): Promise<import("@prezly/sdk").ExtendedStory | null>;
35
+ getCategories(): Promise<Category[]>;
36
+ getCategoryBySlug(slug: string): Promise<Category | undefined>;
37
+ searchStories: typeof PrezlySDK.prototype.stories.search;
38
+ getGalleries({ page, pageSize }: GetGalleriesOptions): Promise<import("@prezly/sdk/dist/Sdk/NewsroomGalleries/types").NewsroomGalleriesListResponse>;
39
+ getGallery(uuid: string): Promise<import("@prezly/sdk").NewsroomGallery>;
40
+ getThemePreset(): Promise<import("@prezly/sdk").NewsroomThemePreset>;
41
+ getBasePageProps(request: IncomingMessage | undefined, nextLocaleIsoCode?: string, story?: Story): Promise<BasePageProps>;
42
+ }
43
+ export {};
@@ -0,0 +1,139 @@
1
+ import PrezlySDK from '@prezly/sdk';
2
+ import { LocaleObject } from '../../intl';
3
+ import { DEFAULT_PAGE_SIZE } from '../constants';
4
+ import { getAlgoliaSettings } from '../lib/getAlgoliaSettings';
5
+ import { getCompanyInformation, getDefaultLanguage, getLanguageFromNextLocaleIsoCode, getLanguageFromStory, getShortestLocaleCode, } from './languages';
6
+ import { getContactsQuery, getGalleriesQuery, getSlugQuery, getSortByPublishedDate, getStoriesQuery, } from './queries';
7
+ const DEFAULT_SORT_ORDER = 'desc';
8
+ export class PrezlyApi {
9
+ sdk;
10
+ newsroomUuid;
11
+ constructor(accessToken, newsroomUuid) {
12
+ const baseUrl = process.env.API_BASE_URL_OVERRIDE ?? undefined;
13
+ this.sdk = new PrezlySDK({
14
+ accessToken,
15
+ baseUrl,
16
+ headers: { 'X-Convert-v1-To-v3': 'true' },
17
+ });
18
+ this.newsroomUuid = newsroomUuid;
19
+ }
20
+ getStory(uuid) {
21
+ return this.sdk.stories.get(uuid);
22
+ }
23
+ async getNewsroom() {
24
+ return this.sdk.newsrooms.get(this.newsroomUuid);
25
+ }
26
+ async getNewsroomContacts() {
27
+ return this.sdk.newsroomContacts.search(this.newsroomUuid, {
28
+ query: JSON.stringify(getContactsQuery()),
29
+ });
30
+ }
31
+ async getNewsroomLanguages() {
32
+ return (await this.sdk.newsroomLanguages.list(this.newsroomUuid)).languages;
33
+ }
34
+ async getAllStories(order = DEFAULT_SORT_ORDER) {
35
+ const sortOrder = getSortByPublishedDate(order);
36
+ const newsroom = await this.getNewsroom();
37
+ const jsonQuery = JSON.stringify(getStoriesQuery(newsroom.uuid));
38
+ const maxStories = newsroom.stories_number;
39
+ const chunkSize = 200;
40
+ const pages = Math.ceil(maxStories / chunkSize);
41
+ const storiesPromises = Array.from({ length: pages }, (_, pageIndex) => this.searchStories({
42
+ limit: chunkSize,
43
+ sortOrder,
44
+ jsonQuery,
45
+ offset: pageIndex * chunkSize,
46
+ }));
47
+ const stories = (await Promise.all(storiesPromises)).flatMap((response) => response.stories);
48
+ return stories;
49
+ }
50
+ async getStories({ page = undefined, pageSize = DEFAULT_PAGE_SIZE, order = DEFAULT_SORT_ORDER, include, localeCode, } = {}) {
51
+ const sortOrder = getSortByPublishedDate(order);
52
+ const jsonQuery = JSON.stringify(getStoriesQuery(this.newsroomUuid, undefined, localeCode));
53
+ const { stories, pagination } = await this.searchStories({
54
+ limit: pageSize,
55
+ offset: typeof page === 'undefined' ? undefined : (page - 1) * pageSize,
56
+ sortOrder,
57
+ jsonQuery,
58
+ include,
59
+ });
60
+ const storiesTotal = pagination.matched_records_number;
61
+ return { stories, storiesTotal };
62
+ }
63
+ async getStoriesFromCategory(category, { page = undefined, pageSize = DEFAULT_PAGE_SIZE, order = DEFAULT_SORT_ORDER, include, localeCode, } = {}) {
64
+ const sortOrder = getSortByPublishedDate(order);
65
+ const jsonQuery = JSON.stringify(getStoriesQuery(this.newsroomUuid, category.id, localeCode));
66
+ const { stories, pagination } = await this.searchStories({
67
+ limit: pageSize,
68
+ offset: typeof page === 'undefined' ? undefined : (page - 1) * pageSize,
69
+ sortOrder,
70
+ jsonQuery,
71
+ include,
72
+ });
73
+ const storiesTotal = pagination.matched_records_number;
74
+ return { stories, storiesTotal };
75
+ }
76
+ async getStoryBySlug(slug) {
77
+ const jsonQuery = JSON.stringify(getSlugQuery(this.newsroomUuid, slug));
78
+ const { stories } = await this.searchStories({
79
+ limit: 1,
80
+ jsonQuery,
81
+ });
82
+ if (stories[0]) {
83
+ return this.getStory(stories[0].uuid);
84
+ }
85
+ return null;
86
+ }
87
+ async getCategories() {
88
+ const categories = await this.sdk.newsroomCategories.list(this.newsroomUuid);
89
+ return Array.isArray(categories) ? categories : Object.values(categories);
90
+ }
91
+ async getCategoryBySlug(slug) {
92
+ const categories = await this.getCategories();
93
+ return categories.find((category) => Object.values(category.i18n).some((t) => t.slug === slug));
94
+ }
95
+ searchStories = (options) => this.sdk.stories.search(options);
96
+ async getGalleries({ page = undefined, pageSize }) {
97
+ return this.sdk.newsroomGalleries.list(this.newsroomUuid, {
98
+ limit: pageSize,
99
+ offset: typeof page === 'undefined' ? undefined : (page - 1) * pageSize,
100
+ scope: getGalleriesQuery(),
101
+ });
102
+ }
103
+ async getGallery(uuid) {
104
+ return this.sdk.newsroomGalleries.get(this.newsroomUuid, uuid);
105
+ }
106
+ async getThemePreset() {
107
+ return this.sdk.newsroomThemes.getActive(this.newsroomUuid);
108
+ }
109
+ async getBasePageProps(request, nextLocaleIsoCode, story) {
110
+ const [newsroom, languages, categories, themePreset] = await Promise.all([
111
+ this.getNewsroom(),
112
+ this.getNewsroomLanguages(),
113
+ this.getCategories(),
114
+ this.getThemePreset(),
115
+ ]);
116
+ const currentLanguage = story
117
+ ? getLanguageFromStory(languages, story)
118
+ : getLanguageFromNextLocaleIsoCode(languages, nextLocaleIsoCode);
119
+ const defaultLanguage = getDefaultLanguage(languages);
120
+ const { code: localeCode } = currentLanguage || defaultLanguage;
121
+ const locale = LocaleObject.fromAnyCode(localeCode);
122
+ const shortestLocaleCode = getShortestLocaleCode(languages, locale);
123
+ // TODO: if no information given for current language, show boilerplate from default language
124
+ const companyInformation = getCompanyInformation(languages, locale);
125
+ const algoliaSettings = getAlgoliaSettings(request);
126
+ return {
127
+ newsroom,
128
+ companyInformation,
129
+ categories,
130
+ languages,
131
+ localeCode,
132
+ shortestLocaleCode,
133
+ localeResolved: Boolean(currentLanguage),
134
+ themePreset,
135
+ algoliaSettings,
136
+ };
137
+ }
138
+ }
139
+ //# sourceMappingURL=PrezlyApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PrezlyApi.js","sourceRoot":"","sources":["../../../src/data-fetching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CAAC;QAChE,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACpD,MAAM,kBAAkB,GAAG,qBAAqB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpE,6FAA6F;QAC7F,MAAM,kBAAkB,GAAG,qBAAqB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpE,MAAM,eAAe,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAEpD,OAAO;YACH,QAAQ;YACR,kBAAkB;YAClB,UAAU;YACV,SAAS;YACT,UAAU;YACV,kBAAkB;YAClB,cAAc,EAAE,OAAO,CAAC,eAAe,CAAC;YACxC,WAAW;YACX,eAAe;SAClB,CAAC;IACN,CAAC;CACJ"}
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage } from 'http';
3
+ import { PrezlyApi } from './PrezlyApi';
4
+ export declare const getPrezlyApi: (req?: IncomingMessage | undefined) => PrezlyApi;
@@ -0,0 +1,17 @@
1
+ import { getEnvVariables } from '../lib/getEnvVariables';
2
+ import { PrezlyApi } from './PrezlyApi';
3
+ export const getPrezlyApi = (req) => {
4
+ if (process.browser) {
5
+ throw new Error('"getPrezlyApi" should only be used on back-end side.');
6
+ }
7
+ // `getEnvVariables` handles both cases for envs parsing - .env and request headers
8
+ const { PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID } = getEnvVariables(req);
9
+ if (!PREZLY_ACCESS_TOKEN) {
10
+ throw new Error('"PREZLY_ACCESS_TOKEN" is not set in env variables.');
11
+ }
12
+ if (!PREZLY_NEWSROOM_UUID) {
13
+ throw new Error('"PREZLY_NEWSROOM_UUID" is not set in env variables.');
14
+ }
15
+ return new PrezlyApi(PREZLY_ACCESS_TOKEN, PREZLY_NEWSROOM_UUID);
16
+ };
17
+ //# sourceMappingURL=getPrezlyApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPrezlyApi.js","sourceRoot":"","sources":["../../../src/data-fetching/api/getPrezlyApi.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAqB,EAAa,EAAE;IAC7D,IAAI,OAAO,CAAC,OAAO,EAAE;QACjB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;KAC3E;IAED,mFAAmF;IACnF,MAAM,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;IAE3E,IAAI,CAAC,mBAAmB,EAAE;QACtB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;KACzE;IAED,IAAI,CAAC,oBAAoB,EAAE;QACvB,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;KAC1E;IAED,OAAO,IAAI,SAAS,CAAC,mBAAmB,EAAE,oBAAoB,CAAC,CAAC;AACpE,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './getPrezlyApi';
2
+ export * from './languages';
@@ -0,0 +1,3 @@
1
+ export * from './getPrezlyApi';
2
+ export * from './languages';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/data-fetching/api/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { NewsroomLanguageSettings, Story } from '@prezly/sdk';
2
+ import { LocaleObject } from '../../intl';
3
+ export declare function getDefaultLanguage(languages: NewsroomLanguageSettings[]): NewsroomLanguageSettings;
4
+ export declare function getUsedLanguages(languages: NewsroomLanguageSettings[]): NewsroomLanguageSettings[];
5
+ /**
6
+ * Retrieve a culture with fallback to legacy codes support.
7
+ * Pulled from https://github.com/prezly/prezly/blob/de9900d9890a33502780494aa3fb85c9a732b3c3/lib/model/CulturePeer.php#L91-L114
8
+ */
9
+ export declare function getLanguageFromNextLocaleIsoCode(languages: NewsroomLanguageSettings[], nextLocaleIsoCode?: string): NewsroomLanguageSettings | undefined;
10
+ export declare function getLanguageFromStory(languages: NewsroomLanguageSettings[], story: Story): NewsroomLanguageSettings | undefined;
11
+ export declare function getCompanyInformation(languages: NewsroomLanguageSettings[], locale: LocaleObject): import("@prezly/sdk").NewsroomCompanyInformation;
12
+ /**
13
+ * Get shortest locale code possible from full locale code
14
+ * First: try shorting to neutral language code (there should be no locales with the same language code)
15
+ * Then: try shorting to region code (there should be no locales with the same region code)
16
+ * Finally: return the original locale code (shorting is not possible)
17
+ */
18
+ export declare function getShortestLocaleCode(languages: NewsroomLanguageSettings[], locale: LocaleObject): string | false;
@@ -0,0 +1,117 @@
1
+ import { DUMMY_DEFAULT_LOCALE, LocaleObject } from '../../intl';
2
+ export function getDefaultLanguage(languages) {
3
+ return languages.find(({ is_default }) => is_default) || languages[0];
4
+ }
5
+ export function getUsedLanguages(languages) {
6
+ return languages.filter((language) => language.public_stories_count > 0);
7
+ }
8
+ function getLanguageByExactLocaleCode(languages, locale) {
9
+ const localeCode = locale.toUnderscoreCode();
10
+ return languages.find(({ code }) => code === localeCode);
11
+ }
12
+ // See https://github.com/prezly/prezly/blob/master/lib/model/CulturePeer.php#L123
13
+ function getLanguageByNeutralLocaleCode(languages, locale) {
14
+ const neutralLanguageCode = locale.toNeutralLanguageCode();
15
+ // Prefer default language
16
+ const defaultLanguage = getDefaultLanguage(languages);
17
+ if (defaultLanguage.code === locale.toUnderscoreCode()) {
18
+ return defaultLanguage;
19
+ }
20
+ // Try to look in used cultures first (giving priority to used ones)
21
+ const usedLanguages = getUsedLanguages(languages);
22
+ const usedLanguage = usedLanguages.find(({ code }) => LocaleObject.fromAnyCode(code).toNeutralLanguageCode() === neutralLanguageCode ||
23
+ code === neutralLanguageCode);
24
+ if (usedLanguage) {
25
+ return usedLanguage;
26
+ }
27
+ // Search in all languages
28
+ return languages.find(({ code }) => LocaleObject.fromAnyCode(code).toNeutralLanguageCode() === neutralLanguageCode ||
29
+ code === neutralLanguageCode);
30
+ }
31
+ // See https://github.com/prezly/prezly/blob/master/lib/model/CulturePeer.php#L159
32
+ function getLanguageByShortRegionCode(languages, locale) {
33
+ const shortRegionCode = locale.toRegionCode();
34
+ // Prefer default language
35
+ const defaultLanguage = getDefaultLanguage(languages);
36
+ if (LocaleObject.fromAnyCode(defaultLanguage.code).toRegionCode() === shortRegionCode) {
37
+ return defaultLanguage;
38
+ }
39
+ // Try to look in used cultures first (giving priority to used ones)
40
+ const usedLanguages = getUsedLanguages(languages);
41
+ const usedLanguage = usedLanguages.find(({ code }) => LocaleObject.fromAnyCode(code).toRegionCode() === shortRegionCode);
42
+ if (usedLanguage) {
43
+ return usedLanguage;
44
+ }
45
+ // Search in all languages
46
+ return languages.find(({ code }) => LocaleObject.fromAnyCode(code).toRegionCode() === shortRegionCode);
47
+ }
48
+ /**
49
+ * Retrieve a culture with fallback to legacy codes support.
50
+ * Pulled from https://github.com/prezly/prezly/blob/de9900d9890a33502780494aa3fb85c9a732b3c3/lib/model/CulturePeer.php#L91-L114
51
+ */
52
+ export function getLanguageFromNextLocaleIsoCode(languages, nextLocaleIsoCode) {
53
+ const defaultLanguage = getDefaultLanguage(languages);
54
+ if (!nextLocaleIsoCode || nextLocaleIsoCode === DUMMY_DEFAULT_LOCALE) {
55
+ return defaultLanguage;
56
+ }
57
+ const locale = LocaleObject.fromAnyCode(nextLocaleIsoCode);
58
+ let targetLanguage;
59
+ if (nextLocaleIsoCode.length === 2) {
60
+ targetLanguage =
61
+ getLanguageByExactLocaleCode(languages, locale) ||
62
+ getLanguageByNeutralLocaleCode(languages, locale) ||
63
+ getLanguageByShortRegionCode(languages, locale);
64
+ if (targetLanguage) {
65
+ return targetLanguage;
66
+ }
67
+ }
68
+ else {
69
+ targetLanguage = getLanguageByExactLocaleCode(languages, locale);
70
+ }
71
+ return targetLanguage;
72
+ }
73
+ export function getLanguageFromStory(languages, story) {
74
+ const { code: storyLocaleCode } = story.culture;
75
+ return languages.find(({ code }) => code === storyLocaleCode);
76
+ }
77
+ export function getCompanyInformation(languages, locale) {
78
+ const currentLanguage = getLanguageByExactLocaleCode(languages, locale) || getDefaultLanguage(languages);
79
+ return currentLanguage.company_information;
80
+ }
81
+ /**
82
+ * Get shortest locale code possible from full locale code
83
+ * First: try shorting to neutral language code (there should be no locales with the same language code)
84
+ * Then: try shorting to region code (there should be no locales with the same region code)
85
+ * Finally: return the original locale code (shorting is not possible)
86
+ */
87
+ export function getShortestLocaleCode(languages, locale) {
88
+ const localeCode = locale.toUnderscoreCode();
89
+ const defaultLanguage = getDefaultLanguage(languages);
90
+ // If it's a default locale, return false (no locale needed in URL)
91
+ if (localeCode === defaultLanguage.code) {
92
+ return false;
93
+ }
94
+ // Try shorting to neutral language code
95
+ const neutralLanguageCode = locale.toNeutralLanguageCode();
96
+ // The code is already as short as possible
97
+ if (neutralLanguageCode === localeCode) {
98
+ return localeCode;
99
+ }
100
+ const matchingLanguagesByNeutralCode = languages.filter(({ code }) => LocaleObject.fromAnyCode(code).toNeutralLanguageCode() === neutralLanguageCode ||
101
+ code === neutralLanguageCode);
102
+ if (matchingLanguagesByNeutralCode.length === 1) {
103
+ return neutralLanguageCode;
104
+ }
105
+ // Try shorting to region code
106
+ const shortRegionCode = locale.toRegionCode();
107
+ const matchingLanguagesByRegionCode = languages.filter(({ code }) => LocaleObject.fromAnyCode(code).toRegionCode() === shortRegionCode);
108
+ // Prevent collision with neutral language codes
109
+ const mathchingNeutralLanguagesByRegionCode = languages.filter(({ code }) => LocaleObject.fromAnyCode(code).toNeutralLanguageCode() === shortRegionCode ||
110
+ code === shortRegionCode);
111
+ if (matchingLanguagesByRegionCode.length === 1 &&
112
+ !mathchingNeutralLanguagesByRegionCode.length) {
113
+ return shortRegionCode;
114
+ }
115
+ return localeCode;
116
+ }
117
+ //# sourceMappingURL=languages.js.map
@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AAK,eAAe,CAAC,IAAI,EAAE;QACrC,OAAO,KAAK,CAAC;KAChB;IAED,wCAAwC;IACxC,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,EAAE,CAAC;IAC3D,2CAA2C;IAC3C,IAAI,mBAAmB,KAAK,UAAU,EAAE;QACpC,OAAO,UAAU,CAAC;KACrB;IAED,MAAM,8BAA8B,GAAG,SAAS,CAAC,MAAM,CACnD,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CACT,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,qBAAqB,EAAE,KAAK,mBAAmB;QAC9E,IAAI,KAAK,mBAAmB,CACnC,CAAC;IACF,IAAI,8BAA8B,CAAC,MAAM,KAAK,CAAC,EAAE;QAC7C,OAAO,mBAAmB,CAAC;KAC9B;IAED,8BAA8B;IAC9B,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;IAC9C,MAAM,6BAA6B,GAAG,SAAS,CAAC,MAAM,CAClD,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,KAAK,eAAe,CAClF,CAAC;IACF,gDAAgD;IAChD,MAAM,qCAAqC,GAAG,SAAS,CAAC,MAAM,CAC1D,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CACT,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,qBAAqB,EAAE,KAAK,eAAe;QAC1E,IAAI,KAAK,eAAe,CAC/B,CAAC;IACF,IACI,6BAA6B,CAAC,MAAM,KAAK,CAAC;QAC1C,CAAC,qCAAqC,CAAC,MAAM,EAC/C;QACE,OAAO,eAAe,CAAC;KAC1B;IAED,OAAO,UAAU,CAAC;AACtB,CAAC"}
@@ -0,0 +1,45 @@
1
+ export declare const getSlugQuery: (newsroomUuid: string, slug: string) => {
2
+ $and: ({
3
+ lifecycle_status: {
4
+ $in: string[];
5
+ };
6
+ visibility?: undefined;
7
+ } | {
8
+ visibility: {
9
+ $in: string[];
10
+ };
11
+ lifecycle_status?: undefined;
12
+ } | {
13
+ 'newsroom.uuid': {
14
+ $in: string[];
15
+ };
16
+ slug?: undefined;
17
+ } | {
18
+ slug: {
19
+ $eq: string;
20
+ };
21
+ 'newsroom.uuid'?: undefined;
22
+ })[];
23
+ };
24
+ export declare const getStoriesQuery: (newsroomUuid: string, categoryId?: number | undefined, localeCode?: string | undefined) => any;
25
+ export declare const getSortByPublishedDate: (order: 'asc' | 'desc') => "-published_at" | "published_at";
26
+ export declare const getGalleriesQuery: () => {
27
+ $and: ({
28
+ status: {
29
+ $eq: string;
30
+ };
31
+ images_number?: undefined;
32
+ } | {
33
+ images_number: {
34
+ $gt: number;
35
+ };
36
+ status?: undefined;
37
+ })[];
38
+ };
39
+ export declare const getContactsQuery: () => {
40
+ $and: {
41
+ is_featured: {
42
+ $eq: boolean;
43
+ };
44
+ }[];
45
+ };
@@ -0,0 +1,35 @@
1
+ const publishedAndAccessible = [
2
+ { lifecycle_status: { $in: ['published'] } },
3
+ { visibility: { $in: ['public', 'private'] } },
4
+ ];
5
+ const publishedAndPublic = [
6
+ { lifecycle_status: { $in: ['published'] } },
7
+ { visibility: { $in: ['public'] } },
8
+ ];
9
+ export const getSlugQuery = (newsroomUuid, slug) => ({
10
+ $and: [
11
+ { 'newsroom.uuid': { $in: [newsroomUuid] } },
12
+ { slug: { $eq: slug } },
13
+ ...publishedAndAccessible,
14
+ ],
15
+ });
16
+ export const getStoriesQuery = (newsroomUuid, categoryId, localeCode) => {
17
+ const query = {
18
+ $and: [...publishedAndPublic, { 'newsroom.uuid': { $in: [newsroomUuid] } }],
19
+ };
20
+ if (categoryId) {
21
+ query.$and.push({ 'category.id': { $any: [categoryId] } });
22
+ }
23
+ if (localeCode) {
24
+ query.$and.push({ locale: { $in: [localeCode] } });
25
+ }
26
+ return query;
27
+ };
28
+ export const getSortByPublishedDate = (order) => order === 'desc' ? '-published_at' : 'published_at';
29
+ export const getGalleriesQuery = () => ({
30
+ $and: [{ status: { $eq: 'public' } }, { images_number: { $gt: 0 } }],
31
+ });
32
+ export const getContactsQuery = () => ({
33
+ $and: [{ is_featured: { $eq: true } }],
34
+ });
35
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../../src/data-fetching/api/queries.ts"],"names":[],"mappings":"AAAA,MAAM,sBAAsB,GAAG;IAC3B,EAAE,gBAAgB,EAAE,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE;IAC5C,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,EAAE,EAAE;CACjD,CAAC;AAEF,MAAM,kBAAkB,GAAG;IACvB,EAAE,gBAAgB,EAAE,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE;IAC5C,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE;CACtC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,YAAoB,EAAE,IAAY,EAAE,EAAE,CAAC,CAAC;IACjE,IAAI,EAAE;QACF,EAAE,eAAe,EAAE,EAAE,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE;QAC5C,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACvB,GAAG,sBAAsB;KAC5B;CACJ,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,YAAoB,EAAE,UAAmB,EAAE,UAAmB,EAAE,EAAE;IAC9F,MAAM,KAAK,GAAQ;QACf,IAAI,EAAE,CAAC,GAAG,kBAAkB,EAAE,EAAE,eAAe,EAAE,EAAE,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,EAAE,CAAC;KAC9E,CAAC;IAEF,IAAI,UAAU,EAAE;QACZ,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC;KAC9D;IAED,IAAI,UAAU,EAAE;QACZ,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC;KACtD;IAED,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,KAAqB,EAAE,EAAE,CAC5D,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,cAAc,CAAC;AAExD,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,EAAE,CAAC,CAAC;IACpC,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,EAAE,aAAa,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;CACvE,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,EAAE,CAAC,CAAC;IACnC,IAAI,EAAE,CAAC,EAAE,WAAW,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC;CACzC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const ASSETS_URL = "https://cdn.uc.assets.prezly.com";
2
+ export declare const DEFAULT_PAGE_SIZE = 12;
@@ -0,0 +1,3 @@
1
+ export const ASSETS_URL = 'https://cdn.uc.assets.prezly.com';
2
+ export const DEFAULT_PAGE_SIZE = 12;
3
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/data-fetching/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,kCAAkC,CAAC;AAC7D,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function getAssetsUrl(uuid: string): string;
@@ -0,0 +1,5 @@
1
+ import { ASSETS_URL } from './constants';
2
+ export function getAssetsUrl(uuid) {
3
+ return `${ASSETS_URL}/${uuid}/`;
4
+ }
5
+ //# sourceMappingURL=getAssetsUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAssetsUrl.js","sourceRoot":"","sources":["../../src/data-fetching/getAssetsUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,UAAU,YAAY,CAAC,IAAY;IACrC,OAAO,GAAG,UAAU,IAAI,IAAI,GAAG,CAAC;AACpC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type { Category } from '@prezly/sdk';
2
+ import { LocaleObject } from '../intl';
3
+ import { AlgoliaCategoryRef } from '../types';
4
+ export declare function getLocalizedCategoryData(category: Category | AlgoliaCategoryRef, locale: LocaleObject): {
5
+ description: string | null;
6
+ name: string;
7
+ slug: string | null;
8
+ };
9
+ export declare function getCategoryUrl(category: Category | AlgoliaCategoryRef, locale: LocaleObject): string;
10
+ export declare function getCategoryHasTranslation(category: Category, locale: LocaleObject): boolean;