<commit_before>// MIT © 2017 azu

import * as ClientOAuth2 from "client-oauth2";
import { Token } from "client-oauth2";

const inoreaderAuth = new ClientOAuth2({
    clientId: process.env.REACT_APP_INOREADER_CLIENT_ID,
    clientSecret: process.env.REACT_APP_INOREADER_CLIENT_KEY,
    accessTokenUri: process.env.REACT_APP_INOREADER_ACCESS_TOKEN_URL,
    authorizationUri: "https://www.inoreader.com/oauth2/auth",
    redirectUri: process.env.PUBLIC_URL,
    scopes: ["read", "write"],
    state: "inoreader"
});

export interface TokenJSON {
    accessToken: string;
    refreshToken: string;
    tokenType: string;
    expires: Date;
}

export const saveToken = (token: Token) => {
    let tokenJSOn = {
        accessToken: token.accessToken,
        refreshToken: token.refreshToken,
        tokenType: token.tokenType,
        expires: (token as any).expires
    };
    localStorage.setItem("inoreader-token", JSON.stringify(tokenJSOn));
};
export const loadToken = (): TokenJSON | undefined => {
    const savedTokenString = localStorage.getItem("inoreader-token");
    if (!savedTokenString) {
        return;
    }
    const parsed = JSON.parse(savedTokenString);
    return {
        accessToken: parsed.accessToken,
        refreshToken: parsed.refreshToken,
        tokenType: parsed.tokenType,
        expires: new Date(parsed.expires)
    };
};
export const saveTokenFromCallbackURL = (url: string) => {
    return inoreaderAuth.code.getToken(url).then(token => {
        saveToken(token);
        return token;
    });
};
export const getToken = () => {
    console.info("process.env", {
        clientId: process.env.REACT_APP_INOREADER_CLIENT_ID,
        clientSecret: process.env.REACT_APP_INOREADER_CLIENT_KEY,
        accessTokenUri: process.env.REACT_APP_INOREADER_ACCESS_TOKEN_URL,
        redirectUri: process.env.PUBLIC_URL
    });
    const parsed = require("url").parse(process.env.PUBLIC_URL);
    console.log("parsed", parsed);
    const savedTokenJSON = loadToken();
    if (!savedTokenJSON) {
        return Promise.reject(new Error("Token is not found"));
    }
    const token = inoreaderAuth.createToken(
        savedTokenJSON.accessToken,
        savedTokenJSON.refreshToken,
        savedTokenJSON.tokenType,
        {}
    );
    token.expiresIn(savedTokenJSON.expires);
    // Refresh the current users access token.
    if (token.expired()) {
        return token.refresh().then(function(updatedToken) {
            saveToken(updatedToken);
            return updatedToken;
        });
    }
    return Promise.resolve(token);
};

export const getNewTokenUrl = () => {
    return inoreaderAuth.code.getUri();
};<commit_msg>add slash<commit_after>// MIT © 2017 azu

import * as ClientOAuth2 from "client-oauth2";
import { Token } from "client-oauth2";

const inoreaderAuth = new ClientOAuth2({
    clientId: process.env.REACT_APP_INOREADER_CLIENT_ID,
    clientSecret: process.env.REACT_APP_INOREADER_CLIENT_KEY,
    accessTokenUri: process.env.REACT_APP_INOREADER_ACCESS_TOKEN_URL,
    authorizationUri: "https://www.inoreader.com/oauth2/auth",
    redirectUri: `${process.env.PUBLIC_URL}/`,
    scopes: ["read", "write"],
    state: "inoreader"
});

export interface TokenJSON {
    accessToken: string;
    refreshToken: string;
    tokenType: string;
    expires: Date;
}

export const saveToken = (token: Token) => {
    let tokenJSOn = {
        accessToken: token.accessToken,
        refreshToken: token.refreshToken,
        tokenType: token.tokenType,
        expires: (token as any).expires
    };
    localStorage.setItem("inoreader-token", JSON.stringify(tokenJSOn));
};
export const loadToken = (): TokenJSON | undefined => {
    const savedTokenString = localStorage.getItem("inoreader-token");
    if (!savedTokenString) {
        return;
    }
    const parsed = JSON.parse(savedTokenString);
    return {
        accessToken: parsed.accessToken,
        refreshToken: parsed.refreshToken,
        tokenType: parsed.tokenType,
        expires: new Date(parsed.expires)
    };
};
export const saveTokenFromCallbackURL = (url: string) => {
    return inoreaderAuth.code.getToken(url).then(token => {
        saveToken(token);
        return token;
    });
};
export const getToken = () => {
    const savedTokenJSON = loadToken();
    if (!savedTokenJSON) {
        return Promise.reject(new Error("Token is not found"));
    }
    const token = inoreaderAuth.createToken(
        savedTokenJSON.accessToken,
        savedTokenJSON.refreshToken,
        savedTokenJSON.tokenType,
        {}
    );
    token.expiresIn(savedTokenJSON.expires);
    // Refresh the current users access token.
    if (token.expired()) {
        return token.refresh().then(function(updatedToken) {
            saveToken(updatedToken);
            return updatedToken;
        });
    }
    return Promise.resolve(token);
};

export const getNewTokenUrl = () => {
    return inoreaderAuth.code.getUri();
};
{'fraction_non_alphanumeric': 0.11687259469313348, 'fraction_numerical': 0.0036459388292485314, 'mean_word_length': 3.1989795918367347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2720051', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1515, 'n_words': 291}