Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module-http): add functionality for blob query #1242

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/lazy-flowers-pull.md
Original file line number Diff line number Diff line change
@@ -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 <img src={url} />
```
23 changes: 20 additions & 3 deletions packages/modules/http/src/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,8 +26,10 @@ export type HttpClientCreateOptions<
};

/** Base http client for executing requests */
export class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse = FetchResponse>
implements IHttpClient<TRequest, TResponse>
export class HttpClient<
TRequest extends FetchRequest = FetchRequest,
TResponse extends FetchResponse = FetchResponse,
> implements IHttpClient<TRequest, TResponse>
{
readonly requestHandler: IHttpRequestHandler<TRequest>;

Expand Down Expand Up @@ -109,6 +111,21 @@ export class HttpClient<TRequest extends FetchRequest = FetchRequest, TResponse
return firstValueFrom(this.json$<T>(path, args));
}

public blob$(
path: string,
args?: FetchRequestInit<Blob, TRequest, TResponse>,
): StreamResponse<Blob> {
const selector = args?.selector ?? blobSelector;
return this.fetch$(path, {
...args,
selector,
} as FetchRequestInit<Blob, TRequest, TResponse>);
}

public blob(path: string, args?: FetchRequestInit<Blob, TRequest, TResponse>): Promise<Blob> {
return firstValueFrom(this.blob$(path, args));
}

/** @deprecated */
public jsonAsync<T = unknown>(
path: string,
Expand Down
11 changes: 11 additions & 0 deletions packages/modules/http/src/lib/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,22 @@ export interface IHttpClient<TRequest extends FetchRequest = FetchRequest, TResp
init?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
): Promise<T>;

/** @deprecated */
jsonAsync<T = unknown>(
path: string,
args?: FetchRequestInit<T, JsonRequest<TRequest>, TResponse>,
): Promise<T>;

blob(
path: string,
args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>,
): Promise<Blob>;

blob$(
path: string,
args?: FetchRequestInit<Blob, JsonRequest<TRequest>, TResponse>,
): StreamResponse<Blob>;

/**
* Abort all ongoing request for current client
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/modules/http/src/lib/selectors/blob-selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const blobSelector = <TResponse extends Response = Response>(
response: TResponse,
): Promise<Blob> => {
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;
1 change: 1 addition & 0 deletions packages/modules/http/src/lib/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { jsonSelector } from './json-selector';
export { blobSelector } from './blob-selector';
Loading