Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create new Metabase client #3970

Merged
merged 18 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
118 changes: 117 additions & 1 deletion api.planx.uk/modules/analytics/metabase/shared/client.test.ts
Original file line number Diff line number Diff line change
@@ -1 +1,117 @@
test.todo("should test configuration and errors");
import axios from "axios";
import {
validateConfig,
createMetabaseClient,
MetabaseError,
} from "./client.js";
import nock from "nock";

const axiosCreateSpy = vi.spyOn(axios, "create");

Check warning on line 9 in api.planx.uk/modules/analytics/metabase/shared/client.test.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

'axiosCreateSpy' is assigned a value but never used. Allowed unused vars must match /^_/u

describe("Metabase client", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
});

afterEach(() => {
vi.unstubAllEnvs();
});

test("returns configured client", async () => {
const client = createMetabaseClient();
expect(client.defaults.headers["X-API-Key"]).toBe(
process.env.METABASE_API_KEY,
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved
);
});

describe("validates configuration", () => {
test("throws error when URL_EXT is missing", () => {
vi.stubEnv("METABASE_URL_EXT", undefined);
expect(() => validateConfig()).toThrow(
"Missing environment variable 'METABASE_URL_EXT'",
);
});

test("throws error when API_KEY is missing", () => {
vi.stubEnv("METABASE_API_KEY", undefined);
expect(() => validateConfig()).toThrow(

Check failure on line 38 in api.planx.uk/modules/analytics/metabase/shared/client.test.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > validates configuration > throws error when API_KEY is missing

AssertionError: expected [Function] to throw error including 'Missing environment variable \'METABA…' but got 'Missing environment variable \'METABA…' Expected: "Missing environment variable 'METABASE_API_KEY'" Received: "Missing environment variable 'METABASE_URL_EXT'" ❯ modules/analytics/metabase/shared/client.test.ts:38:38
"Missing environment variable 'METABASE_API_KEY'",
);
});

test("returns valid config object", () => {
const config = validateConfig();
expect(config).toMatchObject({
baseURL: process.env.METABASE_URL_EXT,
apiKey: process.env.METABASE_API_KEY,
timeout: 30_000,
retries: 3,
});
});
});

describe("Error handling", () => {
test("retries then succeeds on 5xx errors", async () => {
const baseURL = process.env.METABASE_URL_EXT;
if (!baseURL) {
throw new Error("METABASE_URL_EXT must be defined for tests");

Check failure on line 58 in api.planx.uk/modules/analytics/metabase/shared/client.test.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > Error handling > retries then succeeds on 5xx errors

Error: METABASE_URL_EXT must be defined for tests ❯ modules/analytics/metabase/shared/client.test.ts:58:15
}

const metabaseScope = nock(baseURL);
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved

metabaseScope
.get("/test")
.reply(500, { message: "Internal Server Error" })
.get("/test")
.reply(200, { data: "success" });

const client = createMetabaseClient();
const response = await client.get("/test");

expect(response.data).toEqual({ data: "success" });
expect(metabaseScope.isDone()).toBe(true);
});

test("throws an error if all requests fail", async () => {
const metabaseScope = nock(process.env.METABASE_URL_EXT!);

Check failure on line 77 in api.planx.uk/modules/analytics/metabase/shared/client.test.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > Error handling > throws an error if all requests fail

TypeError: Invalid URL ❯ normalizeUrl node_modules/.pnpm/[email protected]/node_modules/nock/lib/scope.js:33:25 ❯ new Scope node_modules/.pnpm/[email protected]/node_modules/nock/lib/scope.js:95:23 ❯ Proxy.module.exports node_modules/.pnpm/[email protected]/node_modules/nock/index.js:21:41 ❯ modules/analytics/metabase/shared/client.test.ts:77:29 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { input: 'undefined', code: 'ERR_INVALID_URL' }

metabaseScope
.get("/test")
.times(4)
.reply(500, { message: "Internal Server Error" });

const client = createMetabaseClient();

try {
await client.get("/test");
expect.fail("Should have thrown an error");
} catch (error) {
expect(error).toBeInstanceOf(MetabaseError);
expect((error as MetabaseError).statusCode).toBe(500);
expect(metabaseScope.isDone()).toBe(true);
}
});

test("does not retry on non-5xx errors", async () => {
const metabaseScope = nock(process.env.METABASE_URL_EXT!);

Check failure on line 97 in api.planx.uk/modules/analytics/metabase/shared/client.test.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > Error handling > does not retry on non-5xx errors

TypeError: Invalid URL ❯ normalizeUrl node_modules/.pnpm/[email protected]/node_modules/nock/lib/scope.js:33:25 ❯ new Scope node_modules/.pnpm/[email protected]/node_modules/nock/lib/scope.js:95:23 ❯ Proxy.module.exports node_modules/.pnpm/[email protected]/node_modules/nock/index.js:21:41 ❯ modules/analytics/metabase/shared/client.test.ts:97:29 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { input: 'undefined', code: 'ERR_INVALID_URL' }

metabaseScope.get("/test").once().reply(200, { data: "success" });

const client = createMetabaseClient();
const response = await client.get("/test");

expect(response.data).toEqual({ data: "success" });

// All expected requests were made
expect(metabaseScope.isDone()).toBe(true);

// No pending mocks left
expect(metabaseScope.pendingMocks()).toHaveLength(0);

// Double check that no other requests were intercepted
const requestCount = metabaseScope.activeMocks().length;
expect(requestCount).toBe(0);
});
});
});
129 changes: 129 additions & 0 deletions api.planx.uk/modules/analytics/metabase/shared/client.ts
Original file line number Diff line number Diff line change
@@ -1,0 +1,129 @@
import axios from "axios";
import type {
AxiosInstance,
AxiosError,
InternalAxiosRequestConfig,
} from "axios";

