Exporter

View source

Abstract contract for converting typed asset values into request bodies.

Use a subclass for one asset constructor. Concrete exporters provide the conversion for each request-body format they support and use Exporter#getExtensions to return the exact extension labels that identify those formats. A conversion can use TypeRegistry for reflected or nested typed values.

Examples

import { Exporter } from '@wimaengine/asset'

class TextAsset {
  constructor(value) {
    this.value = value
  }
}

class TextExporter extends Exporter {
  constructor() {
    super(TextAsset)
  }

  async serialize(asset, _typeRegistry) {
    return asset.value
  }

  getExtensions() {
    return ['txt']
  }
}

const exporter = new TextExporter()

Constructor


constructor<T>(asset: Constructor<T>)

View source

Associates the exporter with an asset constructor.

The constructor is assigned directly to Exporter#asset. No validation or other initialization is performed.

Type Parameters

T

Parameters

NameDescriptionDefault
asset

Asset constructor associated with this exporter.

—

Properties


readonlyasset: Constructor<T>

View source

Asset constructor associated with values handled by this exporter.

The constructor is stored as supplied; this class does not validate it.

Methods


serialize(_asset: T, _typeRegistry: TypeRegistry): Promise<BodyInit>

View source

Converts an asset value into the body used by a save request.

A concrete exporter should resolve a value accepted by BodyInit; the resolved value is supplied as the request body. Use the TypeRegistry argument when the output format needs reflected or nested typed values.

The base implementation always rejects with an Error because it has no serialization format to produce.

Parameters

NameDescriptionDefault
_asset

Asset value to convert into a request body.

—
_typeRegistry

Registry available to the exporter.

—

Throws

The base implementation always rejects with an error.

Returns

Promise resolving to the request body, or to undefined when no body is returned. The base implementation always rejects before resolving.

getExtensions(): string[]

View source

Returns the extension labels supported by this exporter.

Each returned label is matched literally against the extension extracted from a save path. Omit the leading dot; spelling and case must match exactly, and no normalization is applied. Return one label for each supported output format.

The base implementation always throws an Error because it does not know which formats the concrete exporter supports. The empty array below is only an unreachable fallback after the throw.

Throws

The base implementation always throws an error.

Returns

Supported extension labels, such as ['json'].