From 76027b9bc16e777cb8424c3f3eabb91ec138a0a7 Mon Sep 17 00:00:00 2001 From: Mikkel ALMONTE--RINGAUD Date: Wed, 18 Sep 2024 11:01:46 +0200 Subject: [PATCH] feat: add `extractActivationURL` step in case it needs to be separated --- examples/mobile.ts | 3 ++- src/api/tokenize.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/mobile.ts b/examples/mobile.ts index 9cbd44d..eb041d8 100644 --- a/examples/mobile.ts +++ b/examples/mobile.ts @@ -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... diff --git a/src/api/tokenize.ts b/src/api/tokenize.ts index e6d016c..acbfa7b 100644 --- a/src/api/tokenize.ts +++ b/src/api/tokenize.ts @@ -8,7 +8,7 @@ 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 => { let response = await fetcher({ url: new URL(url), redirect: "manual" }); const location = getHeaderFromResponse(response, "Location"); @@ -16,9 +16,13 @@ export const tokenize = async (url: string, fetcher: Fetcher = defaultFetcher) = throw new Error("URL to tokenize expired"); } + return location; +}; + +export const tokenize = async (url: string, fetcher: Fetcher = defaultFetcher) => { // encoded like this: // izly://SBSCR// - const parts = location.split("/"); + const parts = url.split("/"); const code = parts.pop()!; const identifier = parts.pop()!; @@ -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, "", ""); if (!result) throw new Error("No found in response");