Skip to content
Snippets Groups Projects
Commit eccec228 authored by David Sehnal's avatar David Sehnal
Browse files

better typing for ajaxGet

parent 2e0e7ca7
No related branches found
No related tags found
No related merge requests found
......@@ -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];
......
......@@ -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) {
......
......@@ -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();
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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 });
......
......@@ -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
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment