From 221c9a83f744ca280315fa362480a38c101d1c49 Mon Sep 17 00:00:00 2001 From: David Graham Date: Mon, 1 Aug 2022 13:13:59 -0600 Subject: [PATCH 1/2] Add usage instructions Co-authored-by: iheanyi --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 11c5f78..b7af176 100644 --- a/README.md +++ b/README.md @@ -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: '', + 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: '', + 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: '', + 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. From 0888b01c32eb37c89ac80c9c60cede1294228c50 Mon Sep 17 00:00:00 2001 From: David Graham Date: Mon, 1 Aug 2022 13:15:50 -0600 Subject: [PATCH 2/2] Customize fetch function Allow a fetch function to be provided in environments older than Node 18. Co-authored-by: iheanyi --- src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 9d06be8..72575fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ export interface Credentials { username: string password: string host: string + fetch?: (input: RequestInfo, init?: RequestInit) => Promise } export interface QueryResultRow { @@ -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 } @@ -100,7 +104,7 @@ export class Connection { private async postJSON(url: string | URL, body = {}): Promise { 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: {