Skip to content
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

Change fetch method to a string #65

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
6 changes: 3 additions & 3 deletions examples/fetch_examples.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let _ = {
}

let _ = {
Fetch.fetchWithInit("/api/hello", Fetch.RequestInit.make(~method_=Post, ()))
Fetch.fetchWithInit("/api/hello", Fetch.RequestInit.make(~method="POST", ()))
->then(Fetch.Response.text)
->then(text => print_endline(text)->resolve)
}
Expand All @@ -31,7 +31,7 @@ let _ = {
Fetch.fetchWithInit(
"/api/hello",
Fetch.RequestInit.make(
~method_=Post,
~method="POST",
~body=Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))),
~headers=Fetch.HeadersInit.make({"Content-Type": "application/json"}),
(),
Expand All @@ -51,7 +51,7 @@ let _ = {
Fetch.fetchWithInit(
"/api/upload",
Fetch.RequestInit.make(
~method_=Post,
~method="POST",
~body=Fetch.BodyInit.makeWithFormData(formData),
~headers=Fetch.HeadersInit.make({"Accept": "*"}),
(),
Expand Down
6 changes: 3 additions & 3 deletions lib/js/examples/fetch_examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fetch("/api/hellos/1").then(function (prim) {
return Promise.resolve((console.log(text), undefined));
});

fetch("/api/hello", Webapi__Fetch.RequestInit.make(/* Post */2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
fetch("/api/hello", Webapi__Fetch.RequestInit.make("POST", undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
return prim.text();
}).then(function (text) {
return Promise.resolve((console.log(text), undefined));
Expand All @@ -33,7 +33,7 @@ var payload = {};

payload["hello"] = "world";

fetch("/api/hello", Webapi__Fetch.RequestInit.make(/* Post */2, {
fetch("/api/hello", Webapi__Fetch.RequestInit.make("POST", {
"Content-Type": "application/json"
}, Caml_option.some(JSON.stringify(payload)), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
return prim.json();
Expand All @@ -47,7 +47,7 @@ formData.append("image0", {
name: "image0.jpg"
}, undefined);

fetch("/api/upload", Webapi__Fetch.RequestInit.make(/* Post */2, {
fetch("/api/upload", Webapi__Fetch.RequestInit.make("POST", {
Accept: "*"
}, Caml_option.some(formData), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
return prim.json();
Expand Down
50 changes: 4 additions & 46 deletions src/Webapi/Webapi__Fetch.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,6 @@ type urlSearchParams /* URL */
type blob = Webapi__Blob.t
type file = Webapi__File.t

type requestMethod =
| Get
| Head
| Post
| Put
| Delete
| Connect
| Options
| Trace
| Patch
| Other(string)
let encodeRequestMethod = x =>
/* internal */

switch x {
| Get => "GET"
| Head => "HEAD"
| Post => "POST"
| Put => "PUT"
| Delete => "DELETE"
| Connect => "CONNECT"
| Options => "OPTIONS"
| Trace => "TRACE"
| Patch => "PATCH"
| Other(method_) => method_
}
let decodeRequestMethod = x =>
/* internal */

switch x {
| "GET" => Get
| "HEAD" => Head
| "POST" => Post
| "PUT" => Put
| "DELETE" => Delete
| "CONNECT" => Connect
| "OPTIONS" => Options
| "TRACE" => Trace
| "PATCH" => Patch
| method_ => Other(method_)
}

type referrerPolicy =
| None
Expand Down Expand Up @@ -332,7 +291,7 @@ module RequestInit = {

@obj
external make: (
~_method: string=?,
~method: string=?,
~headers: headersInit=?,
~body: bodyInit=?,
~referrer: string=?,
Expand All @@ -347,7 +306,7 @@ module RequestInit = {
unit,
) => requestInit = ""
let make = (
~method_: option<requestMethod>=?,
~method: option<string>=?,
~headers: option<headersInit>=?,
~body: option<bodyInit>=?,
~referrer: option<string>=?,
Expand All @@ -361,7 +320,7 @@ module RequestInit = {
~signal: option<signal>=?,
) =>
make(
~_method=?map(encodeRequestMethod, method_),
~method?,
~headers?,
~body?,
~referrer?,
Expand All @@ -388,8 +347,7 @@ module Request = {
@new external makeWithRequest: t => t = "Request"
@new external makeWithRequestInit: (t, requestInit) => t = "Request"

@get external method_: t => string = "method"
let method_: t => requestMethod = self => decodeRequestMethod(method_(self))
@get external method: t => string = "method"
@get external url: t => string = "url"
@get external headers: t => headers = "headers"
@get external type_: t => string = "type"
Expand Down
15 changes: 2 additions & 13 deletions src/Webapi/Webapi__Fetch.resi
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ type signal = AbortController.signal
type blob
type file

type requestMethod =
| Get
| Head
| Post
| Put
| Delete
| Connect
| Options
| Trace
| Patch
| Other(string)

type referrerPolicy =
| None
Expand Down Expand Up @@ -151,7 +140,7 @@ module RequestInit: {
type t = requestInit

let make: (
~method_: requestMethod=?,
~method: string=?,
~headers: headersInit=?,
~body: bodyInit=?,
~referrer: string=?,
Expand All @@ -175,7 +164,7 @@ module Request: {
@new external makeWithRequest: t => t = "Request"
@new external makeWithRequestInit: (t, requestInit) => t = "Request"

let method_: t => requestMethod
@get external method: t => string = "method"
@get external url: t => string = "url"
@get external headers: t => headers = "headers"
let type_: t => requestType
Expand Down