npm package: @simpleview/dam-client
Description: Client for communicating with dam-kube
Author: Owen Allen
Latest version: 1.0.2

# dam-client

DAM (Digital Asset Management) client for accessing DAM resources.

* [Installation](#installation)
* [Private Assets](#private-assets)
* [Api](#api)
	* [assets](#assets)
	* [assets_remove](#assets_remove)
	* [assets_upsert](#assets_upsert)
	* [convertFileToFileObject](#convertfiletofileobject)
* [GraphQL Endpoints](#graphql-endpoints)
	* [Query](#dam_query)
		* [Assets](#assets-1)
	* [Mutation](#dam_mutation)
		* [Assets_Remove](#assets_remove-1)
		* [Assets_Upsert](#assets_upsert-1)
* [Development](#development)
* [Additional Documentation](#additional-documentation)

## Installation
```
npm install @simpleview/dam-client
```

## Private Assets
The DAM supports the upload of private assets. As long as an asset is marked as private, it will not appear in any query unless the correct id of the asset is supplied in the query filter.

## API

### DamPrefix
`DamPrefix` can be loaded into `sv-graphql-client` `GraphServer` to use as a client library for managing assets.  The DAM client requires authentication.

```
const { GraphServer } = require("@simpleview/sv-graphql-client");
const { AuthPrefix } = require("@simpleview/sv-auth-client");
const { DamPrefix } = require('@simpleview/dam-client');

const prefixes = [AuthPrefix, DamPrefix];

const server = new GraphServer({ graphUrl, prefixes });
```


### assets
Queries for assets allowing sorting and filtering operations.
* filter - Requires a dam_assets_filter defining assets to return.
* fields - Requires an object defining the GraphQL fields to be returned from graph.
* context - Requires an object with a valid auth token and the account_id of the platform the query should be run on.

#### Retrieve Private Asset
```js
const supaSecretAsset = await server.dam.assets({
	{ filter : { id : '000000000000000000000001'} },
	{ `docs { asset_url }` },
	{ token, acct_id }
});
```

#### Retrieve Multiple Assets
```js
const assetsPage2 = await server.dam.assets({
	{ filter : { }, options : { limit : 5, skip : 5, sort : { field : 'file_name' dir : 'asc' } } },
	{ `docs { asset_url }` },
	{ token, acct_id }
});
```

### assets_upsert
Allows for creating new assets and updating existing assets. 
* input - Requires a DAM FileObject and accepts optional private boolean and id string.
* fields - Requires an object defining the GraphQL fields to be returned from graph.
* context - Requires an object with a valid auth token and the account_id of the platform the upsert should be run on.

```js
const upsertResult = await server.dam.assets_upsert({
	{ fileObject },
	{ `success id` },
	{ token, acct_id }
});
```

### assets_remove
Allows for the soft delete of assets from the DAM.
* filter - Requires a dam_assets_remove_filter defining assets to remove.
* fields - Requires an object defining the GraphQL fields to be returned from graph.
* context - Requires an object with a valid auth token and the account_id of the platform the remove should be run on.

```js
const upsertResult = await server.dam.assets_remove({
	{ id : '000000000000000000000001' },
	{ `success message` },
	{ token, acct_id }
});
```

### convertFileToFileObject
Converts a browser's [File](https://developer.mozilla.org/en-US/docs/Web/API/File) into a DAM FileObject.
* file - An object matching the Web File API.

```js
const file = document.body.querySelector('[data-file-picker]').files[0];
const damFileObject = await server.dam.convertFileToFileObject({ file });
```

## GraphQL Endpoints
This section is provided to provide additional information about the GraphQL endpoints. To see the input parameters and output of each endpoint, please view the Schema in the GraphQL Explorer at https://graphql.simpleviewinc.com/.

_All GraphQL endpoints for the DAM require an auth bearer token and an account id_

### dam_query

#### assets
Returns an object containing an array of assets and a count of the assets found. Supports a filter for selecting specific assets and options for limiting the return.
```
{
	dam(acct_id: "1") {
		assets{ 
			count
			docs { 
				id
				asset_url
			}
		}
	}
}
```
### dam_mutation

#### assets_remove
Requires a filter object and performs a soft delete on a matching assets in the system. Returns an object containing a success boolean and a message relating to the removal.
```
mutation {
	dam(acct_id: "1") { 
		assets_remove(
			filter : {
				id : "000000000000000000000001"
			}
		){
			success
			message
		}
	}
}
```

#### assets_upsert
Requires an dam_assets_upsert object and either inserts or updates an asset based on whether or not an existing id is provided.
```
	mutation {
	dam(acct_id: "1") { 
		assets_upsert(
			input : {
				id : "000000000000000000000001",
				file_name : "cutePuppyInPuddle.jpg",
				file_type : "image",
				cdn_id : "dam/clients/dam/kbrid7wjepu7ljc8n3sa" 
			}
		){
			success
			message
			doc {
				id
				acct_id
				asset_url
				file_name
			}
		}
	}
}
```

## Development
* Tests located in `dam-kube`
* Publish - `sudo npm run publish SEMVER`


## Additional Documentation
* [dam-kube](https://github.com/simpleviewinc/dam-kube)
* [sv-auth-client](https://github.com/simpleviewinc/sv-auth-client)
* [sv-graphql-client](https://github.com/simpleviewinc/sv-graphql-client)