diff --git a/examples/online-services.ts b/examples/online-services.ts new file mode 100644 index 0000000..ac322a6 --- /dev/null +++ b/examples/online-services.ts @@ -0,0 +1,13 @@ +import * as crous from "../src"; +// ^^^^^^^^ +// Replace with: from "crowous"; +// when using the package. + +void async function main() { + const identifier = "bordeaux"; + const onlineServices = await crous.onlineServices(identifier); + + for (const onlineService of onlineServices) { + console.log(`- ${onlineService.title} : ${onlineService.shortDescription ?? "(no description)"} @ ${onlineService.url}`); + } +}(); diff --git a/src/api/index.ts b/src/api/index.ts index 2f0d94f..d6f6cd9 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,3 +1,4 @@ export * from "./feeds"; export * from "./news"; +export * from "./online-services"; export * from "./restaurants"; diff --git a/src/api/online-services.ts b/src/api/online-services.ts new file mode 100644 index 0000000..08facfd --- /dev/null +++ b/src/api/online-services.ts @@ -0,0 +1,13 @@ +import { defaultFetcher, type Fetcher } from "@literate.ink/utilities"; +import { Request } from "~/core/request"; +import { parseXML } from "~/core/xml"; +import type { OnlineService } from "~/models"; +import { decodeOnlineService } from "~/decoders/online-service"; + +export const onlineServices = async (identifier: string, fetcher: Fetcher = defaultFetcher): Promise> => { + const request = new Request(`${identifier}/${identifier}-online.xml`); + const response = await fetcher(request); + const content = parseXML(response.content); + + return content.root.online.map(decodeOnlineService); +}; diff --git a/src/decoders/online-service.ts b/src/decoders/online-service.ts new file mode 100644 index 0000000..5427a95 --- /dev/null +++ b/src/decoders/online-service.ts @@ -0,0 +1,9 @@ +import type { OnlineService } from "~/models"; + +export const decodeOnlineService = (xml: any): OnlineService => ({ + id: xml.id, + imageURL: xml.image, + title: xml.title, + shortDescription: xml.short_desc || null, + url: xml.link +}); diff --git a/src/models/index.ts b/src/models/index.ts index 31b82c3..3d14f1b 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -8,6 +8,7 @@ export * from "./meal"; export * from "./menu"; export * from "./moment"; export * from "./news-article"; +export * from "./online-service"; export * from "./payment-method"; export * from "./restaurant-kind"; export * from "./restaurant"; diff --git a/src/models/news-article.ts b/src/models/news-article.ts index 580ec23..64bc0ea 100644 --- a/src/models/news-article.ts +++ b/src/models/news-article.ts @@ -1,4 +1,4 @@ -export type NewsArticle = { +export type NewsArticle = Readonly<{ id: string title: string publicationDate: Date @@ -8,4 +8,4 @@ export type NewsArticle = { */ content: string category: string -}; +}>; diff --git a/src/models/online-service.ts b/src/models/online-service.ts new file mode 100644 index 0000000..9d41ecd --- /dev/null +++ b/src/models/online-service.ts @@ -0,0 +1,7 @@ +export type OnlineService = Readonly<{ + id: string; + imageURL: string; + title: string; + shortDescription: string | null; + url: string; +}>;