Skip to content

Commit

Permalink
feat(browser): browser requests are now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Nov 23, 2022
1 parent f2d5d34 commit 3315a22
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
16 changes: 14 additions & 2 deletions lib/core/api/conclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DispatchedResponse, RequestAPIResponse } from "../../types";

import { deepMerge, clone, isEmpty } from "../../utils";
import { DEFAULT_URBEX_RESPONSE } from "../constants";
import { environment } from "../../environment";
import { UrbexHeaders } from "../../core/headers";

type ConcludeRequest = (config: RequestAPIResponse) => Promise<DispatchedResponse>;

Expand Down Expand Up @@ -46,8 +48,18 @@ export async function startRequest(config: InternalConfiguration): Promise<Concl

if (incomingResult.response) {
response.headers = incomingResult.response.headers;
response.status = incomingResult.response.statusCode;
response.statusText = incomingResult.response.statusMessage;

if (environment.isNode) {
response.status = incomingResult.response.statusCode;
response.statusText = incomingResult.response.statusMessage;
} else {
const parsedHeaders = UrbexHeaders.parse(response.headers);

response.headers = parsedHeaders;

response.status = incomingResult.response.status;
response.statusText = incomingResult.response.statusText;
}
}

if (!isEmpty(config.pipelines.response)) {
Expand Down
60 changes: 57 additions & 3 deletions lib/core/api/xhr.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
import type { InternalConfiguration } from "../../exportable-types";
import type { DispatchedResponse, UrbexRequestApi, DispatchedAPIRequest } from "../../types";
import type {
DispatchedResponse,
UrbexRequestApi,
DispatchedAPIRequest,
ResponseTypes
} from "../../types";

import { createEmptyScheme } from "../../utils";
import { UrbexError, TimeoutError } from "../error";
import { createEmptyScheme, uppercase, forEach, isUndefined, merge } from "../../utils";

type BrowserResponseTypes = "arraybuffer" | "blob" | "document" | "json" | "text";

const BROWSER_RESPONSE_TYPES = ["arraybuffer", "blob", "document", "json", "text"];

export class BrowserRequest implements UrbexRequestApi {
public send(config: InternalConfiguration): DispatchedAPIRequest {
return new Promise(() => {});
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();

request.open(uppercase(config.method), config.url.href, true);

if (BROWSER_RESPONSE_TYPES.includes(config.responseType)) {
request.responseType = config.responseType as BrowserResponseTypes;
}

if (isUndefined(config.data)) {
config.headers.delete("Content-Type");
}

forEach(config.headers.get(), (key, value) => {
request.setRequestHeader(key, value);
});

if (config.timeout) {
request.timeout = config.timeout;
}

request.ontimeout = function () {};

request.onabort = function () {};

request.onerror = function () {};

// https://plnkr.co/edit/ycQbBr0vr7ceUP2p6PHy?p=preview&preview

request.onload = function () {
resolve({
data: request.response || request.responseText,
request: request,
response: {
status: request.status,
statusText: request.statusText,
headers: request.getAllResponseHeaders()
}
});
};

request.onreadystatechange = function () {};

request.send(config.data);
});
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
capitalize,
argumentIsNotProvided,
isEmpty,
isString,
lowercase,
stringReplacer
} from "../utils";
Expand Down Expand Up @@ -103,7 +104,7 @@ export class UrbexHeaders {
* Parse a headers string into an object
*/
static parse(headers: string): NormalizedHeaders {
if (argumentIsNotProvided(headers)) {
if (argumentIsNotProvided(headers) || !isString(headers)) {
return {};
}

Expand Down

0 comments on commit 3315a22

Please sign in to comment.