0

Effect:

getUserStatus$ = createEffect(() => this.actions$.pipe(
    ofType(getUserStatus),
    switchMap(_ => this.userApiService.getUserStatus().pipe(
        map(userStatusApiResponse = > getUserStatusSuccess({
            userStatusApiResponse
        })),
        catchError(error = > of(getUserStatusFailed({
            error
        })))))
));

Service:

public getUserStatus(): Observable<UserStatusApiResponse> {
    return this.httpClient.get<CommonResponse<UserStatusApiResponse>> (`$ {
        USER_STATUS_API
    }`).pipe(
        map(UserApiService.extractUserStatusApiResponse),
        catchError(commonApiError)
    );
}

Function for extracting GET response:

private static extractUserStatusApiResponse(response: CommonResponse<UserStatusApiResponse>): UserStatusApiResponse {
    return response.data;
}

GET response

{
    "httpStatus": 200,
    "data": {
        "necessaryField1: "f1",
        "necessaryField2: "f2",
        "unecessaryField3: "f3",
        "unecessaryField4: "f4",
        "arrayUsers": [
            {
                "A": {
                    "a1": "aaa",
                    "a2": "bbb",
                    "a3": 0
                },
                "B": {
                    "b1": "aaa",
                    "b2": "bbb",
                    "b3": 0
                },
                "C": {
                    "c1": "aaa",
                    "c2": "bbb",
                    "c3": 0
                }
            }
        ]
    }
}

I need from above object below response model:

export interface UserStatusApiResponse {
    "necessaryField1: string;
    "necessaryField2: string;
    "arrayUsers": ArrayUsers[]
}

Where from arrayUsers I need only object A and B:

export interface ArrayUsers {
    a: A;
    b: B;
}

Reducer:

on(getUserStatusSuccess, (state, action) = > ({
        ...state,
    error: null,
    necessaryArray: {
        necessaryField1: action.userStatusApiResponse.necessaryField1,
        necessaryField2: action.userStatusApiResponse.necessaryField2,
    },
    arrayUsers: action.userStatusApiResponse.arrayUsers
})),

My problem:

Always in store I have all network responses, irrespective of UserStatusApiResponse. Another field, which is not in UserStatusApiResponse automatically expands the store.

How can I properly handle above problem? I would like to control the type of each object/array, but always in store I see all objects. Also with unecessaryField3: f3, unecessaryField4: f4 and object C...

3
  • getUserStatusSuccess action definition? Commented Nov 14, 2019 at 18:50
  • @altu I think, it is not important in that case. There is simple action which only handling rest query in effect-service. export const getUserStatus= createAction( '[USER_API] User status' ); Commented Nov 14, 2019 at 21:26
  • 1
    I was asking about getUserStatusSuccess, although I could deduct it from the reducer or effect it's still just guesswork. On 2nd sight there are 2 ways to solve this. Mapping a partial response from the complete response.data or creating a selector for only the sought after part/s. Commented Nov 15, 2019 at 9:14

1 Answer 1

1

Keep in mind that the typing is just for the compilation. It's compiled in JS and then, you haven't any type.

So if you want only particular fields, then you need to map your response to a new object with only the wanted keys.

So :

extractUserStatusApiResponse(response: ...){
   return {
      necessaryField1: response.data.necessaryField1,
      necessaryField2: response.data.necessaryField2,
      arrayUsers: response.data.arrayUsers.map(user => ({A:user.A, B:user.B}))
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank You. But... What is the proper way? It is good solution: creating, in my case, userStatusApiResponse model? Or just get all response without checking anything?
If you have a control on All your data in your code, this is less error prone. And by control, I mean that you have exacly what you want. What you have done so far is good. But with the mapping, it will be better :)
So, when I have very big rest response. And I need, f.e. 80% of data from the response, I need mapping object by object etc.?
It will be cleaner if you want to control all your flow
Thank you so much. I thought it is too much code, and I did ev. wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.