Skip to content

Commit

Permalink
Test no good work yet
Browse files Browse the repository at this point in the history
  • Loading branch information
cabraviva committed Apr 13, 2023
1 parent 2db93b1 commit 0cb10ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "dist/main.d.ts",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "tsc",
"build": "tsc && uglifyjs -o dist/main.js --compress --mangle -- dist/main.js",
"prepublishOnly": "npm run build"
},
Expand Down
74 changes: 37 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ type HTTPMethod = 'GET' | 'POST' | 'HEAD' | 'OPTIONS' | 'PUT' | 'DELETE' | 'PATC
/* @__PURE__ */ function execXHR(method: HTTPMethod, sendData: boolean, url: string, options?: RequestOptions, data?: RequestData): Promise<KnorryResponse> {
return new Promise(function (resolve, promiseReject) {
// Merge options
options = mergeObject(namespace().__knorry__.options, options || {})
options = mergeObject((namespace().__knorry__ || {}).options || {}, options || {})

var reject: Function
if (typeof options.errorHandler === 'function') {
Expand Down Expand Up @@ -294,8 +294,43 @@ type HTTPMethod = 'GET' | 'POST' | 'HEAD' | 'OPTIONS' | 'PUT' | 'DELETE' | 'PATC
}
xhr.withCredentials = options.withCredentials

var response: KnorryResponse

// Load event
xhr.addEventListener('load', function () {
// XHR was successfull
// Parse headers
const respHeaders = parseResponseHeaders(xhr.getAllResponseHeaders())

// Content-Type specific data
var data: any = xhr.responseText
if (respHeaders['content-type'] && respHeaders['content-type'] === 'application/json') {
try {
data = JSON.parse(data)
} catch (_) { }
}


// Create response
response = createKnorryResponse({
knorryError: false,
data,
headers: respHeaders,
status: xhr.status,
statusText: xhr.statusText,
serverError: xhr.status >= 500 && xhr.status < 600,
successfull: xhr.status >= 200 && xhr.status < 300,
clientError: xhr.status >= 400 && xhr.status < 500
})

resolve(response)
})

// Open request
xhr.open(method, url, true, (options.auth || {}).username, (options.auth || {}).password)

// Headers
var headers = options.headers || {}
var headers = options.headers || {}
var headerNames = Object.keys(headers)
for (var i = 0; i <= headerNames.length; i += 1) {
xhr.setRequestHeader(headerNames[i], headers[headerNames[i]])
Expand Down Expand Up @@ -385,41 +420,6 @@ type HTTPMethod = 'GET' | 'POST' | 'HEAD' | 'OPTIONS' | 'PUT' | 'DELETE' | 'PATC
}
}

var response: KnorryResponse

// Load event
xhr.addEventListener('load', function () {
// XHR was successfull
// Parse headers
const respHeaders = parseResponseHeaders(xhr.getAllResponseHeaders())

// Content-Type specific data
var data: any = xhr.responseText
if (respHeaders['content-type'] && respHeaders['content-type'] === 'application/json') {
try {
data = JSON.parse(data)
} catch (_) { }
}


// Create response
response = createKnorryResponse({
knorryError: false,
data,
headers: respHeaders,
status: xhr.status,
statusText: xhr.statusText,
serverError: xhr.status >= 500 && xhr.status < 600,
successfull: xhr.status >= 200 && xhr.status < 300,
clientError: xhr.status >= 400 && xhr.status < 500
})

resolve(response)
})

// Open request
xhr.open(method, url, true, (options.auth || {}).username, (options.auth || {}).password)

// Before send
if (typeof options.beforeSend === 'function') {
var cachedXHR = xhr
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"outDir": "./dist",
"declarationMap": true,
"sourceMap": true,
}
},
"include": [
"src/**/*"
],
"exclude": [
"src/**/*.test.*"
]
}

0 comments on commit 0cb10ac

Please sign in to comment.