Skip to content

Use polymorphic variants for some Request properties #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 39 additions & 115 deletions src/Webapi/Webapi__Fetch.res
Original file line number Diff line number Diff line change
Expand Up @@ -166,105 +166,33 @@ let decodeRequestDestination = x =>
| e => raise(Failure("Unknown requestDestination: " ++ e))
}

type requestMode =
| Navigate
| SameOrigin
| NoCORS
| CORS
let encodeRequestMode = x =>
/* internal */

switch x {
| Navigate => "navigate"
| SameOrigin => "same-origin"
| NoCORS => "no-cors"
| CORS => "cors"
}
let decodeRequestMode = x =>
/* internal */

switch x {
| "navigate" => Navigate
| "same-origin" => SameOrigin
| "no-cors" => NoCORS
| "cors" => CORS
| e => raise(Failure("Unknown requestMode: " ++ e))
}

type requestCredentials =
| Omit
| SameOrigin
| Include
let encodeRequestCredentials = x =>
/* internal */

switch x {
| Omit => "omit"
| SameOrigin => "same-origin"
| Include => "include"
}
let decodeRequestCredentials = x =>
/* internal */

switch x {
| "omit" => Omit
| "same-origin" => SameOrigin
| "include" => Include
| e => raise(Failure("Unknown requestCredentials: " ++ e))
}

type requestCache =
| Default
| NoStore
| Reload
| NoCache
| ForceCache
| OnlyIfCached
let encodeRequestCache = x =>
/* internal */

switch x {
| Default => "default"
| NoStore => "no-store"
| Reload => "reload"
| NoCache => "no-cache"
| ForceCache => "force-cache"
| OnlyIfCached => "only-if-cached"
}
let decodeRequestCache = x =>
/* internal */

switch x {
| "default" => Default
| "no-store" => NoStore
| "reload" => Reload
| "no-cache" => NoCache
| "force-cache" => ForceCache
| "only-if-cached" => OnlyIfCached
| e => raise(Failure("Unknown requestCache: " ++ e))
}

type requestRedirect =
| Follow
| Error
| Manual
let encodeRequestRedirect = x =>
/* internal */

switch x {
| Follow => "follow"
| Error => "error"
| Manual => "manual"
}
let decodeRequestRedirect = x =>
/* internal */

switch x {
| "follow" => Follow
| "error" => Error
| "manual" => Manual
| e => raise(Failure("Unknown requestRedirect: " ++ e))
}
type requestMode = [
| #navigate
| #"same-origin"
| #"no-cors"
| #cors
]

type requestCredentials = [
| #omit
| #"same-origin"
| #"include"
]

type requestCache = [
| #default
| #"no-store"
| #reload
| #"no-cache"
| #"force-cache"
| #"only-if-cached"
]

type requestRedirect = [
| #follow
| #error
| #manual
]

module HeadersInit = {
type t = headersInit
Expand Down Expand Up @@ -337,10 +265,10 @@ module RequestInit = {
~body: bodyInit=?,
~referrer: string=?,
~referrerPolicy: string=?,
~mode: string=?,
~credentials: string=?,
~cache: string=?,
~redirect: string=?,
~mode: requestMode=?,
~credentials: requestCredentials=?,
~cache: requestCache=?,
~redirect: requestRedirect=?,
~integrity: string=?,
~keepalive: bool=?,
~signal: signal=?,
Expand All @@ -366,10 +294,10 @@ module RequestInit = {
~body?,
~referrer?,
~referrerPolicy=encodeReferrerPolicy(referrerPolicy),
~mode=?map(encodeRequestMode, mode),
~credentials=?map(encodeRequestCredentials, credentials),
~cache=?map(encodeRequestCache, cache),
~redirect=?map(encodeRequestRedirect, redirect),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea this =?map syntax was valid 🤨 this was just how it converted from the original OCaml. Looks like it's a convenient way to pass an optional function return value as an optional method argument 🤔

Learn something new every day, I guess!

~mode?,
~credentials?,
~cache?,
~redirect?,
~integrity,
~keepalive?,
~signal?,
Expand Down Expand Up @@ -399,14 +327,10 @@ module Request = {
@get external referrer: t => string = "referrer"
@get external referrerPolicy: t => string = "referrerPolicy"
let referrerPolicy: t => referrerPolicy = self => decodeReferrerPolicy(referrerPolicy(self))
@get external mode: t => string = "mode"
let mode: t => requestMode = self => decodeRequestMode(mode(self))
@get external credentials: t => string = "credentials"
let credentials: t => requestCredentials = self => decodeRequestCredentials(credentials(self))
@get external cache: t => string = "cache"
let cache: t => requestCache = self => decodeRequestCache(cache(self))
@get external redirect: t => string = "redirect"
let redirect: t => requestRedirect = self => decodeRequestRedirect(redirect(self))
@get external mode: t => requestMode = "mode"
@get external credentials: t => requestCredentials = "credentials"
@get external cache: t => requestCache = "cache"
@get external redirect: t => requestRedirect = "redirect"
@get external integrity: t => string = "integrity"
@get external keepalive: t => bool = "keepalive"
@get external signal: t => signal = "signal"
Expand Down
52 changes: 28 additions & 24 deletions src/Webapi/Webapi__Fetch.resi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AbortController = Webapi__Fetch__AbortController

/** Alias for anyone upgrading */
/* * Alias for anyone upgrading */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of ReScript support for docs comments is annoying. Maybe we should just delete this comment now 🤷 and arguably the alias, too, since this will be going onto the 2.0 branch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to delete the aliases in a followup PR when all of these are merged as I don't fancy like dealing with the conflicts 😆

module FormData = Webapi__FormData
type body
type bodyInit
Expand Down Expand Up @@ -74,29 +74,33 @@ type requestDestination =
| Worker
| Xslt

type requestMode =
| Navigate
| SameOrigin
| NoCORS
| CORS

type requestCredentials =
| Omit
| SameOrigin
| Include

type requestCache =
| Default
| NoStore
| Reload
| NoCache
| ForceCache
| OnlyIfCached

type requestRedirect =
| Follow
| Error
| Manual
type requestMode = [
| #navigate
| #"same-origin"
| #"no-cors"
| #cors
]

type requestCredentials = [
| #omit
| #"same-origin"
| #"include"
]

type requestCache = [
| #default
| #"no-store"
| #reload
| #"no-cache"
| #"force-cache"
| #"only-if-cached"
]

type requestRedirect = [
| #follow
| #error
| #manual
]

module HeadersInit: {
type t = headersInit
Expand Down