Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions packages/shared-ui/src/flow-gen/app-catalyst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export interface AppCatalystG1CreditsResponse {

export type CheckAppAccessResponse =
| {
canAccess: false;
accessStatus: string;
termsOfService?: {
version: number;
terms: string;
};
}
canAccess: false;
accessStatus: string;
termsOfService?: {
version: number;
terms: string;
};
}
| { canAccess: true; accessStatus: string };

export interface AppCatalystContentChunk {
Expand Down Expand Up @@ -99,12 +99,12 @@ export class AppCatalystApiClient {
request: AppCatalystG1SubscriptionStatusRequest
): Promise<AppCatalystG1SubscriptionStatusResponse> {
const url = new URL("v1beta1/getG1SubscriptionStatus", this.#apiBaseUrl);
url.searchParams.set(
"include_credit_data",
String(request.include_credit_data)
);
const response = await this.#fetchWithCreds(url, {
method: "GET",
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(request),
});
if (!response.ok) {
throw new Error(
Expand All @@ -119,7 +119,11 @@ export class AppCatalystApiClient {
async getG1Credits(): Promise<AppCatalystG1CreditsResponse> {
const url = new URL("v1beta1/getG1Credits", this.#apiBaseUrl);
const response = await this.#fetchWithCreds(url, {
method: "GET",
method: "POST",
headers: {
"content-type": "application/json",
},
body: "{}",
});
if (!response.ok) {
throw new Error(`Failed to get G1 credits: ${response.statusText}`);
Expand Down
56 changes: 56 additions & 0 deletions packages/shared-ui/src/tests/app-catalyst.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import { test, describe, it, mock } from "node:test";
import assert from "node:assert";
import { AppCatalystApiClient } from "../flow-gen/app-catalyst.js";

describe("AppCatalystApiClient", () => {
it("should construct correct URL and headers for getG1SubscriptionStatus", async () => {
const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => {
return new Response(JSON.stringify({ is_member: true, remaining_credits: 10 }), { status: 200 });
});

const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com");
await client.getG1SubscriptionStatus({ include_credit_data: true });

assert.strictEqual(fetchMock.mock.calls.length, 1);
const [url, init] = fetchMock.mock.calls[0].arguments;

assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/getG1SubscriptionStatus");
assert.strictEqual(init?.method, "POST");
assert.deepStrictEqual(init?.headers, { "content-type": "application/json" });
assert.strictEqual(init?.body, JSON.stringify({ include_credit_data: true }));
});

it("should construct correct URL and headers for getG1Credits", async () => {
const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => {
return new Response(JSON.stringify({ remaining_credits: 5 }), { status: 200 });
});

const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com");
await client.getG1Credits();

assert.strictEqual(fetchMock.mock.calls.length, 1);
const [url, init] = fetchMock.mock.calls[0].arguments;

assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/getG1Credits");
assert.strictEqual(init?.method, "POST");
assert.deepStrictEqual(init?.headers, { "content-type": "application/json" });
assert.strictEqual(init?.body, "{}");
});

it("should construct correct URL and headers for checkAppAccess", async () => {
const fetchMock = mock.fn(async (_url: URL | RequestInfo, _init?: RequestInit) => {
// Mock the response structure expected by checkAppAccess
return new Response(JSON.stringify({ accessStatus: "ACCESS_STATUS_OK" }), { status: 200 });
});

const client = new AppCatalystApiClient(fetchMock as any, "https://api.example.com");
await client.checkTos();

assert.strictEqual(fetchMock.mock.calls.length, 1);
const [url, init] = fetchMock.mock.calls[0].arguments;

assert.strictEqual(url.toString(), "https://api.example.com/v1beta1/checkAppAccess");
assert.ok(!init?.method || init.method === "GET", "Method should be GET or undefined");
});
});