Skip to content

Commit

Permalink
chore: build
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 5, 2024
1 parent 80847b2 commit f3f62ca
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 19 deletions.
104 changes: 89 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,100 @@
# bun starter
# UndbSDK

## Getting Started
UndbSDK is a TypeScript SDK for interacting with the Undb API. It provides authentication services, base services, and an OpenAPI client.

Click the [Use this template](https://github.com/wobsoriano/bun-lib-starter/generate) button to create a new repository with the contents starter.
## Installation

OR
Install UndbSDK using npm:

Run `bun create wobsoriano/bun-lib-starter ./my-lib`.
```bash
npm install @undb/js-sdk
# or
yarn add @undb/js-sdk
# or
pnpm add @undb/js-sdk
# or
bun add @undb/js-sdk
```

## Setup
## Usage

```bash
# install dependencies
bun install
Import and use UndbSDK in your project:

```typescript
import { UndbSDK } from '@undb/js-sdk'

const sdk = new UndbSDK({
baseURL: 'http://localhost:3721/openapi', // https://app.undb.io/openapi if you are using the hosted version
fetch: fetch, // Optional, use a custom fetch function
})

sdk.auth.setToken('secret')

const client = sdk.getOpenapiClient<paths>()

const res = await client.GET('/bases/templates/tables/templates/records')
```

### Authentication

Access the authentication service using the `auth` property:

#### Set token

```typescript
sdk.auth.setToken('secret')
```

### Usage with Base & Table names

Get a `BaseService` instance using the `base` method:

# test the app
bun test
```typescript
const baseName = 'your-base-name'
const baseService = sdk.base(baseName)
// Access a specific table
const tableService = baseService.table('your-table-name')

# build the app, available under dist
bun run build
// Get records
const records = await tableService.getRecords()
// Create a record
const newRecord = await tableService.createRecord({ values: { / your field values / } })
// Update a record
const updatedRecord = await tableService.updateRecord('record-id', { values: { / updated field values / } })
// Delete a record
await tableService.deleteRecord('record-id')
```

#### Access a specific view

```typescript
const viewService = baseService.table('your-table-name', 'your-view-name')

// Get records
const records = await viewService.getRecords()
```

### Usage with OpenAPI Client

Get a `OpenApiClient` instance using the `getOpenapiClient` method:

```typescript
const client = sdk.getOpenapiClient<paths>()

const res = await client.GET('/bases/templates/tables/templates/records')
```

#### Generate OpenAPI types from undb

TODO

### Development

```bash
bun install
bun run test:watch
```

## License
### License

MIT
[MIT](./LICENSE)
2 changes: 1 addition & 1 deletion src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AuthService {
this.client = client
}

async loginWithEmail(email: string, password: string): Promise<AuthResponse> {
async loginWithEmailPassword(email: string, password: string): Promise<AuthResponse> {
if (this.token) {
throw new Error('Already authenticated with API token.')
}
Expand Down
6 changes: 3 additions & 3 deletions test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UndbSDK } from '../src/Sdk'
import type { components, paths } from './templates'

const SECRET = 'secret'
const BASE_URL = 'http://localhost:3721/api'
const BASE_URL = 'http://localhost:3721/openapi'

describe('template', () => {
let sdk: UndbSDK
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('template', () => {
const request = mockFetch.mock.calls[0][0]

expect(request).toHaveProperty('url')
expect(request.url).toBe('http://localhost:3721/api/bases/templates/tables/templates/records')
expect(request.url).toBe('http://localhost:3721/openapi/bases/templates/tables/templates/records')
expect(request).toHaveProperty('headers')
expect(request.headers).toBeInstanceOf(Headers)
expect((request.headers as Headers).get('x-undb-api-token')).toBe(SECRET)
Expand All @@ -47,7 +47,7 @@ describe('template', () => {
})

const request = mockFetch.mock.calls[0][0]
expect(request.url).toBe(`http://localhost:3721/api/bases/templates/tables/templates/records/${recordId}`)
expect(request.url).toBe(`http://localhost:3721/openapi/bases/templates/tables/templates/records/${recordId}`)
})
})

Expand Down

0 comments on commit f3f62ca

Please sign in to comment.