Comparing version 1.1.0 to 1.1.1
@@ -17,3 +17,3 @@ export declare class AjaonPromise<Res = any, Error = string> extends Promise<Res> { | ||
}; | ||
export default function ajaon(apiUrl: string, sessKeyKey?: string | SessKeyKey, storage?: object, verbose?: boolean): { | ||
export default function ajaon(apiUrl?: string, sessKeyKey?: string | SessKeyKey, storage?: object, verbose?: boolean): { | ||
post: <Res = GenericObject>(url: string | string[], body: string | object, headers?: HeadersInit, verbose?: boolean) => AjaonPromise<Res, string>; | ||
@@ -20,0 +20,0 @@ get: <Res_1>(...url: string[]) => Promise<Res_1>; |
import clone from "tiny-clone"; | ||
import getBaseUrl from "get-base-url"; | ||
export class AjaonPromise extends Promise { | ||
@@ -25,3 +26,2 @@ constructor(f) { | ||
} | ||
const dir = "/"; | ||
const setVerboseFalseNote = "\n\nIf this behaviour is intended and youd like to disable this warning, set verbose to false."; | ||
@@ -81,5 +81,7 @@ const postString = "POST"; | ||
} | ||
const endsWithSlash = endsWith("/"); | ||
const endsWithSlash = endsWith(["/", "\\"]); | ||
const constructConsoleWarnVerbose = constructConsoleType("warn"); | ||
export default function ajaon(apiUrl, sessKeyKey, storage = localStorage, verbose = true) { | ||
const commonLoginApiCalls = ["login", "auth", "session"]; | ||
const baseUrl = getBaseUrl(); | ||
export default function ajaon(apiUrl = baseUrl, sessKeyKey, storage = localStorage, verbose = true) { | ||
const defaultVervose = verbose; | ||
@@ -102,2 +104,3 @@ let warn = constructConsoleWarnVerbose(verbose); | ||
warn = constructConsoleWarnVerbose(verbose); | ||
const assembledUrl = assembleUrl(url); | ||
body = typeof body === "string" ? JSON.parse(body) : body; | ||
@@ -109,8 +112,18 @@ if (sess) { | ||
body[sess.sessKeyKeyForApi] = storage[sess.sessKeyKeyForStorage]; | ||
else | ||
error("No sessionKey found on the client under " + sess.sessKeyKeyForStorage + "."); | ||
else { | ||
let isCommon = false; | ||
const lowerCaseUrl = assembledUrl.toLocaleLowerCase(); | ||
for (let i = 0; i < commonLoginApiCalls.length; i++) { | ||
if (lowerCaseUrl.includes(commonLoginApiCalls[i])) { | ||
isCommon = true; | ||
} | ||
} | ||
if (!isCommon) { | ||
error("No sessionKey found on the client under " + sess.sessKeyKeyForStorage + "."); | ||
} | ||
} | ||
} | ||
body = JSON.stringify(body); | ||
try { | ||
res(await (await fetch(validateURL(url), { | ||
res(await (await fetch(assembledUrl, { | ||
headers: headers, | ||
@@ -129,3 +142,3 @@ method: postString, | ||
try { | ||
res(await (await fetch(validateURL(url), { | ||
res(await (await fetch(assembleUrl(url), { | ||
headers: headers, | ||
@@ -139,5 +152,5 @@ method: postString, | ||
apiUrl = apiUrlHasNOTBeenWith + apiUrlWithoutHTTPSPrefix; | ||
let urlA = validateURL(url); | ||
let urlA = assembleUrl(url); | ||
apiUrl = apiUrlSave; | ||
let urlB = validateURL(url); | ||
let urlB = assembleUrl(url); | ||
error("POST request failed at " + urlA + " and " + urlB + "."); | ||
@@ -147,3 +160,3 @@ } | ||
else { | ||
error("POST request failed at " + validateURL(url) + "."); | ||
error("POST request failed at " + assembleUrl(url) + "."); | ||
} | ||
@@ -156,18 +169,19 @@ } | ||
try { | ||
res(await (await fetch(validateURL(url))).json()); | ||
res(await (await fetch(assembleUrl(url))).json()); | ||
} | ||
catch (e) { | ||
constructConsoleType(console.error, fail)(verbose)("GET request failed at \"" + validateURL(url) + "\"."); | ||
constructConsoleType(console.error, fail)(verbose)("GET request failed at \"" + assembleUrl(url) + "\"."); | ||
} | ||
}); | ||
} | ||
function validateURL(url) { | ||
function assembleUrl(url) { | ||
url = (url instanceof Array) ? url : [url]; | ||
let fullUrl = ""; | ||
url.forEach((urlPart) => { | ||
if (urlPart.charAt(urlPart.length - 1) === dir) | ||
if (endsWithSlash(urlPart)) | ||
urlPart = urlPart.substring(0, urlPart.length - 1); | ||
fullUrl += urlPart; | ||
fullUrl += urlPart + "/"; | ||
}); | ||
if (url[0].substring(0, 4) !== "http") | ||
fullUrl = fullUrl.substring(0, fullUrl.length - 1); | ||
if (!startsWithHTTPS(fullUrl)) | ||
fullUrl = apiUrl + fullUrl; | ||
@@ -174,0 +188,0 @@ return fullUrl; |
import clone from "tiny-clone" | ||
import getBaseUrl from "get-base-url" | ||
@@ -35,3 +36,2 @@ | ||
const dir = "/" | ||
const setVerboseFalseNote = "\n\nIf this behaviour is intended and youd like to disable this warning, set verbose to false." | ||
@@ -82,3 +82,3 @@ const postString = "POST" | ||
} | ||
const endsWithSlash = endsWith("/") | ||
const endsWithSlash = endsWith(["/", "\\"]) | ||
@@ -90,5 +90,6 @@ const constructConsoleWarnVerbose = constructConsoleType("warn") | ||
const commonLoginApiCalls = ["login", "auth", "session"] | ||
const baseUrl = getBaseUrl() | ||
export default function ajaon(apiUrl: string, sessKeyKey?: string | SessKeyKey, storage: object = localStorage, verbose: boolean = true) { | ||
export default function ajaon(apiUrl: string = baseUrl, sessKeyKey?: string | SessKeyKey, storage: object = localStorage, verbose: boolean = true) { | ||
const defaultVervose = verbose | ||
@@ -114,3 +115,3 @@ let warn = constructConsoleWarnVerbose(verbose) | ||
if (verbose !== defaultVervose) warn = constructConsoleWarnVerbose(verbose) | ||
const assembledUrl = assembleUrl(url) | ||
@@ -121,3 +122,15 @@ body = typeof body === "string" ? JSON.parse(body) : body | ||
else if (storage[sess.sessKeyKeyForStorage] !== undefined) body[sess.sessKeyKeyForApi] = storage[sess.sessKeyKeyForStorage] | ||
else error("No sessionKey found on the client under " + sess.sessKeyKeyForStorage + ".") | ||
else { | ||
let isCommon = false | ||
const lowerCaseUrl = assembledUrl.toLocaleLowerCase() | ||
for (let i = 0; i < commonLoginApiCalls.length; i++) { | ||
if (lowerCaseUrl.includes(commonLoginApiCalls[i])) { | ||
isCommon = true | ||
} | ||
} | ||
if (!isCommon) { | ||
error("No sessionKey found on the client under " + sess.sessKeyKeyForStorage + ".") | ||
} | ||
} | ||
} | ||
@@ -128,3 +141,3 @@ | ||
try { | ||
res(await (await fetch(validateURL(url), { | ||
res(await (await fetch(assembledUrl, { | ||
headers: headers, | ||
@@ -140,3 +153,3 @@ method: postString, | ||
try { | ||
res(await (await fetch(validateURL(url), { | ||
res(await (await fetch(assembleUrl(url), { | ||
headers: headers, | ||
@@ -150,5 +163,5 @@ method: postString, | ||
apiUrl = apiUrlHasNOTBeenWith + apiUrlWithoutHTTPSPrefix | ||
let urlA = validateURL(url) | ||
let urlA = assembleUrl(url) | ||
apiUrl = apiUrlSave | ||
let urlB = validateURL(url) | ||
let urlB = assembleUrl(url) | ||
@@ -161,3 +174,3 @@ | ||
else { | ||
error("POST request failed at " + validateURL(url) + ".") | ||
error("POST request failed at " + assembleUrl(url) + ".") | ||
} | ||
@@ -174,6 +187,6 @@ | ||
try { | ||
res(await (await fetch(validateURL(url))).json()) | ||
res(await (await fetch(assembleUrl(url))).json()) | ||
} | ||
catch(e) { | ||
constructConsoleType(console.error, fail)(verbose)("GET request failed at \"" + validateURL(url) + "\"."); | ||
constructConsoleType(console.error, fail)(verbose)("GET request failed at \"" + assembleUrl(url) + "\"."); | ||
} | ||
@@ -183,13 +196,15 @@ }) | ||
function validateURL(url: string[] | string) { | ||
function assembleUrl(url: string[] | string) { | ||
url = (url instanceof Array) ? url : [url]; | ||
let fullUrl = ""; | ||
let fullUrl = "" | ||
url.forEach((urlPart) => { | ||
if (urlPart.charAt(urlPart.length - 1) === dir) urlPart = urlPart.substring(0, urlPart.length - 1); | ||
fullUrl += urlPart; | ||
}); | ||
if (url[0].substring(0, 4) !== "http") fullUrl = apiUrl + fullUrl; | ||
return fullUrl; | ||
if (endsWithSlash(urlPart)) urlPart = urlPart.substring(0, urlPart.length - 1) | ||
fullUrl += urlPart + "/" | ||
}) | ||
fullUrl = fullUrl.substring(0, fullUrl.length - 1); | ||
if (!startsWithHTTPS(fullUrl)) fullUrl = apiUrl + fullUrl; | ||
return fullUrl | ||
} | ||
@@ -196,0 +211,0 @@ |
{ | ||
"name": "ajaon", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Asynchron js and json. A tiny, fetch based http client for the web.", | ||
@@ -11,3 +11,4 @@ "main": "app/dist/ajaon.js", | ||
"deploy": "npm run build && npm publish", | ||
"test": "node ./test/dist/test-bundle" | ||
"test": "node ./test/dist/test-bundle", | ||
"dev-backend": "nodemon server" | ||
}, | ||
@@ -38,2 +39,4 @@ "repository": { | ||
"cssnano": "^4.1.8", | ||
"express": "^4.17.1", | ||
"nodemon": "^2.0.2", | ||
"postcss-loader": "^3.0.0", | ||
@@ -51,4 +54,5 @@ "terser-webpack-plugin": "^1.2.2", | ||
"dependencies": { | ||
"get-base-url": "^1.0.1", | ||
"tiny-clone": "^1.0.0" | ||
} | ||
} |
import ajaon from "./../../app/src/ajaon" | ||
//const testElem = document.querySelector("#test") | ||
ajaon() | ||
let ajax = ajaon(); | ||
(async () => { | ||
console.log(await ajax.post("log", {ok: "ok"})) | ||
})() | ||
Sorry, the diff of this file is not supported yet
20962
13
473
2
16
+ Addedget-base-url@^1.0.1
+ Addedbrowser-or-node@1.3.0(transitive)
+ Addedget-base-url@1.0.2(transitive)