You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { getCorsJSON } from 'sonar-ui-common/helpers/request';
  21. interface PrismicRef {
  22. id: string;
  23. ref: string;
  24. }
  25. export interface PrismicNews {
  26. data: { title: string };
  27. last_publication_date: string;
  28. uid: string;
  29. }
  30. interface PrismicResponse {
  31. page: number;
  32. results: PrismicResult[];
  33. results_per_page: number;
  34. total_results_size: number;
  35. }
  36. interface PrismicResult {
  37. data: {
  38. notification: string;
  39. publication_date: string;
  40. body: PrismicResultFeature[];
  41. };
  42. }
  43. interface PrismicResultFeature {
  44. items: Array<{
  45. category: {
  46. data: {
  47. color: string;
  48. name: string;
  49. };
  50. };
  51. }>;
  52. primary: {
  53. description: string;
  54. read_more_link: {
  55. url?: string;
  56. };
  57. };
  58. }
  59. export interface PrismicFeatureNews {
  60. notification: string;
  61. publicationDate: string;
  62. features: Array<{
  63. categories: Array<{
  64. color: string;
  65. name: string;
  66. }>;
  67. description: string;
  68. readMore?: string;
  69. }>;
  70. }
  71. const PRISMIC_API_URL = 'https://sonarsource.cdn.prismic.io/api/v2';
  72. export function fetchPrismicRefs() {
  73. return getCorsJSON(PRISMIC_API_URL).then((response: { refs: PrismicRef[] }) => {
  74. const master = response && response.refs.find(ref => ref.id === 'master');
  75. if (!master) {
  76. return Promise.reject('No master ref found');
  77. }
  78. return master;
  79. });
  80. }
  81. export function fetchPrismicNews(data: {
  82. accessToken: string;
  83. ps?: number;
  84. ref: string;
  85. tag?: string;
  86. }) {
  87. const q = ['[[at(document.type, "blog_sonarsource_post")]]'];
  88. if (data.tag) {
  89. q.push(`[[at(document.tags,["${data.tag}"])]]`);
  90. }
  91. return getCorsJSON(PRISMIC_API_URL + '/documents/search', {
  92. access_token: data.accessToken,
  93. orderings: '[document.first_publication_date desc]',
  94. pageSize: data.ps || 1,
  95. q,
  96. ref: data.ref
  97. }).then(({ results }: { results: PrismicNews[] }) => results);
  98. }
  99. export function fetchPrismicFeatureNews(data: {
  100. accessToken: string;
  101. p?: number;
  102. ps?: number;
  103. ref: string;
  104. }): Promise<{ news: PrismicFeatureNews[]; paging: T.Paging }> {
  105. return getCorsJSON(PRISMIC_API_URL + '/documents/search', {
  106. access_token: data.accessToken,
  107. fetchLinks: 'sc_category.color,sc_category.name',
  108. orderings: '[my.sc_product_news.publication_date desc]',
  109. page: data.p || 1,
  110. pageSize: data.ps || 1,
  111. q: ['[[at(document.type, "sc_product_news")]]'],
  112. ref: data.ref
  113. }).then(({ page, results, results_per_page, total_results_size }: PrismicResponse) => ({
  114. news: results.map(result => ({
  115. notification: result.data.notification,
  116. publicationDate: result.data.publication_date,
  117. features: result.data.body.map(feature => ({
  118. categories: feature.items.map(item => item.category.data).filter(Boolean),
  119. description: feature.primary.description,
  120. readMore: feature.primary.read_more_link.url
  121. }))
  122. })),
  123. paging: {
  124. pageIndex: page,
  125. pageSize: results_per_page,
  126. total: total_results_size
  127. }
  128. }));
  129. }