Skip to content

Commit

Permalink
refactor: send PGID for CMS calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Jun 16, 2020
1 parent 7f41b47 commit a5bfff5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/app/core/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,19 @@ export class ApiService {
/**
* appends API token to requests if available and request is not an authorization request
*/
private appendAPITokenToHeaders(): MonoTypeOperatorFunction<HttpHeaders> {
private appendAPITokenToHeaders(path: string): MonoTypeOperatorFunction<HttpHeaders> {
return headers$ =>
headers$.pipe(
withLatestFrom(this.store.pipe(select(getAPIToken))),
map(([headers, apiToken]) =>
apiToken && !headers.has(ApiService.AUTHORIZATION_HEADER_KEY)
? headers.set(ApiService.TOKEN_HEADER_KEY, apiToken)
: headers
),
// TODO: workaround removing auth token for cms if pgid is not available
withLatestFrom(this.store.pipe(select(getPGID))),
map(([headers, pgid]) =>
!pgid && path.startsWith('cms') ? headers.delete(ApiService.TOKEN_HEADER_KEY) : headers
)
);
}
Expand All @@ -133,7 +138,7 @@ export class ApiService {
/**
* merges supplied and default headers
*/
private constructHeaders(options?: AvailableOptions): Observable<HttpHeaders> {
private constructHeaders(path: string, options?: AvailableOptions): Observable<HttpHeaders> {
const defaultHeaders = new HttpHeaders().set('content-type', 'application/json').set('Accept', 'application/json');

return of(
Expand All @@ -148,7 +153,7 @@ export class ApiService {
? // captcha headers
this.appendCaptchaTokenToHeaders(options.captcha.captcha, options.captcha.captchaAction)
: // default to api token
this.appendAPITokenToHeaders()
this.appendAPITokenToHeaders(path)
);
}

Expand Down Expand Up @@ -215,7 +220,7 @@ export class ApiService {
): Observable<[string, { headers: HttpHeaders; params: HttpParams }]> {
return forkJoin([
this.constructUrlForPath(path, options),
this.constructHeaders(options).pipe(
this.constructHeaders(path, options).pipe(
map(headers => ({
params: options?.params,
headers,
Expand Down
20 changes: 12 additions & 8 deletions src/app/core/services/cms/cms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ export class CMSService {
return throwError('getContentInclude() called without an includeId');
}

return this.apiService.get<ContentPageletEntryPointData>(`cms/includes/${includeId}`).pipe(
map(x => this.contentPageletEntryPointMapper.fromData(x)),
map(({ pageletEntryPoint, pagelets }) => ({ include: pageletEntryPoint, pagelets }))
);
return this.apiService
.get<ContentPageletEntryPointData>(`cms/includes/${includeId}`, { sendPGID: true })
.pipe(
map(x => this.contentPageletEntryPointMapper.fromData(x)),
map(({ pageletEntryPoint, pagelets }) => ({ include: pageletEntryPoint, pagelets }))
);
}

/**
Expand All @@ -41,9 +43,11 @@ export class CMSService {
return throwError('getContentPage() called without an pageId');
}

return this.apiService.get<ContentPageletEntryPointData>(`cms/pages/${pageId}`).pipe(
map(x => this.contentPageletEntryPointMapper.fromData(x)),
map(({ pageletEntryPoint, pagelets }) => ({ page: pageletEntryPoint, pagelets }))
);
return this.apiService
.get<ContentPageletEntryPointData>(`cms/pages/${pageId}`, { sendPGID: true })
.pipe(
map(x => this.contentPageletEntryPointMapper.fromData(x)),
map(({ pageletEntryPoint, pagelets }) => ({ page: pageletEntryPoint, pagelets }))
);
}
}

0 comments on commit a5bfff5

Please sign in to comment.