Skip to content

Provides a generic abstract factory accepting classes as constructor parameters to create objects(using a simple mapping logic)

License

Notifications You must be signed in to change notification settings

titans55/generic-typescript-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

generic-typescript-factory

Provides a generic abstract factory, accepting classes as constructor parameters to create objects (using a simple mapping logic).

Installation

Use the node package manager npm to install.

npm i generic-typescript-factory

Here's an example of usage

First define your factory class which extends from GenericFactory.

import { DataserviceGeneralErrors } from "../general-errors";
import { DataserviceFluentValidationErrors } from "../fluent-validation-errors";
import { SupportedErrorsType } from "../../supported-errors.type";
import { GenericFactory } from "generic-typescript-factory";

export class SupportedErrorsFactory extends GenericFactory<
	SupportedErrorsType
> {
	constructor() {
		super([DataserviceFluentValidationErrors, DataserviceGeneralErrors]);
	}

	protected onCreateObj(
		createdErrorObj: SupportedErrorsType
	): SupportedErrorsType {
		createdErrorObj.setErrorsToToast();
		return createdErrorObj;
	}
}

Now you would be able to instantiate the objects of DataserviceFluentValidationErrors or DataserviceGeneralErrors classes by using new SupportedErrorsFactory().create(obj).

  • onCreateObj method will be called on creation of the object.(You can apply your custom logic inside that method)
this.httpClient
	.post(
		urlToPost,
		values
	)
	.toPromise()
	.catch((response: HttpErrorResponse) => {
		let errorObj: SupportedErrorsType = new SupportedErrorsFactory().create(
			response.error
		);
		errorObj.toastErrors();
		return Promise.reject("promise rejected");
	});

In here we catched SupportedErrorsType's (error types that can return from our api) instantiated the objects(using our factory) and applied our logic to toast those errors.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

About

Provides a generic abstract factory accepting classes as constructor parameters to create objects(using a simple mapping logic)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published