Importer

View source

Defines how a response becomes a typed asset value.

Extend this class for one asset constructor, implement Importer#deserialize to consume the response body, and implement Importer#getExtensions to declare the path labels it supports. Register the concrete importer with AssetImporterPlugin so AssetServer can select it by asset type and extension. The TypeRegistry argument is available when the response format needs reflected or nested typed values; self-contained formats can ignore it.

Examples

import { Importer } from '@wimaengine/asset'

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

class TextImporter extends Importer {
  constructor() {
    super(TextAsset)
  }

  async deserialize(response, _typeRegistry) {
    return new TextAsset(await response.text())
  }

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

const importer = new TextImporter()

Constructor


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

View source

Associates the importer with an asset constructor.

The constructor is stored on Importer#asset and identifies the asset type handled by this importer. This constructor only records the association; it does not parse responses or register the importer with AssetServer.

Type Parameters

T

Parameters

NameDescriptionDefault
asset

Constructor of the asset value returned by this importer.

—

Properties


readonlyasset: Constructor<T>

View source

Constructor for the asset values produced by this importer.

AssetServer uses this constructor to identify the asset type when the importer is registered. Keep it aligned with the constructor supplied to AssetImporterPlugin; the base class does not validate the association.

Methods


deserialize(_response: Response, _typeRegistry: TypeRegistry): Promise<T>

View source

Converts a fetched response into the typed asset value.

Override this method to read the response body, parse its contents, and resolve a T. Use the TypeRegistry argument when the format stores reflected or nested values; a self-contained response can ignore it. A concrete importer should resolve an asset value that can be stored for the requested asset.

The base implementation always rejects with an Error because it has no response format to parse. The undefined return below is only an unreachable fallback after that error.

Parameters

NameDescriptionDefault
_response

Response whose body the importer reads.

—
_typeRegistry

Registry available for typed values.

—

Throws

The base implementation always rejects with an error.

Returns

A concrete importer should resolve the imported asset. The base implementation always rejects before resolving.

getExtensions(): string[]

View source

Lists the path extensions handled by this importer.

AssetServer indexes each returned label with the importer’s asset constructor. Labels must omit the leading dot and match the extension extracted from a load path exactly, including case. Return one label for each supported response format.

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

Throws

The base implementation always throws an error.

Returns

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