import axios from "axios"; export interface IJitzSPContext { client: IJitzSPHttpClient; siteUrl: string; init: () => void; } export class JitzSPContext implements IJitzSPContext { public client: IJitzSPHttpClient; public siteUrl: string; constructor(siteUrl: string) { this.siteUrl = siteUrl; this.client = new JitzSPHttpClient(siteUrl); } public init = async () => { await this.client.init(); }; } export interface IJitzSPHttpClient { init: () => void; get: (url: string, header?: any) => any; post: (url: string, data: any, header?: any) => any; postWithOutStringify: (url: string, data: any, header?: any) => any; } export default class JitzSPHttpClient implements IJitzSPHttpClient { public siteUrl: string; public client: any; public securityToken: string = ""; private header: any = { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", }; constructor(siteUrl: string = "") { this.siteUrl = siteUrl; this.securityToken = ""; this.client = axios.create({ baseURL: this.siteUrl, }); } public init = async () => { await this.refreshSecurityValidation(); }; public get = (url: string, header?: any) => { header = header || this.header; header["X-RequestDigest"] = this.securityToken; var authOptions = { method: "GET", url: url, data: JSON.stringify({}), headers: header, json: true, }; return this.client(authOptions); // return this.getSecurityValidation().then((ctx: any) => { // this.securityToken = ctx.data.d.GetContextWebInformation.FormDigestValue; // header["X-RequestDigest"] = this.securityToken; // var authOptions = { // method: "GET", // url: url, // data: JSON.stringify({}), // headers: header, // json: true, // }; // return this.client(authOptions); // }); }; public post = (url: string, data: any, header?: any) => { // debugger; header = header || this.header; header["X-RequestDigest"] = this.securityToken; var authOptions = { method: "POST", url: url, data: JSON.stringify(data), headers: header, json: true, }; return this.client(authOptions); // return this.getSecurityValidation().then((ctx: any) => { // this.securityToken = ctx.data.d.GetContextWebInformation.FormDigestValue; // header["X-RequestDigest"] = this.securityToken; // var authOptions = { // method: "POST", // url: url, // data: JSON.stringify(data), // headers: header, // json: true, // }; // return this.client(authOptions); // }); }; public postWithOutStringify = (url: string, data: any, header?: any) => { // debugger; header = header || this.header; header["X-RequestDigest"] = this.securityToken; var authOptions = { method: "POST", url: url, data: data, headers: header, json: true, }; return this.client(authOptions); }; public getSecurityValidation = () => { var authOptions = { method: "POST", url: "/_api/contextinfo", data: JSON.stringify({}), headers: { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", }, json: true, }; return this.client(authOptions); }; public refreshSecurityValidation = async () => { var authOptions = { method: "POST", url: "/_api/contextinfo", data: JSON.stringify({}), headers: { Accept: "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", }, json: true, }; await this.client(authOptions).then((context: any) => { this.securityToken = context.data.d.GetContextWebInformation.FormDigestValue; // console.log("Token:- "+this.securityToken); var interval = setInterval(() => { this.refreshSecurityValidation(); clearInterval(interval); }, 600000); //300000 }); }; }