Skip to content

Commit

Permalink
feat(api): add online-services
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Sep 27, 2024
1 parent eb62f2b commit 733f235
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
13 changes: 13 additions & 0 deletions examples/online-services.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}();
1 change: 1 addition & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./feeds";
export * from "./news";
export * from "./online-services";
export * from "./restaurants";
13 changes: 13 additions & 0 deletions src/api/online-services.ts
Original file line number Diff line number Diff line change
@@ -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<Array<OnlineService>> => {
const request = new Request(`${identifier}/${identifier}-online.xml`);
const response = await fetcher(request);
const content = parseXML(response.content);

return content.root.online.map(decodeOnlineService);
};
9 changes: 9 additions & 0 deletions src/decoders/online-service.ts
Original file line number Diff line number Diff line change
@@ -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
});
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
4 changes: 2 additions & 2 deletions src/models/news-article.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type NewsArticle = {
export type NewsArticle = Readonly<{
id: string
title: string
publicationDate: Date
Expand All @@ -8,4 +8,4 @@ export type NewsArticle = {
*/
content: string
category: string
};
}>;
7 changes: 7 additions & 0 deletions src/models/online-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type OnlineService = Readonly<{
id: string;
imageURL: string;
title: string;
shortDescription: string | null;
url: string;
}>;

0 comments on commit 733f235

Please sign in to comment.