Skip to content

Commit

Permalink
BREAKING CHANGE: processResponse available only as result of precossR…
Browse files Browse the repository at this point in the history
…equest function, and AppSdk is predefined in processRequest too
  • Loading branch information
nightnei committed Dec 17, 2021
1 parent 9d6bdef commit ee2d4be
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 43 deletions.
17 changes: 2 additions & 15 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,10 @@ export default class IlcAppSdk implements IIlcAppSdk {
*
* CUSTOM 404:
* At SSR in processResponse it sets 404 status code and "X-ILC-Override" header to response.
* At CSR it renders own not found component from fragment.
* At CSR it renders own not found route.
*/
render404: Render404 = (withCustomContent) => {
// SSR
if (this.adapter.set404Response) {
this.adapter.set404Response(withCustomContent);
return;
}

// CSR
if (!withCustomContent) {
window.dispatchEvent(
new CustomEvent('ilc:404', {
detail: { appId: this.appId },
}),
);
}
this.adapter.trigger404Page(withCustomContent);
};

unmount() {
Expand Down
6 changes: 0 additions & 6 deletions src/app/interfaces/ResponseData.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/app/interfaces/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ResponseData from './ResponseData';

/**
* Result of the "processRequest" method
*/
Expand Down Expand Up @@ -29,11 +27,10 @@ export interface AppSdkAdapter {
/** Unique application ID, if same app will be rendered twice on a page - it will get different IDs */
appId: string;
intl: IntlAdapter | null;
set404Response: (withCustomContent?: boolean) => void;
getResponseData: () => ResponseData | undefined;
trigger404Page: (withCustomContent?: boolean) => void;
}

export type Render404 = (isCustomPage?: boolean) => void;
export type Render404 = (withCustomContent?: boolean) => void;

export interface IntlConfig {
locale?: string;
Expand Down
41 changes: 26 additions & 15 deletions src/server/IlcSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { intlSchema } from './IlcProtocol';
import defaultIntlAdapter from '../app/defaultIntlAdapter';
import * as clientTypes from '../app/interfaces/common';
import { IlcSdkLogger } from './IlcSdkLogger';
import ResponseData from '../app/interfaces/ResponseData';
import AppSdk from '../app';
import * as internalTypes from './internalTypes';

/**
* Entrypoint for SDK that should be used within application server that executes SSR bundle
Expand All @@ -28,7 +29,9 @@ export class IlcSdk {
/**
* Processes incoming request and returns object that can be used to fetch information passed by ILC to the application.
*/
public processRequest<RegistryProps = unknown>(req: IncomingMessage): types.RequestData<RegistryProps> {
public processRequest<RegistryProps = unknown>(
req: IncomingMessage,
): internalTypes.ProcessedRequest<RegistryProps> {
const url = this.parseUrl(req);
const routerProps = this.parseRouterProps(url);
const requestedUrls = this.getRequestUrls(url, routerProps);
Expand Down Expand Up @@ -56,26 +59,31 @@ export class IlcSdk {
originalUri = '/';
}

let responseData: ResponseData;
let tmpResponseData: internalTypes.TmpResponseData = {};

return {
const requestData = {
getCurrentReqHost: () => host,
getCurrentReqUrl: () => requestedUrls.requestUrl,
getCurrentBasePath: () => requestedUrls.basePageUrl,
getCurrentReqOriginalUri: () => originalUri,
getCurrentPathProps: () => passedProps,
appId,
intl: this.parseIntl(req),
set404Response: (withCustomContent) => {
responseData = { code: 404 };
trigger404Page: (withCustomContent?: boolean) => {
tmpResponseData.code = 404;

if (withCustomContent) {
responseData.headers = {
tmpResponseData.headers = {
['X-ILC-Override']: 'error-page-content',
};
}
},
getResponseData: () => responseData,
};

return {
requestData,
appSdk: new AppSdk(requestData),
processResponse: this.processResponse.bind(this, tmpResponseData),
};
}

Expand All @@ -84,13 +92,16 @@ export class IlcSdk {
*
* **WARNING:** this method should be called before response headers were send.
*/
public processResponse(reqData: types.RequestData, res: ServerResponse, data?: types.ResponseData): void {
const responseData = reqData.getResponseData();
if (responseData?.code) {
res.statusCode = responseData.code;
}
if (responseData?.headers) {
for (const [key, value] of Object.entries(responseData.headers)) {
private processResponse(
tmpResponseData: internalTypes.TmpResponseData,
res: ServerResponse,
data?: types.ResponseData,
): void {
if (tmpResponseData.code) {
res.statusCode = tmpResponseData.code;
}
if (tmpResponseData.headers) {
for (const [key, value] of Object.entries(tmpResponseData.headers)) {
res.setHeader(key, value);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/server/interfaces/ProcessedRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as types from '../types';
import { ServerResponse } from 'http';
import AppSdk from '../../app';

export interface ProcessedRequest<RegistryProps = unknown> {
requestData: types.RequestData<RegistryProps>;
appSdk: AppSdk;
processResponse: (res: ServerResponse, data?: types.ResponseData) => void;
}
4 changes: 4 additions & 0 deletions src/server/interfaces/TmpResponseData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type TmpResponseData = {
code?: number;
headers?: Record<string, string>;
};
2 changes: 2 additions & 0 deletions src/server/internalTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './interfaces/ProcessedRequest';
export * from './interfaces/TmpResponseData';
4 changes: 2 additions & 2 deletions test/server/IlcSdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('IlcSdk', () => {
const res = new MockRes();

const pRes = ilcSdk.processRequest(req);
pRes.set404Response();
pRes.trigger404Page();
ilcSdk.processResponse(pRes, res);

expect(res.statusCode).to.eq(404);
Expand All @@ -311,7 +311,7 @@ describe('IlcSdk', () => {

const pRes = ilcSdk.processRequest(req);
const withCustomContent = true;
pRes.set404Response(withCustomContent);
pRes.trigger404Page(withCustomContent);
ilcSdk.processResponse(pRes, res);

expect(res.statusCode).to.eq(NotFound);
Expand Down

0 comments on commit ee2d4be

Please sign in to comment.