Skip to content

Commit b14ed88

Browse files
teallarsongirarda
andauthored
feat: callback added to demo, add a server, and remove vite dependency (#11)
Co-authored-by: Alexandre Girard <[email protected]>
1 parent 2b02ecf commit b14ed88

15 files changed

+1197
-961
lines changed

README.md

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The built files will be in the `dist` directory.
3636

3737
## Usage
3838

39+
### Authentication
40+
3941
To use this library, you will first need to fetch an Airbyte Embedded token. You should do this in your server, though if you are simply testing this locally, you can use:
4042

4143
```bash
@@ -50,20 +52,45 @@ curl --location '$AIRBYTE_BASE_URL/api/public/v1/embedded/widget' \
5052

5153
`AIRBYTE_BASE_URL`: where your Airbyte instance is deployed
5254
`CUSTOMER_WORKSPACE_ID`: the workspace you have associated with this customer
53-
`EMBEDDING_ORIGIN` here refers to where you are adding this widget to. It will be used to generate an `allowedOrigin` parameter for the webapp to open communications with the widget. If you are running the widget locally using our demo app, the allowed origin should be `https://localhost:3003`, for example.
55+
`EMBEDDING_ORIGIN` here refers to where you are adding this widget to. It will be used to generate an `allowedOrigin` parameter for the webapp to open communications with the widget.
5456

5557
You can also, optionally, send an `externalUserId` in your request and we will attach it to the jwt encoded within the Airbyte Embedded token for provenance purposes.
5658

5759
Embedded tokens are short-lived (15-minutes) and only allow an end user to create and edit Airbyte source configurations within the workspace you have created for them.
5860

59-
These values should be passed to where you initializze the widget like so:
61+
### Event Callbacks
62+
63+
The widget also accepts an `onEvent` function as an argument. This function, if provided, will be executed when a user completes the integration setup or update flow. These events have the following format:
64+
65+
```typescript
66+
// successful events:
67+
{
68+
type: "end_user_action_result";
69+
message: "partial_user_config_created" | "partial_user_config_updated";
70+
data: PartialUserConfigRead; // an object containing all data related to the user's configuration, including an actorId identifying the source created/updated
71+
}
72+
73+
// errored events:
74+
{
75+
type: "end_user_action_result";
76+
message: "partial_user_config_update_error" | "partial_user_config_create_error";
77+
error: Error; // an error object, including a message property
78+
}
79+
```
80+
81+
This allows you to trigger operations based upon different types of results.
82+
83+
### Configuration
84+
85+
These values should be passed to where you initialize the widget like so:
6086

6187
```typescript
6288
import { EmbeddedWidget } from "airbyte-embedded-widget";
6389

6490
// Initialize the widget
6591
const widget = new EmbeddedWidget({
6692
token: res.token,
93+
onEvent: yourEventFunction,
6794
});
6895

6996
// Mount the widget
@@ -74,38 +101,10 @@ widget.mount("#widget-container");
74101

75102
The demo application in the `/demo` directory shows a complete example of integrating the widget. It includes:
76103

77-
- HTTPS setup with Vite
78104
- Environment variable configuration
79105
- Basic styling and layout
80106
- API token handling
81107

82-
To configure the demo, create a `.env` file in the `/demo` directory:
83-
84-
```env
85-
VITE_AB_EMBEDDED_TOKEN=""
86-
```
87-
88-
You can fetch an Airbyte Embedded token using the curl request example above.
89-
90-
You can then run the demo app using `pnpm dev` and access a very simple example UI at `https://localhost:3003` in your browser.
91-
92-
## Development
93-
94-
To develop with the widget running against a locally running version of the Airbyte webapp, you can run
95-
96-
```bash
97-
cd demo
98-
pnpm dev:local
99-
```
100-
101-
and add the following to a .env file within `demo`:
102-
103-
```env
104-
VITE_AB_EMBEDDED_TOKEN="your_embedded_token"
105-
AB_WEBAPP_URL="your_locally_running_webapp_url"
106-
VITE_AB_ADMIN_TOKEN="your_instance_admin_auth_token" # optional, allows for developing with a longer lived token
107-
```
108-
109108
## Publishing
110109

111110
This repository is configured to publish to npmjs.org whenever:

demo/.env.test

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,39 @@ This is a demo application showcasing the usage of the Airbyte Embedded Widget.
1818
pnpm install
1919
```
2020

21-
2. Fetch embedded token
22-
23-
To use this library, you will first need to fetch an Airbyte Embedded token. You should do this in your server, though if you are simply testing this locally, you can use:
21+
3. Create a `.env` file in the `/demo` directory:
2422

25-
```
26-
curl --location '$AIRBYTE_BASE_URL/api/public/v1/embedded/widget' \
27-
--header 'Content-Type: application/json' \
28-
--header 'Accept: text/plain' \
29-
--data '{
30-
"workspaceId": "$CUSTOMER_WORKSPACE_ID",
31-
"allowedOrigin": "$EMBEDDING_ORIGIN"
32-
}'
23+
```env
24+
CLIENT_ID=
25+
CLIENT_SECRET=
26+
ORGANIZATION_ID=
27+
WORKSPACE_ID=
28+
ALLOWED_ORIGIN=
29+
BASE_URL=
3330
```
3431

