bpixrm
Version:
Bpifrance XRM Package
154 lines (115 loc) • 6.19 kB
text/typescript
import { IType, E401, E404, EException} from './index';
class IDynamicsBaseClass {
private accessToken: string = "";
private ressourceId: string = "";
private ressourceRefBase: string = "";
private endPointLink: string = "";
private top: number = 100;
// ressourceId: string = adalConfig.resourceId
// ressourceRefBase = "/api/data/v8.2/"
/**
* Creates a new instance of Dynamics.
* @class
*
* @param {string} accessToken - a valid accessToken generated by Ms Service
* @param {string} ressourceId - The ressource id is the base of Dynamics env - Each env use a specific ressourceID - For example, 'https://xxxx-dev-v2.crm4.dynamics.com'design the dev env forn dynamics
* @param {string} ressourceRefBase - a part of the link used to access the MS Web API - for example, "/api/data/v8.2/" design the use of the 8.2 version of MS Web API
*
*/
constructor(accessToken: string, ressourceId: string, ressourceRefBase: string) {
this.accessToken = accessToken;
this.ressourceId = ressourceId;
this.ressourceRefBase = ressourceRefBase;
this.endPointLink = ressourceId + ressourceRefBase;
}
protected static getHeaders($accessToken: string): [] {
return {
'Content-Type': 'application/json;charset=utf8',
'Authorization': `Bearer ` + $accessToken,
'Prefer': 'odata.include-annotations="OData.Community.Display.V1.FormattedValue"',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0'
}
}
protected static getIType(item: any, fieldname: string, defaultValue: any = undefined, defaultFormatted: any = undefined): IType {
const value = (item[fieldname] == null) ? defaultValue : item[fieldname];
const formatted = (item[fieldname + "@OData.Community.Display.V1.FormattedValue"] == null) ? defaultFormatted : item[fieldname + "@OData.Community.Display.V1.FormattedValue"];
return {
value: value, //item[fieldname],
formatted: formatted, ///item[fieldname + "@OData.Community.Display.V1.FormattedValue"],
} as IType;
}
protected static getItem(item: any, fieldname: string, defaultValue: any = undefined): string {
if (item === undefined) return defaultValue;
return (item[fieldname] === null) ? defaultValue : item[fieldname];
}
//================================================================================================================
private static async myFetch(url: string, headers: []): Promise<void | any> {
console.log("IDynamicsService", "myFetch", "url", {url: url});
return fetch(url, {
headers: headers
})
.then(response => {
if (response.status == 401) {
console.log("IDynamicsService", "myFetch", "url", url);
console.log("IDynamicsService", "myFetch", "response", response);
console.log("IDynamicsService", "myFetch", "Errreur de type 401");
throw new E401("401 - url: " + url, {response});
//Promise.reject(new E401("401 - url: " + url, {response}));
} else if (response.status == 404) {
console.log("IDynamicsService", "myFetch", "url", url);
console.log("IDynamicsService", "myFetch", "response", response);
throw new E404("404 - url: " + url, {response});
//Promise.reject(new E404("404 - url: " + url));
} else if (response.status !== 200) {
console.log("IDynamicsService", "myFetch", "url", url);
console.log("IDynamicsService", "myFetch", "response", response);
console.log("Error response pas bon:", response.status);
}
return response.json()
})
}
//================================================================================================================
protected async getService(serviceName: string, headers: [] = IDynamicsBaseClass.getHeaders(this.accessToken)): Promise<void | any> {
const url:string = this.endPointLink + serviceName;
console.log("IDynamicsService", "getService", "serviceName", serviceName);
console.log("IDynamicsService", "getService", "url", url);
return IDynamicsBaseClass.myFetch(url, headers);
}
//================================================================================================================
/**
* This function let you get the GUID of the active user
*
* @returns {Promise<void | string>} Promise object represent the GUID of the active user
*/
public async whoAmI(): Promise<void | string> {
let url: string = `WhoAmI`;
console.log("IDynamicsService", "whoAmI", "url", url);
return this.getService(url)
.then(data => {
console.log("IDynamicsService", "whoAmI", "data", data);
return data.UserId;
})
}
//================================================================================================================
protected getAccessToken(): string{
return this.accessToken;
}
//================================================================================================================
protected getEndPointLink(): string{
return this.endPointLink;
}
//================================================================================================================
protected getTop(): number{
return this.top;
}
//================================================================================================================
protected getRessourceId():string{
return this.ressourceId;
}
//================================================================================================================
protected getRessourceBase():string{
return this.ressourceRefBase;
}
}
export { IDynamicsBaseClass };