/ src / api / news.ts
news.ts
 1  import type * as definitions from "~/definitions";
 2  import { deserialize } from "desero";
 3  import { HttpRequest, send } from "schwi";
 4  import { BASE_URL } from "~/core/constants";
 5  import { Article } from "~/models";
 6  
 7  /**
 8   * Get all {@link Article|articles} for a given identifier.
 9   *
10   * @param identifier - Where we should look for articles.
11   * @returns A list of all {@link Article|articles} for a given identifier.
12   *
13   * @example
14   * const news = await getNewsFrom("bordeaux");
15   *
16   * for (const article of news) {
17   *   console.log(article.category, article.title);
18   * }
19   *
20   * @example
21   * const feeds = await getFeeds();
22   * const news = await getNewsFrom(feeds[0].identifier);
23   * // ...
24   */
25  export async function getNewsFrom(identifier: string): Promise<Array<Article>> {
26    const request = new HttpRequest.Builder(BASE_URL + `${identifier}/externe/actu.xml`).build();
27    const response = await send(request);
28  
29    const xml = await response.toXML<{
30      root: {
31        article: Array<definitions.article>;
32      };
33    }>();
34  
35    return xml.root.article.map((article) => deserialize(Article, article));
36  };