Skip to content

Commit c2290d1

Browse files
committed
ESM support & various other compat improvements (#100)
* update eslint config * run eslint --fix + fix other linting issues * added & fixed consistent-type-* lints * migrated source code to esm * made tests run using tsx/esm import * removed hard unstable dep on fetch-h2... now defaults to plain fetch by default * removed hard unstable dep on events... now uses minimal event emitter implementaiton * added dual support for cjs & esm * use tslib * update examples to work with latest version of astra-db-ts * fixed node10 & node16:cjs type resolution issues * update examples to reflect new compat changes * update some http options documentation * update readme
1 parent 0c8d7ea commit c2290d1

File tree

320 files changed

+5315
-3860
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

320 files changed

+5315
-3860
lines changed

README.md

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
- [High-level architecture](#high-level-architecture)
1515
- [Options hierarchy](#options-hierarchy)
1616
- [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)
2121

2222
## Quickstart
2323

@@ -315,78 +315,43 @@ as necessary, depending on the Data API backend. Tokens may even be omitted if n
315315

316316
(See `examples/non-astra-backends` for a full example of this in action.)
317317

318-
## Non-standard environment support
318+
## Browser support
319319

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.
327321

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.
330324

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).
335327

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.
345329

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
349331

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.
351333

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.
353335

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:
357337

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.
362340

363-
```typescript
364-
import { DataAPIClient } from '@datastax/astra-db-ts';
341+
```ts
365342
import * as fetchH2 from 'fetch-h2';
343+
// or `const fetchH2 = require('fetch-h2');`
366344

367-
const client = new DataAPIClient('*TOKEN*', {
368-
httpOptions: { fetchH2 },
345+
const client = new DataAPIClient({
346+
httpOptions: {
347+
client: 'fetch-h2',
348+
fetchH2: fetchH2,
349+
},
369350
});
370351
```
371352

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`.
378354

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
391356

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.

api-extractor.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*
4646
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
4747
*/
48-
"mainEntryPointFilePath": "dist/index.d.ts",
48+
"mainEntryPointFilePath": "dist/esm/index.d.ts",
4949

5050
/**
5151
* A list of NPM package names whose exports should be treated as part of this package.
@@ -110,7 +110,7 @@
110110
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
111111
* DEFAULT VALUE: "<projectFolder>/tsconfig.json"
112112
*/
113-
// "tsconfigFilePath": "tsconfig.build.json",
113+
"tsconfigFilePath": "<projectFolder>/etc/tsconfig.esm.json"
114114
/**
115115
* Provides a compiler configuration that will be used instead of reading the tsconfig.json file from disk.
116116
* The object must conform to the TypeScript tsconfig schema:

eslint.config.mjs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,39 @@ import ts from 'typescript-eslint';
66

77
export default ts.config(
88
{
9-
ignores: ['dist/**/*', 'examples/**/*'],
10-
},
11-
js.configs.recommended,
12-
...ts.configs.recommended,
13-
{
9+
files: ['src/**/*.ts', 'tests/**/*.ts'],
10+
extends: [
11+
js.configs.recommended,
12+
...ts.configs.recommendedTypeChecked,
13+
...ts.configs.stylisticTypeChecked,
14+
],
1415
languageOptions: {
16+
parserOptions: {
17+
projectService: true,
18+
tsconfigRootDir: import.meta.dirname,
19+
},
1520
globals: globals.node,
1621
},
1722
rules: {
1823
// We are *way* past this point lmao
1924
'@typescript-eslint/no-explicit-any': 'off',
25+
'@typescript-eslint/no-unsafe-assignment': 'off',
26+
'@typescript-eslint/no-unsafe-argument': 'off',
27+
'@typescript-eslint/no-unsafe-call': 'off',
28+
'@typescript-eslint/no-unsafe-member-access': 'off',
29+
'@typescript-eslint/no-unsafe-return': 'off',
2030

2131
// Only way I can do indentation in ts-doc
2232
'no-irregular-whitespace': 'off',
2333

24-
// sorry.
34+
// no.
35+
'@typescript-eslint/restrict-template-expressions': 'off',
36+
37+
// sorry :(
2538
'@typescript-eslint/no-unused-expressions': 'off',
39+
'@typescript-eslint/consistent-type-assertions': 'off',
40+
'@typescript-eslint/prefer-nullish-coalescing': 'off',
41+
'@typescript-eslint/no-empty-function': 'off',
2642

2743
// Makes underscore variables not throw a fit
2844
'@typescript-eslint/no-unused-vars': ['error', {
@@ -40,10 +56,22 @@ export default ts.config(
4056
// Linting
4157
'semi': 'error',
4258
'comma-dangle': ['error', 'always-multiline'],
59+
60+
'@typescript-eslint/consistent-type-exports': 'error',
61+
'@typescript-eslint/consistent-type-imports': 'error',
62+
},
63+
},
64+
{
65+
// Laxer rules for test files
66+
files: ['tests/**/*.ts'],
67+
rules: {
68+
'@typescript-eslint/no-magic-numbers': 'off',
69+
'@typescript-eslint/require-await': 'off',
70+
'@typescript-eslint/no-implied-eval': 'off',
4371
},
4472
},
4573
{
46-
// disable type-aware linting on JS files
74+
// Disable type-aware linting on JS files
4775
files: ['**/*.*js'],
4876
...ts.configs.disableTypeChecked,
4977
rules: { 'no-undef': 'off' },

0 commit comments

Comments
 (0)