// Custom error class for Metabase-specific errors
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved
export class MetabaseError extends Error {
constructor(
message: string,
public statusCode?: number,
public response?: unknown,
) {
super(message);
this.name = "MetabaseError";
}
}

interface MetabaseConfig {
baseURL: string;
apiKey: string;
timeout?: number;
retries?: number;
}

// Validate environment variables
export const validateConfig = (): MetabaseConfig => {
const baseURL = process.env.METABASE_URL_EXT;
const apiKey = process.env.METABASE_API_KEY;

const METABASE_TIMEOUT = 30_000;
const METABASE_MAX_RETRIES = 3;

assert(baseURL, "Missing environment variable 'METABASE_URL_EXT'");

Check failure on line 35 in api.planx.uk/modules/analytics/metabase/shared/client.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > returns configured client

AssertionError: Missing environment variable 'METABASE_URL_EXT' ❯ validateConfig modules/analytics/metabase/shared/client.ts:35:3 ❯ Module.createMetabaseClient modules/analytics/metabase/shared/client.ts:53:18 ❯ modules/analytics/metabase/shared/client.test.ts:22:20

Check failure on line 35 in api.planx.uk/modules/analytics/metabase/shared/client.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

modules/analytics/metabase/shared/client.test.ts > Metabase client > validates configuration > returns valid config object

AssertionError: Missing environment variable 'METABASE_URL_EXT' ❯ Module.validateConfig modules/analytics/metabase/shared/client.ts:35:3 ❯ modules/analytics/metabase/shared/client.test.ts:44:22
assert(apiKey, "Missing environment variable 'METABASE_API_KEY'");

return {
baseURL,
apiKey,
timeout: METABASE_TIMEOUT,
retries: METABASE_MAX_RETRIES,
};
};

// Extended request config to include retry count
interface ExtendedAxiosRequestConfig extends InternalAxiosRequestConfig {
retryCount?: number;
}

// Create and configure Axios instance
export const createMetabaseClient = (): AxiosInstance => {
const config = validateConfig();

const client = axios.create({
baseURL: config.baseURL,
headers: {
"X-API-Key": config.apiKey,
"Content-Type": "application/json",
},
timeout: config.timeout,
});

client.interceptors.response.use(
(response) => {
return response;
},
async (error: AxiosError) => {
const originalRequest = error.config as ExtendedAxiosRequestConfig;

if (!originalRequest) {
throw new MetabaseError("No request config available");
}

// Initialise retry count if not present
if (typeof originalRequest.retryCount === "undefined") {
originalRequest.retryCount = 0;
}

// Handle retry logic
if (error.response) {
// Retry on 5xx errors
DafyddLlyr marked this conversation as resolved.
Show resolved Hide resolved
if (
error.response.status >= 500 &&
originalRequest.retryCount < (config.retries ?? 3)
) {
originalRequest.retryCount++;
return client.request(originalRequest);
}

// Transform error response
const errorMessage =
typeof error.response.data === "object" &&
error.response.data !== null &&
"message" in error.response.data
? String(error.response.data.message)
: "Metabase request failed";

throw new MetabaseError(
errorMessage,
error.response.status,
error.response.data,
);
}

// Handle network errors
if (error.request) {
throw new MetabaseError(
"Network error occurred",
undefined,
error.request,
);
}

// Handle other errors
throw new MetabaseError(error.message);
},
);

return client;
};

// // Export both client and instance with delayed instantiation for test purposes
// export let metabaseClient: AxiosInstance;

// export const initializeMetabaseClient = () => {
// metabaseClient = createMetabaseClient();
// return metabaseClient;
// };
Loading