thepassle / app-tools

128 stars 8 forks source link

Use globalThis instead of window for globals #2

Closed jfsiii closed 2 years ago

jfsiii commented 2 years ago

Summary

If you try to use this in non-browser runtimes, like node and Cloudflare Workers, you'll get an Uncaught ReferenceError: window is not defined error

Use globalThis instead of window and the code will use the global fetch if it's available; just like in the browser

Out of scope

There's another blocker for using outside the browser. https://github.com/thepassle/app-tools/blob/630152938fb9d94b4f78c160ddfd089e40625fb3/api/index.js#L1 uses document

I worked around this locally by commenting out https://github.com/thepassle/app-tools/blob/399532e22113987436980c6fb5ed0c4721fa1430/api/index.js#L92 and https://github.com/thepassle/app-tools/blob/399532e22113987436980c6fb5ed0c4721fa1430/api/index.js#L101

Perhaps getCookie could become a plugin, param, or some other way the consumer could say how to access/manage the cookie?

thepassle commented 2 years ago

The xsrf cookie/header can definitely be a plugin, good catch 🙂 im not at a laptop right now but ill take a look at that tomorrow

thepassle commented 2 years ago

Published and extracted the xsrf stuff to a plugin 🙂

Would love to see how youre using the api client btw, if it happens to be open source.