Added enhanced request query building

This commit is contained in:
2022-01-24 13:50:22 +01:00
parent 78fbd0234c
commit cbf5b814fc
3 changed files with 58 additions and 13 deletions

View File

@@ -10,6 +10,21 @@ async function unknownResponse(resp: Response): Promise<Error> {
}
}
function buildQuery(options: any[][]): string {
let query: string[]
options.forEach(element => {
if (element[1] !== undefined) {
if (element[1] instanceof Date) {
element[1] = element[1].getSeconds()
} else if (element[1] instanceof Array) {
element[1] = JSON.stringify(element[1])
}
query.push(`${element[0]}&${element[1]}`)
}
});
return query.join("=")
}
interface Author {
id: number,
name: string,
@@ -50,7 +65,9 @@ async function login(username: string, password: string): Promise<void> {
}
async function authors(name?: string, limit?: number): Promise<Author[]> {
let result = await fetch(`${prefix}/api/authors?name=${name}&limit=${limit}`)
let query = [["name", name], ["limit", limit]]
let result = await fetch(`${prefix}/api/authors?${buildQuery(query)}`)
if (result.status == 200) {
return await result.json()
} else {
@@ -59,7 +76,9 @@ async function authors(name?: string, limit?: number): Promise<Author[]> {
}
async function tags(name?: string, limit?: number): Promise<string[]> {
let result = await fetch(`${prefix}/api/tags?name=${name}&limit=${limit}`)
let query = [["name", name], ["limit", limit]]
let result = await fetch(`${prefix}/api/tags?${buildQuery(query)}`)
if (result.status == 200) {
return await result.json()
} else {
@@ -68,7 +87,9 @@ async function tags(name?: string, limit?: number): Promise<string[]> {
}
async function recent(limit: number = 20): Promise<ArticleSummary[]> {
let result = await fetch(`${prefix}/api/recent?limit=${limit}`)
let query = [["limit", limit]]
let result = await fetch(`${prefix}/api/recent?limit=${buildQuery(query)}`)
if (result.status == 200) {
return await result.json()
} else {
@@ -85,8 +106,10 @@ interface SearchQuery {
limit?: number
}
async function search(query: SearchQuery): Promise<ArticleSummary[]> {
let result = await fetch(`${prefix}/api/search?q=${query.query}&from=${query.from.getSeconds()}&to=${query.to.getSeconds()}&authors=${JSON.stringify(query.authors)}&tags=${JSON.stringify(query.tags)}&limit=${query.limit}`)
async function search(q: SearchQuery): Promise<ArticleSummary[]> {
let query = [["q", q.query], ["from", q.from], ["to", q.to], ["authors", q.authors], ["tags", q.tags], ["limit", q.limit]]
let result = await fetch(`${prefix}/api/search?${buildQuery(query)}`)
if (result.status == 200) {
return await result.json()
} else {
@@ -171,7 +194,9 @@ async function deleteArticle(id: number): Promise<void> {
}
async function getAssets(name?: string, limit: number = 20): Promise<Asset[]> {
let result = await fetch(`${prefix}/api/assets?q=${name}&limit=${limit}`, {
let query = [["q", name], ["limit", limit]]
let result = await fetch(`${prefix}/api/assets?${buildQuery(query)}`, {
method: "GET",
})
switch (result.status) {