fable-compiler / fable-fetch Goto Github PK
View Code? Open in Web Editor NEWFable bindings for Browsers' Fetch API
License: MIT License
Fable bindings for Browsers' Fetch API
License: MIT License
It looks like the response gets turned into a string for all response codes other than 2xx
:
Line 379 in b79a153
However, I need to handle HTTP codes differently in my app.
How can I get the HTTP code for error cases (e.g. 401
)?
I wanted to be able to abort fetches while using these bindings but realised it doesn't have support for the signal property. I made a start at implementing it however I realised its AbortController
hasn't been implemented in Node.js as far as I can tell so isn't possible to test it. Does this browser specific binding belong in this library or are you trying to keep it as generic as possible? If it doesn't belong here what would be a good approach to making it available to use?
https://developer.mozilla.org/en-US/docs/Web/API/AbortController
Fetch tests
√ fetch: requests work (89ms)
√ fetch: parallel requests work (176ms)
√ fetch: unsuccessful HTTP status codes throw an error
√ tryFetch: unsuccessful HTTP status codes returns an Error
√ tryFetch: Successful HTTP OPTIONS request (1689ms)
√ tryFetch: Failed HTTP OPTIONS request on Fable.io server
√ tryFetch: exceptions return an Error
√ tryOptionsRequest: Successful HTTP OPTIONS request (1522ms)
√ tryOptionsRequest: Failed HTTP OPTIONS request on Fable.io server
√ fetchAs: works with manual decoder (393ms)
√ fetchAs: fails if the decoder fail (659ms)
√ fetchAs: works with auto decoder (384ms)
√ fetchAs: fails if the auto decoder fail (377ms)
√ tryFetchAs: works with manual decoder (474ms)
√ tryFetchAs: fails if the decoder fail (687ms)
√ tryFetchAs: works with auto decoder (151ms)
√ tryFetchAs: fails if the auto decoder fail (130ms)
17 passing (7s)
Hi, I noticed that form data support was commented out in one of the beta commits. Do you have any plans on putting it back in? Any recommendations what to use instead when you need to support form data?
It is very hard to find documentation for Fable which negatively affects adopting it. Please add how-to guides for basic things like Fetch/Ajax.
The simple and banal switch of the alias for BodyInit from U2 to U3 broke Thoth.Fetch.
Fix is here: thoth-org/Thoth.Fetch#3
Fetch.fs?8131:379 Uncaught (in promise) Error: 0 for URL
at a (Fetch.fs?8131:379)
When I try to run function (based on sample from Fable):
let getAutoComplete (state: State) =
let url = match state.FromBase with
| true -> sprintf "%s/api/v1/base_terms/?text=%s&translation_language_id=%i&page=1&per_page=5"
Domain state.SearchText state.CurrentLanguage.Value.id
| false -> sprintf "%s/api/v1/translations/?text=%s&base_term_language_id=%i&page=1&per_page=5¤t=true"
Domain state.SearchText state.CurrentBaseLanguage.Value.id
fetch url [Types.RequestProperties.Mode Types.RequestMode.Nocors] // use the fetch api to load our resource
|> Promise.bind (fun res -> res.text()) // get the resul
|> Promise.map (fun txt -> // bind the result to make further operation
printfn "Woof! Woof! %s" txt
let decoded = Decode.Auto.fromString<Page<ResultTranslation>> (txt, true)
match decoded with
| Ok translationPage -> // everything went well! great!
let actualDogURL = translationPage.pages
printfn "Woof! Woof! %i" actualDogURL
| Error decodingError -> // oh the decoder encountered an error. The string that was sent back from our fetch request does not map well to our PictureInfo type.
failwith (sprintf "was unable to decode: %s. Reason: %s" txt decodingError)
)
Proper URL is: https://slownik-oriin.kropleduszy.pl:8000/api/v1/base_terms/?text=&translation_language_id=1&page=1&per_page=5
Whole code:
namespace OrinDic
open Feliz
open Elmish
open OrinDic.Models
open Fable.Core.JsInterop
open Fable.Import
open Fetch
open Thoth.Json
module App =
type State = {
SearchText : string
SearchPage : int
ItemsPerPage : int
MaxSearchPage : int
SearchResultsCount : int
CurrentLanguage : Language option
CurrentBaseLanguage : Language option
Languages : Language list
BaseLanguages : Language list
Users : obj list
Translations : Translation list
TranslationPage : bool
TranslationsPage : Page<ResultTranslation> option
Loading : bool
FromBase : bool
ShowKeyboard : bool
AutoCompletes : ResultTranslation list
}
type Msg =
| Increment
| Decrement
let [<Literal>] Domain = "https://slownik-oriin.kropleduszy.pl:8000"
let init() =
let plLanguage = {
id = 1
name = "Polski"
code="PL"
special_characters = []}
{
SearchText = ""
SearchPage = 1
ItemsPerPage = 100
MaxSearchPage = 0
SearchResultsCount = 0
CurrentLanguage = Some plLanguage
CurrentBaseLanguage = Some plLanguage
Languages = []
BaseLanguages = []
Users = []
Translations = []
TranslationPage = false
TranslationsPage = None
Loading = true
FromBase = true
ShowKeyboard = false
AutoCompletes = []
}, Cmd.none
let getAutoComplete (state: State) =
let url = match state.FromBase with
| true -> sprintf "%s/api/v1/base_terms/?text=%s&translation_language_id=%i&page=1&per_page=5"
Domain state.SearchText state.CurrentLanguage.Value.id
| false -> sprintf "%s/api/v1/translations/?text=%s&base_term_language_id=%i&page=1&per_page=5¤t=true"
Domain state.SearchText state.CurrentBaseLanguage.Value.id
fetch url [Types.RequestProperties.Mode Types.RequestMode.Nocors] // use the fetch api to load our resource
|> Promise.bind (fun res -> res.text()) // get the resul
|> Promise.map (fun txt -> // bind the result to make further operation
printfn "Woof! Woof! %s" txt
let decoded = Decode.Auto.fromString<Page<ResultTranslation>> (txt, true)
match decoded with
| Ok translationPage -> // everything went well! great!
let actualDogURL = translationPage.pages
printfn "Woof! Woof! %i" actualDogURL
| Error decodingError -> // oh the decoder encountered an error. The string that was sent back from our fetch request does not map well to our PictureInfo type.
failwith (sprintf "was unable to decode: %s. Reason: %s" txt decodingError)
)
let update (msg: Msg) (state: State) =
match msg with
| Increment ->
let a = getAutoComplete state
{ state with SearchPage = state.SearchPage + 1 }, Cmd.none
| Decrement -> { state with SearchPage = state.SearchPage - 1 }, Cmd.none
let render (state: State) (dispatch: Msg -> unit) =
Html.div [
Html.button [
prop.onClick (fun _ -> dispatch Increment)
prop.text "Increment me"
]
Html.button [
prop.onClick (fun _ -> dispatch Decrement)
prop.text "Decrement"
]
Html.h1 state.SearchPage
]
namespace OrinDic
module Models =
type public BaseTerm =
{
id : int
language_id : int
slug : string
name : string
synonyms : obj list
definition : string
last_edit_id : int
}
type public Translation =
{
id : int
base_term_id : int
language_id : int
current : bool
name : string
synonyms : obj list
definition : string
last_edit_id : int
last_approval_id : obj
}
type public ResultTranslation =
{
base_term : BaseTerm
translation : Translation
}
type public Language =
{
id : int
name : string
code : string
special_characters : char list
}
type public Page<'a> =
{
pages : int
count : int
current_page : int
results : 'a list
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.