Skip to content

Commit 6305f0f

Browse files
chore: add playground for testing the library
1 parent b56d956 commit 6305f0f

4 files changed

Lines changed: 513 additions & 1 deletion

File tree

apps/playground/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "playground",
3+
"version": "1.0.0",
4+
"description": "A playground for testing Splitwise TypeScript SDK",
5+
"keywords": [],
6+
"author": "",
7+
"scripts": {
8+
"dev": "npx listhen ./src/index.ts"
9+
},
10+
"dependencies": {
11+
"h3": "^1.15.3",
12+
"listhen": "^1.9.0",
13+
"splitwise-ts": "workspace:*"
14+
},
15+
"license": "ISC",
16+
"packageManager": "pnpm@10.11.1"
17+
}

apps/playground/src/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createApp, eventHandler } from "h3";
2+
import { OAuth2User, Client } from 'splitwise-ts'
3+
4+
export const app = createApp();
5+
6+
/**
7+
* This is a simple handler that retrieves the current user
8+
* using the Splitwise TypeScript SDK.
9+
* You can run this app using the command: `pnpm dev`
10+
*/
11+
app.use("/", eventHandler(async (event) => {
12+
// Replace <consumer-key> and <consumer-secret> with your actual Splitwise OAuth credentials
13+
const user = new OAuth2User({
14+
clientId: process.env.SPLITWISE_CONSUMER_KEY as string,
15+
clientSecret: process.env.SPLITWISE_CONSUMER_SECRET as string,
16+
})
17+
18+
// Request an access token with error handling
19+
try {
20+
await user.requestAccessToken()
21+
} catch (error) {
22+
return { error: "Failed to obtain access token", details: error instanceof Error ? error.message : String(error) }
23+
}
24+
25+
// Create a new Splitwise client with the authenticated user
26+
const client = new Client(user)
27+
28+
const myGroup = (await client.groups.getGroups()).groups?.find(group => {
29+
return group.name === 'Test Group in Town'
30+
})
31+
32+
const expense = await client.expenses.createExpense({
33+
group_id: myGroup?.id,
34+
description: 'This is a text expense from splitwise-ts client',
35+
cost: '5000',
36+
split_equally: true,
37+
})
38+
39+
return expense
40+
}));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "splitwise-ts",
33
"scripts": {
44
"build": "turbo run build",
5-
"dev": "turbo run dev",
5+
"dev": "turbo run dev --filter=playground",
66
"lint": "turbo run lint",
77
"check-types": "turbo run check-types"
88
},

0 commit comments

Comments
 (0)