Skip to content

Commit

Permalink
feat: add extractActivationURL step in case it needs to be separated
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Sep 18, 2024
1 parent 6252ae5 commit 76027b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void async function main () {
const url = prompt(`Please, enter the URL you want to tokenize for UID '${uid}':`)?.trim();
if (!url) throw new Error("No URL provided");

const { identification, profile, configuration, balance } = await izly.tokenize(url);
const activationURL = await izly.extractActivationURL(url);
const { identification, profile, configuration, balance } = await izly.tokenize(activationURL);

// Let's save the auth object for usage in other files without
// re-doing the whole authentication...
Expand Down
10 changes: 7 additions & 3 deletions src/api/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ import { decodeBalance } from "~/decoders/balance";
import type { Profile, Identification, Configuration } from "~/models";
// import { setDeviceToken } from "./private/set-device-token";

export const tokenize = async (url: string, fetcher: Fetcher = defaultFetcher) => {
export const extractActivationURL = async (url: string, fetcher: Fetcher = defaultFetcher): Promise<string> => {
let response = await fetcher({ url: new URL(url), redirect: "manual" });
const location = getHeaderFromResponse(response, "Location");

if (!location) {
throw new Error("URL to tokenize expired");
}

return location;
};

export const tokenize = async (url: string, fetcher: Fetcher = defaultFetcher) => {
// encoded like this:
// izly://SBSCR/<identifier>/<code>
const parts = location.split("/");
const parts = url.split("/");
const code = parts.pop()!;
const identifier = parts.pop()!;

Expand Down Expand Up @@ -50,7 +54,7 @@ export const tokenize = async (url: string, fetcher: Fetcher = defaultFetcher) =
method: "POST"
};

response = await fetcher(request);
const response = await fetcher(request);

const result = findValueBetween(response.content, "<LogonResult>", "</LogonResult>");
if (!result) throw new Error("No <LogonResult> found in response");
Expand Down

0 comments on commit 76027b9

Please sign in to comment.