Skip to content

Commit

Permalink
feat(people-provider): update PeopleApiClient photo method return type
Browse files Browse the repository at this point in the history
Update PeopleApiClient.photo method to return PersonPhotoApiResponse instead of Blob since the IHttpClient interface has changed

Adjust related types and methods to handle the updated return type
Modify PersonController to map the API response to Blob and handle errors appropriately
  • Loading branch information
odinr committed May 27, 2024
1 parent 4d16aa6 commit fc51e93
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
33 changes: 33 additions & 0 deletions .changeset/dry-experts-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
'@equinor/fusion-framework-react-components-people-provider': minor
'@equinor/fusion-framework-module-services': minor
---

## @equinor/fusion-framework-module-services

Updated the `PeopleApiClient.photo` method to properly type the response as `PersonPhotoApiResponse<TVersion>` instead of `Blob`. This allows for more accurate type checking when using the method.

To update your code:

- If you are using the `PeopleApiClient.photo` method directly, no changes are needed. The method will now properly type the response.
- If you have custom type assertions or checks around the response from `PeopleApiClient.photo`, you may need to update them to handle `PersonPhotoApiResponse<TVersion>` instead of `Blob`.

Example:

```ts
// Before
const photoResponse: Blob = await peopleApiClient.photo(
'v2',
'blob',
{ azureId: '123' }
);
console.log(typeof photoResponse); // Blob

// After
const photoResponse: PersonPhotoApiResponse<'v2'> = await peopleApiClient.photo(
'v2',
'blob',
{ azureId: '123' }
);
console.log(typeof photoResponse); // Object - { filename: string, blob: Blob }
```
2 changes: 1 addition & 1 deletion packages/modules/services/src/people/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class PeopleApiClient<
public photo<
TVersion extends PersonPhotoSupportedApiVersion,
TArgs extends PersonPhotoApiRequestArgs<TVersion>,
TResult extends Blob = PersonPhotoApiResponse<TVersion>,
TResult extends PersonPhotoApiResponse<TVersion> = PersonPhotoApiResponse<TVersion>,
TMethod extends keyof ClientDataMethod = keyof ClientDataMethod,
>(
version: TVersion,
Expand Down
14 changes: 9 additions & 5 deletions packages/modules/services/src/people/person-photo/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ClientRequestInit, IHttpClient } from '@equinor/fusion-framework-module-http/client';
import {
type ClientRequestInit,
type IHttpClient,
} from '@equinor/fusion-framework-module-http/client';

import { generateParameters } from './generate-parameters';

Expand All @@ -22,12 +25,13 @@ export const client =
version: TVersion,
method: TMethod = 'blob' as TMethod,
) =>
<T extends Blob = ApiResponse<TVersion>>(
<T extends ApiResponse<TVersion> = ApiResponse<TVersion>>(
args: TArgs,
init?: ClientRequestInit<TClient, T>,
): ClientDataMethod[TMethod] =>
client[method](
): ClientDataMethod<T>[TMethod] => {
return client[method](
...generateParameters<T, TVersion, TClient>(version, args, init),
) as ClientDataMethod[TMethod];
) as ClientDataMethod<T>[TMethod];
};

export default client;
3 changes: 2 additions & 1 deletion packages/modules/services/src/people/person-photo/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlobResult } from '@equinor/fusion-framework-module-http/client';
import { ApiVersion } from '../static';
import { ClientDataMethod } from '../../types';

Expand All @@ -15,7 +16,7 @@ export type ApiRequestArgs<T extends SupportedApiVersion> =
ApiRequestArgsMap[(typeof ApiVersion)[T]];

type ApiResponseTypes = {
[ApiVersion.v2]: Blob;
[ApiVersion.v2]: BlobResult;
};

export type ApiResponse<T extends SupportedApiVersion> = ApiResponseTypes[(typeof ApiVersion)[T]];
Expand Down
9 changes: 5 additions & 4 deletions packages/modules/services/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
ClientRequestInit,
IHttpClient,
FetchResponse,
StreamResponse,
BlobResult,
FetchResponse,
} from '@equinor/fusion-framework-module-http/client';

export type ApiClientFactory<TClient extends IHttpClient = IHttpClient> = (
Expand Down Expand Up @@ -38,9 +39,9 @@ export type ClientMethod<T = unknown> = {
json$: StreamResponse<T>;
};

export type ClientDataMethod = {
blob: Blob;
blob$: StreamResponse<Blob>;
export type ClientDataMethod<T extends BlobResult = BlobResult> = {
blob: Promise<T>;
blob$: StreamResponse<T>;
};

export type ClientMethodType = keyof ClientMethod;
14 changes: 10 additions & 4 deletions packages/react/components/people-resolver/src/PersonController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ export class PersonController implements IPersonController {
queueOperator: 'merge',
key: ({ azureId }) => azureId,
client: {
fn: ({ azureId }, signal) => {
fn: ({ azureId }, signal): Observable<Blob> => {
return client.photo('v2', 'blob$', { azureId }, { signal }).pipe(
map((result) => {
return result.blob;
}),
catchError((err) => {
const apiError = err as ApiProviderError;
if (apiError.response.status === 404 && options?.fallbackImage) {
if (
(err as Error).name === 'ApiProviderError' &&
(err as ApiProviderError).response?.status === 404 &&
options?.fallbackImage
) {
return of(options?.fallbackImage);
}
return of(err);
throw err;
}),
);
},
Expand Down

0 comments on commit fc51e93

Please sign in to comment.