Current section

Files

Jump to
gossamer src gossamer.ffi.mjs
Raw

src/gossamer.ffi.mjs

// src/gossamer/request_init.ts
import * as $requestInit from "./gossamer/request_init.mjs";
// src/gossamer/http_method.ts
import * as $hm from "./gossamer/http_method.mjs";
function toHttpMethod(value) {
if ($hm.HttpMethod$isConnect(value)) return "CONNECT";
if ($hm.HttpMethod$isDelete(value)) return "DELETE";
if ($hm.HttpMethod$isGet(value)) return "GET";
if ($hm.HttpMethod$isHead(value)) return "HEAD";
if ($hm.HttpMethod$isOptions(value)) return "OPTIONS";
if ($hm.HttpMethod$isPatch(value)) return "PATCH";
if ($hm.HttpMethod$isPost(value)) return "POST";
if ($hm.HttpMethod$isPut(value)) return "PUT";
if ($hm.HttpMethod$isOther(value)) return $hm.HttpMethod$Other$0(value);
return "TRACE";
}
// src/gossamer/referrer_policy.ts
import * as $rp from "./gossamer/referrer_policy.mjs";
function toReferrerPolicy(value) {
if ($rp.ReferrerPolicy$isNoReferrer(value)) return "no-referrer";
if ($rp.ReferrerPolicy$isNoReferrerWhenDowngrade(value)) {
return "no-referrer-when-downgrade";
}
if ($rp.ReferrerPolicy$isOrigin(value)) return "origin";
if ($rp.ReferrerPolicy$isOriginWhenCrossOrigin(value)) {
return "origin-when-cross-origin";
}
if ($rp.ReferrerPolicy$isSameOrigin(value)) return "same-origin";
if ($rp.ReferrerPolicy$isStrictOrigin(value)) return "strict-origin";
if ($rp.ReferrerPolicy$isUnsafeUrl(value)) return "unsafe-url";
if ($rp.ReferrerPolicy$isOther(value)) {
return $rp.ReferrerPolicy$Other$0(value);
}
return "strict-origin-when-cross-origin";
}
// src/gossamer/request_cache.ts
import * as $requestCache from "./gossamer/request_cache.mjs";
function toRequestCache(value) {
if ($requestCache.RequestCache$isForceCache(value)) return "force-cache";
if ($requestCache.RequestCache$isNoCache(value)) return "no-cache";
if ($requestCache.RequestCache$isNoStore(value)) return "no-store";
if ($requestCache.RequestCache$isOnlyIfCached(value)) return "only-if-cached";
if ($requestCache.RequestCache$isReload(value)) return "reload";
if ($requestCache.RequestCache$isOther(value)) {
return $requestCache.RequestCache$Other$0(value);
}
return "default";
}
// src/gossamer/request_credentials.ts
import * as $rc from "./gossamer/request_credentials.mjs";
function toRequestCredentials(value) {
if ($rc.RequestCredentials$isInclude(value)) return "include";
if ($rc.RequestCredentials$isOmit(value)) return "omit";
if ($rc.RequestCredentials$isOther(value)) {
return $rc.RequestCredentials$Other$0(value);
}
return "same-origin";
}
// src/gossamer/request_mode.ts
import * as $requestMode from "./gossamer/request_mode.mjs";
function toRequestMode(value) {
if ($requestMode.RequestMode$isNavigate(value)) return "navigate";
if ($requestMode.RequestMode$isNoCors(value)) return "no-cors";
if ($requestMode.RequestMode$isSameOrigin(value)) return "same-origin";
if ($requestMode.RequestMode$isOther(value)) {
return $requestMode.RequestMode$Other$0(value);
}
return "cors";
}
// src/gossamer/request_redirect.ts
import * as $rr from "./gossamer/request_redirect.mjs";
function toRequestRedirect(value) {
if ($rr.RequestRedirect$isError(value)) return "error";
if ($rr.RequestRedirect$isManual(value)) return "manual";
if ($rr.RequestRedirect$isOther(value)) {
return $rr.RequestRedirect$Other$0(value);
}
return "follow";
}
// src/gossamer/request_init.ts
function toRequestInit(options) {
const result = {};
for (const option of options) {
if ($requestInit.RequestInit$isMethod(option)) {
result.method = toHttpMethod($requestInit.RequestInit$Method$0(option));
} else if ($requestInit.RequestInit$isHeaders(option)) {
result.headers = $requestInit.RequestInit$Headers$0(option);
} else if ($requestInit.RequestInit$isBody(option)) {
result.body = $requestInit.RequestInit$Body$0(option);
} else if ($requestInit.RequestInit$isCache(option)) {
result.cache = toRequestCache(
$requestInit.RequestInit$Cache$0(option)
);
} else if ($requestInit.RequestInit$isCredentials(option)) {
result.credentials = toRequestCredentials(
$requestInit.RequestInit$Credentials$0(option)
);
} else if ($requestInit.RequestInit$isIntegrity(option)) {
result.integrity = $requestInit.RequestInit$Integrity$0(option);
} else if ($requestInit.RequestInit$isKeepalive(option)) {
result.keepalive = $requestInit.RequestInit$Keepalive$0(option);
} else if ($requestInit.RequestInit$isMode(option)) {
result.mode = toRequestMode(
$requestInit.RequestInit$Mode$0(option)
);
} else if ($requestInit.RequestInit$isRedirect(option)) {
result.redirect = toRequestRedirect(
$requestInit.RequestInit$Redirect$0(option)
);
} else if ($requestInit.RequestInit$isReferrer(option)) {
result.referrer = $requestInit.RequestInit$Referrer$0(option);
} else if ($requestInit.RequestInit$isReferrerPolicy(option)) {
result.referrerPolicy = toReferrerPolicy(
$requestInit.RequestInit$ReferrerPolicy$0(option)
);
} else if ($requestInit.RequestInit$isSignal(option)) {
result.signal = $requestInit.RequestInit$Signal$0(option);
}
}
return result;
}
// src/utils/list.ts
import {
List$Empty,
List$isNonEmpty,
List$NonEmpty,
List$NonEmpty$first,
List$NonEmpty$rest
} from "../prelude.mjs";
function toArray(list) {
const array = [];
let current = list;
while (List$isNonEmpty(current)) {
array.push(List$NonEmpty$first(current));
current = List$NonEmpty$rest(current);
}
return array;
}
// src/utils/result.ts
import { Result$Error, Result$Ok } from "../prelude.mjs";
function toResult(value) {
return value === null || value === void 0 ? Result$Error(void 0) : Result$Ok(value);
}
toResult.fromThrows = function(throws) {
try {
return Result$Ok(throws());
} catch (error) {
return Result$Error(
error instanceof Error ? error.message : String(error)
);
}
};
toResult.fromPromise = function(promise) {
return promise.then(
(value) => Result$Ok(value),
(error) => Result$Error(error instanceof Error ? error.message : String(error))
);
};
// src/gossamer.ffi.ts
var structured_clone = (value) => {
return toResult.fromThrows(() => globalThis.structuredClone(value));
};
var atob = (encoded) => {
return toResult.fromThrows(() => globalThis.atob(encoded));
};
var btoa = (data) => {
return toResult.fromThrows(() => globalThis.btoa(data));
};
var clear_interval = (id) => {
globalThis.clearInterval(id);
};
var clear_timeout = (id) => {
globalThis.clearTimeout(id);
};
var queue_microtask = (func) => {
globalThis.queueMicrotask(func);
};
var set_interval = (delay, callback) => {
return globalThis.setInterval(callback, delay);
};
var set_timeout = (delay, callback) => {
return globalThis.setTimeout(callback, delay);
};
var user_agent = () => {
return globalThis.navigator.userAgent;
};
var fetch_ = (url) => {
return toResult.fromPromise(globalThis.fetch(url));
};
var fetch_with_init = (url, init) => {
return toResult.fromPromise(
globalThis.fetch(url, toRequestInit(toArray(init)))
);
};
var fetch_request = (request) => {
return toResult.fromPromise(globalThis.fetch(request));
};
export {
atob,
btoa,
clear_interval,
clear_timeout,
fetch_,
fetch_request,
fetch_with_init,
queue_microtask,
set_interval,
set_timeout,
structured_clone,
user_agent
};