api-client: fix url joining

This commit is contained in:
Thomas Hobson 2021-02-27 19:29:15 +13:00
parent 8a0f7b3abd
commit e5225f05c9
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
1 changed files with 8 additions and 3 deletions

View File

@ -1,5 +1,10 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
function url_join(base, endpoint){
return base + endpoint
//return new URL(endpoint, base).href;
}
class APIWrapper { class APIWrapper {
#base; #base;
constructor(base_url){ constructor(base_url){
@ -7,7 +12,7 @@ class APIWrapper {
} }
async query(endpoint, options={}){ async query(endpoint, options={}){
const url = new URL(endpoint, this.#base).href; const url = url_join(this.#base, endpoint);
return await fetch(url, options) return await fetch(url, options)
.then(res=>res.json()) .then(res=>res.json())
.then(res=>{if(res.data)return res.data; throw new Error(res.message)}); .then(res=>{if(res.data)return res.data; throw new Error(res.message)});
@ -44,7 +49,7 @@ class APIWrapper {
class PistonEngineRepositoryPackage extends APIWrapper { class PistonEngineRepositoryPackage extends APIWrapper {
constructor(repo, {language, language_version, author, buildfile, size, dependencies, installed}){ constructor(repo, {language, language_version, author, buildfile, size, dependencies, installed}){
super(new URL(`/packages/${language}/${language_version}`,repo.url_base)) super(url_join(repo.url_base, `/packages/${language}/${language_version}`))
this.language = language; this.language = language;
this.language_version = language_version; this.language_version = language_version;
@ -67,7 +72,7 @@ class PistonEngineRepositoryPackage extends APIWrapper {
class PistonEngineRepository extends APIWrapper { class PistonEngineRepository extends APIWrapper {
constructor(engine, {slug, url, packages}){ constructor(engine, {slug, url, packages}){
super(new URL(`/repos/${slug}`, engine.url_base,)) super(url_join(engine.url_base,`/repos/${slug}`))
this.slug = slug; this.slug = slug;
this.url = url; this.url = url;