From 1a73ed731ea17c46ac7823577143047097927326 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Thu, 9 Jul 2020 00:00:28 -0700 Subject: check for valid API key, then display extracted hypothes.is username --- src/client/apis/hypothesis/HypothesisApiUtils.ts | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/client/apis/hypothesis') diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index fe35f5831..ab83630a9 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -14,7 +14,7 @@ export namespace Hypothesis { * Searches for annotations made by @param username that * contain @param searchKeyWord */ - export const searchAnnotation = async (username: String, searchKeyWord: String) => { + export const searchAnnotation = async (username: string, searchKeyWord: string) => { const base = 'https://api.hypothes.is/api/search'; const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); @@ -26,6 +26,19 @@ export namespace Hypothesis { } }; + export const fetchUser = async (apiKey: string) => { + const response = await fetch('https://api.hypothes.is/api/profile', { + headers: { + 'Authorization': `Bearer ${apiKey}`, + }, + }); + if (response.ok) { + return response.json(); + } else { + throw new Error('DASH: Error in fetchUser GET request'); + } + }; + // Find the most recent placeholder annotation created, and return its ID export const getPlaceholderId = async (username: String, searchKeyWord: String) => { const getResponse = await Hypothesis.searchAnnotation(username, searchKeyWord); @@ -54,9 +67,9 @@ export namespace Hypothesis { return `https://hyp.is/${annotationId}/${baseUrl}`; }; - // export const checkValidApiKey = async (apiKey: string) => { - // const response = await fetch("https://api.hypothes.is/api/profile", { - - // }); - // }; + // Extract username from Hypothe.is's userId format + export const extractUsername = (userid: string) => { + const exp: RegExp = /(?<=\:)(.*?)(?=\@)/; + return exp.exec(userid)![0]; + }; } \ No newline at end of file -- cgit v1.2.3-70-g09d2