# SDTFClient

## Properties

### engine

This property represents the SDTF engine used by the SDTFClient.

```typescript
readonly engine: SDTFEngine;
```

### repository

This property represents the repository that the SDTFClient is interacting with.

```typescript
readonly repository: {
    readonly id: string;
    readonly name: string;
    readonly version: number;
    readonly createdAt: string;
    readonly updatedAt: string;
};
```

## Methods

### getJSONTokenTree

Returns the JSON token tree from the current repository.

```typescript
getJSONTokenTree(): SpecifyDesignTokenFormat;
```

### clone

Create a new SDTFClient instance to avoid mutating the current token tree. Especially useful when you want to perform multiple distinct operations on the same token tree.

```typescript
clone(): SDTFClient;
```

### pick

Narrow the current token tree by picking a subtree based on the given path.

```typescript
pick(path: Array<string>): this;
```

### query

Create a clone of the current SDTF and only keep the selection of the query. When creating a new tree it's possible that tokens, groups and collections names will collide.

```typescript
query(query: SDTFQuery, dedupeFn?: MergeDedupeFn): SDTFClient;
```

### renameNode

Rename a node to a new name. If you specify a type, it'll rename the node only if it's matching the type parameter. If it doesn't, it'll throw an error.

```typescript
renameNode(options: {
    atPath: Array<string>;
    name: string;
    type?: 'group' | 'collection' | 'token';
}): this;
```

#### Example

```typescript
sdtf.renameNode({ atPath: ['my', 'group'], name: 'newName' })
```

### update

Execute an update on the current token tree. Note that the update won't be applied to the remote repository.

```typescript
update(...updaters: Array<Updater>): this;
```

### withQuery

Execute multiple updater functions with the same query.

```typescript
withQuery(query: SDTFQuery): {
    update: (...updaters: Array<Updater>) => SDTFClient;
};
```

### resolveAliases

Resolve aliases in the current token tree. Useful before pick to avoid unresolvable aliases.

```typescript
resolveAliases(): this;
```

### remove

Narrow the current token tree by removing any matching node based on the given query.

```typescript
remove(query: SDTFQuery): this;
```

### reset

Resets the current token tree to its initial value. The initial value being the token tree of the repository at the time of the creation of the first SDTFClient instance.

```typescript
reset(): this;
```

### executeEngine

Tap into the current token tree to perform custom side effects.

```typescript
executeEngine(fn: (engine: SDTFEngine) => void): this;
```

### forEachTokenState

Iterate against the tokenStates of the current token tree.

```typescript
forEachTokenState(fn: (tokenState: TokenState, engine: SDTFEngine) => void): this;
```

### mapTokenStates

Iterate against the tokenStates of the current token tree, and accumulate the results in an array.

```typescript
mapTokenStates<T>(fn: (tokenState: TokenState, engine: SDTFEngine) => T): Array<T>;
```

### getTokenState

Get a token state for a given path.

```typescript
getTokenState(path: Array<string>): TokenState | undefined;
```

### getAllTokenStates

Get all the token states.

```typescript
getAllTokenStates(): Array<TokenState>;
```

### forEachCollectionState

Iterate against the collectionStates of the current token tree.

```typescript
forEachCollectionState(fn: (collectionState: CollectionState, engine: SDTFEngine) => void): this;
```

### mapCollectionStates

Iterate against the collectionStates of the current token tree, and accumulate the results in an array.

```typescript
mapCollectionStates<T>(fn: (collectionState: CollectionState, engine: SDTFEngine) => T): Array<T>;
```

### getCollectionState

Get a collection state for a given path.

```typescript
getCollectionState(path: Array<string>): CollectionState | undefined;
```

### getAllCollectionStates

Get all the collection states.

```typescript
getAllCollectionStates(): Array<CollectionState>;
```

### forEachGroupState

Iterate against the groupStates of the current token tree.

```typescript
forEachGroupState(fn: (groupState: GroupState, engine: SDTFEngine) => void): this;
```

### mapGroupStates

Iterate against the groupStates of the current token tree, and accumulate the results in an array.

```typescript
mapGroupStates<T>(fn: (groupState: GroupState, engine: SDTFEngine) => T): Array<T>;
```

### getGroupState

Get a group state for a given path.

```typescript
getGroupState(path: Array<string>): GroupState | undefined;
```

### getAllGroupStates

Get all the group states.

```typescript
getAllGroupStates(): Array<GroupState>;
```

### forEachQueryResult

Iterate against the nodeStates given by the query.

```typescript
forEachQueryResult(query: SDTFQuery, fn: (treeNodeState: SDTFNodeState, engine: SDTFEngine, queryResult: QueryResult) => void): this;
```

### mapQueryResults

Iterate against the nodeStates given by the query, and accumulate the result in an array.

```typescript
mapQueryResults<T>(query: SDTFQuery, fn: (treeNodeState: SDTFNodeState, engine: SDTFEngine, queryResult: QueryResult) => T): T[];
```

### createParsersPipelines

Create a parsers engine executor from the custom or built-in parser functions passed as arguments. All pipelines are executed in parallel, if you need to chain parsers, have a look to the `chainParserFunctions` util.

```typescript
createParsersPipelines(
  ...parsers: Array<ParserFunction<SDTFEngineDataBox>>
): () => Promise<ParsersEngineResults>;
```

### createParsersPipelinesFromRules

Create a parsers engine executor from the built-in parser rules passed as arguments. All pipelines are executed in parallel, if you need to chain parsers, have a look to the chainParserFunctions util.

```typescript
createParsersPipelinesFromRules(
  ...parsers: Array<BuiltInGenerationParserRule>
): () => Promise<ParsersEngineResults>;
```