35-
`AIRBYTE_BASE_URL`: where your Airbyte instance is deployed
36-
`CUSTOMER_WORKSPACE_ID`: the workspace you have associated with this customer
37-
`EMBEDDING_ORIGIN` here refers to where you are adding this widget to. It will be used to generate an `allowedOrigin` parameter for the webapp to open communications with the widget. If you are running the widget locally using our demo app, the allowed origin should be `https://localhost:3003`, for example.
32+
4. Start the backend server
3833

39-
You can also, optionally, send an `externalUserId` in your request and we will attach it to the jwt encoded within the Airbyte Embedded token for provenance purposes.
34+
```bash
35+
env $(cat .env | xargs) node server.js
36+
```
4037

41-
Embedded tokens are short-lived (15-minutes) and only allow an end user to create and edit Airbyte source configurations within the workspace you have created for them.
38+
The server will start at `https://localhost:3001`.
4239

43-
3. Create a `.env` file in the `/demo` directory:
40+
This server will be responsible for fetching your Airbyte Embedded token and sending it to the Embedded Widget. It is also called from the frontend of the demo to log the event responses from Airbyte when a user sets up or updates an integration.
4441

45-
```env
46-
VITE_AB_EMBEDDED_TOKEN=""
47-
```
42+
5. Start the frontend server:
4843

49-
You can fetch an Airbyte Embedded token using the curl request example above.
44+
```bash
45+
npx serve .
46+
```
5047

51-
4. Run the demo app using `pnpm dev` and access a very simple example UI at `https://localhost:3003` in your browser.
48+
You may need to accept the self-signed certificate warning in your browser.
5249

5350
## Project Structure
5451

55-
- `index.html` - Main HTML file with widget container
56-
- `vite.config.ts` - Vite configuration with HTTPS setup
52+
- `/public/index.html` - Main HTML file with widget container
53+
- `server.js` - Backend server requesting the embedded URL
5754
- `.env` - Environment variables (not committed to git)
5855

5956
## Note

demo/index.html

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,44 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Airbyte Widget Demo</title>
7-
<style>
8-
.loading {
9-
display: none;
10-
color: #fff;
11-
margin-top: 1rem;
12-
}
13-
.error {
14-
color: red;
15-
margin-top: 1rem;
16-
}
17-
#app {
18-
min-height: 200px;
19-
}
20-
</style>
5+
<title>Airbyte Embedded Widget</title>
216
</head>
227
<body>
23-
<h1>Airbyte Widget Demo</h1>
24-
<div id="app">
25-
<div id="loading" class="loading">Loading widget...</div>
26-
<div id="error" class="error"></div>
27-
</div>
28-
<script type="module">
29-
import { EmbeddedWidget } from "@airbyte-embedded/airbyte-embedded-widget";
8+
<div id="widget-container"></div>
309

31-
const loadingEl = document.getElementById("loading");
32-
const errorEl = document.getElementById("error");
10+
<script type="module">
11+
import { EmbeddedWidget } from "https://cdn.jsdelivr.net/npm/@airbyte-embedded/[email protected]/+esm";
3312

34-
async function initializeWidget() {
13+
window.addEventListener("load", async () => {
3514
try {
36-
new EmbeddedWidget({
37-
token: import.meta.env.VITE_AB_EMBEDDED_TOKEN,
38-
});
15+
const response = await fetch("http://localhost:3001/api/widget_token");
16+
const data = await response.json();
3917

40-
// Hide loading state on success
41-
loadingEl.style.display = "none";
42-
} catch (error) {
43-
console.error("Error initializing widget:", error);
44-
// Hide loading state and show error
45-
loadingEl.style.display = "none";
46-
errorEl.innerHTML = `
47-
<p>Failed to initialize widget:</p>
48-
<pre style="white-space: pre-wrap;">${error.message}</pre>
49-
<p>Check console for more details.</p>
50-
`;
51-
}
52-
}
18+
if (!data.token) {
19+
throw new Error("Missing 'token' in response");
20+
}
5321

54-
// Start initialization
55-
initializeWidget();
22+
const widgetResultCallback = (result) => {
23+
// Send the result data to the embedded_response endpoint
24+
fetch("http://localhost:3001/api/embedded_response", {
25+
method: "POST",
26+
headers: {
27+
"Content-Type": "application/json",
28+
},
29+
body: JSON.stringify(result),
30+
})
31+
.then((response) => console.log("Callback response:", response.status))
32+
.catch((err) => console.error("Callback error:", err));
33+
};
34+
35+
const widget = new EmbeddedWidget({
36+
token: data.token,
37+
onEvent: widgetResultCallback,
38+
});
39+
} catch (err) {
40+
console.error("Failed to load widget:", err);
41+
}
42+
});
5643
</script>
5744
</body>
5845
</html>

0 commit comments

Comments
 (0)