obsidian#requestUrl TypeScript Examples

The following examples show how to use obsidian#requestUrl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: corsFetch.tsx    From obsidian-annotator with GNU Affero General Public License v3.0 7 votes vote down vote up
async function requestUrlFetch(requestInfo: RequestInfo, requestInit: RequestInit): Promise<Response> {
    let response: RequestUrlResponse;
    if (typeof requestInfo == 'string') {
        response = await requestUrl({
            url: requestInfo,
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            ...(requestInit as any)
        });
    } else {
        response = await requestUrl({
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            ...(requestInfo as any),
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            ...(requestInit as any)
        });
    }

    return new Response(response.arrayBuffer, {
        status: response.status,
        statusText: 'ok',
        headers: new Headers(response.headers)
    });
}
Example #2
Source File: remoteForOnedrive.ts    From remotely-save with Apache License 2.0 6 votes vote down vote up
deleteJson = async (pathFragOrig: string) => {
    const theUrl = this.buildUrl(pathFragOrig);
    log.debug(`deleteJson, theUrl=${theUrl}`);
    if (VALID_REQURL) {
      await requestUrl({
        url: theUrl,
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
        },
      });
    } else {
      await fetch(theUrl, {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
        },
      });
    }
  };
Example #3
Source File: remoteForOnedrive.ts    From remotely-save with Apache License 2.0 6 votes vote down vote up
putArrayBuffer = async (pathFragOrig: string, payload: ArrayBuffer) => {
    const theUrl = this.buildUrl(pathFragOrig);
    log.debug(`putArrayBuffer, theUrl=${theUrl}`);
    // TODO:
    // 20220401: On Android, requestUrl has issue that text becomes base64.
    // Use fetch everywhere instead!
    if (false /*VALID_REQURL*/) {
      await requestUrl({
        url: theUrl,
        method: "PUT",
        body: payload,
        contentType: DEFAULT_CONTENT_TYPE,
        headers: {
          "Content-Type": DEFAULT_CONTENT_TYPE,
          Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
        },
      });
    } else {
      await fetch(theUrl, {
        method: "PUT",
        body: payload,
        headers: {
          "Content-Type": DEFAULT_CONTENT_TYPE,
          Authorization: `Bearer ${await this.authGetter.getAccessToken()}`,
        },
      });
    }
  };
Example #4
Source File: remoteForOnedrive.ts    From remotely-save with Apache License 2.0 6 votes vote down vote up
downloadFromRemoteRaw = async (
  client: WrappedOnedriveClient,
  fileOrFolderPath: string
): Promise<ArrayBuffer> => {
  await client.init();
  const key = getOnedrivePath(fileOrFolderPath, client.remoteBaseDir);
  const rsp = await client.getJson(
    `${key}[email protected]`
  );
  const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"];
  if (VALID_REQURL) {
    const content = (
      await requestUrl({
        url: downloadUrl,
        headers: { "Cache-Control": "no-cache" },
      })
    ).arrayBuffer;
    return content;
  } else {
    const content = await // cannot set no-cache here, will have cors error
    (await fetch(downloadUrl)).arrayBuffer();
    return content;
  }
}
Example #5
Source File: googleScraperAPI.ts    From obsidian-dictionary with GNU Affero General Public License v3.0 5 votes vote down vote up
async requestDefinitions(query: string, lang: string): Promise<DictionaryWord> {
        const result = await requestUrl({
            url: `https://www.google.com/search?q=define+${query.replace(/\s/g, '+')}+${GoogleScraperDefinitionProvider.LANGUAGES[lang]}`,
            headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' }
        });
        console.log(result);

        const doc = new DOMParser().parseFromString(result.text, 'text/html');
        const data = doc.querySelector(`div[data-query-term=${query}]`);

        if (!data) throw "";

        const def: DictionaryWord = {
            phonetics: [],
            meanings: [],
            word: data.querySelector('span[data-dobid="hdw"]')?.textContent ?? query
        };

        //Something like eɪpr(ɪ)l (April)
        const phoneticText = data.querySelector('.LTKOO > span')?.textContent;
        if (phoneticText) {
            def.phonetics.push({
                text: phoneticText,
                audio: data.querySelector('audio > source')?.getAttribute('src') ?? undefined
            });
        }

        //Something like noun
        const type = data.querySelector('.vmod i')?.textContent;
        if (type) {
            const defGenerator = (defs: NodeList) => {

                const out: Definition[] = [];
                const syns: string[] = [];
                const tmp = data.querySelectorAll('.lr_container div[role="button"] span');
                tmp.forEach((el) => {
                    if (!el.parentElement?.getAttribute('data-topic') && el.textContent) {
                        syns.push(el.textContent.trim());
                    }
                })
                defs.forEach((el, idx) => {
                    out.push({
                        definition: el.textContent,
                        example: el.nextSibling?.textContent,
                        synonyms: !idx ? syns : undefined
                    })
                })
                return out;
            }

            def.meanings.push({
                partOfSpeech: type,
                definitions: defGenerator(data.querySelectorAll('div[data-dobid="dfn"]'))
            });
        }

        return def;
    }
