Skip to content

Commit

Permalink
feat(viewer): add customheaders
Browse files Browse the repository at this point in the history
  • Loading branch information
icaparros-at-wiris committed Jan 19, 2024
1 parent f1b6d48 commit 3d24b09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/viewer/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ main(window);
*/
async function main(w: Window): Promise<void> {

const properties: Properties = await Properties.generate();
const properties: Properties = await Properties.getInstance();

// Expose the globals to the browser
if (!w.viewer) {
Expand Down
45 changes: 28 additions & 17 deletions packages/viewer/src/properties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { configurationJson, StatusError } from './services';
import Util from '@wiris/mathtype-html-integration-devkit/src/util';

// Helper types for Config below
type Viewer = 'none' | 'image' | 'mathml' | 'latex';
Expand All @@ -13,6 +14,7 @@ export type Config = {
backendConfig?: {
wirispluginperformance?: Wirispluginperformance,
wiriseditormathmlattribute?: string,
wiriscustomheaders?: object,
},
dpi?: number,
element?: string,
Expand All @@ -29,7 +31,8 @@ const defaultValues: Config = {
editorServicesExtension: '',
backendConfig: {
wirispluginperformance: 'true',
wiriseditormathmlattribute: 'data-mathml'
wiriseditormathmlattribute: 'data-mathml',
wiriscustomheaders: {},
},
dpi: 96,
element: 'body',
Expand All @@ -43,6 +46,8 @@ const defaultValues: Config = {
*/
export class Properties {

private static instance: Properties | null = null;

render: () => Promise<void> = async () => {};

// Get URL properties (retrocompatibility).
Expand All @@ -54,13 +59,18 @@ export class Properties {
*/
private new() {}

static getInstance(): Properties {
if (!Properties.instance) {
Properties.instance = new Properties();
Properties.instance.initialize();
}
return Properties.instance;
}

/**
* Creates and sets up a new instance of class Properties
*/
static async generate(): Promise<Properties> {

const instance = new Properties();

private async initialize(): Promise<void> {
// Get URL parameters from <script>
const pluginName = 'WIRISplugins.js';
const script: HTMLScriptElement = document.querySelector(`script[src*="${pluginName}"]`);
Expand All @@ -71,31 +81,34 @@ export class Properties {
const urlParams = new URLSearchParams(params);

if (urlParams.get('dpi') !== null && urlParams.get('dpi') !== undefined) {
instance.config.dpi = +urlParams.get('dpi');
Properties.instance.config.dpi = +urlParams.get('dpi');
}
if (urlParams.get('element') !== null && urlParams.get('element') !== undefined) {
instance.config.element = urlParams.get('element');
Properties.instance.config.element = urlParams.get('element');
}
if (urlParams.get('lang') !== null && urlParams.get('lang') !== undefined) {
instance.config.lang = urlParams.get('lang');
Properties.instance.config.lang = urlParams.get('lang');
}
if (urlParams.get('viewer') !== null && urlParams.get('viewer') !== undefined) {
instance.config.viewer = (urlParams.get('viewer') as Viewer);
Properties.instance.config.viewer = (urlParams.get('viewer') as Viewer);
}
if (urlParams.get('zoom') !== null && urlParams.get('zoom') !== undefined) {
instance.config.zoom = +urlParams.get('zoom');
Properties.instance.config.zoom = +urlParams.get('zoom');
}
}

instance.checkServices();
Properties.instance.checkServices();

// Get backend parameters calling the configurationjson service
try {
instance.config.backendConfig = await configurationJson(
['wirispluginperformance', 'wiriseditormathmlattribute'],
instance.editorServicesRoot,
instance.editorServicesExtension,
Properties.instance.config.backendConfig = await configurationJson(
['wirispluginperformance', 'wiriseditormathmlattribute', 'wiriscustomheaders'],
Properties.instance.editorServicesRoot,
Properties.instance.editorServicesExtension
);

// [TODO]
Properties.instance.config.backendConfig.wiriscustomheaders = Util.convertStringToObject(Properties.instance.config.backendConfig.wiriscustomheaders);
} catch(e) {
if (e instanceof StatusError) {
// Do nothing; leave default values.
Expand All @@ -104,8 +117,6 @@ export class Properties {
throw e;
}
}

return instance;
}

/**
Expand Down
13 changes: 10 additions & 3 deletions packages/viewer/src/services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Parser from '@wiris/mathtype-html-integration-devkit/src/parser';
import { Properties } from './properties';

enum MethodType {
Post = "POST",
Expand Down Expand Up @@ -49,11 +50,17 @@ export async function processJsonResponse(response: Promise<Response>): Promise<
export async function callService(query: object, serviceName: string, method: MethodType, serverURL: string, extension: string) : Promise<any> {
try {
const url = new URL(serviceName + extension, serverURL);

const properties = Properties.getInstance();
const headers = {
'Content-type': 'application/x-www-form-urlencoded; charset=utf-8',
...properties.config.backendConfig.wiriscustomheaders
}

// const test = Properties.
const init: RequestInit = {
method,
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=utf-8',
},
headers
};

if (method === MethodType.Get) {
Expand Down

0 comments on commit 3d24b09

Please sign in to comment.