Skip to content

Commit

Permalink
add file serve
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Dec 24, 2023
1 parent 51c775b commit 653eeb9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
34 changes: 34 additions & 0 deletions src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface UserManagementOptions {
}

class UserManagementService {
token: string;
request: <A = any>(path: string, payload?: any) => Promise<A>;

getAccessToken: (
Expand Down Expand Up @@ -55,6 +56,7 @@ class UserManagementService {
this.instance = { uuid: tokenDecoded.instance };
this.product = { id: tokenDecoded.product };
this.request = U.request(token, options.urlPrefix || urlPrefix);
this.token = token;

const jwtSecretOrPrivateKey =
typeof jwtSecret === "string" ? jwtSecret : jwtSecret.privateKey;
Expand Down Expand Up @@ -474,6 +476,38 @@ class UserManagementService {
uuid: string;
}): Promise<{ success: boolean; updated: number }> =>
this.request("/file/delete", data);

/**
* Serves a requested file as an ArrayBuffer.
*
* This asynchronous function is designed to handle requests for specific files identified by their UUID.
* When called with a UUID, it retrieves the corresponding file from the backend and serves it as an ArrayBuffer.
* This is particularly useful for delivering binary data, like images or documents, to the client.
* The function ensures that the file associated with the given UUID is retrieved and then
* converts it into an ArrayBuffer format, which is suitable for transmission over network protocols.
*
* @param {string} uuid - The UUID of the file to be served.
* @returns {Promise<ArrayBuffer>} A promise that resolves to the file data in ArrayBuffer format,
* or rejects in case of any errors during file retrieval or conversion.
*/
fileServe = async (uuid: string): Promise<ArrayBuffer> => {
const headers = {
Authorization: "Bearer " + this.token,
"content-type": "application/json",
};

const response = await U.requestToResponse(
this.token,
urlPrefix + "/file/serve",
{ uuid }
);

if (response.status !== 200) {
throw Error(await response.text());
}

return response.arrayBuffer();
};
}

export default UserManagementService;
37 changes: 22 additions & 15 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@ import * as T from "./type";

export const jwtAlgorithmDefault: JWT.Algorithm = "RS256";

export const requestToResponse = async (
token: string,
url: string,
payload: any
): Promise<Response> => {
const body = JSON.stringify(payload);
const headers = {
Authorization: "Bearer " + token,
"content-type": "application/json",
};

return fetch(url, {
method: "POST",
body,
headers,
});
};

export const request =
(token: string, urlPrefix: string) =>
async <A = any>(path: string, payload: any): Promise<A> => {
const body = JSON.stringify(payload);
const headers = {
Authorization: "Bearer " + token,
"content-type": "application/json",
};

const url = urlPrefix + path;
const response = await requestToResponse(token, url, payload);

const r = await fetch(url, {
method: "POST",
body,
headers,
});

if (r.status !== 200) {
const t = await r.text();
if (response.status !== 200) {
const t = await response.text();

throw Error(t);
}

return r.json();
return response.json();
};

export const getAccessToken =
Expand Down

0 comments on commit 653eeb9

Please sign in to comment.