Skip to content

Commit

Permalink
Merge pull request #33 from polyfact/feat/add-usage-email
Browse files Browse the repository at this point in the history
✨ Add usage & email
  • Loading branch information
lowczarc authored Aug 17, 2023
2 parents 5428bbd + c80ae8f commit 82eeecb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import memoryClient, {
MemoryClient,
} from "./memory";
import { splitString, tokenCount } from "./split";
import userClient, { usage, UserClient } from "./user";
import { InputClientOptions } from "./clientOpts";
import kvClient, { get as KVGet, set as KVSet, KVClient } from "./kv";

Expand Down Expand Up @@ -54,13 +55,15 @@ export {
Chat,
Memory,
kv,
usage,
};

type Client = GenerationClient &
GenerationWithTypeClient &
TranscribeClient &
MemoryClient &
ChatClient & { kv: KVClient };
ChatClient &
UserClient & { kv: KVClient };

function client(co: InputClientOptions): Client {
return {
Expand All @@ -69,6 +72,7 @@ function client(co: InputClientOptions): Client {
...transcribeClient(co),
...memoryClient(co),
...chatClient(co),
...userClient(co),
kv: kvClient(co),
};
}
Expand Down Expand Up @@ -241,6 +245,7 @@ type Provider = "github" | "google";
export function usePolyfact({ project, endpoint }: { project: string; endpoint?: string }): {
polyfact: Client | undefined;
login: ((input: { provider: Provider }) => Promise<void>) | undefined;
email?: string;
loading: boolean;
} {
if (typeof window === "undefined") {
Expand All @@ -249,6 +254,7 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:

const react = require("react"); // eslint-disable-line
const [polyfact, setPolyfact] = react.useState();
const [email, setEmail] = react.useState();
const [loading, setLoading] = react.useState(true);
const [login, setLogin] = react.useState();

Expand Down Expand Up @@ -294,6 +300,10 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:
data.session?.refresh_token,
);
}

const { data } = await supabase.auth.getUser(token);

setEmail(data.user?.email);
const p = await Polyfact.endpoint(endpoint || "https://api2.polyfact.com")
.project(project)
.signInWithToken(token);
Expand All @@ -312,5 +322,5 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:
})();
}, []);

return { polyfact, login, loading };
return { polyfact, login, loading, email };
}
55 changes: 55 additions & 0 deletions lib/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import axios, { AxiosError } from "axios";
import * as t from "polyfact-io-ts";
import { InputClientOptions, defaultOptions } from "./clientOpts";
import { ApiError, ErrorData } from "./helpers/error";

const UsageResultType = t.intersection([
t.type({
usage: t.number,
}),
t.partial({
rate_limit: t.union([t.undefined, t.null, t.number]),
}),
]);

export async function usage(
clientOptions: InputClientOptions = {},
): Promise<{ usage: number; rateLimit?: number }> {
const { token, endpoint } = await defaultOptions(clientOptions);

try {
const res = await axios.get(`${endpoint}/usage`, {
headers: {
"Content-Type": "application/json",
"X-Access-Token": token,
},
});

if (!UsageResultType.is(res.data)) {
throw new ApiError({
code: "mismatched_response",
message: "The response from the API does not match the expected format",
});
}

return {
usage: res.data.usage,
rateLimit: res.data.rate_limit ?? undefined,
};
} catch (e: unknown) {
if (e instanceof AxiosError) {
throw new ApiError(e?.response?.data as ErrorData);
}
throw e;
}
}

export type UserClient = {
usage: () => Promise<{ usage: number; rateLimit?: number }>;
};

export default function client(clientOptions: InputClientOptions = {}): UserClient {
return {
usage: () => usage(clientOptions),
};
}

0 comments on commit 82eeecb

Please sign in to comment.