# @aster-js/core

> Aster core library part of Aster js library

Latest version **1.3.3** (published 2025-07-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install @aster-js/core
pnpm add @aster-js/core
yarn add @aster-js/core
bun add @aster-js/core
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2025-07-16 |
| First published | 2021-01-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 86.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | sebdoucet |
| Maintainers | aster-js |

## Links

- npm: https://www.npmjs.com/package/@aster-js/core
- Repository: https://github.com/asterlibraryjs/aster-js-core
- Homepage: https://github.com/asterlibraryjs/aster-js-core#readme
- Issues: https://github.com/asterlibraryjs/aster-js-core/issues
- npm.io page: https://npm.io/package/@aster-js/core

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.6.2

## Recent versions

- 1.3.3 (latest) — 2025-07-16
- 1.3.2 — 2024-11-14
- 1.3.1 — 2024-02-03
- 1.3.0 — 2024-01-13
- 1.2.6 — 2023-03-25
- 1.2.4 — 2022-05-09
- 1.2.3 — 2022-05-08
- 1.2.2 — 2021-11-12
- 1.2.1 — 2021-10-31
- 1.1.0 — 2021-02-06
- 1.0.6 — 2021-02-02
- 1.0.5 — 2021-02-02
- 1.0.4 — 2021-02-01
- 1.0.3 — 2021-02-01
- 1.0.1 — 2021-01-19
- … 3 more at https://npm.io/package/@aster-js/core/versions

## README

# @aster-js/core

## Lifecycle

### Lazy
Lazy allow to get lazy references that will create the instance when its used not when its referenced.

```ts
class CustomService {
    constructor()  {
        console.debug("constructor");
    }
    hello() {
        console.debug("Hello!");
    }
}

const service = Lazy.get(
    () => new CustomService()),
    CustomService
);
console.debug("Reference to CustomService done!", service instanceof CustomService);
service.hello();
```

Output:

```
> Reference to CustomService done!, true
> constructor
> Hello!
```

> See full documentation on [Lazy](./doc/lazy.md)
### Disposable
Provide multiple way to reset objects or free handlers, resources or anything more safely using this disposable implementation.
```ts
function $on(target: EventTarget, name: string, callback: EventListener): IDisposable {
    target.addEventListener(name, callback);
    return IDisposable.create(() => target.removeEventListener(name, callback));
}

const @event = $on(document, "click", ()=> console.info("Click !"));

// ... later
IDisposable.safeDispose(@event);
```
> See more about [Disposable usage](./doc/disposable.md).

## Metadata
### `Tag()`

Attach metadata to Types and instances.

**Declaration**
```ts
// Local read-write tag
const apiUrlTag = Tag<string>("Api url");

// External read-only tag
export const ApiUrlTag = apiUrlTag.readOnly();

// Only way to add metadata will be using this decorator
export const apiUrl = (url: string) =>
    (target: Function) => apiUrlTag(target, url);
```
**Tag Consumer**
```ts
import { ApiUrlTag } from "./xxx";
// Tag Consumer
export class ApiClientBase {
    protected async getJson(relativeUrl: string): Promise<any> {
        // The metadata has been associated with the type
        // so we have to provide `this.constructor`
        const baseUrl = ApiUrlTag(this.constructor);

        const fullUrl = new URL(relativeUrl, baseUrl);
        const result = await fetch(fullUrl.toString());
        return await result.json();
    }
}
```
**Usage**
```ts
import { apiUrl, ApiClientBase } from "./xxx";

@apiUrl("https://my-api-client/v1/")
export class MyApiClient extends ApiClientBase {
    getData(): any{
        return this.getJson("/data");
    }
}
```

> See more about [Tag()](./doc/tag.md) or [Tag.lazy()](./doc/lazy-tag.md)

---
_Source: https://npm.io/package/@aster-js/core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
