Skip to content

Commit 83e137e

Browse files
committed
adjustments to return type, styles, etc
1 parent 8143741 commit 83e137e

File tree

10 files changed

+74
-38
lines changed

10 files changed

+74
-38
lines changed

__tests__/lib/ezql.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ describe('register EZQL', () => {
8181
test('prompt.data yields data', async () => {
8282
expect.assertions(2)
8383

84-
const mockedResponse = '42'
85-
fetch.mockResolvedValue(new Response(mockedResponse))
84+
fetch.mockResolvedValue(new Response('42'))
8685

8786
const query = 'What is the answer to life, the univerise and everything?'
8887
const response = await ob.prompt(query, Prompt.data)
@@ -92,23 +91,26 @@ describe('register EZQL', () => {
9291
body: JSON.stringify({ query, run: true }),
9392
method: 'POST',
9493
})
95-
expect(response).toEqual(mockedResponse)
94+
expect(response).toEqual(42)
9695
})
9796

9897
test('prompt.sql yields SQL', async () => {
9998
expect.assertions(2)
10099

101-
const mockedResponse = 'SELECT answer FROM ultimate_question;'
100+
const serverResponse = {"response": {"query": {"text": "SELECT answer FROM ultimate_question;"}}, "success": true}
101+
const mockedResponse = JSON.stringify(serverResponse)
102+
102103
fetch.mockResolvedValue(new Response(mockedResponse))
103104

104105
const query = 'What is the answer to life, the univerise and everything?'
105-
const response = await ob.prompt(query, Prompt.sql)
106+
const clientResponse = await ob.prompt(query, Prompt.sql)
106107

107108
expect(fetch).toHaveBeenCalledWith(`https://${DEFAULT_HOST}/api/v1/ezql`, {
108109
headers: { 'Content-Type': 'application/json', 'x-source-token': token },
109110
body: JSON.stringify({ query, run: false }),
110111
method: 'POST',
111112
})
112-
expect(response).toEqual(mockedResponse)
113+
114+
expect(clientResponse).toEqual(serverResponse)
113115
})
114116
})

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ezql",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Ask your database questions, anywhere.",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/lib/ezql.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ export type EZQLOpts = {
33
host?: string
44
}
55

6+
type EZQLResponse = {
7+
success: boolean
8+
response: {
9+
query: {
10+
text: string
11+
}
12+
}
13+
}
14+
615
export enum Prompt {
716
sql = 'sql',
817
data = 'data',
@@ -42,7 +51,7 @@ export class EZQL {
4251
}
4352
}
4453

45-
async prompt(phrase: string, type: Prompt): Promise<string> {
54+
async prompt(phrase: string, type: Prompt): Promise<EZQLResponse> {
4655
// console.debug(`prompt("${phrase}", ${type})`)
4756
if (!phrase || !type) throw new Error(`EZQL.prompt requires a 'phrase' and 'type' parameter`)
4857
if (!Object.values(Prompt).includes(type))
@@ -60,7 +69,7 @@ export class EZQL {
6069
}),
6170
})
6271

63-
if (result.status === 200) return result.text()
72+
if (result.status === 200) return result.json()
6473
else {
6574
throw new Error(`${result.status}: ${result.statusText}`)
6675
}

src/react-ezql/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ npm install --save-dev react-ezql
77
## Usage
88

99
**Minimal usage**
10+
- `token` is provided from the Outerbase dashboard
11+
- `suggestions` - a list of queries that can be chosen from a list
12+
- `setShouldDisplayEzql` is a setter for toggling whether the modal is displayed; provide a setter that when true will remove the modal from your page
13+
- `didSubmitWithValue` is called when a query has been submitted to the modal _before_ a request has been made to Outerbase
14+
- `onResults` is called with the resulting SQL string or JSON data (depending on the Prompt type specified)
15+
- `className` provides optional styling classes to be added to the modal
1016

