Skip to content

Commit

Permalink
PERF-1344: refactored structure according to Ilya's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VSevostianov committed Feb 17, 2025
1 parent 201795a commit 4a0ad17
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/interfaces/client_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
14 changes: 3 additions & 11 deletions src/lokalise/base_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/lokalise/lokalise_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/lokalise/lokalise_api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4a0ad17

Please sign in to comment.