From eccec228ed5eca13db0dc69302b067fc2e7a56be Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Tue, 26 Feb 2019 17:24:55 +0100
Subject: [PATCH] better typing for ajaxGet

---
 src/examples/proteopedia-wrapper/helpers.ts   |  2 +-
 src/examples/proteopedia-wrapper/index.ts     |  2 +-
 .../pdbe/structure-quality-report.ts          |  4 +--
 src/mol-model-props/rcsb/assembly-symmetry.ts |  4 +--
 src/mol-plugin/behavior/dynamic/volume.ts     |  2 +-
 src/mol-plugin/context.ts                     |  8 +-----
 src/mol-plugin/state/transforms/data.ts       |  2 +-
 src/mol-util/data-source.ts                   | 25 +++++++++++--------
 src/mol-util/graphql-client.ts                |  6 ++---
 9 files changed, 27 insertions(+), 28 deletions(-)

diff --git a/src/examples/proteopedia-wrapper/helpers.ts b/src/examples/proteopedia-wrapper/helpers.ts
index 913465a6f..d3154f7c1 100644
--- a/src/examples/proteopedia-wrapper/helpers.ts
+++ b/src/examples/proteopedia-wrapper/helpers.ts
@@ -21,7 +21,7 @@ export namespace ModelInfo {
         if (model.label.length <= 3) return void 0;
         try {
             const id = model.label.toLowerCase();
-            const src = await ctx.runTask(ctx.fetch(`https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/${id}`)) as string;
+            const src = await ctx.runTask(ctx.fetch({ url: `https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary/${id}` })) as string;
             const json = JSON.parse(src);
             const data = json && json[id];
 
diff --git a/src/examples/proteopedia-wrapper/index.ts b/src/examples/proteopedia-wrapper/index.ts
index 4dab77950..90e8be083 100644
--- a/src/examples/proteopedia-wrapper/index.ts
+++ b/src/examples/proteopedia-wrapper/index.ts
@@ -210,7 +210,7 @@ class MolStarProteopediaWrapper {
         },
         download: async (url: string) => {
             try {
-                const data = await this.plugin.runTask(this.plugin.fetch(url));
+                const data = await this.plugin.runTask(this.plugin.fetch({ url }));
                 const snapshot = JSON.parse(data);
                 await this.plugin.state.setSnapshot(snapshot);
             } catch (e) {
diff --git a/src/mol-model-props/pdbe/structure-quality-report.ts b/src/mol-model-props/pdbe/structure-quality-report.ts
index 12cb6d018..b534c465f 100644
--- a/src/mol-model-props/pdbe/structure-quality-report.ts
+++ b/src/mol-model-props/pdbe/structure-quality-report.ts
@@ -83,7 +83,7 @@ export namespace StructureQualityReport {
         }
     }
 
-    export function createAttachTask(mapUrl: (model: Model) => string, fetch: (url: string, type: 'string' | 'binary') => Task<string | Uint8Array>) {
+    export function createAttachTask(mapUrl: (model: Model) => string, fetch: import('mol-util/data-source').AjaxTask) {
         return (model: Model) => Task.create('PDBe Structure Quality Report', async ctx => {
             if (get(model)) return true;
 
@@ -97,7 +97,7 @@ export namespace StructureQualityReport {
             // } else
             {
                 const url = mapUrl(model);
-                const dataStr = await fetch(url, 'string').runInContext(ctx) as string;
+                const dataStr = await fetch({ url }).runInContext(ctx) as string;
                 const data = JSON.parse(dataStr)[model.label.toLowerCase()];
                 if (!data) return false;
                 info = PropertyWrapper.createInfo();
diff --git a/src/mol-model-props/rcsb/assembly-symmetry.ts b/src/mol-model-props/rcsb/assembly-symmetry.ts
index 0c620737b..c67ed0aca 100644
--- a/src/mol-model-props/rcsb/assembly-symmetry.ts
+++ b/src/mol-model-props/rcsb/assembly-symmetry.ts
@@ -183,7 +183,7 @@ export function AssemblySymmetry(db: AssemblySymmetry.Database): AssemblySymmetr
 type SymmetryKind = 'GLOBAL' | 'LOCAL' | 'PSEUDO'
 type SymmetryType = 'ASYMMETRIC' | 'CYCLIC' | 'DIHEDRAL' | 'HELICAL' | 'ICOSAHEDRAL' | 'OCTAHEDRAL' | 'TETRAHEDRAL'
 
-const Client = new GraphQLClient(AssemblySymmetry.GraphQLEndpointURL, (url: string, type: 'string' | 'binary', body?: string) => ajaxGet({ url, type, body }) )
+const Client = new GraphQLClient(AssemblySymmetry.GraphQLEndpointURL, ajaxGet)
 
 export namespace AssemblySymmetry {
     export function is(x: any): x is AssemblySymmetry {
@@ -283,7 +283,7 @@ export namespace AssemblySymmetry {
         return true;
     }
 
-    export function createAttachTask(fetch: (url: string, type: 'string' | 'binary') => Task<string | Uint8Array>) {
+    export function createAttachTask(fetch: import('mol-util/data-source').AjaxTask) {
         return (model: Model) => Task.create('RCSB Assembly Symmetry', async ctx => {
             if (get(model)) return true;
 
diff --git a/src/mol-plugin/behavior/dynamic/volume.ts b/src/mol-plugin/behavior/dynamic/volume.ts
index b826f37c0..9808a806a 100644
--- a/src/mol-plugin/behavior/dynamic/volume.ts
+++ b/src/mol-plugin/behavior/dynamic/volume.ts
@@ -78,7 +78,7 @@ export namespace VolumeStreaming {
                 return data;
             }
 
-            const cif = await this.ctx.runTask(this.ctx.fetch(url, 'binary'));
+            const cif = await this.ctx.runTask(this.ctx.fetch({ url, type: 'binary' }));
             data = await this.parseCif(cif as Uint8Array);
             if (!data) {
                 return;
diff --git a/src/mol-plugin/context.ts b/src/mol-plugin/context.ts
index 0d8818117..5a5632db2 100644
--- a/src/mol-plugin/context.ts
+++ b/src/mol-plugin/context.ts
@@ -124,13 +124,7 @@ export class PluginContext {
      * This should be used in all transform related request so that it could be "spoofed" to allow
      * "static" access to resources.
      */
-    fetch(url: string, type?: 'string', body?: string): Task<string>
-    fetch(url: string, type?: 'binary', body?: string): Task<Uint8Array>
-    fetch(url: string, type: 'string' | 'binary' = 'string', body?: string): Task<string | Uint8Array> {
-        return ajaxGet({ url, type, body });
-        // const req = await fetch(url, { referrerPolicy: 'origin-when-cross-origin' });
-        // return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
-    }
+    readonly fetch = ajaxGet
 
     runTask<T>(task: Task<T>) {
         return this.tasks.run(task);
diff --git a/src/mol-plugin/state/transforms/data.ts b/src/mol-plugin/state/transforms/data.ts
index 4733c0dc6..9bcb441ab 100644
--- a/src/mol-plugin/state/transforms/data.ts
+++ b/src/mol-plugin/state/transforms/data.ts
@@ -31,7 +31,7 @@ const Download = PluginStateTransform.BuiltIn({
 })({
     apply({ params: p }, globalCtx: PluginContext) {
         return Task.create('Download', async ctx => {
-            const data = await globalCtx.fetch(p.url, p.isBinary ? 'binary' : 'string').runInContext(ctx);
+            const data = await globalCtx.fetch({ url: p.url, type: p.isBinary ? 'binary' : 'string' }).runInContext(ctx);
             return p.isBinary
                 ? new SO.Data.Binary(data as Uint8Array, { label: p.label ? p.label : p.url })
                 : new SO.Data.String(data as string, { label: p.label ? p.label : p.url });
diff --git a/src/mol-util/data-source.ts b/src/mol-util/data-source.ts
index 05730c20f..c370cc066 100644
--- a/src/mol-util/data-source.ts
+++ b/src/mol-util/data-source.ts
@@ -9,16 +9,16 @@
 import { Task, RuntimeContext } from 'mol-task';
 import { utf8Read } from 'mol-io/common/utf8';
 
-export enum DataCompressionMethod {
-    None,
-    Gzip
-}
+// export enum DataCompressionMethod {
+//     None,
+//     Gzip
+// }
 
-export interface AjaxGetParams {
+export interface AjaxGetParams<T extends 'string' | 'binary' = 'string'> {
     url: string,
-    type: 'string' | 'binary',
+    type?: T,
     title?: string,
-    compression?: DataCompressionMethod
+    // compression?: DataCompressionMethod
     body?: string
 }
 
@@ -42,11 +42,16 @@ export function ajaxGetUint8Array(url: string, title?: string) {
     return <Task<Uint8Array>>ajaxGetInternal(title, url, true, false);
 }
 
-export function ajaxGet(params: AjaxGetParams) {
-    return <Task<string | Uint8Array>>ajaxGetInternal(params.title, params.url, params.type === 'binary', params.compression === DataCompressionMethod.Gzip, params.body);
+export function ajaxGet(url: string): Task<string>
+export function ajaxGet(params: AjaxGetParams<'string'>): Task<string>
+export function ajaxGet(params: AjaxGetParams<'binary'>): Task<Uint8Array>
+export function ajaxGet(params: AjaxGetParams<'string' | 'binary'>): Task<string | Uint8Array>
+export function ajaxGet(params: AjaxGetParams<'string' | 'binary'> | string) {
+    if (typeof params === 'string') return ajaxGetInternal(params, params, false, false);
+    return ajaxGetInternal(params.title, params.url, params.type === 'binary', false /* params.compression === DataCompressionMethod.Gzip */, params.body);
 }
 
-export type AjaxTask = (url: string, type: 'string' | 'binary') => Task<string | Uint8Array>
+export type AjaxTask = typeof ajaxGet
 
 function decompress(buffer: Uint8Array): Uint8Array {
     // TODO
diff --git a/src/mol-util/graphql-client.ts b/src/mol-util/graphql-client.ts
index d6f8e5c71..174acc732 100644
--- a/src/mol-util/graphql-client.ts
+++ b/src/mol-util/graphql-client.ts
@@ -6,7 +6,7 @@
  * Adapted from https://github.com/prisma/graphql-request, Copyright (c) 2017 Graphcool, MIT
  */
 
-import { Task, RuntimeContext } from 'mol-task';
+import { RuntimeContext } from 'mol-task';
 
 type Variables = { [key: string]: any }
 
@@ -58,7 +58,7 @@ export class ClientError extends Error {
 }
 
 export class GraphQLClient {
-    constructor(private url: string, private fetch: (url: string, type: 'string' | 'binary', body?: string) => Task<string | Uint8Array>) {
+    constructor(private url: string, private fetch: import('mol-util/data-source').AjaxTask) {
         this.url = url
     }
 
@@ -69,7 +69,7 @@ export class GraphQLClient {
             variables: variables ? variables : undefined,
         })
 
-        const resultStr = await this.fetch(this.url, 'string', body).runInContext(ctx) as string
+        const resultStr = await this.fetch({ url: this.url, body }).runInContext(ctx)
         const result = JSON.parse(resultStr)
 
         if (!result.errors && result.data) {
-- 
GitLab