1117
```tsx
1218
import EzqlPrompt from 'react-ezql'
1319

1420
<EzqlPrompt
21+
token={'arbitrary-value-asdf-1234'}
1522
setShouldDisplayEzql={setShouldDisplayEzql}
1623
suggestions={['How many books sold last week', 'How many new users signed up today']}
1724
didSubmitWithValue={(value) => {
@@ -93,3 +100,17 @@ Each HTML Element has been an assigned a meaningful classname that you can style
93100
color: white;
94101
}
95102
```
103+
104+
## Local development of react-ezql
105+
pnpm is recommended as `npm link` does not work properly (as of 03/09/2023) with regards to `npm link`ing `lib/ezql` to `react-ezql`. Instead, do this from the `react-ezql` directory:
106+
107+
```sh
108+
pnpm install
109+
pnpm link ../../
110+
```
111+
112+
And be sure to have also built your latest changes in `lib/ezql`, i.e.:
113+
114+
```sh
115+
npm run build
116+
```

src/react-ezql/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/react-ezql/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"c8": "^7.13.0",
6464
"eslint": "^8.35.0",
6565
"eslint-plugin-react": "^7.32.2",
66-
"ezql": "^0.0.3",
66+
"ezql": "^0.0.4",
6767
"jsdom": "^21.1.0",
6868
"postcss": "^8.4.21",
6969
"react": "^18.2.0",

src/react-ezql/src/App.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ function App() {
4646
token={token}
4747
setShouldDisplayEzql={setShouldDisplayEzql}
4848
suggestions={suggestions}
49-
didSubmitWithValue={(value) => {
50-
console.log(`query: ${value}`)
49+
didSubmitWithValue={(_value) => {
5150
setShouldDisplayEzql(false)
5251
}}
53-
onResults={(json) => console.dir(json)}
52+
onResults={(sql) => console.log(sql)}
5453
className="optional-for-styling-convenience"
5554
/>
5655
)}

src/react-ezql/src/components/ezql-prompt.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,47 @@ import { useCallback, useRef, useState } from 'react'
22
import Modal from './modal'
33
import { SuggestSearchListItem } from './suggest-search-list-item'
44
import { classNames } from '../lib/class-names'
5-
import {EZQL, Prompt} from 'ezql'
5+
import { EZQL, Prompt } from 'ezql'
66

77
import styles from './ezql-prompt.module.css'
88

99
export type EzqlPromptOpts = {
1010
token: string
1111
setShouldDisplayEzql: (value: boolean) => void
12-
onResults: (data: object) => void
12+
onResults: (data: string | Object) => void
1313
didSubmitWithValue?: (query: string) => void
1414
suggestions?: Array<string>
1515
className?: string
1616
}
1717

1818
export default function EzqlPrompt({
19-
token,
20-
setShouldDisplayEzql,
21-
didSubmitWithValue,
22-
onResults,
23-
suggestions,
24-
className,
19+
token, // authenticates and identifies the scope of this request with Outerbase
20+
setShouldDisplayEzql, // toggles display of the modal
21+
didSubmitWithValue, // fires before the request goes to Outerbase
22+
onResults, // fires upon receiving a response from Outerbase
23+
suggestions, // pre-populate the list of suggested queries
24+
className, // optional styling classes
2525
}: EzqlPromptOpts) {
2626
const [query, setQuery] = useState('')
27-
27+
2828
const listRef = useRef<HTMLUListElement>(null)
2929

30-
const onSubmission = useCallback(async (phrase: string) => {
31-
if (!token) throw new Error("Missing token")
32-
if (didSubmitWithValue) didSubmitWithValue(phrase)
30+
const onSubmission = useCallback(
31+
async (phrase: string) => {
32+
if (!token) throw new Error('Missing token')
33+
if (didSubmitWithValue) didSubmitWithValue(phrase)
3334

34-
// TODO make request to Outerbase
35-
const ezql = new EZQL({ token })
36-
const result = await ezql.prompt(phrase, Prompt.sql)
37-
console.dir(result)
38-
// and pass the result to onResults()
39-
}, [token])
35+
const ezql = new EZQL({ token, host: 'app.dev.outerbase.com' })
36+
const {
37+
response: {
38+
query: { text: sql },
39+
},
40+
} = await ezql.prompt(phrase, Prompt.sql)
41+
42+
onResults(sql)
43+
},
44+
[token]
45+
)
4046

4147
return (
4248
<Modal didDismiss={() => setShouldDisplayEzql(false)} className={classNames('ezql-prompt-modal', className)}>

src/react-ezql/vite.config.web.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineConfig } from 'vite'
22
import react from '@vitejs/plugin-react-swc'
3-
import path from 'path'
43

54
// https://vitejs.dev/config/
65
export default defineConfig({

0 commit comments

Comments
 (0)