Skip to content

Commit

Permalink
feat: Export types (#19)
Browse files Browse the repository at this point in the history
* feat: export types

* fix: add missing types

* fix: tidy up exported types
  • Loading branch information
kylekz authored May 16, 2024
1 parent f493b14 commit 88420d5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
23 changes: 18 additions & 5 deletions src/api-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ export interface ApiToken {
name: string;
}

export interface ApiTokenWithJWT extends ApiToken {
token: string;
}

export interface RevokedApiToken {
token: string;
}

export interface ApiTokenValidation {
valid: boolean;
expiry: number;
}

export class ApiTokenClient {
constructor(private config: TursoConfig) {}

Expand All @@ -18,8 +31,8 @@ export class ApiTokenClient {
return response.tokens ?? [];
}

async create(name: string) {
const response = await TursoClient.request<ApiToken & { token: string }>(
async create(name: string): Promise<ApiTokenWithJWT> {
const response = await TursoClient.request<ApiTokenWithJWT>(
`auth/api-tokens/${name}`,
this.config,
{
Expand All @@ -33,8 +46,8 @@ export class ApiTokenClient {
return response;
}

async revoke(name: string) {
const response = await TursoClient.request<{ token: string }>(
async revoke(name: string): Promise<RevokedApiToken> {
const response = await TursoClient.request<RevokedApiToken>(
`auth/api-tokens/${name}`,
this.config,
{
Expand All @@ -45,7 +58,7 @@ export class ApiTokenClient {
return response;
}

async validate(token: string): Promise<{ valid: boolean; expiry: number }> {
async validate(token: string): Promise<ApiTokenValidation> {
const response = await TursoClient.request<{ exp: number }>(
"auth/api-tokens/validate",
this.config,
Expand Down
26 changes: 16 additions & 10 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ export interface ApiCreateDatabaseResponse {
Name: string;
}

export interface DatabaseCreateResponse {
export interface CreatedDatabase {
name: string;
id: string;
hostname: string;
}

export interface DatabaseInstanceUsageDetail {
interface DatabaseInstanceUsageDetail {
rows_read: number;
rows_written: number;
storage_bytes: number;
}

export interface DatabaseInstanceUsage {
interface DatabaseInstanceUsage {
uuid: string;
usage: DatabaseInstanceUsageDetail;
}
Expand Down Expand Up @@ -70,6 +70,14 @@ export interface DatabaseInstance {
hostname: string;
}

export interface DeletedDatabase {
database: string;
}

export interface DatabaseToken {
jwt: string;
}

type MultiDBSchemaOptions =
| { is_schema: boolean; schema?: never }
| { is_schema?: never; schema: string }
Expand Down Expand Up @@ -118,7 +126,7 @@ export class DatabaseClient {
timestamp?: string | Date;
};
} & MultiDBSchemaOptions
): Promise<DatabaseCreateResponse> {
): Promise<CreatedDatabase> {
if (hasIsSchemaOption(options) && hasSchemaOption(options)) {
throw new Error("'is_schema' and 'schema' cannot both be provided");
}
Expand Down Expand Up @@ -163,7 +171,7 @@ export class DatabaseClient {
}

async delete(dbName: string) {
const response = await TursoClient.request<{ database: string }>(
const response = await TursoClient.request<DeletedDatabase>(
`organizations/${this.config.org}/databases/${dbName}`,
this.config,
{
Expand Down Expand Up @@ -205,7 +213,7 @@ export class DatabaseClient {
expiration: string;
authorization: "read-only" | "full-access";
}
) {
): Promise<DatabaseToken> {
const queryParams = new URLSearchParams();

if (options?.expiration) {
Expand All @@ -216,7 +224,7 @@ export class DatabaseClient {
queryParams.set("authorization", options.authorization);
}

const response = await TursoClient.request<{ jwt: string }>(
const response = await TursoClient.request<DatabaseToken>(
`organizations/${this.config.org}/databases/${dbName}/auth/tokens?${queryParams}`,
this.config,
{
Expand Down Expand Up @@ -286,9 +294,7 @@ export class DatabaseClient {
};
}

private formatCreateResponse(
db: ApiCreateDatabaseResponse
): DatabaseCreateResponse {
private formatCreateResponse(db: ApiCreateDatabaseResponse): CreatedDatabase {
return {
id: db.DbId,
hostname: db.Hostname,
Expand Down
8 changes: 6 additions & 2 deletions src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export type ExtensionType =
| "uuid"
| "regexp";

export interface GroupToken {
jwt: string;
}

export class GroupClient {
constructor(private config: TursoConfig) {}

Expand Down Expand Up @@ -113,7 +117,7 @@ export class GroupClient {
expiration: string;
authorization: "read-only" | "full-access";
}
) {
): Promise<GroupToken> {
const queryParams = new URLSearchParams();

if (options?.expiration) {
Expand All @@ -124,7 +128,7 @@ export class GroupClient {
queryParams.set("authorization", options.authorization);
}

const response = await TursoClient.request<{ jwt: string }>(
const response = await TursoClient.request<GroupToken>(
`organizations/${this.config.org}/groups/${groupName}/auth/tokens?${queryParams}`,
this.config,
{
Expand Down
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import "whatwg-fetch";

export { createClient } from "./client";

export type {
ApiToken,
ApiTokenWithJWT,
RevokedApiToken,
ApiTokenValidation,
} from "./api-token";
export type { TursoClientError } from "./client";
export type { TursoConfig } from "./config";
export type {
Database,
CreatedDatabase,
DatabaseUsage,
InstanceUsages,
TotalUsage,
DatabaseInstance,
DeletedDatabase,
DatabaseToken,
} from "./database";
export type { Group, ExtensionType, GroupToken } from "./group";
export type { LocationKeys, Location, ClosestLocation } from "./location";
export type {
Organization,
OrganizationMember,
OrganizationInvite,
Invoice,
OrganizationMemberRole,
OrganizationAddedMember,
OrganizationRemovedMember,
} from "./organization";
10 changes: 6 additions & 4 deletions src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export type Location = {
[K in keyof LocationKeys]: { code: K; description: LocationKeys[K] };
}[keyof LocationKeys];

export interface ClosestLocation {
server: keyof LocationKeys;
client: keyof LocationKeys;
}

export class LocationClient {
constructor(private config: TursoConfig) {}

Expand All @@ -58,10 +63,7 @@ export class LocationClient {
}));
}

async closest(): Promise<{
server: keyof LocationKeys;
client: keyof LocationKeys;
}> {
async closest(): Promise<ClosestLocation> {
return fetch("https://region.turso.io/").then((res) => res.json());
}
}
17 changes: 14 additions & 3 deletions src/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ export interface Invoice {
invoice_pdf: string;
}

export type OrganizationMemberRole = "admin" | "member";

export interface OrganizationAddedMember {
member: string;
role: OrganizationMemberRole;
}

export interface OrganizationRemovedMember {
member: string;
}

export class OrganizationClient {
constructor(private config: TursoConfig) {}

Expand Down Expand Up @@ -80,7 +91,7 @@ export class OrganizationClient {
async addMember(
username: string,
role?: "admin" | "member"
): Promise<{ member: string; role: "admin" | "member" }> {
): Promise<OrganizationAddedMember> {
return TursoClient.request(
`organizations/${this.config.org}/members/${username}`,
this.config,
Expand All @@ -94,7 +105,7 @@ export class OrganizationClient {
);
}

async removeMember(username: string): Promise<{ member: string }> {
async removeMember(username: string): Promise<OrganizationRemovedMember> {
return TursoClient.request(
`organizations/${this.config.org}/members/${username}`,
this.config,
Expand All @@ -106,7 +117,7 @@ export class OrganizationClient {

async inviteUser(
email: string,
role?: "admin" | "member"
role?: OrganizationMemberRole
): Promise<OrganizationInvite> {
const response = await TursoClient.request<{ invited: OrganizationInvite }>(
`organizations/${this.config.org}/invites`,
Expand Down

0 comments on commit 88420d5

Please sign in to comment.