Skip to content

Commit

Permalink
PERF-1344: added JWT authentication headers (#496)
Browse files Browse the repository at this point in the history
* PERF-1344: added JWT authentication headers

* PERF-1344: added JWT test

* PERF-1344: refactored structure according to Ilya's comments
  • Loading branch information
VSevostianov authored Feb 17, 2025
1 parent 43dba4e commit 4c9e182
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/client_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export type ClientParams = {
*/
apiKey?: string;

/**
* Header for auth requests, if nothing is set default one will be used.
*/
header?: string;

/**
* Whether to enable response compression (e.g., gzip).
* Defaults to `false` if not specified.
Expand Down
2 changes: 1 addition & 1 deletion src/lokalise/base_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class BaseClient {
const { apiKey } = params;
if (!apiKey || apiKey.trim().length === 0) {
throw new Error(
"Instantiation failed: A non-empty API key must be provided.",
"Instantiation failed: A non-empty API key or JWT must be provided.",
);
}

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
10 changes: 9 additions & 1 deletion test/lokalise/lokalise_api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("LokaliseApi", () => {
it("is expected to throw an error if the API key is not provided", () => {
expect(() => {
new LokaliseApi({ apiKey: "" });
}).toThrow("Instantiation failed: A non-empty API key must be provided.");
}).toThrow("Instantiation failed: A non-empty API key or JWT must be provided.");
});

it("is expected to contain clientData", () => {
Expand All @@ -15,6 +15,14 @@ describe("LokaliseApi", () => {
expect(client.clientData.enableCompression).to.be.false;
expect(client.clientData.version).to.eq("api2");
});

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;
expect(client.clientData.version).to.eq("api2");
});
});

describe("LokaliseApi host", () => {
Expand Down

0 comments on commit 4c9e182

Please sign in to comment.