Skip to content

Commit

Permalink
Merge pull request #808 from supertokens/chore/sdk-logs
Browse files Browse the repository at this point in the history
Chore: Send logs
  • Loading branch information
rishabhpoddar authored Jun 27, 2024
2 parents 7f34265 + baeb758 commit 59ebc6f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
15 changes: 8 additions & 7 deletions v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { getAnalytics } from "./utils";
import { getAnalytics, sendSDKLogsToBackend } from "./utils";

export enum HTTP_REQUEST_ERROR {
SESSION_EXPIRED,
Expand Down Expand Up @@ -105,7 +105,7 @@ export async function simpleGETRequest(url: string, userConfig: any = {}, versio
let response = await axios.get(url, userConfig);
let data = await response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data, headers };
}

Expand All @@ -125,7 +125,7 @@ export async function simplePOSTRequest(url: string, data: any, userConfig: POST
let response = await axios.post(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers, status: response.status, statusText: response.statusText };
}

Expand All @@ -145,7 +145,7 @@ export async function simplePATCHRequest(url: string, data: any, userConfig: PAT
let response = await axios.patch(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers };
}

Expand All @@ -165,7 +165,7 @@ export async function simplePUTRequest(url: string, data: any, userConfig: POSTR
let response = await axios.put(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data: responseData, headers };
}

Expand All @@ -189,11 +189,11 @@ export async function simpleDELETERequest(url: string, userConfig: DELETERequest
let response = await axios.delete(url, userConfig);
let data = await response.data;
let headers = response.headers;
sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
return { data, headers };
}

function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: boolean, headers: any) {
async function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: boolean, headers: any) {
if (!frontTokenExists) {
return;
}
Expand All @@ -205,6 +205,7 @@ function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: boolean
url,
headers
});
await sendSDKLogsToBackend()
}
}

Expand Down
67 changes: 67 additions & 0 deletions v2/src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import axios from "axios";

declare global {
interface Window {
Expand Down Expand Up @@ -106,4 +107,70 @@ export const recursiveMapAllChildren = (

export function mockDelay(timeout = 2000) {
return new Promise(resolve => setTimeout(resolve, timeout));
}

export function isDev() {
return window.location.host.startsWith("localhost");
}


const isSuperTokensSDKLog = (data: any) => {
return typeof data === "string" && data.includes("supertokens-website-ver:");
};

type ConsoleLog = typeof globalThis.console.log;

export const overrideConsoleImplementation = (customImplementation: ConsoleLog) => {
const oldConsoleLogImplementation = globalThis.console.log;
globalThis.console.log = (data) => {
customImplementation(data, oldConsoleLogImplementation);
};
};

export const SDK_LOGS_STORAGE_KEY = "Supertokens-sdk-logs";

export const saveSDKLogsConsoleOverride = (data: any, oldConsoleImplementation: ConsoleLog) => {
if (isSuperTokensSDKLog(data)) {
const logArrayStr = localStorage.getItem(SDK_LOGS_STORAGE_KEY) || "[]";
const logArray = JSON.parse(logArrayStr) as string[];

if (logArray.length === 1000) {
logArray.shift();
}
logArray.push(data);

localStorage.setItem(SDK_LOGS_STORAGE_KEY, JSON.stringify(logArray));
} else {
oldConsoleImplementation(data);
}
};

export async function sendSDKLogsToBackend() {
const sdkLogs = localStorage.getItem(SDK_LOGS_STORAGE_KEY) || "[]";
const parsedSDKLogs = JSON.parse(sdkLogs);

if (isDev()) {
console.log(parsedSDKLogs, "auth_error_sdk_logs");
localStorage.removeItem(SDK_LOGS_STORAGE_KEY);
} else {
await axios
.post(
"https://api.supertokens.com/0/antcs/ents",
{
eventName: "auth_error_sdk_logs",
data: {
version: "1",
userId: "1",
timestamp: Date.now(),
page: "",
type: "auth_error_sdk_logs",
logs: parsedSDKLogs,
},
},
{}
)
.then(() => {
localStorage.removeItem(SDK_LOGS_STORAGE_KEY);
});
}
}
4 changes: 3 additions & 1 deletion v2/src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Head from '@docusaurus/Head';
import { useLocation } from '@docusaurus/router';
import './styles.css';
import supertokens from "supertokens-website";
import {overrideConsoleImplementation,saveSDKLogsConsoleOverride} from '../../components/utils'
import styles from "./styles.module.css";


Expand All @@ -31,11 +32,12 @@ if (typeof window !== 'undefined') {
API_DOMAIN = "https://dev.api.supertokens.com"
API_BASE_PATH = "/0/auth"
}

overrideConsoleImplementation(saveSDKLogsConsoleOverride);
let sessionExpiredStatusCode = 401;
supertokens.init({
apiDomain: API_DOMAIN,
apiBasePath: API_BASE_PATH,
enableDebugLogs:true,
sessionExpiredStatusCode,
preAPIHook: async (context) => {
return {
Expand Down

0 comments on commit 59ebc6f

Please sign in to comment.