Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refacto for the advanced logic other extensions (#508)
- Loading branch information
Showing
7 changed files
with
545 additions
and
667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,69 @@ | ||
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; | ||
|
||
import Utils from '@requestnetwork/utils'; | ||
|
||
/** | ||
* Implementation of the content data extension | ||
*/ | ||
const contentData: ExtensionTypes.ContentData.IContentData = { | ||
applyActionToExtension, | ||
createCreationAction, | ||
}; | ||
export default contentData; | ||
import { ExtensionTypes } from '@requestnetwork/types'; | ||
import AbstractExtension from './abstract-extension'; | ||
|
||
const CURRENT_VERSION = '0.1.0'; | ||
|
||
/** | ||
* Creates the extensionsData to create the extension content-data | ||
* Should be called to create the extensionsData of a request | ||
* | ||
* @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create | ||
* | ||
* @returns IExtensionCreationAction the extensionsData to be store in the request | ||
*/ | ||
function createCreationAction( | ||
creationParameters: ExtensionTypes.ContentData.ICreationParameters, | ||
): ExtensionTypes.IAction { | ||
if (!creationParameters.content) { | ||
throw Error('No content has been given for the extension content-data'); | ||
} | ||
|
||
return { | ||
action: ExtensionTypes.ContentData.ACTION.CREATE, | ||
id: ExtensionTypes.ID.CONTENT_DATA, | ||
parameters: creationParameters, | ||
version: CURRENT_VERSION, | ||
}; | ||
} | ||
|
||
/** | ||
* Applies the extension action to the request | ||
* Is called to interpret the extensions data when applying the transaction | ||
* | ||
* @param extensionsState IExtensionStates previous state of the extensions | ||
* @param extensionAction IAction action to apply | ||
* @param requestState IRequest request state read-only | ||
* | ||
* @returns state of the request updated | ||
* Implementation of the content data extension | ||
*/ | ||
function applyActionToExtension( | ||
extensionsState: RequestLogicTypes.IExtensionStates, | ||
extensionAction: ExtensionTypes.IAction, | ||
requestState: RequestLogicTypes.IRequest, | ||
): RequestLogicTypes.IExtensionStates { | ||
if (extensionAction.action !== ExtensionTypes.ContentData.ACTION.CREATE) { | ||
throw Error(`Unknown action: ${extensionAction.action}`); | ||
export default class ContentDataExtension< | ||
TCreationParameters extends ExtensionTypes.ContentData.ICreationParameters = ExtensionTypes.ContentData.ICreationParameters | ||
> extends AbstractExtension<TCreationParameters> { | ||
public constructor( | ||
public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.CONTENT_DATA, | ||
public currentVersion: string = CURRENT_VERSION, | ||
) { | ||
super(ExtensionTypes.TYPE.CONTENT_DATA, extensionId, currentVersion); | ||
} | ||
|
||
if (requestState.extensions[ExtensionTypes.ID.CONTENT_DATA]) { | ||
throw Error(`This extension has already been created`); | ||
} | ||
/** | ||
* Creates the extensionsData to create the extension content-data | ||
* Should be called to create the extensionsData of a request | ||
* | ||
* @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create | ||
* | ||
* @returns IExtensionCreationAction the extensionsData to be store in the request | ||
*/ | ||
public createCreationAction( | ||
creationParameters: TCreationParameters, | ||
): ExtensionTypes.IAction<TCreationParameters> { | ||
if (!creationParameters.content) { | ||
throw Error('No content has been given for the extension content-data'); | ||
} | ||
|
||
if (!extensionAction.parameters.content) { | ||
throw Error('No content has been given for the extension content-data'); | ||
return super.createCreationAction( | ||
creationParameters, | ||
) as ExtensionTypes.IAction<TCreationParameters>; | ||
} | ||
|
||
// Deep copy to not mutate the input parameter | ||
const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState); | ||
/** | ||
* Applies a creation | ||
* | ||
* @param extensionAction action to apply | ||
* @param timestamp | ||
* | ||
* @returns state of the extension created | ||
*/ | ||
protected applyCreation( | ||
extensionAction: ExtensionTypes.IAction, | ||
timestamp: number, | ||
): ExtensionTypes.IState { | ||
if (!extensionAction.parameters.content) { | ||
throw Error('No content has been given for the extension content-data'); | ||
} | ||
|
||
copiedExtensionState[ExtensionTypes.ID.CONTENT_DATA] = { | ||
events: [], | ||
id: ExtensionTypes.ID.CONTENT_DATA, | ||
type: ExtensionTypes.TYPE.CONTENT_DATA, | ||
values: { content: extensionAction.parameters.content }, | ||
version: CURRENT_VERSION, | ||
}; | ||
const genericCreationAction = super.applyCreation(extensionAction, timestamp); | ||
|
||
return copiedExtensionState; | ||
return { | ||
...genericCreationAction, | ||
events: [], | ||
id: this.extensionId, | ||
type: this.extensionType, | ||
values: { | ||
...genericCreationAction.values, | ||
content: extensionAction.parameters.content, | ||
}, | ||
version: this.currentVersion, | ||
}; | ||
} | ||
} |
Oops, something went wrong.