Example #6
Source File: remoteForOnedrive.ts    From remotely-save with Apache License 2.0 5 votes vote down vote up
/**
   * A specialized function to upload large files by parts
   * @param pathFragOrig
   * @param payload
   * @param rangeMin
   * @param rangeEnd the end, exclusive
   * @param size
   */
  putUint8ArrayByRange = async (
    pathFragOrig: string,
    payload: Uint8Array,
    rangeStart: number,
    rangeEnd: number,
    size: number
  ) => {
    const theUrl = this.buildUrl(pathFragOrig);
    log.debug(
      `putUint8ArrayByRange, theUrl=${theUrl}, range=${rangeStart}-${
        rangeEnd - 1
      }, len=${rangeEnd - rangeStart}, size=${size}`
    );
    // NO AUTH HEADER here!
    // TODO:
    // 20220401: On Android, requestUrl has issue that text becomes base64.
    // Use fetch everywhere instead!
    if (false /*VALID_REQURL*/) {
      const res = await requestUrl({
        url: theUrl,
        method: "PUT",
        body: bufferToArrayBuffer(payload.subarray(rangeStart, rangeEnd)),
        contentType: DEFAULT_CONTENT_TYPE,
        headers: {
          // no "Content-Length" allowed here
          "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`,
          /* "Cache-Control": "no-cache", not allowed here!!! */
        },
      });
      return res.json as DriveItem | UploadSession;
    } else {
      const res = await fetch(theUrl, {
        method: "PUT",
        body: payload.subarray(rangeStart, rangeEnd),
        headers: {
          "Content-Length": `${rangeEnd - rangeStart}`,
          "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`,
          "Content-Type": DEFAULT_CONTENT_TYPE,
          /* "Cache-Control": "no-cache", not allowed here!!! */
        },
      });
      return (await res.json()) as DriveItem | UploadSession;
    }
  };
Example #7
Source File: remoteForS3.ts    From remotely-save with Apache License 2.0 4 votes vote down vote up
async handle(
    request: HttpRequest,
    { abortSignal }: HttpHandlerOptions = {}
  ): Promise<{ response: HttpResponse }> {
    if (abortSignal?.aborted) {
      const abortError = new Error("Request aborted");
      abortError.name = "AbortError";
      return Promise.reject(abortError);
    }

    let path = request.path;
    if (request.query) {
      const queryString = buildQueryString(request.query);
      if (queryString) {
        path += `?${queryString}`;
      }
    }

    const { port, method } = request;
    const url = `${request.protocol}//${request.hostname}${
      port ? `:${port}` : ""
    }${path}`;
    const body =
      method === "GET" || method === "HEAD" ? undefined : request.body;

    const transformedHeaders: Record<string, string> = {};
    for (const key of Object.keys(request.headers)) {
      const keyLower = key.toLowerCase();
      if (keyLower === "host" || keyLower === "content-length") {
        continue;
      }
      transformedHeaders[keyLower] = request.headers[key];
    }

    let contentType: string = undefined;
    if (transformedHeaders["content-type"] !== undefined) {
      contentType = transformedHeaders["content-type"];
    }

    let transformedBody: any = body;
    if (ArrayBuffer.isView(body)) {
      transformedBody = bufferToArrayBuffer(body);
    }

    const param: RequestUrlParam = {
      body: transformedBody,
      headers: transformedHeaders,
      method: method,
      url: url,
      contentType: contentType,
    };

    const raceOfPromises = [
      requestUrl(param).then((rsp) => {
        const headers = rsp.headers;
        const headersLower: Record<string, string> = {};
        for (const key of Object.keys(headers)) {
          headersLower[key.toLowerCase()] = headers[key];
        }
        const stream = new ReadableStream<Uint8Array>({
          start(controller) {
            controller.enqueue(new Uint8Array(rsp.arrayBuffer));
            controller.close();
          },
        });
        return {
          response: new HttpResponse({
            headers: headersLower,
            statusCode: rsp.status,
            body: stream,
          }),
        };
      }),
      requestTimeout(this.requestTimeoutInMs),
    ];

    if (abortSignal) {
      raceOfPromises.push(
        new Promise<never>((resolve, reject) => {
          abortSignal.onabort = () => {
            const abortError = new Error("Request aborted");
            abortError.name = "AbortError";
            reject(abortError);
          };
        })
      );
    }
    return Promise.race(raceOfPromises);
  }