Add something else of news write

This commit is contained in:
FullGreaM 2026-04-29 04:45:09 +03:00
parent 79abcb348f
commit 54f4014302
4 changed files with 56 additions and 28 deletions

View File

@ -1,11 +1,31 @@
export interface NewsSource {
import { StringUrl } from "../../utils/requests";
/*export interface NewsSource {
name: string,
icon?: string,
sourceURL?: string,
type: "RSS",
itemURL?: string,
sourceURL?: StringUrl,
type: "system" | "RSS",
itemURL?: StringUrl,
}*/
interface SystemNewsSource {
name: string;
icon?: string;
type: "system";
sourceURL?: never;
itemURL?: never;
}
interface OtherNewsSource {
name: string;
icon?: string;
type: "RSS";
sourceURL: StringUrl;
itemURL?: StringUrl;
}
export type NewsSource = SystemNewsSource | OtherNewsSource;
export interface NewsItem {
id : number;
title : string;

View File

@ -11,7 +11,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -21,7 +21,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -31,7 +31,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -41,7 +41,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -51,7 +51,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -61,7 +61,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -71,7 +71,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -81,7 +81,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -91,7 +91,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -101,7 +101,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -111,7 +111,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -121,7 +121,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
},
{
@ -131,7 +131,7 @@ export function getTestNews (): NewsItem[] {
publishedAt: new Date(),
source: {
name: "Test Destination",
type: "RSS",
type: "system",
}
}
];

View File

@ -1,4 +1,5 @@
import { NewsSource, NewsItem } from "../../api/models/news";
import requests from "../requests";
class NewsSourceTarget {
protected source : NewsSource;
@ -9,6 +10,10 @@ class NewsSourceTarget {
}
protected async readerRSS () : Promise<NewsItem[]> {
if (!this.source.sourceURL) {
throw new TypeError("RSS must have sourceURL");
}
await requests.get(this.source.sourceURL, "string");
return [];
}
@ -16,8 +21,10 @@ class NewsSourceTarget {
switch (this.source.type) {
case "RSS":
return this.readerRSS();
case "system":
return [];
default:
const impossible: never = this.source.type;
const impossible: never = this.source;
return impossible;
}
}

View File

@ -2,28 +2,27 @@ import http from "http";
import https from "https";
import { Readable } from "stream";
type StringUrl = `http${"s" | ""}://${string}`;
export type StringUrl = `http${"s" | ""}://${string}`;
type Results = string | Buffer | Readable;
type ResultType = "string" | "buffer" | "stream";
export function get(url: StringUrl, type: "string", queries : Record<string, string>): Promise<string>;
export function get(url: StringUrl, type: "buffer", queries : Record<string, string>): Promise<Buffer>;
export function get(url: StringUrl, type: "stream", queries : Record<string, string>): Promise<Readable>;
function get(url: StringUrl, type: "string", queries ?: Record<string, string>): Promise<string>;
function get(url: StringUrl, type: "buffer", queries ?: Record<string, string>): Promise<Buffer>;
function get(url: StringUrl, type: "stream", queries ?: Record<string, string>): Promise<Readable>;
export async function get(
async function get(
url : StringUrl,
type : ResultType,
queries : Record<string, string> = {}
queries ?: Record<string, string>
): Promise<Results> {
const protocol = url.startsWith("https") ? https : http;
// Build query string from queries object
const queryString = Object.entries(queries)
const queryString = Object.entries(queries ?? {})
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
const fullUrl = queryString ? `${url}?${queryString}` : url;
const fullUrl = queryString ? `${url}${url.includes('?') ? '&' : '?'}${queryString}` : url;
return new Promise((resolve, reject) => {
protocol.get(fullUrl, (res) => {
if (type === "stream") {
@ -41,4 +40,6 @@ export async function get(
}
}).on("error", reject);
});
}
}
export default { get };