From 4a0ad175ee0d34ba6a96726a7f9f7c21466ea7c4 Mon Sep 17 00:00:00 2001 From: VSevostyanov Date: Mon, 17 Feb 2025 14:16:52 +0100 Subject: [PATCH] PERF-1344: refactored structure according to Ilya's comments --- src/interfaces/client_params.ts | 4 ++-- src/lokalise/base_client.ts | 14 +++----------- src/lokalise/lokalise_api.ts | 1 + test/lokalise/lokalise_api.spec.ts | 4 ++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/interfaces/client_params.ts b/src/interfaces/client_params.ts index 09b8cdf3..f829dc65 100644 --- a/src/interfaces/client_params.ts +++ b/src/interfaces/client_params.ts @@ -8,9 +8,9 @@ export type ClientParams = { apiKey?: string; /** - * The JWT for authenticating requests. Either this or api key must be provided. + * Header for auth requests, if nothing is set default one will be used. */ - jwt?: string; + header?: string; /** * Whether to enable response compression (e.g., gzip). diff --git a/src/lokalise/base_client.ts b/src/lokalise/base_client.ts index bfb99abd..87c696c3 100644 --- a/src/lokalise/base_client.ts +++ b/src/lokalise/base_client.ts @@ -23,22 +23,14 @@ export class BaseClient { * @throws Error if the API key is not provided or is empty. */ constructor(params: ClientParams) { - const { apiKey, jwt } = params; - if ((!apiKey || apiKey.trim().length === 0) && (!jwt || jwt.trim().length === 0)) { + const { apiKey } = params; + if (!apiKey || apiKey.trim().length === 0) { throw new Error( "Instantiation failed: A non-empty API key or JWT must be provided.", ); } - if (apiKey && apiKey.trim().length > 0) { - this.clientData.token = apiKey; - } - - if (jwt && jwt.trim().length > 0) { - this.clientData.token = jwt; - this.clientData.authHeader = "Authorization"; - } - + this.clientData.token = apiKey; this.clientData.enableCompression = params.enableCompression ?? false; this.clientData.tokenType = params.tokenType ?? ""; this.clientData.host = params.host; diff --git a/src/lokalise/lokalise_api.ts b/src/lokalise/lokalise_api.ts index b04a9018..5ca45679 100644 --- a/src/lokalise/lokalise_api.ts +++ b/src/lokalise/lokalise_api.ts @@ -43,6 +43,7 @@ export class LokaliseApi extends BaseClient { // Default to "api2" version if not explicitly provided this.clientData.version = params.version ?? "api2"; + this.clientData.authHeader = params.header ?? this.clientData.authHeader; } /** diff --git a/test/lokalise/lokalise_api.spec.ts b/test/lokalise/lokalise_api.spec.ts index 1b741c89..b4142855 100644 --- a/test/lokalise/lokalise_api.spec.ts +++ b/test/lokalise/lokalise_api.spec.ts @@ -16,8 +16,8 @@ describe("LokaliseApi", () => { expect(client.clientData.version).to.eq("api2"); }); - it("is expected to contain clientData when jwt is provided", () => { - const client = new LokaliseApi({ jwt: process.env.API_KEY }); + it("is expected to contain custom header", () => { + const client = new LokaliseApi({ apiKey: process.env.API_KEY, header:"Authorization" }); expect(client.clientData.token).to.eq(process.env.API_KEY); expect(client.clientData.authHeader).to.eq("Authorization"); expect(client.clientData.enableCompression).to.be.false;