Add something else of news write
This commit is contained in:
parent
79abcb348f
commit
54f4014302
@ -1,11 +1,31 @@
|
|||||||
export interface NewsSource {
|
import { StringUrl } from "../../utils/requests";
|
||||||
|
|
||||||
|
/*export interface NewsSource {
|
||||||
name: string,
|
name: string,
|
||||||
icon?: string,
|
icon?: string,
|
||||||
sourceURL?: string,
|
sourceURL?: StringUrl,
|
||||||
type: "RSS",
|
type: "system" | "RSS",
|
||||||
itemURL?: string,
|
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 {
|
export interface NewsItem {
|
||||||
id : number;
|
id : number;
|
||||||
title : string;
|
title : string;
|
||||||
|
|||||||
@ -11,7 +11,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -21,7 +21,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -31,7 +31,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -41,7 +41,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -51,7 +51,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -61,7 +61,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -71,7 +71,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -81,7 +81,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -91,7 +91,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -101,7 +101,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -111,7 +111,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -121,7 +121,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -131,7 +131,7 @@ export function getTestNews (): NewsItem[] {
|
|||||||
publishedAt: new Date(),
|
publishedAt: new Date(),
|
||||||
source: {
|
source: {
|
||||||
name: "Test Destination",
|
name: "Test Destination",
|
||||||
type: "RSS",
|
type: "system",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { NewsSource, NewsItem } from "../../api/models/news";
|
import { NewsSource, NewsItem } from "../../api/models/news";
|
||||||
|
import requests from "../requests";
|
||||||
|
|
||||||
class NewsSourceTarget {
|
class NewsSourceTarget {
|
||||||
protected source : NewsSource;
|
protected source : NewsSource;
|
||||||
@ -9,6 +10,10 @@ class NewsSourceTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async readerRSS () : Promise<NewsItem[]> {
|
protected async readerRSS () : Promise<NewsItem[]> {
|
||||||
|
if (!this.source.sourceURL) {
|
||||||
|
throw new TypeError("RSS must have sourceURL");
|
||||||
|
}
|
||||||
|
await requests.get(this.source.sourceURL, "string");
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,8 +21,10 @@ class NewsSourceTarget {
|
|||||||
switch (this.source.type) {
|
switch (this.source.type) {
|
||||||
case "RSS":
|
case "RSS":
|
||||||
return this.readerRSS();
|
return this.readerRSS();
|
||||||
|
case "system":
|
||||||
|
return [];
|
||||||
default:
|
default:
|
||||||
const impossible: never = this.source.type;
|
const impossible: never = this.source;
|
||||||
return impossible;
|
return impossible;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,28 +2,27 @@ import http from "http";
|
|||||||
import https from "https";
|
import https from "https";
|
||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
|
|
||||||
type StringUrl = `http${"s" | ""}://${string}`;
|
export type StringUrl = `http${"s" | ""}://${string}`;
|
||||||
type Results = string | Buffer | Readable;
|
type Results = string | Buffer | Readable;
|
||||||
type ResultType = "string" | "buffer" | "stream";
|
type ResultType = "string" | "buffer" | "stream";
|
||||||
|
|
||||||
export function get(url: StringUrl, type: "string", queries : Record<string, string>): Promise<string>;
|
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>;
|
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: "stream", queries ?: Record<string, string>): Promise<Readable>;
|
||||||
|
|
||||||
export async function get(
|
async function get(
|
||||||
url : StringUrl,
|
url : StringUrl,
|
||||||
type : ResultType,
|
type : ResultType,
|
||||||
queries : Record<string, string> = {}
|
queries ?: Record<string, string>
|
||||||
): Promise<Results> {
|
): Promise<Results> {
|
||||||
const protocol = url.startsWith("https") ? https : http;
|
const protocol = url.startsWith("https") ? https : http;
|
||||||
|
|
||||||
// Build query string from queries object
|
// Build query string from queries object
|
||||||
const queryString = Object.entries(queries)
|
const queryString = Object.entries(queries ?? {})
|
||||||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
||||||
.join('&');
|
.join('&');
|
||||||
|
|
||||||
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
const fullUrl = queryString ? `${url}${url.includes('?') ? '&' : '?'}${queryString}` : url;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
protocol.get(fullUrl, (res) => {
|
protocol.get(fullUrl, (res) => {
|
||||||
if (type === "stream") {
|
if (type === "stream") {
|
||||||
@ -42,3 +41,5 @@ export async function get(
|
|||||||
}).on("error", reject);
|
}).on("error", reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default { get };
|
||||||
Loading…
Reference in New Issue
Block a user