# Accessor

Source code is hosted on [GitHub](https://github.com/corets/accessor)

This library allows you to create a statically typed object accessor based on the [Proxy API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy). The main purpose of this package is to allow dynamic object access in a static manner, without having to rely on dynamic, string based keys. Code won't compile if you try to access a path that does not exist. You keep control over what is returned from the getter, by providing a custom access handler.

{% tabs %}
{% tab title="yarn" %}

```bash
yarn add @corets/accessor
```

{% endtab %}

{% tab title="npm" %}

```bash
npm install --save @corets/accessor
```

{% endtab %}
{% endtabs %}

## createAccessor() <a href="#createaccessor" id="createaccessor"></a>

Create an accessor instance of the type `ObjectAccessor`.

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })
```

Now you can statically access every object field:

```typescript
accessor.some.field.get()
```

You can provide a custom access handler to customise the access behaviour. Below is an example of how one could statically access translations, proxied by a translation library, using the accessor.

```typescript
import { createAccessor } from "@corets/accessor"

const translations = {  
    pages: {    
        home: {      
            title: "Home {replacement}"    
        },    
        about: {      
            title: "About {replacement}"    
        }  
    }
}

const translator = createSomeSortOfTranslator(translations)

const locales = createAccessor(translations, (source, key, ...replacements: any[]): string => {  
    return translator.get(key, { replacements })
})
```

Now you can indirectly access translations through the accessor, without using any dynamic keys.

```typescript
const translatedHomePageTitle = locales.pages.home.title.get("Page")
```

{% hint style="info" %}
Trying to access a missing key will result in a compilation error. 💥
{% endhint %}

This library is used internally by this translation library, to power its static translations access:

{% content-ref url="translator" %}
[translator](https://docs.corets.io/services/translator)
{% endcontent-ref %}

## ObjectAccessor.key() <a href="#objectaccessorkey" id="objectaccessorkey"></a>

Get absolute field key:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.key()
```

## ObjectAccessor.keyAt() <a href="#objectaccessorkeyat" id="objectaccessorkeyat"></a>

Get absolute field key, bypassing the static typing:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.keyAt("field")
```

## ObjectAccessor.get() <a href="#objectaccessorget" id="objectaccessorget"></a>

Get field value coming from the access handler:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.get()
```

## ObjectAccessor.getAt() <a href="#objectaccessorgetat" id="objectaccessorgetat"></a>

Get field value coming from the access handler, bypassing the static typing:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.getAt("field")
```

## ObjectAccessor.has() <a href="#objectaccessorhas" id="objectaccessorhas"></a>

Check if a field exists:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.field.has()
```

## ObjectAccessor.hasAt() <a href="#objectaccessorhasat" id="objectaccessorhasat"></a>

Check if a field exists, bypassing the static typing:

```typescript
import { createAccessor } from "@corets/accessor"

const accessor = createAccessor({ some: { field: "value" } })

accessor.some.hasAt("field")
```
