-
Notifications
You must be signed in to change notification settings - Fork 121
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
clearly doc requirements for lib use #34
Comments
Before we do this we need need to figure out how and if we want to bundle dependencies. How this currently works in that The other way we could do this is like this:
This method moves the responsibility to polyfill to the users application. This means a little more setup time and a little more managing of dependencies for the user, but it means that they can bring their own This also gets me thinking about something @ajturner mentioned. Should we allow passing in a different export interface IFetchableOptions {
body: FormData | string;
method: HTTPMethods;
}
export interface IFetchableResponse {
json(): Promise<object>;
text(): Promise<string>;
blob(): Promise<Blob>;
}
export type IFetchable = (
url: string,
options: IFetchableOptions
) => Promise<IFetchableResponse>; This is the subset of the // import angular deps
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
// import toPromise so we can convert the observable into a Promise to match
// the fetch spec
import 'rxjs/add/operator/toPromise';
// import IFetchable so can typecheck the implimentation
import { IFetchable } from "@esri/rest-request";
// export a service that provides a `fetch` implimentation powered by
// Angulars HTTP module
@Injectable()
export class FetchService {
constructor(private http: Http) { }
get fetch () : IFetchable {
return function (url, options) {
return this.http.request(url, options).toPromise();
}
}
}
// now in some other file we can get our FetchService and pass its fetch into
// the requests like we do for auth.
import { getItems } from "@esri/rest-portal";
@Injectable()
export class FetchService {
constructor(private fetch: FetchService) { }
getItems () {
// now we just pass our fetch implementation down the underlying @esri/rest-request.
return getItems(username, {}, {
authentication: session,
fetch: fetch.fetch
})
}
} |
sounds like there's no easy way to ship something straightforward, self-contained and ready to use and simultaneously leave a backdoor to allow folks to substitute comparable dependencies from their framework or alternate lib of choice. i don't have a strong opinion about this but my gut instinct is to prioritize the first use case and leave more advanced devs to their own devices. real user feedback here would be extremely helpful. |
I'll be so bold as to make a few assumptions:
|
I'm strongly in favor of not having dependencies (peer or otherwise) on any fetch/promise libraries. I think catering to the lowest common denominator ends up optimizing for legacy and penalizing everyone else. Instead I prefer @patrickarlt's proposal of relying on consumers to bring their own polyfill (yes it's that kind of party) if they need to support scenarios where built-in fetch isn't available. I looked into this kind of thing for promisifying esri-loader, and I got good suggestion on SO that proposes to go a step further and polyfill the built-ins at runtime if they are missing. I'd say that's reasonable in an app, but not the job of a library. Generally speaking, I'm in favor of optimising for evergreen browsers and pushing the burden of supporting legacy browsers to the specific scenarios where it's required. I also think it's really important to let consumers provide a specific implementation of IFetchable if, for example, they want to use their framework's fetch implementation as @patrickarlt demonstrates above. In that example, I assume that if the |
@tomwayson @jgravois I'm fine with dropping peer dependencies and just making sure the globals are available. We probably just need to advise people to so the following:
Dynamically importing polyfills is something I never really considered but I really like the idea of it because it makes using the library a no brainer. If we do do this we just have to note that we will import the polyfills from a CDN (unpkg? cdnjs?) and modify the global scope if dependencies do not exist. If users want to use specific polyfills or versions of polyfills they just have to load them before they make their first request. I agree with @tomwayson this this might fit better in an app but it makes using the library so easy that I still think we should do it. I"m going to work on the following:
|
I'd still prefer to leave the dynamic polyfilling to apps, maybe we could just include a sample app that does it, and/or a detailed doc? That said, if you're going to do it, the ideas in this article really resonated w/ me. Looks like polyfill.io is the way to go. |
Leaving this here for future reference https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,Promise&flags=always |
@tomwayson @jgravois, I'm at a bit of a sticky point here... I have automatic polyfilling working in browsers. In browsers lacking The sticky part is what the behavior should be in Node.js. You cannot do this: if (isNode) {
require('isomorphic-fetch');
} else {
// load from polyfill.io
} This means that Webpack/Rollup/Browserify will read the I'm more in favor of throwing the error. |
unless you're @patrickarlt. 😎 i think having a plug and play browser lib that polyfills only when necessary and gives advanced developers an opportunity to roll their own fetch is really awesome! and call it a double standard, but i never gave any thought to node developers in this regard because dependency management is so much less of a hassle when bundlers/frameworks aren't part of the equation. if we have doc that outlines the node specific dependencies and ship an example, just throwing an informative error seems totally reasonable to me. |
I never looped back and updated this issue. Right now we are simply relying on globals that we expect users to install but we don't throw any errors or try anything fancy if they are not there or polyfills aren't loaded. I could try to make the following work:
I'm also ok with just throwing an error that links to the get stared guide if we cant find the globals. |
Works for me. We can always add the fancy stuff later. |
@tomwayson @jgravois ok then this probably needs to be a v1 issue then. I'll wrap this up tomorrow. |
FYI - here's what I ended up doing for esri-loader: |
Add error messages for missing deps, fix #34
The text was updated successfully, but these errors were encountered: