Skip to content

Commit

Permalink
added get new by id
Browse files Browse the repository at this point in the history
  • Loading branch information
CalumW1 committed Nov 11, 2024
1 parent 879676c commit 8c953c6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export const UBI_GETNEWS = (
) =>
`?categoriesFilter=${categoriesFilter}&mediaFilter=${mediaFilter}&placementFilter=${placementFilter}&locale=${locale}&fallbackLocale=${fallbackLocale}&limit=${limit}&skip=${skip}&startIndex=${startIndex}&tags=BR-rainbow-six%20GA-siege`;

export const UBI_GETNEWSBYID = (id: string, locale: string, fallbackLocale: string) =>
`/${id}?entryId=${id}&locale=${locale}&fallbackLocale=${fallbackLocale}&tags=BR-rainbow-six%20GA-siege`;

export const AvatarURI = (userId: string, size: number) =>
`https://avatars.ubisoft.com/${userId}/default_${
size === 500 ? 'tall' : `${size}_${size}`
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { AdvancedSearch } from './methods/advancedSearchMarketplace';
import { GetTransactionsPending } from './methods/getPendingTransactions';
import { GetTransactionHistroy } from './methods/getTransactionHistory';
import { GetNews } from './methods/getNews';
import { News } from './interfaces/news';
import { GetNewsById } from './methods/getNewsById';

export class R6StatAPI {
public async login(email: string, password: string): Promise<string> {
Expand Down Expand Up @@ -112,7 +114,7 @@ export class R6StatAPI {
limit: number,
skip: number,
startIndex: number
): Promise<any> {
): Promise<News> {
return await GetNews(
categoriesFilter,
mediaFilter,
Expand All @@ -124,4 +126,8 @@ export class R6StatAPI {
startIndex
);
}

public async GetNewsById(id: string, locale: string, fallbackLocale: string): Promise<News> {
return await GetNewsById(id, locale, fallbackLocale);
}
}
24 changes: 11 additions & 13 deletions src/methods/getNews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export const GetNews = async (
};

const BuildNews = async (data: any): Promise<News> => {

const news : News = {
const news: News = {
total: data.total,
tags: data.tags as string[],
mediaFilter: data.mediaFilter,
Expand All @@ -45,17 +44,16 @@ const BuildNews = async (data: any): Promise<News> => {
limit: data.limit,
startIndex: data.startIndex,
skip: data.skip,
items: await BuildNewsItems(data.items)
}
items: await BuildNewsItems(data.items),
};
return news;
}
};

const BuildNewsItems = async (newsItems: any): Promise<NewsItems[]> => {

const news: NewsItems[] = [];

newsItems.forEach(async (item: any) => {
const newsItem : NewsItems = {
const newsItem: NewsItems = {
id: item.id,
type: item.type,
tag: item.tag,
Expand All @@ -68,14 +66,14 @@ const BuildNewsItems = async (newsItems: any): Promise<NewsItems[]> => {
trackingPageValue: item.trackingPageValue,
readTime: item.readTime,
author: item.authors,
thumbnail: item?.thumbnail as Thumbnail ?? {},
button: item?.button as Button ?? {},
thumbnail: (item?.thumbnail as Thumbnail) ?? {},
button: (item?.button as Button) ?? {},
index: item?.index ?? 0,
prevNode: item?.prevNode as Node ?? {},
nextNode: item?.nextNode as Node ?? {},
}
prevNode: (item?.prevNode as Node) ?? {},
nextNode: (item?.nextNode as Node) ?? {},
};
news.push(newsItem);
});

return news;
}
};
63 changes: 63 additions & 0 deletions src/methods/getNewsById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { NEWS_AUTH_TOKEN, UBI_GETNEWSBYID, UBI_NEWS_URI } from '../constants';
import { Button, News, NewsItems, Thumbnail, Node } from '../interfaces/news';
import { ApiClient } from './apiClient';

export const GetNewsById = async (
id: string,
locale: string,
fallbackLocale: string
): Promise<News> => {
const header = {
Authorization: NEWS_AUTH_TOKEN,
};

const URI = UBI_NEWS_URI + UBI_GETNEWSBYID(id, locale, fallbackLocale);

const data = await ApiClient(URI, header, 'GET');

return await BuildNews(data);
};

const BuildNews = async (data: any): Promise<News> => {
const news: News = {
total: data.total,
tags: data.tags as string[],
mediaFilter: data.mediaFilter,
categoriesFilter: data.categoriesFilter,
placementFilter: data.placementFilter as string[],
limit: data.limit,
startIndex: data.startIndex,
skip: data.skip,
items: await BuildNewsItems(data.items),
};
return news;
};

const BuildNewsItems = async (newsItems: any): Promise<NewsItems[]> => {
const news: NewsItems[] = [];

newsItems.forEach(async (item: any) => {
const newsItem: NewsItems = {
id: item.id,
type: item.type,
tag: item.tag,
categories: item.categories as string[],
placement: item.placement,
date: item.date,
title: item.title,
abstract: item.abstract,
content: item.content,
trackingPageValue: item.trackingPageValue,
readTime: item.readTime,
author: item.authors,
thumbnail: (item?.thumbnail as Thumbnail) ?? {},
button: (item?.button as Button) ?? {},
index: item?.index ?? 0,
prevNode: (item?.prevNode as Node) ?? {},
nextNode: (item?.nextNode as Node) ?? {},
};
news.push(newsItem);
});

return news;
};

0 comments on commit 8c953c6

Please sign in to comment.