|
14 | 14 | - [High-level architecture](#high-level-architecture)
|
15 | 15 | - [Options hierarchy](#options-hierarchy)
|
16 | 16 | - [Datatypes](#datatypes)
|
17 |
| -[Non-astra support](#non-astra-support) |
18 |
| -- [Non-standard environment support](#non-standard-environment-support) |
19 |
| - - [HTTP/2 with minification](#http2-with-minification) |
20 |
| - - [Browser support](#browser-support) |
| 17 | +- [Non-astra support](#non-astra-support) |
| 18 | +- [Browser support](#browser-support) |
| 19 | +- [Using HTTP/2](#using-http2) |
| 20 | +- [Examples](#examples) |
21 | 21 |
|
22 | 22 | ## Quickstart
|
23 | 23 |
|
@@ -315,78 +315,43 @@ as necessary, depending on the Data API backend. Tokens may even be omitted if n
|
315 | 315 |
|
316 | 316 | (See `examples/non-astra-backends` for a full example of this in action.)
|
317 | 317 |
|
318 |
| -## Non-standard environment support |
| 318 | +## Browser support |
319 | 319 |
|
320 |
| -`astra-db-ts` is designed first and foremost to work in Node.js environments. |
321 |
| - |
322 |
| -However, it will work in edge runtimes and other non-node environments as well, though it may use the native `fetch` API for HTTP |
323 |
| -requests, as opposed to `fetch-h2` which provides extended HTTP/2 and HTTP/1.1 support for performance. |
324 |
| - |
325 |
| -By default, it'll attempt to use `fetch-h2` if available, and fall back to `fetch` if not available in that environment. |
326 |
| -You can explicitly force the fetch implementation when instantiating the client: |
| 320 | +`astra-db-ts` is designed to work in server-side environments, but it can technically work in the browser as well. |
327 | 321 |
|
328 |
| -```typescript |
329 |
| -import { DataAPIClient } from '@datastax/astra-db-ts'; |
| 322 | +However, if, for some reason, you really want to use this in a browser, you may need to install the `events` polyfill, |
| 323 | +and possibly set up a CORS proxy (such as [CORS Anywhere](https://github.com/Rob--W/cors-anywhere)) to forward requests to the Data API. |
330 | 324 |
|
331 |
| -const client = new DataAPIClient('*TOKEN*', { |
332 |
| - httpOptions: { client: 'fetch' }, |
333 |
| -}); |
334 |
| -``` |
| 325 | +But keep in mind that this may be very insecure, especially if you're hardcoding sensitive data into your client-side |
| 326 | +code, as it's trivial for anyone to inspect the code and extract the token (through XSS attacks or otherwise). |
335 | 327 |
|
336 |
| -There are four different behaviours for setting the client: |
337 |
| -- Not setting the `httpOptions` at all |
338 |
| - - This will attempt to use `fetch-h2` if available, and fall back to `fetch` if not available |
339 |
| -- `client: 'default'` or `client: undefined` (or unset) |
340 |
| - - This will attempt to use `fetch-h2` if available, and throw an error if not available |
341 |
| -- `client: 'fetch'` |
342 |
| - - This will always use the native `fetch` API |
343 |
| -- `client: 'custom'` |
344 |
| - - This will allow you to pass a custom `Fetcher` implementation to the client |
| 328 | +See [`examples/browser`](./examples/browser) for a full example of browser usage in action, and steps on how to use `astra-db-ts` in your own browser application. |
345 | 329 |
|
346 |
| -On some environments, such as Cloudflare Workers, you may additionally need to use the events |
347 |
| -polyfill for the client to work properly (i.e. `npm i events`). Cloudflare's node-compat won't |
348 |
| -work here. |
| 330 | +## Using HTTP/2 |
349 | 331 |
|
350 |
| -Check out the `examples/` subdirectory for some non-standard runtime examples with more info. |
| 332 | +`astra-db-ts` uses the native `fetch` API by default, but it can also work with `HTTP/2` using the `fetch-h2` module. |
351 | 333 |
|
352 |
| -### HTTP/2 with minification |
| 334 | +However, due to compatability reasons, `fetch-h2` is no longer dynamically imported by default, and must be provided to the client manually. |
353 | 335 |
|
354 |
| -Due to the variety of different runtimes JS can run in, `astra-db-ts` does its best to be as flexible as possible. |
355 |
| -Unfortunately however, because we need to dynamically require the `fetch-h2` module to test whether it works, the |
356 |
| -dynamic import often breaks in minified environments, even if the runtime properly supports HTTP/2. |
| 336 | +Luckily, it is only a couple of easy steps to get `HTTP/2` working in your project: |
357 | 337 |
|
358 |
| -There is a simple workaround however, consisting of the following steps, if you really want to use HTTP/2: |
359 |
| -1. Install `fetch-h2` as a dependency (`npm i fetch-h2`) |
360 |
| -2. Import the `fetch-h2` module in your code as `fetchH2` (i.e. `import * as fetchH2 from 'fetch-h2'`) |
361 |
| -3. Set the `httpOptions.fetchH2` option to the imported module when instantiating the client |
| 338 | +1. Install `fetch-h2` by running `npm i fetch-h2`. |
| 339 | +2. Provide `fetch-h2` to the client. |
362 | 340 |
|
363 |
| -```typescript |
364 |
| -import { DataAPIClient } from '@datastax/astra-db-ts'; |
| 341 | +```ts |
365 | 342 | import * as fetchH2 from 'fetch-h2';
|
| 343 | +// or `const fetchH2 = require('fetch-h2');` |
366 | 344 |
|
367 |
| -const client = new DataAPIClient('*TOKEN*', { |
368 |
| - httpOptions: { fetchH2 }, |
| 345 | +const client = new DataAPIClient({ |
| 346 | + httpOptions: { |
| 347 | + client: 'fetch-h2', |
| 348 | + fetchH2: fetchH2, |
| 349 | + }, |
369 | 350 | });
|
370 | 351 | ```
|
371 | 352 |
|
372 |
| -This way, the dynamic import is avoided, and the client will work in minified environments. |
373 |
| - |
374 |
| -Note this is not required if you don't explicitly need HTTP/2 support, as the client will default |
375 |
| -to the native fetch implementation instead if importing fails. |
376 |
| - |
377 |
| -(But keep in mind this defaulting will only happen if `httpOptions` is not set at all). |
| 353 | +See the [using HTTP/2 example](./examples/using-http2) for a full example of this in action, and more information on how to use `astra-db-ts` with `HTTP/2`. |
378 | 354 |
|
379 |
| -(See `examples/http2-when-minified` for a full example of this workaround in action.) |
380 |
| - |
381 |
| -### Browser support |
382 |
| - |
383 |
| -`astra-db-ts` is designed to work in server-side environments, but it can technically work in the browser as well. |
384 |
| - |
385 |
| -However, if, for some reason, you really want to use this in a browser, you may need to install the `events` polyfill, |
386 |
| -and possibly set up a CORS proxy (such as [CORS Anywhere](https://github.com/Rob--W/cors-anywhere)) to forward requests |
387 |
| -to the Data API. |
388 |
| - |
389 |
| -But keep in mind that this may be very insecure, especially if you're hardcoding sensitive data into your client-side |
390 |
| -code, as it's trivial for anyone to inspect the code and extract the token (through XSS attacks or otherwise). |
| 355 | +## Examples |
391 | 356 |
|
392 |
| -(See `examples/browser` for a full example of browser usage in action.) |
| 357 | +Check out the [examples](./examples) directory for more examples on how to use `astra-db-ts` in your own projects. |
0 commit comments