# generic-typescript-factory

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

Latest version **1.0.1** (published 2020-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install generic-typescript-factory
pnpm add generic-typescript-factory
yarn add generic-typescript-factory
bun add generic-typescript-factory
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-05-16 |
| First published | 2020-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Kutay Aslan |
| Maintainers | kutay.aslan |
| Keywords | generic, abstract, factory, typescript |

## Links

- npm: https://www.npmjs.com/package/generic-typescript-factory
- Repository: https://github.com/titans55/generic-typescript-factory
- Homepage: https://github.com/titans55/generic-typescript-factory#readme
- Issues: https://github.com/titans55/generic-typescript-factory/issues
- npm.io page: https://npm.io/package/generic-typescript-factory

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-05-16
- 1.0.0 — 2020-05-16

## README

# 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](https://www.npmjs.com/package/generic-typescript-factory) to install.

```bash
npm i generic-typescript-factory
```

## Here's an example of usage
First define your factory class which extends from **GenericFactory**.

```typescript
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)
```typescript

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](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/generic-typescript-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
