Skip to content
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
22 changes: 13 additions & 9 deletions projects/laji-api-client/src/laji-api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ ExtractContentByResponseType<
);
}

protected getHeaders(lang: string, langFallback = true, personToken?: string) {
protected getFallbackHeaders(lang: string, langFallback = true, personToken?: string) {
const headers: Record<string, string> = {
'API-Version': '1',
'Accept-Language': lang,
Expand All @@ -250,14 +250,14 @@ ExtractContentByResponseType<
}

private getRequestOptions(
queryParams: any,
params: any,
requestBody: any,
lang: string,
langFallback = true,
personToken?: string,
responseType: ResponseType = 'json'
) {
const params = Object.keys((queryParams || {})).reduce((filteredQueryParams, key) => {
const responseType: ResponseType = params?.responseType ?? 'json';
const queryParams = params?.query;
const newParams = Object.keys((queryParams || {})).reduce((filteredQueryParams, key) => {
const param = (queryParams || {})[key];
if (param === undefined) {
return filteredQueryParams;
Expand All @@ -266,8 +266,12 @@ ExtractContentByResponseType<
return filteredQueryParams;
}, {} as any);

const headers = this.getHeaders(lang, langFallback, personToken);
return { params, body: requestBody, headers, responseType };
const fallbackHeaders = this.getFallbackHeaders(lang, langFallback, this.personToken);
const headers = params ? {
...fallbackHeaders,
...params.header
} : fallbackHeaders;
return { params: newParams, body: requestBody, headers, responseType };
}

fetch<
Expand Down Expand Up @@ -295,7 +299,7 @@ ExtractContentByResponseType<
return this.http.request(
method as string,
requestUrl,
this.getRequestOptions((params as any)?.query, requestBody, this.lang, langFallback, this.personToken, params?.responseType)
this.getRequestOptions(params, requestBody, this.lang, langFallback)
) as any;
}

Expand All @@ -320,7 +324,7 @@ ExtractContentByResponseType<
const obs = this.http.request(
method,
requestUrl,
this.getRequestOptions((params as any)?.query, requestBody, this.lang, langFallback, this.personToken, params?.responseType)
this.getRequestOptions(params, requestBody, this.lang, langFallback)
).pipe(
tap(val => {
cachedPath?.set(paramsHash, {
Expand Down
4 changes: 2 additions & 2 deletions projects/laji/src/app/shared/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ export class UserService implements OnDestroy {
this.setNotLoggedIn();
return of(false);
}
this.api.setPersonToken(token);
this.store.next({ ...this.store.value, loginState: { _tag: 'loading' }, user: { _tag: 'loading' } });
return this.api.get('/person').pipe(
return this.api.get('/person', { header: { 'Person-Token': token } }).pipe(
httpOkError([404, 400], null),
retryWithBackoff(300),
tap(person => {
Expand All @@ -224,6 +223,7 @@ export class UserService implements OnDestroy {
}
// if person is valid, we have succesfully logged in
this.persistentState = { ...this.persistentState, loginState: { _tag: 'logged_in', token }};
this.api.setPersonToken(token);
this.store.next({
...this.store.value,
...this.persistentState,
Expand Down
Loading