OAuth helpers for MyMagti API.
Provides helpers to obtain OAuth tokens by mimicking the mobile app requests. You can swap the fetch implementation for testing.
Mocking fetch for tests
Mocking fetch for tests
import { assertEquals } from "@std/assert"; import { fetchToken } from "@hertzg/mymagti-api/oauth"; const mockFetch: typeof fetch = async (_url, _init) => new Response( JSON.stringify({ access_token: "A", autoLogin: 0, expires_in: 3600, jti: "id", phoneNo: "5xxxxxxxx", refresh_token: "R", scope: "*", token_type: "bearer", userId: 1, userIdentifier: "u", }), { status: 200, headers: { "content-type": "application/json" } }, ); const res = await fetchToken( { type: "login", username: "u", password: "p" }, { fetch: mockFetch }, ); assertEquals(res.result, "success"); if (res.result === "success") { assertEquals(typeof res.data.access_token, "string"); }