Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,76 @@
# edge-js
A JavaScript client for the PlanetScale Edge API
# PlanetScale Edge client

A JavaScript client for the PlanetScale Edge API.

## Installation

```
$ npm install @planetscale/edge
```

## Usage

```ts
import { connect } from '@planetscale/edge'

const config = {
host: 'aws.connect.psdb.cloud',
username: '<user>',
password: '<password>'
}

const conn = await connect(config)
const results = await conn.execute('select 1 from dual')
console.log(results)
```

### Connection factory

Use the `Client` connection factory class to create fresh connections for each transaction or web request handler.

```ts
import { Client } from '@planetscale/edge'

const client = new Client({
host: 'aws.connect.psdb.cloud',
username: '<user>',
password: '<password>'
})

const conn = await client.connection()
const results = await conn.execute('select 1 from dual')
console.log(results)
```

### Custom fetch function

Node.js version 18 includes a built-in global `fetch` function. When using an older version of Node.js, you can provide a custom fetch function implementation. We recommend the [`undici`][1] package on which Node's built-in fetch is based.

[1]: https://github.com/nodejs/undici

```ts
import { connect } from '@planetscale/edge'
import { fetch } from 'undici'

const config = {
fetch,
host: 'aws.connect.psdb.cloud',
username: '<user>',
password: '<password>'
}

const conn = await connect(config)
const results = await conn.execute('select 1 from dual')
console.log(results)
```

## Development

```
npm install
npm test
```

## License

Distributed under the Apache 2.0 license. See LICENSE for details.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Credentials {
username: string
password: string
host: string
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>
}

export interface QueryResultRow {
Expand Down Expand Up @@ -78,6 +79,9 @@ export class Connection {
private session: QuerySession | null

constructor(credentials: Credentials) {
if (typeof fetch !== 'undefined') {
credentials = { fetch, ...credentials }
}
this.credentials = credentials
this.session = null
}
Expand All @@ -100,7 +104,7 @@ export class Connection {

private async postJSON<T>(url: string | URL, body = {}): Promise<T> {
const auth = btoa(`${this.credentials.username}:${this.credentials.password}`)
const response = await fetch(url.toString(), {
const response = await this.credentials.fetch(url.toString(), {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand Down