@se-studio/contentful-rest-api 1.0.28 → 1.0.30

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/dist/index.d.ts CHANGED
@@ -403,9 +403,19 @@ declare function lookupAsset(context: ConverterContext, asset: UnresolvedLink<'A
403
403
  type ContentResolverFunction = (context: ConverterContext, entry: Entry<BaseContent, DefaultChainModifier, string>) => unknown;
404
404
  type LinkResolverFunction<TResult extends IInternalLink = IInternalLink> = (context: ConverterContext, entry: Entry<BaseLink, DefaultChainModifier, string>) => TResult;
405
405
  type LinkResolverMap = Map<string, LinkResolverFunction>;
406
+ type UrlCalculators = {
407
+ page: (slug: string) => string;
408
+ pageVariant: (slug: string) => string;
409
+ article: (articleTypeSlug: string, slug: string) => string;
410
+ articleType: (slug: string) => string;
411
+ tag: (slug: string) => string;
412
+ person: (slug: string) => string;
413
+ customType: (slug: string) => string;
414
+ };
406
415
  type BaseConverterContext = {
407
416
  videoPrefix: string;
408
417
  maskResolver?: (maskName: string) => string | undefined;
418
+ urlCalculators: UrlCalculators;
409
419
  linkResolver: LinkResolverMap;
410
420
  contentResolver: Map<string, (context: ConverterContext, entry: Entry<BaseContent, DefaultChainModifier, string>) => unknown>;
411
421
  navigationItemResolver: typeof baseNavigationItemConverter;
@@ -513,6 +523,16 @@ declare function contentfulAllArticleTypeLinks(context: BaseConverterContext, co
513
523
 
514
524
  declare function contentfulPageRest(context: BaseConverterContext, config: ContentfulConfig, slug: string, options?: FetchOptions): Promise<CmsResponse<IBasePage | null>>;
515
525
 
526
+ type PreviewContentType = 'page' | 'pageVariant' | 'article' | 'articleType' | 'tag' | 'person' | 'customType';
527
+ interface PreviewEntryInfo {
528
+ contentType: PreviewContentType;
529
+ entryId: string;
530
+ slug: string;
531
+ articleTypeSlug?: string;
532
+ href: string;
533
+ }
534
+ declare function getPreviewEntryInfo(context: BaseConverterContext, config: ContentfulConfig, entryId: string, options?: Omit<FetchOptions, 'preview'>): Promise<PreviewEntryInfo | null>;
535
+
516
536
  declare function filterRelatedArticles(articles: IArticleLink[], options: RelatedArticlesOptions): IArticleLink[];
517
537
 
518
538
  declare function contentfulPageSitemapEntries(context: BaseConverterContext, config: ContentfulConfig, sitemapConfig?: SitemapContentTypeConfig, options?: FetchOptions): Promise<CmsResponse<ISitemapEntry[]>>;
@@ -633,4 +653,4 @@ declare class RateLimiter {
633
653
  getIntervalMs(): number;
634
654
  }
635
655
 
636
- export { AllTags, ArticleTag, ArticleTypeTag, AssetTag, AuthenticationError, BannerTag, type BaseConverterContext, type CmsError, type CmsResponse, type ContentResolverFunction, ContentfulFetchClient as ContentfulClient, type ContentfulConfig, ContentfulError, ContentfulFetchClient, type ConverterContext, CustomTypeTag, type DefaultChainModifier, EntryNotFoundError, type FetchOptions, GlobalTag, type IContentfulCollection, type IContentfulComponent, type IContentfulPerson, type IContentfulRichText, type IFetchedTemplate, type ISitemapEntry, LocationTag, NavigationTag, PageTag, PersonTag, RateLimitError, RateLimiter, type RelatedArticlesOptions, type RetryConfig, type RevalidationConfig, type SitemapChangeFrequency, type SitemapConfig, type SitemapContentTypeConfig, type SitemapEntryProvider, TagTag, TemplateTag, ValidationError, arrayOrUndefined, articleTag, articleTypeIndexTag, articleTypeTag, assetTag, basePageConverter, calculateBackoffDelay, contentfulAllArticleLinks, contentfulAllArticleTypeLinks, contentfulAllPageLinks, contentfulAllPersonLinks, contentfulAllTagLinks, contentfulArticleRest, contentfulArticleSitemapEntries, contentfulArticleTypeRest, contentfulArticleTypeSitemapEntries, contentfulCustomTypeRest, contentfulPageRest, contentfulPageSitemapEntries, contentfulPersonSitemapEntries, contentfulTagRest, contentfulTagSitemapEntries, contentfulTemplateRest, createBaseConverterContext, createContentfulClient, createContentfulPreviewClient, createResponsiveVisual, createRevalidationHandler, createSitemapProvider, customTypeTag, filterRelatedArticles, getAllSitemapEntries, getCacheTags, getCacheTagsForPreview, getCacheTagsForProduction, getContentfulClient, getRetryAfter, isContentfulError, isRateLimitError, isRetryableError, isValidDate, locationTag, lookupAsset, notEmpty, pageTag, personTag, resolveLink, resolveLinks, resolveRichTextDocument, revalidateSingleTag, revalidateTags, safeDate, tagTag, templateTag, withRetry };
656
+ export { AllTags, ArticleTag, ArticleTypeTag, AssetTag, AuthenticationError, BannerTag, type BaseConverterContext, type CmsError, type CmsResponse, type ContentResolverFunction, ContentfulFetchClient as ContentfulClient, type ContentfulConfig, ContentfulError, ContentfulFetchClient, type ConverterContext, CustomTypeTag, type DefaultChainModifier, EntryNotFoundError, type FetchOptions, GlobalTag, type IContentfulCollection, type IContentfulComponent, type IContentfulPerson, type IContentfulRichText, type IFetchedTemplate, type ISitemapEntry, LocationTag, NavigationTag, PageTag, PersonTag, type PreviewContentType, type PreviewEntryInfo, RateLimitError, RateLimiter, type RelatedArticlesOptions, type RetryConfig, type RevalidationConfig, type SitemapChangeFrequency, type SitemapConfig, type SitemapContentTypeConfig, type SitemapEntryProvider, TagTag, TemplateTag, type UrlCalculators, ValidationError, arrayOrUndefined, articleTag, articleTypeIndexTag, articleTypeTag, assetTag, basePageConverter, calculateBackoffDelay, contentfulAllArticleLinks, contentfulAllArticleTypeLinks, contentfulAllPageLinks, contentfulAllPersonLinks, contentfulAllTagLinks, contentfulArticleRest, contentfulArticleSitemapEntries, contentfulArticleTypeRest, contentfulArticleTypeSitemapEntries, contentfulCustomTypeRest, contentfulPageRest, contentfulPageSitemapEntries, contentfulPersonSitemapEntries, contentfulTagRest, contentfulTagSitemapEntries, contentfulTemplateRest, createBaseConverterContext, createContentfulClient, createContentfulPreviewClient, createResponsiveVisual, createRevalidationHandler, createSitemapProvider, customTypeTag, filterRelatedArticles, getAllSitemapEntries, getCacheTags, getCacheTagsForPreview, getCacheTagsForProduction, getContentfulClient, getPreviewEntryInfo, getRetryAfter, isContentfulError, isRateLimitError, isRetryableError, isValidDate, locationTag, lookupAsset, notEmpty, pageTag, personTag, resolveLink, resolveLinks, resolveRichTextDocument, revalidateSingleTag, revalidateTags, safeDate, tagTag, templateTag, withRetry };
package/dist/index.js CHANGED
@@ -1294,16 +1294,49 @@ function resolveNavigationItem(context, fromId, entry) {
1294
1294
  return result;
1295
1295
  }
1296
1296
  function resolveCollectionContent(context, fromId, entry) {
1297
+ const id = entry.sys.id;
1298
+ const possibleEntry = context.includes.get(id);
1299
+ if (!possibleEntry) {
1300
+ throw new Error(
1301
+ `Cannot find included entry for collection content from ${fromId} with id ${id}`
1302
+ );
1303
+ }
1304
+ if (possibleEntry.resolved) {
1305
+ return possibleEntry.resolved;
1306
+ }
1307
+ const { type } = possibleEntry;
1308
+ if (type === "media") {
1309
+ const visual = convertMediaEntryToVisual(
1310
+ context,
1311
+ possibleEntry.entry
1312
+ );
1313
+ if (!visual) {
1314
+ throw new Error(`Failed to convert media entry with id ${id}`);
1315
+ }
1316
+ possibleEntry.resolved = visual;
1317
+ return visual;
1318
+ }
1319
+ if (type === "externalVideo") {
1320
+ const visual = convertExternalVideoEntryToVisual(
1321
+ context,
1322
+ possibleEntry.entry
1323
+ );
1324
+ if (!visual) {
1325
+ throw new Error(`Failed to convert externalVideo entry with id ${id}`);
1326
+ }
1327
+ possibleEntry.resolved = visual;
1328
+ return visual;
1329
+ }
1297
1330
  return resolveHelper(
1298
1331
  context,
1299
1332
  fromId,
1300
1333
  entry,
1301
- (type) => {
1302
- const resolver = context.contentResolver.get(type);
1334
+ (resolverType) => {
1335
+ const resolver = context.contentResolver.get(resolverType);
1303
1336
  if (resolver) {
1304
1337
  return resolver;
1305
1338
  }
1306
- const linkResolver = context.linkResolver.get(type);
1339
+ const linkResolver = context.linkResolver.get(resolverType);
1307
1340
  if (linkResolver) {
1308
1341
  return linkResolver;
1309
1342
  }
@@ -1724,7 +1757,7 @@ function baseArticleLinkConverter(context, entry) {
1724
1757
  sys.id,
1725
1758
  simpleFields,
1726
1759
  context,
1727
- calculateArticleHref(articleType.slug, fields.slug),
1760
+ context.urlCalculators.article(articleType.slug, fields.slug),
1728
1761
  "Article",
1729
1762
  {
1730
1763
  tags,
@@ -1755,7 +1788,7 @@ function baseArticleTypeLinkConverter(context, entry) {
1755
1788
  slug: fields.slug
1756
1789
  },
1757
1790
  context,
1758
- calculateArticleTypeHref(fields.slug),
1791
+ context.urlCalculators.articleType(fields.slug),
1759
1792
  "ArticleType",
1760
1793
  { indexPageSlug: fields.indexPageSlug }
1761
1794
  );
@@ -1980,7 +2013,7 @@ function baseCustomTypeLinkConverter(context, entry) {
1980
2013
  slug: fields.indexPageSlug
1981
2014
  },
1982
2015
  context,
1983
- calculateCustomTypeHref(fields.indexPageSlug),
2016
+ context.urlCalculators.customType(fields.indexPageSlug),
1984
2017
  "CustomType"
1985
2018
  );
1986
2019
  }
@@ -2174,7 +2207,7 @@ function basePageLinkConverter(context, entry) {
2174
2207
  slug: fields.slug
2175
2208
  },
2176
2209
  context,
2177
- calculatePageHref(fields.slug),
2210
+ context.urlCalculators.page(fields.slug),
2178
2211
  "Page",
2179
2212
  { tags: fields.tags?.map((tag) => resolveLink(context, id, tag)) }
2180
2213
  );
@@ -2203,7 +2236,7 @@ function basePageVariantLinkConverter(context, entry) {
2203
2236
  slug: fields.slug
2204
2237
  },
2205
2238
  context,
2206
- calculatePageVariantHref(fields.slug),
2239
+ context.urlCalculators.pageVariant(fields.slug),
2207
2240
  "Page",
2208
2241
  { tags: fields.tags?.map((tag) => resolveLink(context, id, tag)) }
2209
2242
  );
@@ -2239,7 +2272,7 @@ function basePersonLinkConverter(context, entry) {
2239
2272
  cmsLabel: title
2240
2273
  },
2241
2274
  context,
2242
- calculatePersonHref(fields.slug),
2275
+ context.urlCalculators.person(fields.slug),
2243
2276
  "Person",
2244
2277
  {
2245
2278
  bio: resolveRichTextDocument(context, sys.id, fieldsBio),
@@ -2267,7 +2300,7 @@ function baseTagLinkConverter(context, entry) {
2267
2300
  ...fields
2268
2301
  },
2269
2302
  context,
2270
- calculateTagHref(fields.slug),
2303
+ context.urlCalculators.tag(fields.slug),
2271
2304
  "Tag"
2272
2305
  );
2273
2306
  }
@@ -2338,6 +2371,15 @@ function createBaseConverterContext() {
2338
2371
  "externalComponent",
2339
2372
  baseExternalComponentConverter
2340
2373
  );
2374
+ const urlCalculators = {
2375
+ page: calculatePageHref,
2376
+ pageVariant: calculatePageVariantHref,
2377
+ article: calculateArticleHref,
2378
+ articleType: calculateArticleTypeHref,
2379
+ tag: calculateTagHref,
2380
+ person: calculatePersonHref,
2381
+ customType: calculateCustomTypeHref
2382
+ };
2341
2383
  return {
2342
2384
  pageResolver: basePageConverter,
2343
2385
  navigationItemResolver: baseNavigationItemConverter,
@@ -2349,6 +2391,7 @@ function createBaseConverterContext() {
2349
2391
  collectionResolver: baseCollectionConverter,
2350
2392
  linkResolver,
2351
2393
  contentResolver,
2394
+ urlCalculators,
2352
2395
  videoPrefix: ""
2353
2396
  };
2354
2397
  }
@@ -2510,6 +2553,119 @@ async function contentfulPageRest(context, config, slug, options) {
2510
2553
  );
2511
2554
  }
2512
2555
 
2556
+ // src/api/preview.ts
2557
+ async function getPreviewEntryInfo(context, config, entryId, options) {
2558
+ const client = getContentfulClient(config, true);
2559
+ const response = await client.getEntries(
2560
+ {
2561
+ "sys.id": entryId,
2562
+ include: 1
2563
+ // Need 1 level for Article → ArticleType slug
2564
+ },
2565
+ {
2566
+ ...options,
2567
+ preview: true
2568
+ }
2569
+ );
2570
+ const entry = response.items[0];
2571
+ if (!entry) {
2572
+ return null;
2573
+ }
2574
+ const contentTypeId = entry.sys.contentType.sys.id;
2575
+ const { fields } = entry;
2576
+ switch (contentTypeId) {
2577
+ case "page": {
2578
+ const slug = fields.slug;
2579
+ if (!slug) return null;
2580
+ return {
2581
+ contentType: "page",
2582
+ entryId,
2583
+ slug,
2584
+ href: context.urlCalculators.page(slug)
2585
+ };
2586
+ }
2587
+ case "pageVariant": {
2588
+ const slug = fields.slug;
2589
+ if (!slug) return null;
2590
+ return {
2591
+ contentType: "pageVariant",
2592
+ entryId,
2593
+ slug,
2594
+ href: context.urlCalculators.pageVariant(slug)
2595
+ };
2596
+ }
2597
+ case "article": {
2598
+ const slug = fields.slug;
2599
+ const articleTypeSlug = getArticleTypeSlug(entry, response.includes?.Entry);
2600
+ if (!slug || !articleTypeSlug) return null;
2601
+ return {
2602
+ contentType: "article",
2603
+ entryId,
2604
+ slug,
2605
+ articleTypeSlug,
2606
+ href: context.urlCalculators.article(articleTypeSlug, slug)
2607
+ };
2608
+ }
2609
+ case "articleType": {
2610
+ const slug = fields.indexPageSlug ?? fields.slug;
2611
+ if (!slug) return null;
2612
+ return {
2613
+ contentType: "articleType",
2614
+ entryId,
2615
+ slug,
2616
+ href: context.urlCalculators.articleType(slug)
2617
+ };
2618
+ }
2619
+ case "tag": {
2620
+ const slug = fields.slug;
2621
+ if (!slug) return null;
2622
+ return {
2623
+ contentType: "tag",
2624
+ entryId,
2625
+ slug,
2626
+ href: context.urlCalculators.tag(slug)
2627
+ };
2628
+ }
2629
+ case "person": {
2630
+ const slug = fields.slug;
2631
+ if (!slug) return null;
2632
+ return {
2633
+ contentType: "person",
2634
+ entryId,
2635
+ slug,
2636
+ href: context.urlCalculators.person(slug)
2637
+ };
2638
+ }
2639
+ case "customType": {
2640
+ const slug = fields.indexPageSlug ?? fields.slug;
2641
+ if (!slug) return null;
2642
+ return {
2643
+ contentType: "customType",
2644
+ entryId,
2645
+ slug,
2646
+ href: context.urlCalculators.customType(slug)
2647
+ };
2648
+ }
2649
+ default:
2650
+ return null;
2651
+ }
2652
+ }
2653
+ function getArticleTypeSlug(entry, includes) {
2654
+ const articleTypeField = entry.fields.articleType;
2655
+ if (!articleTypeField) {
2656
+ return void 0;
2657
+ }
2658
+ if (articleTypeField.fields?.slug) {
2659
+ return articleTypeField.fields.slug;
2660
+ }
2661
+ const articleTypeId = articleTypeField.sys?.id;
2662
+ if (!articleTypeId || !includes) {
2663
+ return void 0;
2664
+ }
2665
+ const articleTypeEntry = includes.find((inc) => inc.sys.id === articleTypeId);
2666
+ return articleTypeEntry?.fields?.slug;
2667
+ }
2668
+
2513
2669
  // src/api/related-articles.ts
2514
2670
  function filterRelatedArticles(articles, options) {
2515
2671
  const {
@@ -2920,6 +3076,6 @@ function createRevalidationHandler(config = {}) {
2920
3076
  init_tags();
2921
3077
  init_utils();
2922
3078
 
2923
- export { AllTags, ArticleTag, ArticleTypeTag, AssetTag, AuthenticationError, BannerTag, ContentfulError, CustomTypeTag, EntryNotFoundError, GlobalTag, LocationTag, NavigationTag, PageTag, PersonTag, RateLimitError, RateLimiter, TagTag, TemplateTag, ValidationError, arrayOrUndefined, articleTag, articleTypeIndexTag, articleTypeTag, assetTag, basePageConverter, calculateBackoffDelay, contentfulAllArticleLinks, contentfulAllArticleTypeLinks, contentfulAllPageLinks, contentfulAllPersonLinks, contentfulAllTagLinks, contentfulArticleRest, contentfulArticleSitemapEntries, contentfulArticleTypeRest, contentfulArticleTypeSitemapEntries, contentfulCustomTypeRest, contentfulPageRest, contentfulPageSitemapEntries, contentfulPersonSitemapEntries, contentfulTagRest, contentfulTagSitemapEntries, contentfulTemplateRest, createBaseConverterContext, createContentfulClient, createContentfulPreviewClient, createResponsiveVisual, createRevalidationHandler, createSitemapProvider, customTypeTag, filterRelatedArticles, getAllSitemapEntries, getCacheTags, getCacheTagsForPreview, getCacheTagsForProduction, getContentfulClient, getRetryAfter, isContentfulError, isRateLimitError, isRetryableError, isValidDate, locationTag, lookupAsset, notEmpty, pageTag, personTag, resolveLink, resolveLinks, resolveRichTextDocument, revalidateSingleTag, revalidateTags, safeDate, tagTag, templateTag, withRetry };
3079
+ export { AllTags, ArticleTag, ArticleTypeTag, AssetTag, AuthenticationError, BannerTag, ContentfulError, CustomTypeTag, EntryNotFoundError, GlobalTag, LocationTag, NavigationTag, PageTag, PersonTag, RateLimitError, RateLimiter, TagTag, TemplateTag, ValidationError, arrayOrUndefined, articleTag, articleTypeIndexTag, articleTypeTag, assetTag, basePageConverter, calculateBackoffDelay, contentfulAllArticleLinks, contentfulAllArticleTypeLinks, contentfulAllPageLinks, contentfulAllPersonLinks, contentfulAllTagLinks, contentfulArticleRest, contentfulArticleSitemapEntries, contentfulArticleTypeRest, contentfulArticleTypeSitemapEntries, contentfulCustomTypeRest, contentfulPageRest, contentfulPageSitemapEntries, contentfulPersonSitemapEntries, contentfulTagRest, contentfulTagSitemapEntries, contentfulTemplateRest, createBaseConverterContext, createContentfulClient, createContentfulPreviewClient, createResponsiveVisual, createRevalidationHandler, createSitemapProvider, customTypeTag, filterRelatedArticles, getAllSitemapEntries, getCacheTags, getCacheTagsForPreview, getCacheTagsForProduction, getContentfulClient, getPreviewEntryInfo, getRetryAfter, isContentfulError, isRateLimitError, isRetryableError, isValidDate, locationTag, lookupAsset, notEmpty, pageTag, personTag, resolveLink, resolveLinks, resolveRichTextDocument, revalidateSingleTag, revalidateTags, safeDate, tagTag, templateTag, withRetry };
2924
3080
  //# sourceMappingURL=index.js.map
2925
3081
  //# sourceMappingURL=index.js.map