Skip to content

Commit 1cfcf73

Browse files
authored
feat(sdk): add whoami support for vNext API (#2031)
1 parent 7054bae commit 1cfcf73

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

.changeset/purple-items-post.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lingo.dev/_sdk": patch
3+
---
4+
5+
Add whoami support for vNext API

packages/sdk/src/index.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,27 @@ describe("ReplexicaEngine", () => {
332332
expect(options.headers["X-API-Key"]).toBe("test-key");
333333
expect(result).toBe("fr");
334334
});
335+
336+
it("whoami should call /users/me with GET and X-API-Key header", async () => {
337+
mockFetch.mockResolvedValue({
338+
ok: true,
339+
json: async () => ({ id: "usr_abc", email: "user@example.com" }),
340+
});
341+
342+
const engine = new LingoDotDevEngine({
343+
apiKey: "test-key",
344+
engineId: "eng_123",
345+
});
346+
347+
const result = await engine.whoami();
348+
349+
const [url, options] = mockFetch.mock.calls[0];
350+
expect(url).toMatch(/\/users\/me$/);
351+
expect(options.method).toBe("GET");
352+
expect(options.headers["X-API-Key"]).toBe("test-key");
353+
expect(options.headers["Authorization"]).toBeUndefined();
354+
expect(result).toEqual({ id: "usr_abc", email: "user@example.com" });
355+
});
335356
});
336357

337358
describe("hints support", () => {

packages/sdk/src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -784,17 +784,14 @@ export class LingoDotDevEngine {
784784
async whoami(
785785
signal?: AbortSignal,
786786
): Promise<{ email: string; id: string } | null> {
787-
if (this.isVNext) {
788-
return { email: "vnext-user", id: this.config.engineId! };
789-
}
787+
const url = this.isVNext
788+
? `${this.config.apiUrl}/users/me`
789+
: `${this.config.apiUrl}/whoami`;
790790

791791
try {
792-
const res = await fetch(`${this.config.apiUrl}/whoami`, {
793-
method: "POST",
794-
headers: {
795-
Authorization: `Bearer ${this.config.apiKey}`,
796-
"Content-Type": "application/json",
797-
},
792+
const res = await fetch(url, {
793+
method: this.isVNext ? "GET" : "POST",
794+
headers: this.headers,
798795
signal,
799796
});
800797

0 commit comments

Comments
 (0)