From 236fb6fb8d0f1f3faf4ad837daa3470a9c847cd9 Mon Sep 17 00:00:00 2001 From: Odin Thomas Rochmann Date: Fri, 8 Sep 2023 10:01:26 +0200 Subject: [PATCH] feat(module-http): add functionality for blob query --- .changeset/lazy-flowers-pull.md | 15 ++++++++++++ .../modules/http/src/lib/client/client.ts | 23 ++++++++++++++++--- packages/modules/http/src/lib/client/types.ts | 11 +++++++++ .../http/src/lib/selectors/blob-selector.ts | 19 +++++++++++++++ .../modules/http/src/lib/selectors/index.ts | 1 + 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 .changeset/lazy-flowers-pull.md create mode 100644 packages/modules/http/src/lib/selectors/blob-selector.ts diff --git a/.changeset/lazy-flowers-pull.md b/.changeset/lazy-flowers-pull.md new file mode 100644 index 000000000..612c267e0 --- /dev/null +++ b/.changeset/lazy-flowers-pull.md @@ -0,0 +1,15 @@ +--- +'@equinor/fusion-framework-module-http': minor +--- + +add method for executing blob requests + +- added selector for extracting blob from response +- added function for fetching blob as stream or promise on the http client + + +```tsx +const data = await client.blob('/person/photo'); +const url = URL.createObjectURL(blob); +return +``` \ No newline at end of file diff --git a/packages/modules/http/src/lib/client/client.ts b/packages/modules/http/src/lib/client/client.ts index 2b51a9464..793078937 100644 --- a/packages/modules/http/src/lib/client/client.ts +++ b/packages/modules/http/src/lib/client/client.ts @@ -3,7 +3,7 @@ import { switchMap, takeUntil, tap } from 'rxjs/operators'; import { fromFetch } from 'rxjs/fetch'; import { HttpRequestHandler, HttpResponseHandler } from '../operators'; -import { jsonSelector } from '../selectors'; +import { blobSelector, jsonSelector } from '../selectors'; import type { Observable, ObservableInput } from 'rxjs'; import type { IHttpRequestHandler, IHttpResponseHandler } from '../operators'; @@ -26,8 +26,10 @@ export type HttpClientCreateOptions< }; /** Base http client for executing requests */ -export class HttpClient - implements IHttpClient +export class HttpClient< + TRequest extends FetchRequest = FetchRequest, + TResponse extends FetchResponse = FetchResponse, +> implements IHttpClient { readonly requestHandler: IHttpRequestHandler; @@ -109,6 +111,21 @@ export class HttpClient(path, args)); } + public blob$( + path: string, + args?: FetchRequestInit, + ): StreamResponse { + const selector = args?.selector ?? blobSelector; + return this.fetch$(path, { + ...args, + selector, + } as FetchRequestInit); + } + + public blob(path: string, args?: FetchRequestInit): Promise { + return firstValueFrom(this.blob$(path, args)); + } + /** @deprecated */ public jsonAsync( path: string, diff --git a/packages/modules/http/src/lib/client/types.ts b/packages/modules/http/src/lib/client/types.ts index db6a19e1e..5304c6cef 100644 --- a/packages/modules/http/src/lib/client/types.ts +++ b/packages/modules/http/src/lib/client/types.ts @@ -145,11 +145,22 @@ export interface IHttpClient, TResponse>, ): Promise; + /** @deprecated */ jsonAsync( path: string, args?: FetchRequestInit, TResponse>, ): Promise; + blob( + path: string, + args?: FetchRequestInit, TResponse>, + ): Promise; + + blob$( + path: string, + args?: FetchRequestInit, TResponse>, + ): StreamResponse; + /** * Abort all ongoing request for current client */ diff --git a/packages/modules/http/src/lib/selectors/blob-selector.ts b/packages/modules/http/src/lib/selectors/blob-selector.ts new file mode 100644 index 000000000..90fea9830 --- /dev/null +++ b/packages/modules/http/src/lib/selectors/blob-selector.ts @@ -0,0 +1,19 @@ +export const blobSelector = ( + response: TResponse, +): Promise => { + if (!response.ok) { + throw new Error('network response was not OK'); + } + //Status code 204 is no content + if (response.status === 204) { + throw new Error('no content'); + } + + try { + return response.blob(); + } catch (err) { + throw Error('failed to parse response'); + } +}; + +export default blobSelector; diff --git a/packages/modules/http/src/lib/selectors/index.ts b/packages/modules/http/src/lib/selectors/index.ts index 2cb9f5122..7b413c261 100644 --- a/packages/modules/http/src/lib/selectors/index.ts +++ b/packages/modules/http/src/lib/selectors/index.ts @@ -1 +1,2 @@ export { jsonSelector } from './json-selector'; +export { blobSelector } from './blob-selector';